From 0f767ffa668bed98b5be9249f9b5e970b3245397 Mon Sep 17 00:00:00 2001 From: Mark Trolley Date: Sun, 28 Jun 2020 16:04:29 -0400 Subject: [PATCH] fetchart: Use Cover Art Archive thumbnails The Cover Art Archive API offers pre-resized thumbnails of cover art. If the `maxwidth` option of `fetchart` matches one of the supported Cover Art Archive thumbnail sizes, and a thumbnail of that size exists in the Cover Art Archive, fetch it directly instead of fetching the full size image then resizing it. --- beetsplug/fetchart.py | 27 +++++++++++++++++++++++---- docs/changelog.rst | 3 +++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/beetsplug/fetchart.py b/beetsplug/fetchart.py index 70477a624..86c5b958f 100644 --- a/beetsplug/fetchart.py +++ b/beetsplug/fetchart.py @@ -305,6 +305,7 @@ class RemoteArtSource(ArtSource): class CoverArtArchive(RemoteArtSource): NAME = u"Cover Art Archive" VALID_MATCHING_CRITERIA = ['release', 'releasegroup'] + VALID_THUMBNAIL_SIZES = [250, 500, 1200] if util.SNI_SUPPORTED: URL = 'https://coverartarchive.org/release/{mbid}/front' @@ -317,13 +318,31 @@ class CoverArtArchive(RemoteArtSource): """Return the Cover Art Archive and Cover Art Archive release group URLs using album MusicBrainz release ID and release group ID. """ + release_url = self.URL.format(mbid=album.mb_albumid) + release_group_url = self.GROUP_URL.format(mbid=album.mb_releasegroupid) + + # Cover Art Archive API offers pre-resized thumbnails at several sizes. + # If the maxwidth config matches one of the already available sizes + # fetch it directly intead of fetching the full sized image and + # resizing it. + size_suffix = None + if plugin.maxwidth in self.VALID_THUMBNAIL_SIZES: + size_suffix = "-" + str(plugin.maxwidth) + if 'release' in self.match_by and album.mb_albumid: - yield self._candidate(url=self.URL.format(mbid=album.mb_albumid), + if size_suffix: + release_thumbnail_url = release_url + size_suffix + yield self._candidate(url=release_thumbnail_url, + match=Candidate.MATCH_EXACT) + yield self._candidate(url=release_url, match=Candidate.MATCH_EXACT) if 'releasegroup' in self.match_by and album.mb_releasegroupid: - yield self._candidate( - url=self.GROUP_URL.format(mbid=album.mb_releasegroupid), - match=Candidate.MATCH_FALLBACK) + if size_suffix: + release_group_thumbnail_url = release_group_url + size_suffix + yield self._candidate(url=release_group_thumbnail_url, + match=Candidate.MATCH_FALLBACK) + yield self._candidate(url=release_group_url, + match=Candidate.MATCH_FALLBACK) class Amazon(RemoteArtSource): diff --git a/docs/changelog.rst b/docs/changelog.rst index 73cd89fb8..b6904bb71 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -217,6 +217,9 @@ Fixes: results or fetched lyrics :bug:`2805` * Adapt to breaking changes in Python's ``ast`` module in 3.8 +* :doc:`/plugins/fetchart`: Attempt to fetch pre-resized thumbnails from Cover + Art Archive if the ``maxwidth`` option matches one of the sizes supported by + the Cover Art Archive API. For plugin developers: