From 2db0796fadb248f6786e923d5dbdf0daf67387b2 Mon Sep 17 00:00:00 2001 From: ghbrown Date: Wed, 11 Jan 2023 13:20:54 -0600 Subject: [PATCH 1/7] Implement item_candidates for Discogs --- beetsplug/discogs.py | 49 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/beetsplug/discogs.py b/beetsplug/discogs.py index a474871ac..de6cf45f2 100644 --- a/beetsplug/discogs.py +++ b/beetsplug/discogs.py @@ -180,6 +180,55 @@ class DiscogsPlugin(BeetsPlugin): self._log.debug('Connection error in album search', exc_info=True) return [] + def item_candidates(self, item, artist, title): + """Returns a list of TrackInfo objects for Search API results + matching ``title`` and ``artist``. + :param item: Singleton item to be matched. + :type item: beets.library.Item + :param artist: The artist of the track to be matched. + :type artist: str + :param title: The title of the track to be matched. + :type title: str + :return: Candidate TrackInfo objects. + :rtype: list[beets.autotag.hooks.TrackInfo] + """ + if not self.discogs_client: + return + + query = f'{artist} {title}' + try: + albums = self.get_albums(query) + except DiscogsAPIError as e: + self._log.debug('API Error: {0} (query: {1})', e, query) + if e.status_code == 401: + self.reset_auth() + return self.item_candidates(item, artist, title) + else: + return [] + except CONNECTION_ERRORS: + self._log.debug('Connection error in track search', exc_info=True) + candidates = [] + for album_cur in albums: + self._log.debug(u'searching within album {0}', album_cur.album) + track_list = self.get_tracks_from_album(album_cur) + candidates += track_list + return candidates + + def get_tracks_from_album(self, album_info): + """Return a list of tracks in the release + """ + if not album_info: + return [] + + result = [] + for track_info in album_info.tracks: + # attach artist info if not provided + if not track_info['artist']: + track_info['artist'] = album_info.artist + track_info['artist_id'] = album_info.artist_id + result.append(track_info) + return result + @staticmethod def extract_release_id_regex(album_id): """Returns the Discogs_id or None.""" From 2e916404f93e739750a1d6c5cfadfa8e49c9bcd0 Mon Sep 17 00:00:00 2001 From: ghbrown Date: Thu, 12 Jan 2023 19:41:04 -0600 Subject: [PATCH 2/7] early exit; add data_source --- beetsplug/discogs.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/beetsplug/discogs.py b/beetsplug/discogs.py index de6cf45f2..435d67b40 100644 --- a/beetsplug/discogs.py +++ b/beetsplug/discogs.py @@ -195,6 +195,10 @@ class DiscogsPlugin(BeetsPlugin): if not self.discogs_client: return + if not artist and not title: + self._log.debug('Skipping Discogs query. File missing artist and ' + 'title tags.') + query = f'{artist} {title}' try: albums = self.get_albums(query) @@ -212,12 +216,14 @@ class DiscogsPlugin(BeetsPlugin): self._log.debug(u'searching within album {0}', album_cur.album) track_list = self.get_tracks_from_album(album_cur) candidates += track_list + for candidate in candidates: + candidate.data_source = 'Discogs' return candidates def get_tracks_from_album(self, album_info): """Return a list of tracks in the release """ - if not album_info: + if not album_info: return [] result = [] From 2df41b9e166bd3c5e449c9c55d026519f7d6258e Mon Sep 17 00:00:00 2001 From: ghbrown Date: Mon, 16 Jan 2023 18:43:26 -0600 Subject: [PATCH 3/7] Limit number of returned track candidates --- beetsplug/discogs.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/beetsplug/discogs.py b/beetsplug/discogs.py index 435d67b40..990c1d786 100644 --- a/beetsplug/discogs.py +++ b/beetsplug/discogs.py @@ -218,7 +218,8 @@ class DiscogsPlugin(BeetsPlugin): candidates += track_list for candidate in candidates: candidate.data_source = 'Discogs' - return candidates + # first 10 results, don't overwhelm with options + return candidates[:10] def get_tracks_from_album(self, album_info): """Return a list of tracks in the release From cbb1b214089fed6ac4c1e3a25336ef5c229cace9 Mon Sep 17 00:00:00 2001 From: ghbrown Date: Fri, 20 Jan 2023 21:07:15 -0600 Subject: [PATCH 4/7] Use tracks field in item_candidates; add more info to tracks of AlbumInfo --- beetsplug/discogs.py | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/beetsplug/discogs.py b/beetsplug/discogs.py index 990c1d786..44601e7e9 100644 --- a/beetsplug/discogs.py +++ b/beetsplug/discogs.py @@ -214,27 +214,11 @@ class DiscogsPlugin(BeetsPlugin): candidates = [] for album_cur in albums: self._log.debug(u'searching within album {0}', album_cur.album) - track_list = self.get_tracks_from_album(album_cur) - candidates += track_list + candidates += album_cur.tracks for candidate in candidates: candidate.data_source = 'Discogs' # first 10 results, don't overwhelm with options - return candidates[:10] - - def get_tracks_from_album(self, album_info): - """Return a list of tracks in the release - """ - if not album_info: - return [] - - result = [] - for track_info in album_info.tracks: - # attach artist info if not provided - if not track_info['artist']: - track_info['artist'] = album_info.artist - track_info['artist_id'] = album_info.artist_id - result.append(track_info) - return result + return candidates[:10] @staticmethod def extract_release_id_regex(album_id): @@ -407,9 +391,13 @@ class DiscogsPlugin(BeetsPlugin): for track in tracks: track.media = media track.medium_total = mediums.count(track.medium) + # artist info will be identical for all tracks until #3353 fixed + track.artist = artist + track.artist_id = artist_id # Discogs does not have track IDs. Invent our own IDs as proposed # in #2336. track.track_id = str(album_id) + "-" + track.track_alt + track.data_url = data_url # Retrieve master release id (returns None if there isn't one). master_id = result.data.get('master_id') From a99eb773373dd6816e566c03fe409012d1cb436a Mon Sep 17 00:00:00 2001 From: ghbrown Date: Fri, 20 Jan 2023 22:15:50 -0600 Subject: [PATCH 5/7] Improve where an how data added to tracks of album --- beetsplug/discogs.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/beetsplug/discogs.py b/beetsplug/discogs.py index 44601e7e9..103aa1107 100644 --- a/beetsplug/discogs.py +++ b/beetsplug/discogs.py @@ -215,8 +215,6 @@ class DiscogsPlugin(BeetsPlugin): for album_cur in albums: self._log.debug(u'searching within album {0}', album_cur.album) candidates += album_cur.tracks - for candidate in candidates: - candidate.data_source = 'Discogs' # first 10 results, don't overwhelm with options return candidates[:10] @@ -391,13 +389,15 @@ class DiscogsPlugin(BeetsPlugin): for track in tracks: track.media = media track.medium_total = mediums.count(track.medium) - # artist info will be identical for all tracks until #3353 fixed - track.artist = artist - track.artist_id = artist_id + if not track.artist: # get_track_info often fails to find artist + track.artist = artist + if not track.artist_id: + track.artist_id = artist_id # Discogs does not have track IDs. Invent our own IDs as proposed # in #2336. track.track_id = str(album_id) + "-" + track.track_alt track.data_url = data_url + track.data_source = 'Discogs' # Retrieve master release id (returns None if there isn't one). master_id = result.data.get('master_id') From 47fe387de1163a78dbb21e88acd83649f1af9402 Mon Sep 17 00:00:00 2001 From: ghbrown Date: Sat, 21 Jan 2023 20:56:44 -0600 Subject: [PATCH 6/7] Docs and changelog --- docs/changelog.rst | 1 + docs/plugins/discogs.rst | 8 +++----- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 66802d203..8311bffa9 100755 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -37,6 +37,7 @@ New features: ``=~``. :bug:`4251` * :doc:`/plugins/discogs`: Permit appending style to genre +* :doc:`plugins/discogs`: Implement item_candidates for matching singletons * :doc:`/plugins/convert`: Add a new `auto_keep` option that automatically converts files but keeps the *originals* in the library. :bug:`1840` :bug:`4302` diff --git a/docs/plugins/discogs.rst b/docs/plugins/discogs.rst index a9125e737..e424a4b04 100644 --- a/docs/plugins/discogs.rst +++ b/docs/plugins/discogs.rst @@ -2,8 +2,7 @@ Discogs Plugin ============== The ``discogs`` plugin extends the autotagger's search capabilities to -include matches from the `Discogs`_ database when importing albums. -(The plugin does not yet support matching singleton tracks.) +include matches from the `Discogs`_ database. .. _Discogs: https://discogs.com @@ -100,8 +99,7 @@ Here are two things you can try: * Make sure that your system clock is accurate. The Discogs servers can reject your request if your clock is too out of sync. -The plugin can only match albums, so no Discogs matches will be -reported when importing singletons using ``-s``. One possible -workaround is to use the ``--group-albums`` option. +Support for matching singleton tracks using ``-s`` is in progress. +If this is not working well, try the ``--group-albums`` option in album import mode. .. _python3-discogs-client: https://github.com/joalla/discogs_client From 429dfb3e7ad2793663e6a6a56dacc401698cc833 Mon Sep 17 00:00:00 2001 From: ghbrown Date: Sat, 28 Jan 2023 18:11:22 -0600 Subject: [PATCH 7/7] Fix docs phrasing; fix changelog formatting --- docs/changelog.rst | 4 ++-- docs/plugins/discogs.rst | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 8311bffa9..9587788fa 100755 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -36,8 +36,8 @@ New features: * Add :ref:`exact match ` queries, using the prefixes ``=`` and ``=~``. :bug:`4251` -* :doc:`/plugins/discogs`: Permit appending style to genre -* :doc:`plugins/discogs`: Implement item_candidates for matching singletons +* :doc:`/plugins/discogs`: Permit appending style to genre. +* :doc:`plugins/discogs`: Implement item_candidates for matching singletons. * :doc:`/plugins/convert`: Add a new `auto_keep` option that automatically converts files but keeps the *originals* in the library. :bug:`1840` :bug:`4302` diff --git a/docs/plugins/discogs.rst b/docs/plugins/discogs.rst index e424a4b04..1f0628072 100644 --- a/docs/plugins/discogs.rst +++ b/docs/plugins/discogs.rst @@ -99,7 +99,7 @@ Here are two things you can try: * Make sure that your system clock is accurate. The Discogs servers can reject your request if your clock is too out of sync. -Support for matching singleton tracks using ``-s`` is in progress. -If this is not working well, try the ``--group-albums`` option in album import mode. +Matching tracks by Discogs ID is not yet supported. The ``--group-albums`` +option in album import mode provides an alternative to singleton mode for autotagging tracks that are not in album-related folders. .. _python3-discogs-client: https://github.com/joalla/discogs_client