// -------------------------------------------------------------------------------- // int guGoogleCoverFetcher::AddCoverLinks( int pagenum ) { wxString SearchString = wxString::Format( wxT( "\"%s\" \"%s\"" ), m_Artist.c_str(), m_Album.c_str() ); //guLogMessage( wxT( "URL: %u %s" ), m_CurrentPage, m_SearchString.c_str() ); wxString SearchUrl = wxString::Format( GOOGLE_IMAGES_SEARCH_URL, guURLEncode( SearchString ).c_str(), ( pagenum * GOOGLE_COVERS_PER_PAGE ) ); //guLogMessage( wxT( "URL: %u %s" ), pagenum, SearchUrl.c_str() ); if( !m_MainThread->TestDestroy() ) { wxString Content = GetUrlContent( SearchUrl ); if( Content.Length() ) { if( !m_MainThread->TestDestroy() ) { //guLogMessage( wxT( "Google:====>>>>\n%s\n<<<<====" ), Content.c_str() ); return ExtractImagesInfo( Content, GOOGLE_COVERS_PER_PAGE ); } } else { guLogError( wxT( "Could not get the remote data from connection" ) ); } } return 0; }
// -------------------------------------------------------------------------------- // int guAmazonCoverFetcher::AddCoverLinks( int pagenum ) { wxDateTime CurTime = wxDateTime::Now(); wxString SearchParams = wxString::Format( AMAZON_SEARCH_PARAMS, pagenum + 1, percentEncodeRfc3986( guURLEncode( m_Artist + wxT( " " ) + m_Album ) ).c_str(), guURLEncode( CurTime.ToUTC().Format( wxT( "%Y-%m-%dT%H:%M:%S.000Z" ) ) ).c_str() ); SearchParams.Replace( wxT( "," ), wxT( "%2C" ) ); wxString SignText = GetAmazonSign( SearchParams ); wxString SearchUrl = AMAZON_SEARCH_URL + SearchParams + wxT( "&Signature=" ) + SignText; //guLogMessage( wxT( "URL: %u %s" ), pagenum, SearchUrl.c_str() ); if( !m_MainThread->TestDestroy() ) { //printf( "Buffer:\n%s\n", Buffer ); wxString Content = GetUrlContent( SearchUrl ); //Content = http.GetContent( SearchUrl, 60 ); //guLogMessage( wxT( "Amazon Response:\n%s" ), Content.c_str() ); if( Content.Length() ) { if( !m_MainThread->TestDestroy() ) { return ExtractImagesInfo( Content ); } } else { guLogError( wxT( "Could not get the remote data from connection" ) ); } } return 0; }
// -------------------------------------------------------------------------------- // wxString GetUrlContent( const wxString &url, const wxString &referer, bool gzipped ) { wxCurlHTTP http; //char * Buffer; wxString RetVal = wxEmptyString; http.AddHeader( wxT( "User-Agent: " ) guDEFAULT_BROWSER_USER_AGENT ); http.AddHeader( wxT( "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" ) ); if( gzipped ) { http.AddHeader( wxT( "Accept Encoding: gzip,deflate" ) ); } http.AddHeader( wxT( "Accept-Charset: utf-8" ) ); if( !referer.IsEmpty() ) { http.AddHeader( wxT( "Referer: " ) + referer ); } //guLogMessage( wxT( "Getting content for %s" ), url.c_str() ); wxMemoryOutputStream Buffer; http.SetOpt( CURLOPT_FOLLOWLOCATION, 1 ); http.Get( Buffer, url ); if( Buffer.IsOk() ) { int ResponseCode = http.GetResponseCode(); //guLogMessage( wxT( "ResponseCode: %i" ), ResponseCode ); if( ResponseCode >= 300 && ResponseCode < 400 ) { //guLogMessage( wxT( "Response %u:\n%s\n%s" ), http.GetResponseCode(), http.GetResponseHeader().c_str(), http.GetResponseBody().c_str() ); wxString Location = http.GetResponseHeader(); int Pos = Location.Lower().Find( wxT( "location: " ) ); if( Pos != wxNOT_FOUND ) { Location = Location.Mid( Pos + 10 ); Location.Truncate( Location.Find( wxT( "\r\n" ) ) ); if( Location.StartsWith( wxT( "/" ) ) ) { wxURI Uri( url ); wxString NewURL; if( Uri.HasScheme() ) NewURL = Uri.GetScheme() + wxT( "://" ); NewURL += Uri.GetServer(); NewURL += Location; Location = NewURL; } return GetUrlContent( Location, referer, gzipped ); } } else if( ResponseCode >= 400 ) return wxEmptyString; wxString ResponseHeaders = http.GetResponseHeader(); //guLogMessage( wxT( "Response %u:\n%s\n%s" ), http.GetResponseCode(), http.GetResponseHeader().c_str(), http.GetResponseBody().c_str() ); if( ResponseHeaders.Lower().Find( wxT( "content-encoding: gzip" ) ) != wxNOT_FOUND ) { //guLogMessage( wxT( "Response Headers:\n%s" ), ResponseHeaders.c_str() ); wxMemoryInputStream Ins( Buffer ); wxZlibInputStream ZIn( Ins ); wxStringOutputStream Outs( &RetVal ); ZIn.Read( Outs ); } else { //RetVal = wxString( Buffer, wxConvUTF8 ); // wxStringOutputStream Outs( &RetVal ); // wxMemoryInputStream Ins( Buffer ); // Ins.Read( Outs ); if( Buffer.GetLength() ) { size_t Count = Buffer.GetLength(); const char * pData = ( const char * ) Buffer.GetOutputStreamBuffer()->GetBufferStart(); RetVal = wxString( pData, wxConvUTF8, Count ); if( RetVal.IsEmpty() ) { RetVal = wxString( pData, wxConvISO8859_1, Count ); if( RetVal.IsEmpty() ) { RetVal = wxString( pData, wxConvLibc, Count ); } } } } //free( Buffer ); } else { guLogError( wxT( "Could not get '%s'" ), url.c_str() ); } //guLogMessage( wxT( "Response:\n%s\n###############" ), RetVal.c_str() ); return RetVal; }
// -------------------------------------------------------------------------------- // guJamendoDownloadThread::ExitCode guJamendoDownloadThread::Entry() { int Count; int LoopCount = 0; guConfig * Config = ( guConfig * ) guConfig::Get(); int AudioFormat = Config->ReadNum( CONFIG_KEY_JAMENDO_AUDIOFORMAT, 1, CONFIG_PATH_JAMENDO ); wxString TorrentCmd = Config->ReadStr( CONFIG_KEY_JAMENDO_TORRENT_COMMAND, wxEmptyString, CONFIG_PATH_JAMENDO ); while( !TestDestroy() ) { m_CoversMutex.Lock(); Count = m_Covers.Count(); m_CoversMutex.Unlock(); size_t CurTime = wxGetLocalTimeMillis().GetLo(); if( Count ) { LoopCount = 0; wxString CoverFile = guPATH_JAMENDO_COVERS; CoverFile += wxString::Format( wxT( "%u.jpg" ), m_Covers[ 0 ] ); if( !wxFileExists( CoverFile ) ) { if( !wxDirExists( wxPathOnly( CoverFile ) + wxT( "/" ) ) ) { wxMkdir( wxPathOnly( CoverFile ) + wxT( "/" ), 0770 ); } wxString CoverUrl = wxString::Format( guJAMENDO_COVER_DOWNLOAD_URL, m_Covers[ 0 ], 300 ); DownloadImage( CoverUrl, CoverFile, 300 ); } if( wxFileExists( CoverFile ) ) { int CoverId = m_Db->AddCoverFile( CoverFile ); wxString query = wxString::Format( wxT( "UPDATE songs SET song_coverid = %u WHERE song_albumid = %u" ), CoverId, m_Covers[ 0 ] ); m_Db->ExecuteUpdate( query ); // Notify the panel that the cover is downloaded wxCommandEvent event( wxEVT_MENU, ID_JAMENDO_COVER_DOWNLAODED ); event.SetInt( m_Covers[ 0 ] ); wxPostEvent( m_MediaViewer, event ); } else { guLogMessage( wxT( "Could not get the jamendo cover art %s" ), CoverFile.c_str() ); } m_CoversMutex.Lock(); m_Covers.RemoveAt( 0 ); m_CoversMutex.Unlock(); } else { LoopCount++; if( LoopCount > 8 ) { break; } } if( TestDestroy() ) break; size_t Elapsed = wxGetLocalTimeMillis().GetLo() - CurTime; if( !( Elapsed > 1000 ) ) { Sleep( 1000 - Elapsed ); } // // Album Torrents // m_AlbumsMutex.Lock(); Count = m_Albums.Count(); m_AlbumsMutex.Unlock(); CurTime = wxGetLocalTimeMillis().GetLo(); if( Count ) { LoopCount = 0; wxString Url = wxString::Format( guJAMENDO_TORRENT_DOWNLOAD_URL, m_Albums[ 0 ] ); Url += AudioFormat ? guJAMENDO_DOWNLOAD_FORMAT_OGG : guJAMENDO_DOWNLOAD_FORMAT_MP3; //guLogMessage( wxT( "Getting %s" ), Url.c_str() ); wxString TorrentUrl = GetUrlContent( Url ); //guLogMessage( wxT( "Downloading '%s'" ), TorrentUrl.c_str() ); if( !TorrentUrl.IsEmpty() ) { wxString TmpFileName = wxFileName::CreateTempFileName( wxString::Format( wxT( "%u" ), m_Albums[ 0 ] ) ); TmpFileName += wxT( ".torrent" ); if( DownloadFile( TorrentUrl, TmpFileName ) ) { guExecute( TorrentCmd + wxT( " " ) + TmpFileName ); } } m_AlbumsMutex.Lock(); m_Albums.RemoveAt( 0 ); m_AlbumsMutex.Unlock(); } else { LoopCount++; if( LoopCount > 8 ) { break; } } if( TestDestroy() ) break; Elapsed = wxGetLocalTimeMillis().GetLo() - CurTime; if( !( Elapsed > 1000 ) ) { Sleep( 1000 - Elapsed ); } } return 0; }