mirror of
https://github.com/beetbox/beets.git
synced 2026-01-30 12:02:41 +01:00
Address requested changes from Adrian
This commit is contained in:
parent
c43a957f46
commit
308dccab95
2 changed files with 86 additions and 96 deletions
|
|
@ -24,6 +24,8 @@ from collections import defaultdict
|
|||
|
||||
import re
|
||||
|
||||
mb_regex = r"(\d|\w){8}-(\d|\w){4}-(\d|\w){4}-(\d|\w){4}-(\d|\w){12}"
|
||||
|
||||
|
||||
def apply_item_changes(lib, item, move, pretend, write):
|
||||
"""Store, move and write the item according to the arguments.
|
||||
|
|
@ -84,29 +86,25 @@ class MBSyncPlugin(BeetsPlugin):
|
|||
item_formatted)
|
||||
continue
|
||||
|
||||
valid_trackid = re.match(
|
||||
r"(\d|\w){8}-(\d|\w){4}-(\d|\w){4}-(\d|\w){4}-(\d|\w){12}",
|
||||
item.mb_trackid)
|
||||
valid_trackid = re.match(mb_regex, item.mb_trackid)
|
||||
|
||||
# Do we have a valid MusicBrainz TrackId
|
||||
if valid_trackid:
|
||||
# Get the MusicBrainz recording info.
|
||||
track_info = hooks.track_for_mbid(item.mb_trackid)
|
||||
if not track_info:
|
||||
self._log.info(u'Recording ID not found: {0}' +
|
||||
' for track {0}',
|
||||
item.mb_trackid,
|
||||
item_formatted)
|
||||
continue
|
||||
|
||||
# Apply.
|
||||
with lib.transaction():
|
||||
autotag.apply_item_metadata(item, track_info)
|
||||
apply_item_changes(lib, item, move, pretend, write)
|
||||
else:
|
||||
if not valid_trackid:
|
||||
self._log.info(u'Skipping singleton with invalid mb_trackid:' +
|
||||
' {0}', item_formatted)
|
||||
continue
|
||||
# Get the MusicBrainz recording info.
|
||||
track_info = hooks.track_for_mbid(item.mb_trackid)
|
||||
if not track_info:
|
||||
self._log.info(u'Recording ID not found: {0} for track {0}',
|
||||
item.mb_trackid,
|
||||
item_formatted)
|
||||
continue
|
||||
|
||||
# Apply.
|
||||
with lib.transaction():
|
||||
autotag.apply_item_metadata(item, track_info)
|
||||
apply_item_changes(lib, item, move, pretend, write)
|
||||
|
||||
def albums(self, lib, query, move, pretend, write):
|
||||
"""Retrieve and apply info from the autotagger for albums matched by
|
||||
|
|
@ -122,86 +120,78 @@ class MBSyncPlugin(BeetsPlugin):
|
|||
|
||||
items = list(a.items())
|
||||
|
||||
valid_albumid = re.match(
|
||||
r"(\d|\w){8}-(\d|\w){4}-(\d|\w){4}-(\d|\w){4}-(\d|\w){12}",
|
||||
a.mb_albumid)
|
||||
valid_albumid = re.match(mb_regex, a.mb_albumid)
|
||||
|
||||
# Do we have a valid MusicBrainz AlbumId
|
||||
|
||||
if valid_albumid:
|
||||
# Get the MusicBrainz album information.
|
||||
album_info = hooks.album_for_mbid(a.mb_albumid)
|
||||
if not album_info:
|
||||
self._log.info(u'Release ID {0} not found for album {1}',
|
||||
a.mb_albumid,
|
||||
album_formatted)
|
||||
continue
|
||||
|
||||
# Map release track and recording MBIDs to their information.
|
||||
# Recordings can appear multiple times on a release, so each
|
||||
# MBID maps to a list of TrackInfo objects.
|
||||
releasetrack_index = dict()
|
||||
track_index = defaultdict(list)
|
||||
for track_info in album_info.tracks:
|
||||
releasetrack_index[track_info.release_track_id] = track_info
|
||||
track_index[track_info.track_id].append(track_info)
|
||||
|
||||
# Construct a track mapping according to MBIDs (release track
|
||||
# MBIDs first, if available, and recording MBIDs otherwise).
|
||||
# This should work for albums that have missing or
|
||||
# extra tracks.
|
||||
mapping = {}
|
||||
for item in items:
|
||||
if item.mb_releasetrackid and \
|
||||
item.mb_releasetrackid in releasetrack_index:
|
||||
mapping[item] = releasetrack_index[item.mb_releasetrackid]
|
||||
else:
|
||||
candidates = track_index[item.mb_trackid]
|
||||
if len(candidates) == 1:
|
||||
mapping[item] = candidates[0]
|
||||
else:
|
||||
# If there are multiple copies of a recording,
|
||||
# they aredisambiguated using their disc and
|
||||
# track number.
|
||||
for c in candidates:
|
||||
if (c.medium_index == item.track and
|
||||
c.medium == item.disc):
|
||||
mapping[item] = c
|
||||
break
|
||||
|
||||
# Apply.
|
||||
self._log.debug(u'applying changes to {0}', album_formatted)
|
||||
with lib.transaction():
|
||||
autotag.apply_metadata(album_info, mapping)
|
||||
changed = False
|
||||
# Find any changed item to apply MusicBrainz changes to
|
||||
# album.
|
||||
any_changed_item = items[0]
|
||||
for item in items:
|
||||
item_changed = ui.show_model_changes(item)
|
||||
changed |= item_changed
|
||||
if item_changed:
|
||||
any_changed_item = item
|
||||
apply_item_changes(lib, item, move, pretend, write)
|
||||
|
||||
if not changed:
|
||||
# No change to any item.
|
||||
continue
|
||||
|
||||
if not pretend:
|
||||
# Update album structure to reflect an item in it.
|
||||
for key in library.Album.item_keys:
|
||||
a[key] = any_changed_item[key]
|
||||
a.store()
|
||||
|
||||
# Move album art (and any inconsistent items).
|
||||
if move and \
|
||||
lib.directory in util.ancestry(items[0].path):
|
||||
self._log.debug(
|
||||
u'moving album {0}', album_formatted)
|
||||
a.move()
|
||||
|
||||
else:
|
||||
if not valid_albumid:
|
||||
self._log.info(u'Skipping album with invalid mb_albumid: {0}',
|
||||
album_formatted)
|
||||
continue
|
||||
|
||||
# Get the MusicBrainz album information.
|
||||
album_info = hooks.album_for_mbid(a.mb_albumid)
|
||||
if not album_info:
|
||||
self._log.info(u'Release ID {0} not found for album {1}',
|
||||
a.mb_albumid,
|
||||
album_formatted)
|
||||
continue
|
||||
|
||||
# Map release track and recording MBIDs to their information.
|
||||
# Recordings can appear multiple times on a release, so each MBID
|
||||
# maps to a list of TrackInfo objects.
|
||||
releasetrack_index = dict()
|
||||
track_index = defaultdict(list)
|
||||
for track_info in album_info.tracks:
|
||||
releasetrack_index[track_info.release_track_id] = track_info
|
||||
track_index[track_info.track_id].append(track_info)
|
||||
|
||||
# Construct a track mapping according to MBIDs (release track MBIDs
|
||||
# first, if available, and recording MBIDs otherwise). This should
|
||||
# work for albums that have missing or extra tracks.
|
||||
mapping = {}
|
||||
for item in items:
|
||||
if item.mb_releasetrackid and \
|
||||
item.mb_releasetrackid in releasetrack_index:
|
||||
mapping[item] = releasetrack_index[item.mb_releasetrackid]
|
||||
else:
|
||||
candidates = track_index[item.mb_trackid]
|
||||
if len(candidates) == 1:
|
||||
mapping[item] = candidates[0]
|
||||
else:
|
||||
# If there are multiple copies of a recording, they are
|
||||
# disambiguated using their disc and track number.
|
||||
for c in candidates:
|
||||
if (c.medium_index == item.track and
|
||||
c.medium == item.disc):
|
||||
mapping[item] = c
|
||||
break
|
||||
|
||||
# Apply.
|
||||
self._log.debug(u'applying changes to {}', album_formatted)
|
||||
with lib.transaction():
|
||||
autotag.apply_metadata(album_info, mapping)
|
||||
changed = False
|
||||
# Find any changed item to apply MusicBrainz changes to album.
|
||||
any_changed_item = items[0]
|
||||
for item in items:
|
||||
item_changed = ui.show_model_changes(item)
|
||||
changed |= item_changed
|
||||
if item_changed:
|
||||
any_changed_item = item
|
||||
apply_item_changes(lib, item, move, pretend, write)
|
||||
|
||||
if not changed:
|
||||
# No change to any item.
|
||||
continue
|
||||
|
||||
if not pretend:
|
||||
# Update album structure to reflect an item in it.
|
||||
for key in library.Album.item_keys:
|
||||
a[key] = any_changed_item[key]
|
||||
a.store()
|
||||
|
||||
# Move album art (and any inconsistent items).
|
||||
if move and lib.directory in util.ancestry(items[0].path):
|
||||
self._log.debug(u'moving album {0}', album_formatted)
|
||||
a.move()
|
||||
|
|
|
|||
|
|
@ -47,7 +47,7 @@ Changes:
|
|||
|
||||
* :doc:`/plugins/mbsync` no longer queries MusicBrainz when the either the
|
||||
``mb_albumid`` or ``mb_trackid`` field is invalid
|
||||
Discussion here https://groups.google.com/forum/#!searchin/beets-users/mbsync|sort:date/beets-users/iwCF6bNdh9A/i1xl4Gx8BQAJ
|
||||
.. _Discussion here: https://groups.google.com/forum/#!searchin/beets-users/mbsync|sort:date/beets-users/iwCF6bNdh9A/i1xl4Gx8BQAJ
|
||||
Thanks to :user:`arogl`.
|
||||
|
||||
Fixes:
|
||||
|
|
|
|||
Loading…
Reference in a new issue