From dfa84459807908c977620af992d6b305cde8b347 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Sat, 17 May 2014 22:10:39 -0700 Subject: [PATCH] Style and wording for Google Images (#766) --- beetsplug/fetchart.py | 23 +++++++++++++++-------- docs/changelog.rst | 2 ++ docs/plugins/fetchart.rst | 16 +++++++++------- test/test_art.py | 21 ++++++++++++--------- 4 files changed, 38 insertions(+), 24 deletions(-) diff --git a/beetsplug/fetchart.py b/beetsplug/fetchart.py index e3e9a8bd3..3658d4afb 100644 --- a/beetsplug/fetchart.py +++ b/beetsplug/fetchart.py @@ -122,16 +122,24 @@ def aao_art(asin): else: log.debug(u'fetchart: no image found on page') -# googleapis.org scraper. + +# Google Images scraper. GOOGLE_URL = 'https://ajax.googleapis.com/ajax/services/search/images' + def google_art(album): - """Return art URL from google.org given an album title and interpreter""" - + """Return art URL from google.org given an album title and + interpreter. + """ search_string = (album.albumartist + ',' + album.album).encode('utf-8') - response = requests_session.get(GOOGLE_URL, params={'v': '1.0','q': search_string, 'start':'0'}) - # Get results using JSON + response = requests_session.get(GOOGLE_URL, params={ + 'v': '1.0', + 'q': search_string, + 'start': '0', + }) + + # Get results using JSON. try: results = response.json() data = results['responseData'] @@ -199,8 +207,7 @@ def _source_urls(album): if url: yield url - google_search = config['fetchart']['google_search'].get(bool) - if google_search == True: + if config['fetchart']['google_search']: url = google_art(album) if url: yield url @@ -277,7 +284,7 @@ class FetchArtPlugin(BeetsPlugin): 'maxwidth': 0, 'remote_priority': False, 'cautious': False, - 'google_search' : False, + 'google_search': False, 'cover_names': ['cover', 'front', 'art', 'album', 'folder'], }) diff --git a/docs/changelog.rst b/docs/changelog.rst index 0435740fa..a356c3664 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -12,6 +12,8 @@ Changelog * Compatibility with the latest version of Mutagen, 1.23. * Support for AIFF files. Tags are stored as ID3 frames in one of the file's IFF chunks. +* :doc:`/plugins/fetchart`: You can now optionally search for cover art on + Google Image Search. Thanks to Lemutar. 1.3.6 (May 10, 2014) diff --git a/docs/plugins/fetchart.rst b/docs/plugins/fetchart.rst index a108b8e14..37d1b9056 100644 --- a/docs/plugins/fetchart.rst +++ b/docs/plugins/fetchart.rst @@ -96,16 +96,18 @@ When you choose to apply changes during an import, beets will search for art as described above. For "as-is" imports (and non-autotagged imports using the ``-A`` flag), beets only looks for art on the local filesystem. -Album Art with Google Image Search ------------------------------------ +Google Image Search +''''''''''''''''''' -If no art is found on the common sources there is the possibility get "the" or "a" album -art form google image search. +You can optionally search for cover art on `Google Images`_. This option uses +the first hit for a search query consisting of the artist and album name. It +is therefore approximate: "incorrect" image matches are possible (although +unlikely). -If enabled this function take the first hit with the search text "," on google image search as album art +.. _Google Images: http://images.google.com/ -To enable art gathering form google, just put this in your configuration -file:: +To enable gathering art from Google, enable the ``google_search`` option in +your config file:: fetchart: google_search: true diff --git a/test/test_art.py b/test/test_art.py index 48ded505a..6d81496cc 100644 --- a/test/test_art.py +++ b/test/test_art.py @@ -187,33 +187,36 @@ class AAOTest(_common.TestCase): self.mock_response(self.AAO_URL, 'blah blah') res = fetchart.aao_art(self.ASIN) self.assertEqual(res, None) - + + class GoogleImageTest(_common.TestCase): - GOOGLE_URL = 'https://ajax.googleapis.com/ajax/services/search/images' + _google_url = 'https://ajax.googleapis.com/ajax/services/search/images' @responses.activate def run(self, *args, **kwargs): super(GoogleImageTest, self).run(*args, **kwargs) def mock_response(self, url, json): - responses.add(responses.GET, url, body=json, content_type='application/json') - + responses.add(responses.GET, url, body=json, + content_type='application/json') def test_google_art_finds_image(self): - album = _common.Bag(albumartist="some artist",album="some album") - json = """{"responseData": {"results": [{"unescapedUrl": "url_to_the_image"}]}}""" - self.mock_response(self.GOOGLE_URL, json) + album = _common.Bag(albumartist="some artist", album="some album") + json = """{"responseData": {"results": + [{"unescapedUrl": "url_to_the_image"}]}}""" + self.mock_response(self._google_url, json) result_url = fetchart.google_art(album) self.assertEqual(result_url, 'url_to_the_image') def test_google_art_dont_finds_image(self): - album = _common.Bag(albumartist="some artist",album="some album") + album = _common.Bag(albumartist="some artist", album="some album") json = """bla blup""" - self.mock_response(self.GOOGLE_URL, json) + self.mock_response(self._google_url, json) result_url = fetchart.google_art(album) self.assertEqual(result_url, None) + class ArtImporterTest(_common.TestCase): def setUp(self): super(ArtImporterTest, self).setUp()