1
// -------------------------------------------------------------------------------- //
2
// Copyright (C) 2008-2010 J.Rios
5
// This Program is free software; you can redistribute it and/or modify
6
// it under the terms of the GNU General Public License as published by
7
// the Free Software Foundation; either version 2, or (at your option)
10
// This Program is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
// GNU General Public License for more details.
15
// You should have received a copy of the GNU General Public License
16
// along with this program; see the file LICENSE. If not, write to
17
// the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18
// http://www.gnu.org/copyleft/gpl.html
20
// -------------------------------------------------------------------------------- //
23
#include "CoverEdit.h"
26
#include <wx/arrimpl.cpp>
27
#include <wx/curl/http.h>
28
#include <wx/statline.h>
29
#include <wx/html/htmlpars.h>
31
//#define GOOGLE_IMAGES_SEARCH_URL wxT( "http://images.google.com/images?imgsz=large|xlarge&q=%s&start=%u" )
32
#define GOOGLE_IMAGES_SEARCH_URL wxT( "http://images.google.com/images?&q=%s&sout=1&start=%u" )
34
#define GOOGLE_COVERS_PER_PAGE 15
35
#define GOOGLE_COVERINFO_LINK 3 // 3 -> Link
36
#define GOOGLE_COVERINFO_COMMENT 6 // 6 -> Comment
37
#define GOOGLE_COVERINFO_SIZE 9 // 9 -> Size >> 425 x 283 - 130 KB
39
// -------------------------------------------------------------------------------- //
40
guGoogleCoverFetcher::guGoogleCoverFetcher( guFetchCoverLinksThread * mainthread, guArrayStringArray * coverlinks,
41
const wxChar * artist, const wxChar * album ) :
42
guCoverFetcher( mainthread, coverlinks, artist, album )
46
// -------------------------------------------------------------------------------- //
47
wxArrayString guGoogleCoverFetcher::ExtractImageInfo( const wxString &content )
51
CurParam = wxEmptyString;
54
int count = content.Length();
55
for( index = 0; index < count; index++ )
57
CurChar = content[ index ];
58
if( CurChar == wxT( '\"' ) )
61
while( ( CurChar = content[ index ] ) != wxT( '\"' ) && ( index < count ) )
63
CurParam.Append( CurChar );
67
else if( CurChar == wxT( ',' ) /*|| CurChar == wxT( ')' )*/ )
69
//guLogMessage( wxT( "%i= '%s'" ), RetVal.Count(), CurParam.c_str() );
70
RetVal.Add( CurParam );
71
CurParam = wxEmptyString;
75
CurParam.Append( CurChar );
78
if( !CurParam.IsEmpty() )
79
RetVal.Add( CurParam );
80
//guLogMessage( wxT( "ImageLink: %s" ), RetVal[ 3 ].c_str() );
84
// -------------------------------------------------------------------------------- //
85
wxString ExtractCoverFromGoogleLink( wxString &link )
87
int StrPos = link.Find( wxT( "/imgres?imgurl\\x3d" ) );
88
if( StrPos != wxNOT_FOUND )
91
wxString RetVal = link.Mid( StrPos );
92
StrPos = RetVal.Find( wxT( "\\x26imgrefurl" ) );
93
if( StrPos != wxNOT_FOUND )
95
return RetVal.Mid( 0, StrPos );
101
// -------------------------------------------------------------------------------- //
102
int guGoogleCoverFetcher::ExtractImagesInfo( wxString &content, int count )
104
wxArrayString CurImage;
105
wxArrayString GoogleImage;
108
int StrPos = content.Find( wxT( "dyn.setResults([[" ) );
110
if( StrPos != wxNOT_FOUND )
112
//guLogMessage( wxT( "Content:\n%s" ), Content.c_str() );
113
while( ( StrPos != wxNOT_FOUND ) && !m_MainThread->TestDestroy() )
115
content = content.Mid( StrPos + 3 );
116
StrPos = content.Find( wxT( "],[" ) );
117
if( StrPos == wxNOT_FOUND )
119
//guLogMessage( wxT( "%s" ), Content.Mid( 0, StrPos ).c_str() );
120
wxHtmlEntitiesParser EntitiesParser;
121
GoogleImage = ExtractImageInfo( EntitiesParser.Parse( content.Mid( 0, StrPos ) ) );
122
//RetVal.Add( CurImage );
124
if( GoogleImage[ GOOGLE_COVERINFO_LINK ].IsEmpty() )
126
GoogleImage[ GOOGLE_COVERINFO_LINK ] = ExtractCoverFromGoogleLink( GoogleImage[ 0 ] );
129
if( !GoogleImage[ GOOGLE_COVERINFO_LINK ].IsEmpty() )
131
CurImage.Add( GoogleImage[ GOOGLE_COVERINFO_LINK ] );
132
CurImage.Add( GoogleImage[ GOOGLE_COVERINFO_SIZE ] );
133
m_CoverLinks->Add( CurImage );
135
if( ImageIndex == count )
139
//guLogMessage( wxT( "Pos: %u" ), StrPos );
144
// -------------------------------------------------------------------------------- //
145
int guGoogleCoverFetcher::AddCoverLinks( int pagenum )
147
wxString SearchString = wxString::Format( wxT( "\"%s\" \"%s\"" ), m_Artist.c_str(), m_Album.c_str() );
148
//guLogMessage( wxT( "URL: %u %s" ), m_CurrentPage, m_SearchString.c_str() );
149
wxString SearchUrl = wxString::Format( GOOGLE_IMAGES_SEARCH_URL, guURLEncode( SearchString ).c_str(), ( pagenum * GOOGLE_COVERS_PER_PAGE ) );
150
//guLogMessage( wxT( "URL: %u %s" ), pagenum, SearchUrl.c_str() );
151
if( !m_MainThread->TestDestroy() )
153
//printf( "Buffer:\n%s\n", Buffer );
154
wxString Content = GetUrlContent( SearchUrl );
155
if( Content.Length() )
157
if( !m_MainThread->TestDestroy() )
159
//guLogMessage( Content );
160
return ExtractImagesInfo( Content, GOOGLE_COVERS_PER_PAGE );
165
guLogError( wxT( "Could not get the remote data from connection" ) );
171
// -------------------------------------------------------------------------------- //