bool CScraperUrl::Get(const SUrlEntry& scrURL, string& strHTML, CHTTP& http) { CURL url(scrURL.m_url); http.SetReferer(scrURL.m_spoof); CStdString strCachePath; if (!scrURL.m_cache.IsEmpty()) { CUtil::AddFileToFolder(g_advancedSettings.m_cachePath,"scrapers\\"+scrURL.m_cache,strCachePath); if (XFILE::CFile::Exists(strCachePath)) { XFILE::CFile file; file.Open(strCachePath); char* temp = new char[(int)file.GetLength()]; file.Read(temp,file.GetLength()); strHTML.append(temp,temp+file.GetLength()); file.Close(); delete[] temp; return true; } } if (scrURL.m_post) { CStdString strOptions = url.GetOptions(); strOptions = strOptions.substr(1); url.SetOptions(""); CStdString strUrl; url.GetURL(strUrl); if (!http.Post(strUrl, strOptions, strHTML)) return false; } else if (!http.Get(scrURL.m_url, strHTML)) return false; if (scrURL.m_url.Find(".zip") > -1) { XFILE::CFileZip file; CStdString strBuffer; int iSize = file.UnpackFromMemory(strBuffer,strHTML); if (iSize) { strHTML.clear(); strHTML.append(strBuffer.c_str(),strBuffer.data()+iSize); } } if (!scrURL.m_cache.IsEmpty()) { CStdString strCachePath; CUtil::AddFileToFolder(g_advancedSettings.m_cachePath,"scrapers\\"+scrURL.m_cache,strCachePath); XFILE::CFile file; if (file.OpenForWrite(strCachePath,true,true)) file.Write(strHTML.data(),strHTML.size()); file.Close(); } return true; }
bool CGUIDialogSongInfo::DownloadThumbnail(const CStdString &thumbFile) { // TODO: Obtain the source... CStdString source; CHTTP http; http.Download(source, thumbFile); return true; }
void CLastFmManager::CacheTrackThumb(const int nrInitialTracksToAdd) { DWORD start = timeGetTime(); CSingleLock lock(m_lockCache); int iNrCachedTracks = m_RadioTrackQueue->size(); CPicture pic; CHTTP http; for (int i = 0; i < nrInitialTracksToAdd && i < iNrCachedTracks; i++) { CFileItemPtr item = (*m_RadioTrackQueue)[i]; if (!item->GetMusicInfoTag()->Loaded()) { //cache albumthumb, GetThumbnailImage contains the url to cache if (item->HasThumbnail()) { CStdString coverUrl = item->GetThumbnailImage(); CStdString crcFile; CStdString cachedFile; CStdString thumbFile; Crc32 crc; crc.ComputeFromLowerCase(coverUrl); crcFile.Format("%08x.tbn", (__int32)crc); CUtil::AddFileToFolder(_P(g_advancedSettings.m_cachePath), crcFile, cachedFile); CUtil::AddFileToFolder(g_settings.GetLastFMThumbFolder(), crcFile, thumbFile); item->SetThumbnailImage(""); try { //download to temp, then make a thumb if (CFile::Exists(thumbFile) || (http.Download(coverUrl, cachedFile) && pic.DoCreateThumbnail(cachedFile, thumbFile))) { if (CFile::Exists(cachedFile)) CFile::Delete(cachedFile); item->SetThumbnailImage(thumbFile); } } catch(...) { CLog::Log(LOGERROR, "LastFmManager: exception while caching %s to %s.", coverUrl.c_str(), thumbFile.c_str()); } } if (!item->HasThumbnail()) { item->SetThumbnailImage("defaultAlbumCover.png"); } item->GetMusicInfoTag()->SetLoaded(); } } CLog::Log(LOGDEBUG, "%s: Done (time: %i ms)", __FUNCTION__, (int)(timeGetTime() - start)); }
bool CLastFmManager::RadioHandShake() { if (!m_RadioSession.IsEmpty()) return true; //already signed in if (dlgProgress) { dlgProgress->SetLine(2, 15251);//Connecting to Last.fm.. dlgProgress->Progress(); } m_RadioSession = ""; CHTTP http; CStdString html; CStdString strPassword = g_guiSettings.GetString("lastfm.password"); CStdString strUserName = g_guiSettings.GetString("lastfm.username"); if (strUserName.IsEmpty() || strPassword.IsEmpty()) { CLog::Log(LOGERROR, "Last.fm stream selected but no username or password set."); return false; } CStdString passwordmd5; CreateMD5Hash(strPassword, passwordmd5); CStdString url; CUtil::URLEncode(strUserName); url.Format("http://ws.audioscrobbler.com/radio/handshake.php?version=%s&platform=%s&username=%s&passwordmd5=%s&debug=%i&partner=%s", XBMC_LASTFM_VERSION, XBMC_LASTFM_ID, strUserName, passwordmd5, 0, ""); if (!http.Get(url, html)) { CLog::Log(LOGERROR, "Connect to Last.fm radio failed."); return false; } //CLog::DebugLog("Handshake: %s", html.c_str()); Parameter("session", html, m_RadioSession); Parameter("base_url", html, m_RadioBaseUrl); Parameter("base_path", html, m_RadioBasePath); Parameter("subscriber", html, m_RadioSubscriber); Parameter("banned", html, m_RadioBanned); if (m_RadioSession.CompareNoCase("failed") == 0) { CLog::Log(LOGERROR, "Last.fm return failed response, possible bad username or password?"); m_RadioSession = ""; } return !m_RadioSession.IsEmpty(); }
/// This method queries imdb for movie poster art associated with an imdb number bool CMediaMonitor::imdb_GetMovieArt(CStdString& strPath, CStdString& strPictureUrl, CStdString& strImagePath) { CFileItem item(strPath, false); item.SetVideoThumb(); if (item.HasThumbnail()) { strImagePath = item.GetThumbnailImage(); return true; } CStdString strThum(item.GetCachedVideoThumb()); CStdString strExtension; CUtil::GetExtension(strPictureUrl, strExtension); if (strExtension.IsEmpty()) { return false; } CStdString strTemp; strTemp.Format("Z:\\ram_temp%s", strExtension.c_str()); ::DeleteFile(strTemp.c_str()); CHTTP http; http.Download(strPictureUrl, strTemp); try { CPicture picture; picture.DoCreateThumbnail(strTemp, strThum); } catch (...) { ::DeleteFile(strThum.c_str()); } ::DeleteFile(strTemp.c_str()); if (CFile::Exists(strThum.c_str())) { strImagePath = strThum; return true; } return false; }
bool CFanart::DownloadImage(const CStdString &url, const CStdString &destination) const { // Ideally we'd just call CPicture::CacheImage() directly, but for some // reason curl doesn't seem to like downloading these for us CHTTP http; #ifdef RESAMPLE_CACHED_IMAGES CStdString tempFile = _P("Z:\\fanart_download.jpg"); if (http.Download(url, tempFile)) { CPicture pic; pic.CacheImage(tempFile, destination); XFILE::CFile::Delete(tempFile); return true; } return false; #else return http.Download(url, destination); #endif }
bool CFanart::DownloadThumb(unsigned int index, const CStdString &strDestination) const { if (index >= m_fanart.size()) return false; CStdString thumbURL; if (!m_fanart[index].strPreview.IsEmpty()) { thumbURL = CUtil::AddFileToFolder(m_url, m_fanart[index].strPreview); CHTTP http; if (http.Download(thumbURL, strDestination)) return true; } // try downloading the image instead thumbURL = CUtil::AddFileToFolder(m_url, m_fanart[index].strImage); return DownloadImage(thumbURL, strDestination); }
bool CScraperUrl::DownloadThumbnail(const CStdString &thumb, const CScraperUrl::SUrlEntry& entry) { if (entry.m_url.IsEmpty()) return false; CHTTP http; http.SetReferer(entry.m_spoof); string thumbData; if (http.Get(entry.m_url, thumbData)) { try { CPicture picture; picture.CreateThumbnailFromMemory((const BYTE *)thumbData.c_str(), thumbData.size(), CUtil::GetExtension(entry.m_url), thumb); return true; } catch (...) { ::DeleteFile(thumb.c_str()); } } return false; }
/* <?xml version="1.0" encoding="UTF-8"?> <methodCall> <methodName>method</methodName> <params> <param><value><string>user</string></value></param> <param><value><string>challenge</string></value></param> <param><value><string>auth</string></value></param> <param><value><string>artist</string></value></param> <param><value><string>title</string></value></param> </params> </methodCall> */ bool CLastFmManager::CallXmlRpc(const CStdString& action, const CStdString& artist, const CStdString& title) { CStdString strUserName = g_guiSettings.GetString("lastfm.username"); CStdString strPassword = g_guiSettings.GetString("lastfm.password"); if (strUserName.IsEmpty() || strPassword.IsEmpty()) { CLog::Log(LOGERROR, "Last.fm CallXmlRpc no username or password set."); return false; } if (artist.IsEmpty()) { CLog::Log(LOGERROR, "Last.fm CallXmlRpc no artistname provided."); return false; } if (title.IsEmpty()) { CLog::Log(LOGERROR, "Last.fm CallXmlRpc no tracktitle provided."); return false; } char ti[20]; time_t rawtime; time ( &rawtime ); struct tm *now = gmtime(&rawtime); strftime(ti, sizeof(ti), "%Y-%m-%d %H:%M:%S", now); CStdString strChallenge = ti; CStdString strAuth; CreateMD5Hash(strPassword, strAuth); strAuth.append(strChallenge); CreateMD5Hash(strAuth, strAuth); //create request xml TiXmlDocument doc; TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "UTF-8", "" ); doc.LinkEndChild( decl ); TiXmlElement * elMethodCall = new TiXmlElement( "methodCall" ); doc.LinkEndChild( elMethodCall ); TiXmlElement * elMethodName = new TiXmlElement( "methodName" ); elMethodCall->LinkEndChild( elMethodName ); TiXmlText * txtAction = new TiXmlText( action ); elMethodName->LinkEndChild( txtAction ); TiXmlElement * elParams = new TiXmlElement( "params" ); elMethodCall->LinkEndChild( elParams ); TiXmlElement * elParam = new TiXmlElement( "param" ); elParams->LinkEndChild( elParam ); TiXmlElement * elValue = new TiXmlElement( "value" ); elParam->LinkEndChild( elValue ); TiXmlElement * elString = new TiXmlElement( "string" ); elValue->LinkEndChild( elString ); TiXmlText * txtParam = new TiXmlText( strUserName ); elString->LinkEndChild( txtParam ); elParam = new TiXmlElement( "param" ); elParams->LinkEndChild( elParam ); elValue = new TiXmlElement( "value" ); elParam->LinkEndChild( elValue ); elString = new TiXmlElement( "string" ); elValue->LinkEndChild( elString ); txtParam = new TiXmlText( strChallenge ); elString->LinkEndChild( txtParam ); elParam = new TiXmlElement( "param" ); elParams->LinkEndChild( elParam ); elValue = new TiXmlElement( "value" ); elParam->LinkEndChild( elValue ); elString = new TiXmlElement( "string" ); elValue->LinkEndChild( elString ); txtParam = new TiXmlText( strAuth ); elString->LinkEndChild( txtParam ); elParam = new TiXmlElement( "param" ); elParams->LinkEndChild( elParam ); elValue = new TiXmlElement( "value" ); elParam->LinkEndChild( elValue ); elString = new TiXmlElement( "string" ); elValue->LinkEndChild( elString ); txtParam = new TiXmlText( artist ); elString->LinkEndChild( txtParam ); elParam = new TiXmlElement( "param" ); elParams->LinkEndChild( elParam ); elValue = new TiXmlElement( "value" ); elParam->LinkEndChild( elValue ); elString = new TiXmlElement( "string" ); elValue->LinkEndChild( elString ); txtParam = new TiXmlText( title ); elString->LinkEndChild( txtParam ); CStdString strBody; strBody << doc; CHTTP http; CStdString html; CStdString url = "http://ws.audioscrobbler.com/1.0/rw/xmlrpc.php"; http.SetContentType("text/xml"); if (!http.Post(url, strBody, html)) { CLog::Log(LOGERROR, "Last.fm action %s failed.", action.c_str()); return false; } if (html.Find("fault") >= 0) { CLog::Log(LOGERROR, "Last.fm return failed response: %s", html.c_str()); return false; } return true; }
bool CLastFmManager::RequestRadioTracks() { DWORD start = timeGetTime(); CStdString url; CStdString html; url.Format("http://" + m_RadioBaseUrl + m_RadioBasePath + "/xspf.php?sk=%s&discovery=0&desktop=", m_RadioSession); { CHTTP http; if (!http.Get(url, html)) { m_RadioSession.empty(); CLog::Log(LOGERROR, "LastFmManager: Connect to Last.fm to request tracks failed."); return false; } } //CLog::DebugLog("RequestRadioTracks: %s", html.c_str()); //parse playlist TiXmlDocument xmlDoc; xmlDoc.Parse(html); if (xmlDoc.Error()) { m_RadioSession.empty(); CLog::Log(LOGERROR, "LastFmManager: Unable to parse tracklist Error: %s", xmlDoc.ErrorDesc()); return false; } TiXmlElement* pRootElement = xmlDoc.RootElement(); if (!pRootElement ) { CLog::Log(LOGWARNING, "LastFmManager: No more tracks received"); m_RadioSession.empty(); return false; } TiXmlElement* pBodyElement = pRootElement->FirstChildElement("trackList"); if (!pBodyElement ) { CLog::Log(LOGWARNING, "LastFmManager: No more tracks received, no tracklist"); m_RadioSession.empty(); return false; } TiXmlElement* pTrackElement = pBodyElement->FirstChildElement("track"); if (!pTrackElement) { CLog::Log(LOGWARNING, "LastFmManager: No more tracks received, empty tracklist"); m_RadioSession.empty(); return false; } while (pTrackElement) { CFileItemPtr newItem(new CFileItem); TiXmlElement* pElement = pTrackElement->FirstChildElement("location"); if (pElement) { TiXmlNode* child = pElement->FirstChild(); if (child) { CStdString url = child->Value(); url.Replace("http:", "lastfm:"); newItem->m_strPath = url; } } pElement = pTrackElement->FirstChildElement("title"); if (pElement) { TiXmlNode* child = pElement->FirstChild(); if (child) { newItem->SetLabel(child->Value()); newItem->GetMusicInfoTag()->SetTitle(child->Value()); } } pElement = pTrackElement->FirstChildElement("creator"); if (pElement) { TiXmlNode* child = pElement->FirstChild(); if (child) { newItem->GetMusicInfoTag()->SetArtist(child->Value()); } } pElement = pTrackElement->FirstChildElement("album"); if (pElement) { TiXmlNode* child = pElement->FirstChild(); if (child) { newItem->GetMusicInfoTag()->SetAlbum(child->Value()); } } pElement = pTrackElement->FirstChildElement("duration"); if (pElement) { TiXmlNode* child = pElement->FirstChild(); if (child) { int iDuration = atoi(child->Value())/1000; newItem->GetMusicInfoTag()->SetDuration(iDuration); } } newItem->FillInDefaultIcon(); pElement = pTrackElement->FirstChildElement("image"); if (pElement) { TiXmlNode* child = pElement->FirstChild(); if (child) { CStdString coverUrl = child->Value(); if ((coverUrl != "") && (coverUrl.Find("noimage") == -1) && (coverUrl.Right(1) != "/")) { newItem->SetThumbnailImage(coverUrl); } } } { CSingleLock lock(m_lockCache); m_RadioTrackQueue->Add(newItem); } pTrackElement = pTrackElement->NextSiblingElement(); } //end parse CSingleLock lock(m_lockCache); int iNrCachedTracks = m_RadioTrackQueue->size(); CLog::Log(LOGDEBUG, "%s: Done (time: %i ms)", __FUNCTION__, (int)(timeGetTime() - start)); return iNrCachedTracks > 0; }
bool CLastFmManager::ChangeStation(const CURL& stationUrl) { DWORD start = timeGetTime(); CStdString strUrl; stationUrl.GetURL(strUrl); InitProgressDialog(strUrl); StopRadio(false); if (!RadioHandShake()) { CloseProgressDialog(); CGUIDialogOK::ShowAndGetInput(15200, 15206, 0, 0); return false; } UpdateProgressDialog(15252); // Selecting station... CHTTP http; CStdString url; CStdString html; url.Format("http://" + m_RadioBaseUrl + m_RadioBasePath + "/adjust.php?session=%s&url=%s&debug=%i", m_RadioSession, strUrl, 0); if (!http.Get(url, html)) { CLog::Log(LOGERROR, "Connect to Last.fm to change station failed."); CloseProgressDialog(); return false; } //CLog::DebugLog("ChangeStation: %s", html.c_str()); CStdString strErrorCode; Parameter("error", html, strErrorCode); if (strErrorCode != "") { CLog::Log(LOGERROR, "Last.fm returned an error (%s) response for change station request.", strErrorCode.c_str()); CloseProgressDialog(); return false; } UpdateProgressDialog(261); //Waiting for start.... g_playlistPlayer.ClearPlaylist(PLAYLIST_MUSIC); g_playlistPlayer.SetShuffle(PLAYLIST_MUSIC, false); g_playlistPlayer.SetRepeat(PLAYLIST_MUSIC, PLAYLIST::REPEAT_NONE); RequestRadioTracks(); CacheTrackThumb(XBMC_LASTFM_MINTRACKS); AddToPlaylist(XBMC_LASTFM_MINTRACKS); Create(); //start thread SetEvent(m_hWorkerEvent); //kickstart the thread CSingleLock lock(m_lockPlaylist); CPlayList& playlist = g_playlistPlayer.GetPlaylist(PLAYLIST_MUSIC); if ((int)playlist.size()) { g_application.m_strPlayListFile = strUrl; //needed to highlight the playing item g_playlistPlayer.SetCurrentPlaylist(PLAYLIST_MUSIC); g_playlistPlayer.Play(0); CLog::Log(LOGDEBUG, "%s: Done (time: %i ms)", __FUNCTION__, (int)(timeGetTime() - start)); CloseProgressDialog(); return true; } CloseProgressDialog(); return false; }
void CDownloadQueue::Process() { CLog::Log(LOGNOTICE, "DownloadQueue ready."); CHTTP http; bool bSuccess; while ( !m_bStop ) { while ( CDownloadQueue::Size() > 0 ) { EnterCriticalSection(&m_critical); // get the first item, but don't pop it from our queue // so that the download can be interrupted Command request = m_queue.front(); LeaveCriticalSection(&m_critical); bool bFileRequest = request.content.length() > 0; DWORD dwSize = 0; if (bFileRequest) { ::DeleteFile(request.content.c_str()); bSuccess = http.Download(request.location, request.content, &dwSize); } else { bSuccess = http.Get(request.location, request.content); } // now re-grab the item as we may have cancelled our download // while we were working EnterCriticalSection(&m_critical); request = m_queue.front(); m_queue.pop(); // if the request has been cancelled our observer will be NULL if (NULL != request.observer) { try { if (bFileRequest) { request.observer->OnFileComplete(request.ticket, request.content, dwSize, bSuccess ? IDownloadQueueObserver::Succeeded : IDownloadQueueObserver::Failed ); } else { request.observer->OnContentComplete(request.ticket, request.content, bSuccess ? IDownloadQueueObserver::Succeeded : IDownloadQueueObserver::Failed ); } } catch (...) { CLog::Log(LOGERROR, "exception while updating download observer."); if (bFileRequest) { ::DeleteFile(request.content.c_str()); } } } LeaveCriticalSection(&m_critical); } Sleep(500); } CLog::Log(LOGNOTICE, "DownloadQueue terminated."); }
void CRssReader::Process() { while (m_vecQueue.size()) { int iFeed = m_vecQueue.front(); m_vecQueue.erase(m_vecQueue.begin()); m_strFeed[iFeed] = ""; m_strColors[iFeed] = ""; CHTTP http; http.SetUserAgent("XBMC/pre-2.1 (http://www.xboxmediacenter.com)"); CStdString strXML; CStdString strUrl = m_vecUrls[iFeed]; int nRetries = 3; CURL url(strUrl); if ((url.GetProtocol() == "http" || url.GetProtocol() == "https") && (!g_guiSettings.GetBool("network.enableinternet") || !g_application.getNetwork().IsAvailable())) strXML = "<rss><item><title>"+g_localizeStrings.Get(15301)+"</title></item></rss>"; else { while ( (!m_bStop) && (nRetries > 0) ) { nRetries--; if (url.GetProtocol() != "http" && url.GetProtocol() != "https") { CFile file; if (file.Open(strUrl)) { char *yo = new char[(int)file.GetLength()+1]; file.Read(yo,file.GetLength()); yo[file.GetLength()] = '\0'; strXML = yo; delete[] yo; break; } } else if (http.Get(strUrl, strXML)) { CLog::Log(LOGDEBUG, "Got rss feed: %s", strUrl.c_str()); break; } } } if ((!strXML.IsEmpty()) && m_pObserver) { // erase any <content:encoded> tags (also unsupported by tinyxml) int iStart = strXML.Find("<content:encoded>"); int iEnd = 0; while (iStart > 0) { // get <content:encoded> end position iEnd = strXML.Find("</content:encoded>", iStart) + 18; // erase the section strXML = strXML.erase(iStart, iEnd - iStart); iStart = strXML.Find("<content:encoded>"); } if (Parse((LPSTR)strXML.c_str(),iFeed)) { CLog::Log(LOGDEBUG, "Parsed rss feed: %s", strUrl.c_str()); } } } UpdateObserver(); }