From 72263a1cf71dbcb54022ae6dae14a3debfe41f6e Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Sun, 3 Mar 2013 17:08:07 -0800 Subject: [PATCH] mbsync: use ID lookups instead of full match logic This change uses _album_for_id and _track_for_id instead of the full autotag.match.* functions. This should be faster (requiring fewer calls to the MusicBrainz API) while also being more predictable. It also won't, for example, use acoustic fingerprinting even if the chroma plugin is installed. Finally, this change catches the error case in which MBIDs are erroneous. This can happen, for example, if the user has some track MBIDs left over from before the NGS transition. --- beetsplug/mbsync.py | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/beetsplug/mbsync.py b/beetsplug/mbsync.py index 96dc79319..bbf653f93 100644 --- a/beetsplug/mbsync.py +++ b/beetsplug/mbsync.py @@ -18,6 +18,7 @@ import logging from beets.plugins import BeetsPlugin from beets import autotag, library, ui, util +from beets.autotag import hooks log = logging.getLogger('beets') @@ -54,14 +55,20 @@ def mbsync_singletons(lib, query, move, pretend, write): for s in lib.items(singletons_query): if not s.mb_trackid: log.info(u'Skipping singleton {0}: has no mb_trackid' - .format(s.title)) + .format(s.title)) continue s.old_data = dict(s.record) - candidates, _ = autotag.match.tag_item(s, search_id=s.mb_trackid) - match = candidates[0] + + # Get the MusicBrainz recording info. + track_info = hooks._track_for_id(s.mb_trackid) + if not track_info: + log.info(u'Recording ID not found: {0}'.format(s.mb_trackid)) + continue + + # Apply. with lib.transaction(): - autotag.apply_item_metadata(s, match.info) + autotag.apply_item_metadata(s, track_info) _print_and_apply_changes(lib, s, move, pretend, write) @@ -78,12 +85,24 @@ def mbsync_albums(lib, query, move, pretend, write): for item in items: item.old_data = dict(item.record) - _, _, candidates, _ = \ - autotag.match.tag_album(items, search_id=a.mb_albumid) - match = candidates[0] # There should only be one match! + # Get the MusicBrainz album information. + album_info = hooks._album_for_id(a.mb_albumid) + if not album_info: + log.info(u'Release ID not found: {0}'.format(a.mb_albumid)) + continue + # Construct a track mapping according to MBIDs. This should work + # for albums that have missing or extra tracks. + mapping = {} + for item in items: + for track_info in album_info.tracks: + if item.mb_trackid == track_info.track_id: + mapping[item] = track_info + break + + # Apply. with lib.transaction(): - autotag.apply_metadata(match.info, match.mapping) + autotag.apply_metadata(album_info, mapping) for item in items: _print_and_apply_changes(lib, item, move, pretend, write)