def fetch_google(artist, title):
 query = (u'%s %s' % (artist, title))
 api_key = config['lyrics']['google_API_key'].get(unicode)
 engine_id = config['lyrics']['google_engine_ID'].get(unicode)
 url = (u'https://www.googleapis.com/customsearch/v1?key=%s&cx=%s&q=%s' % (api_key, engine_id, urllib.quote(query.encode('utf8'))))
 data = urllib.urlopen(url)
 data = json.load(data)
 if ('error' in data):
  reason = data['error']['errors'][0]['reason']
  log.debug(u'google lyrics backend error: {0}'.format(reason))
  return
 if ('items' in data.keys()):
  for item in data['items']:
   urlLink = item['link']
   urlTitle = item.get('title', u'')
   if (not is_page_candidate(urlLink, urlTitle, title, artist)):
    continue
   html = fetch_url(urlLink)
   lyrics = scrape_lyrics_from_html(html)
   if (not lyrics):
    continue
   if is_lyrics(lyrics, artist):
    log.debug(u'got lyrics from {0}'.format(item['displayLink']))
    return lyrics

def fetch_google ( artist , title ) : query = ( u'%s %s' % ( artist , title ) ) api_key = config [ 'lyrics' ] [ 'google_API_key' ] . get ( unicode ) engine_id = config [ 'lyrics' ] [ 'google_engine_ID' ] . get ( unicode ) url = ( u'https://www.googleapis.com/customsearch/v1?key=%s&cx=%s&q=%s' % ( api_key , engine_id , urllib . quote ( query . encode ( 'utf8' ) ) ) ) data = urllib . urlopen ( url ) data = json . load ( data ) if ( 'error' in data ) : reason = data [ 'error' ] [ 'errors' ] [ 0 ] [ 'reason' ] log . debug ( u'google lyrics backend error: {0}' . format ( reason ) ) return if ( 'items' in data . keys ( ) ) : for item in data [ 'items' ] : urlLink = item [ 'link' ] urlTitle = item . get ( 'title' , u'' ) if ( not is_page_candidate ( urlLink , urlTitle , title , artist ) ) : continue html = fetch_url ( urlLink ) lyrics = scrape_lyrics_from_html ( html ) if ( not lyrics ) : continue if is_lyrics ( lyrics , artist ) : log . debug ( u'got lyrics from {0}' . format ( item [ 'displayLink' ] ) ) return lyrics
fetch lyrics from google search results .