diff --git a/beetsplug/fetchart.py b/beetsplug/fetchart.py index 4b4f53bea..e3e9a8bd3 100644 --- a/beetsplug/fetchart.py +++ b/beetsplug/fetchart.py @@ -122,6 +122,26 @@ def aao_art(asin): else: log.debug(u'fetchart: no image found on page') +# googleapis.org 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""" + + 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 + try: + results = response.json() + data = results['responseData'] + dataInfo = data['results'] + for myUrl in dataInfo: + return myUrl['unescapedUrl'] + except: + log.debug(u'fetchart: error scraping art page') + return + # Art from the filesystem. @@ -179,6 +199,12 @@ def _source_urls(album): if url: yield url + google_search = config['fetchart']['google_search'].get(bool) + if google_search == True: + url = google_art(album) + if url: + yield url + def art_for_album(album, paths, maxwidth=None, local_only=False): """Given an Album object, returns a path to downloaded art for the @@ -251,6 +277,7 @@ class FetchArtPlugin(BeetsPlugin): 'maxwidth': 0, 'remote_priority': False, 'cautious': False, + 'google_search' : False, 'cover_names': ['cover', 'front', 'art', 'album', 'folder'], }) diff --git a/docs/plugins/fetchart.rst b/docs/plugins/fetchart.rst index fce406fdb..a108b8e14 100644 --- a/docs/plugins/fetchart.rst +++ b/docs/plugins/fetchart.rst @@ -96,6 +96,21 @@ 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 +----------------------------------- + +If no art is found on the common sources there is the possibility get "the" or "a" album +art form google image search. + +If enabled this function take the first hit with the search text "," on google image search as album art + +To enable art gathering form google, just put this in your configuration +file:: + + fetchart: + google_search: true + + Embedding Album Art ------------------- diff --git a/test/test_art.py b/test/test_art.py index 01e786e6e..48ded505a 100644 --- a/test/test_art.py +++ b/test/test_art.py @@ -187,7 +187,32 @@ 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' + + @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') + + + 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) + 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") + json = """bla blup""" + 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):