From 96b89e77f6e077eb70cc62c72be6043bf471259f Mon Sep 17 00:00:00 2001 From: AdrienCos Date: Sat, 7 Oct 2023 22:48:45 +0200 Subject: [PATCH] Fix the error with CAA where no image would be downloaded when thumbnails are missing --- beetsplug/fetchart.py | 13 +++++--- docs/changelog.rst | 3 ++ test/test_art.py | 70 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 4 deletions(-) diff --git a/beetsplug/fetchart.py b/beetsplug/fetchart.py index 13bebfb15..efa7077b2 100644 --- a/beetsplug/fetchart.py +++ b/beetsplug/fetchart.py @@ -399,10 +399,15 @@ class CoverArtArchive(RemoteArtSource): if 'Front' not in item['types']: continue - if preferred_width: - yield item['thumbnails'][preferred_width] - else: - yield item['image'] + # If there is a pre-sized thumbnail of the desired size + # we select it. Otherwise, we return the raw image. + image_url: str = item["image"] + if preferred_width is not None: + if isinstance(item.get("thumbnails"), dict): + image_url = item["thumbnails"].get( + preferred_width, image_url + ) + yield image_url except KeyError: pass diff --git a/docs/changelog.rst b/docs/changelog.rst index a21262f0f..a17d1c886 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -128,6 +128,9 @@ New features: * :doc:`/plugins/fetchart`: Fix the error with CoverArtArchive where the `maxwidth` option would not be used to download a pre-sized thumbnail for release groups, as is already done with releases. +* :doc:`/plugins/fetchart`: Fix the error with CoverArtArchive where no cover + would be found when the `maxwidth` option matches a pre-sized thumbnail size, + but no thumbnail is provided by CAA. We now fallback to the raw image. Bug fixes: diff --git a/test/test_art.py b/test/test_art.py index 4565ea879..62b7393a4 100644 --- a/test/test_art.py +++ b/test/test_art.py @@ -130,6 +130,39 @@ class CAAHelper(): } ], "release": "https://musicbrainz.org/release/releaseid" +}""" + RESPONSE_RELEASE_WITHOUT_THUMBNAILS = """{ + "images": [ + { + "approved": false, + "back": false, + "comment": "GIF", + "edit": 12345, + "front": true, + "id": 12345, + "image": "http://coverartarchive.org/release/rid/12345.gif", + "types": [ + "Front" + ] + }, + { + "approved": false, + "back": false, + "comment": "", + "edit": 12345, + "front": false, + "id": 12345, + "image": "http://coverartarchive.org/release/rid/12345.jpg", + "thumbnails": { + "large": "http://coverartarchive.org/release/rgid/12345-500.jpg", + "small": "http://coverartarchive.org/release/rgid/12345-250.jpg" + }, + "types": [ + "Front" + ] + } + ], + "release": "https://musicbrainz.org/release/releaseid" }""" RESPONSE_GROUP = """{ "images": [ @@ -155,6 +188,23 @@ class CAAHelper(): ], "release": "https://musicbrainz.org/release/release-id" }""" + RESPONSE_GROUP_WITHOUT_THUMBNAILS = """{ + "images": [ + { + "approved": false, + "back": false, + "comment": "", + "edit": 12345, + "front": true, + "id": 12345, + "image": "http://coverartarchive.org/release/releaseid/12345.jpg", + "types": [ + "Front" + ] + } + ], + "release": "https://musicbrainz.org/release/release-id" + }""" def mock_caa_response(self, url, json): responses.add(responses.GET, url, body=json, @@ -537,6 +587,26 @@ class CoverArtArchiveTest(UseThePlugin, CAAHelper): for candidate in candidates: self.assertTrue(f"-{maxwidth}.jpg" in candidate.url) + def test_caa_finds_image_if_maxwidth_is_set_and_thumbnails_is_empty(self): + # CAA provides pre-sized thumbnails of width 250px, 500px, and 1200px + # We only test with one of them here + maxwidth = 1200 + self.settings = Settings(maxwidth=maxwidth) + + album = _common.Bag( + mb_albumid=self.MBID_RELASE, mb_releasegroupid=self.MBID_GROUP + ) + self.mock_caa_response( + self.RELEASE_URL, self.RESPONSE_RELEASE_WITHOUT_THUMBNAILS + ) + self.mock_caa_response( + self.GROUP_URL, self.RESPONSE_GROUP_WITHOUT_THUMBNAILS, + ) + candidates = list(self.source.get(album, self.settings, [])) + self.assertEqual(len(candidates), 3) + for candidate in candidates: + self.assertFalse(f"-{maxwidth}.jpg" in candidate.url) + class FanartTVTest(UseThePlugin): RESPONSE_MULTIPLE = """{