~ubuntu-branches/ubuntu/trusty/guayadeque/trusty

« back to all changes in this revision

Viewing changes to src/.svn/text-base/Google.cpp.svn-base

  • Committer: Bazaar Package Importer
  • Author(s): Alessio Treglia
  • Date: 2011-02-09 13:39:39 UTC
  • mfrom: (3.1.2 maverick)
  • Revision ID: james.westby@ubuntu.com-20110209133939-w4i25g4hvsgw4yvw
Tags: 0.2.7-3
* Upload to unstable.
* Pass -Wl,--as-needed to the linker.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// -------------------------------------------------------------------------------- //
 
2
//      Copyright (C) 2008-2010 J.Rios
 
3
//      anonbeat@gmail.com
 
4
//
 
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)
 
8
//    any later version.
 
9
//
 
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.
 
14
//
 
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
 
19
//
 
20
// -------------------------------------------------------------------------------- //
 
21
#include "Google.h"
 
22
 
 
23
#include "CoverEdit.h"
 
24
#include "Utils.h"
 
25
 
 
26
#include <wx/arrimpl.cpp>
 
27
#include <wx/curl/http.h>
 
28
#include <wx/statline.h>
 
29
#include <wx/html/htmlpars.h>
 
30
 
 
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" )
 
33
 
 
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
 
38
 
 
39
// -------------------------------------------------------------------------------- //
 
40
guGoogleCoverFetcher::guGoogleCoverFetcher( guFetchCoverLinksThread * mainthread, guArrayStringArray * coverlinks,
 
41
                                    const wxChar * artist, const wxChar * album ) :
 
42
    guCoverFetcher( mainthread, coverlinks, artist, album )
 
43
{
 
44
}
 
45
 
 
46
// -------------------------------------------------------------------------------- //
 
47
wxArrayString guGoogleCoverFetcher::ExtractImageInfo( const wxString &content )
 
48
{
 
49
    wxArrayString RetVal;
 
50
    wxString CurParam;
 
51
    CurParam = wxEmptyString;
 
52
    wxChar CurChar;
 
53
    int index;
 
54
    int count = content.Length();
 
55
    for( index = 0; index < count; index++ )
 
56
    {
 
57
        CurChar = content[ index ];
 
58
        if( CurChar == wxT( '\"' ) )
 
59
        {
 
60
            index++;
 
61
            while( ( CurChar = content[ index ] ) != wxT( '\"' ) && ( index < count ) )
 
62
            {
 
63
                CurParam.Append( CurChar );
 
64
                index++;
 
65
            }
 
66
        }
 
67
        else if( CurChar == wxT( ',' ) /*|| CurChar == wxT( ')' )*/ )
 
68
        {
 
69
            //guLogMessage( wxT( "%i= '%s'" ), RetVal.Count(), CurParam.c_str() );
 
70
            RetVal.Add( CurParam );
 
71
            CurParam = wxEmptyString;
 
72
        }
 
73
        else
 
74
        {
 
75
            CurParam.Append( CurChar );
 
76
        }
 
77
    }
 
78
    if( !CurParam.IsEmpty() )
 
79
        RetVal.Add( CurParam );
 
80
    //guLogMessage( wxT( "ImageLink: %s" ), RetVal[ 3 ].c_str() );
 
81
    return RetVal;
 
82
}
 
83
 
 
84
// -------------------------------------------------------------------------------- //
 
85
wxString ExtractCoverFromGoogleLink( wxString &link )
 
86
{
 
87
    int StrPos = link.Find( wxT( "/imgres?imgurl\\x3d" ) );
 
88
    if( StrPos != wxNOT_FOUND )
 
89
    {
 
90
        StrPos += 18;
 
91
        wxString RetVal = link.Mid( StrPos );
 
92
        StrPos = RetVal.Find( wxT( "\\x26imgrefurl" ) );
 
93
        if( StrPos != wxNOT_FOUND )
 
94
        {
 
95
            return RetVal.Mid( 0, StrPos );
 
96
        }
 
97
    }
 
98
    return wxEmptyString;
 
99
}
 
100
 
 
101
// -------------------------------------------------------------------------------- //
 
102
int guGoogleCoverFetcher::ExtractImagesInfo( wxString &content, int count )
 
103
{
 
104
    wxArrayString CurImage;
 
105
    wxArrayString GoogleImage;
 
106
    int ImageIndex = 0;
 
107
 
 
108
    int StrPos = content.Find( wxT( "dyn.setResults([[" ) );
 
109
 
 
110
    if( StrPos != wxNOT_FOUND )
 
111
        StrPos += 14;
 
112
    //guLogMessage( wxT( "Content:\n%s" ), Content.c_str() );
 
113
    while( ( StrPos != wxNOT_FOUND ) && !m_MainThread->TestDestroy() )
 
114
    {
 
115
        content = content.Mid( StrPos + 3 );
 
116
        StrPos = content.Find( wxT( "],[" ) );
 
117
        if( StrPos == wxNOT_FOUND )
 
118
          return 0; //break;
 
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 );
 
123
        CurImage.Empty();
 
124
        if( GoogleImage[ GOOGLE_COVERINFO_LINK ].IsEmpty() )
 
125
        {
 
126
            GoogleImage[ GOOGLE_COVERINFO_LINK ] = ExtractCoverFromGoogleLink( GoogleImage[ 0 ] );
 
127
        }
 
128
 
 
129
        if( !GoogleImage[ GOOGLE_COVERINFO_LINK ].IsEmpty() )
 
130
        {
 
131
            CurImage.Add( GoogleImage[ GOOGLE_COVERINFO_LINK ] );
 
132
            CurImage.Add( GoogleImage[ GOOGLE_COVERINFO_SIZE ] );
 
133
            m_CoverLinks->Add( CurImage );
 
134
            ImageIndex++;
 
135
            if( ImageIndex == count )
 
136
                break;
 
137
        }
 
138
 
 
139
        //guLogMessage( wxT( "Pos: %u" ), StrPos );
 
140
    }
 
141
    return ImageIndex;
 
142
}
 
143
 
 
144
// -------------------------------------------------------------------------------- //
 
145
int guGoogleCoverFetcher::AddCoverLinks( int pagenum )
 
146
{
 
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() )
 
152
    {
 
153
        //printf( "Buffer:\n%s\n", Buffer );
 
154
        wxString Content = GetUrlContent( SearchUrl );
 
155
        if( Content.Length() )
 
156
        {
 
157
            if( !m_MainThread->TestDestroy() )
 
158
            {
 
159
                //guLogMessage( Content );
 
160
                return ExtractImagesInfo( Content, GOOGLE_COVERS_PER_PAGE );
 
161
            }
 
162
        }
 
163
        else
 
164
        {
 
165
            guLogError( wxT( "Could not get the remote data from connection" ) );
 
166
        }
 
167
    }
 
168
    return 0;
 
169
}
 
170
 
 
171
// -------------------------------------------------------------------------------- //