Lyrics plugin: fix google backend

Give config on backends init so the Google backend can get the API key
and Engine Id.
Fix #1226
This commit is contained in:
Bruno Cauet 2015-01-15 11:28:04 +01:00
parent c1ce71f35c
commit b49cd3b73b
2 changed files with 16 additions and 10 deletions

View file

@ -157,7 +157,7 @@ def search_pairs(item):
class Backend(object):
def __init__(self, log):
def __init__(self, config, log):
self._log = log
@staticmethod
@ -335,6 +335,11 @@ def scrape_lyrics_from_html(html):
class Google(Backend):
"""Fetch lyrics from Google search results."""
def __init__(self, config, log):
super(Google, self).__init__(config, log)
self.api_key = config['google_API_key'].get(unicode)
self.engine_id = config['google_engine_ID'].get(unicode)
def is_lyrics(self, text, artist=None):
"""Determine whether the text seems to be valid lyrics.
"""
@ -407,10 +412,8 @@ class Google(Backend):
def fetch(self, artist, title):
query = u"%s %s" % (artist, title)
api_key = self.config['google_API_key'].get(unicode)
engine_id = self.config['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')))
(self.api_key, self.engine_id, urllib.quote(query.encode('utf8')))
data = urllib.urlopen(url)
data = json.load(data)
@ -464,9 +467,9 @@ class LyricsPlugin(plugins.BeetsPlugin):
available_sources.remove('google')
self.config['sources'] = plugins.sanitize_choices(
self.config['sources'].as_str_seq(), available_sources)
self.backends = []
for key in self.config['sources'].as_str_seq():
self.backends.append(self.SOURCE_BACKENDS[key](self._log))
self.backends = [self.SOURCE_BACKENDS[key](self.config, self._log)
for key in self.config['sources'].as_str_seq()]
def commands(self):
cmd = ui.Subcommand('lyrics', help='fetch song lyrics')

View file

@ -18,6 +18,9 @@ import os
import _common
import sys
import re
from mock import MagicMock
from _common import unittest
from beetsplug import lyrics
from beets.library import Item
@ -25,8 +28,8 @@ from beets.util import confit
from beets import logging
log = logging.getLogger('beets.test_lyrics')
raw_backend = lyrics.Backend(log)
google = lyrics.Google(log)
raw_backend = lyrics.Backend({}, log)
google = lyrics.Google(MagicMock(), log)
class LyricsPluginTest(unittest.TestCase):
@ -337,7 +340,7 @@ class LyricsGooglePluginTest(unittest.TestCase):
lyrics.MusiXmatch], DEFAULT_SOURCES):
url = s['url'] + s['path']
if os.path.isfile(url_to_filename(url)):
res = source(log).fetch(s['artist'], s['title'])
res = source({}, log).fetch(s['artist'], s['title'])
self.assertTrue(google.is_lyrics(res), url)
self.assertTrue(is_lyrics_content_ok(s['title'], res), url)