From b49cd3b73b9356936b9f8c0e74e3a47131dcb33c Mon Sep 17 00:00:00 2001 From: Bruno Cauet Date: Thu, 15 Jan 2015 11:28:04 +0100 Subject: [PATCH] Lyrics plugin: fix google backend Give config on backends init so the Google backend can get the API key and Engine Id. Fix #1226 --- beetsplug/lyrics.py | 17 ++++++++++------- test/test_lyrics.py | 9 ++++++--- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/beetsplug/lyrics.py b/beetsplug/lyrics.py index 625e8fff1..5e51b2b7c 100644 --- a/beetsplug/lyrics.py +++ b/beetsplug/lyrics.py @@ -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') diff --git a/test/test_lyrics.py b/test/test_lyrics.py index 21eb87256..90e47aa89 100644 --- a/test/test_lyrics.py +++ b/test/test_lyrics.py @@ -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)