From f17e8550ca4f4fcd53d70e0e54c0c5b1814d0247 Mon Sep 17 00:00:00 2001 From: Johannes Baiter Date: Tue, 28 May 2013 13:49:34 +0200 Subject: [PATCH] Determine IDs from MusicBrainz and Discogs URLs --- beets/autotag/mb.py | 11 +++++++++-- beetsplug/discogs.py | 9 +++++---- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/beets/autotag/mb.py b/beets/autotag/mb.py index 566340e83..6fe7854d3 100644 --- a/beets/autotag/mb.py +++ b/beets/autotag/mb.py @@ -16,6 +16,7 @@ """ import logging import musicbrainzngs +import re import traceback import beets.autotag.hooks @@ -326,13 +327,19 @@ def album_for_id(albumid): object or None if the album is not found. May raise a MusicBrainzAPIError. """ + # Find the first thing that looks like a UUID/MBID. + match = re.search('[a-f0-9]{8}(-[a-f0-9]{4}){3}-[a-f0-9]{12}', albumid) + if not match: + log.error('Invalid MBID.') + return None try: - res = musicbrainzngs.get_release_by_id(albumid, RELEASE_INCLUDES) + res = musicbrainzngs.get_release_by_id(match.group(), + RELEASE_INCLUDES) except musicbrainzngs.ResponseError: log.debug('Album ID match failed.') return None except musicbrainzngs.MusicBrainzError as exc: - raise MusicBrainzAPIError(exc, 'get release by ID', albumid, + raise MusicBrainzAPIError(exc, 'get release by ID', match.group(), traceback.format_exc()) return album_info(res['release']) diff --git a/beetsplug/discogs.py b/beetsplug/discogs.py index 0d30bb39d..2cd872e7f 100644 --- a/beetsplug/discogs.py +++ b/beetsplug/discogs.py @@ -64,12 +64,13 @@ class DiscogsPlugin(BeetsPlugin): """Fetches an album by its Discogs ID and returns an AlbumInfo object or None if the album is not found. """ - log.debug('Searching for release with id %s' % str(album_id)) + log.debug('Searching discogs for release %s' % str(album_id)) # discogs-client can handle both int and str, so we just strip # the leading 'r' that might be accidentally pasted into the form - if not isinstance(album_id, int) and album_id.startswith('r'): - album_id = album_id[1:] - result = Release(album_id) + match = re.search('\d+', album_id) + if not match: + return None + result = Release(match.group()) # Try to obtain title to verify that we indeed have a valid Release try: getattr(result, 'title')