mirror of
https://github.com/rembo10/headphones.git
synced 2026-07-20 07:54:01 +01:00
update Beets
This commit is contained in:
+65
-38
@@ -15,12 +15,12 @@
|
||||
"""Update library's tags using MusicBrainz.
|
||||
"""
|
||||
|
||||
from beets.plugins import BeetsPlugin, apply_item_changes
|
||||
from beets import autotag, library, ui, util
|
||||
from beets.autotag import hooks
|
||||
import re
|
||||
from collections import defaultdict
|
||||
|
||||
import re
|
||||
from beets import autotag, library, ui, util
|
||||
from beets.autotag import hooks
|
||||
from beets.plugins import BeetsPlugin, apply_item_changes
|
||||
|
||||
MBID_REGEX = r"(\d|\w){8}-(\d|\w){4}-(\d|\w){4}-(\d|\w){4}-(\d|\w){12}"
|
||||
|
||||
@@ -30,28 +30,41 @@ class MBSyncPlugin(BeetsPlugin):
|
||||
super().__init__()
|
||||
|
||||
def commands(self):
|
||||
cmd = ui.Subcommand('mbsync',
|
||||
help='update metadata from musicbrainz')
|
||||
cmd = ui.Subcommand("mbsync", help="update metadata from musicbrainz")
|
||||
cmd.parser.add_option(
|
||||
'-p', '--pretend', action='store_true',
|
||||
help='show all changes but do nothing')
|
||||
"-p",
|
||||
"--pretend",
|
||||
action="store_true",
|
||||
help="show all changes but do nothing",
|
||||
)
|
||||
cmd.parser.add_option(
|
||||
'-m', '--move', action='store_true', dest='move',
|
||||
help="move files in the library directory")
|
||||
"-m",
|
||||
"--move",
|
||||
action="store_true",
|
||||
dest="move",
|
||||
help="move files in the library directory",
|
||||
)
|
||||
cmd.parser.add_option(
|
||||
'-M', '--nomove', action='store_false', dest='move',
|
||||
help="don't move files in library")
|
||||
"-M",
|
||||
"--nomove",
|
||||
action="store_false",
|
||||
dest="move",
|
||||
help="don't move files in library",
|
||||
)
|
||||
cmd.parser.add_option(
|
||||
'-W', '--nowrite', action='store_false',
|
||||
default=None, dest='write',
|
||||
help="don't write updated metadata to files")
|
||||
"-W",
|
||||
"--nowrite",
|
||||
action="store_false",
|
||||
default=None,
|
||||
dest="write",
|
||||
help="don't write updated metadata to files",
|
||||
)
|
||||
cmd.parser.add_format_option()
|
||||
cmd.func = self.func
|
||||
return [cmd]
|
||||
|
||||
def func(self, lib, opts, args):
|
||||
"""Command handler for the mbsync function.
|
||||
"""
|
||||
"""Command handler for the mbsync function."""
|
||||
move = ui.should_move(opts.move)
|
||||
pretend = opts.pretend
|
||||
write = ui.should_write(opts.write)
|
||||
@@ -64,25 +77,30 @@ class MBSyncPlugin(BeetsPlugin):
|
||||
"""Retrieve and apply info from the autotagger for items matched by
|
||||
query.
|
||||
"""
|
||||
for item in lib.items(query + ['singleton:true']):
|
||||
for item in lib.items(query + ["singleton:true"]):
|
||||
item_formatted = format(item)
|
||||
if not item.mb_trackid:
|
||||
self._log.info('Skipping singleton with no mb_trackid: {0}',
|
||||
item_formatted)
|
||||
self._log.info(
|
||||
"Skipping singleton with no mb_trackid: {0}", item_formatted
|
||||
)
|
||||
continue
|
||||
|
||||
# Do we have a valid MusicBrainz track ID?
|
||||
if not re.match(MBID_REGEX, item.mb_trackid):
|
||||
self._log.info('Skipping singleton with invalid mb_trackid:' +
|
||||
' {0}', item_formatted)
|
||||
self._log.info(
|
||||
"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('Recording ID not found: {0} for track {0}',
|
||||
item.mb_trackid,
|
||||
item_formatted)
|
||||
self._log.info(
|
||||
"Recording ID not found: {0} for track {0}",
|
||||
item.mb_trackid,
|
||||
item_formatted,
|
||||
)
|
||||
continue
|
||||
|
||||
# Apply.
|
||||
@@ -98,24 +116,29 @@ class MBSyncPlugin(BeetsPlugin):
|
||||
for a in lib.albums(query):
|
||||
album_formatted = format(a)
|
||||
if not a.mb_albumid:
|
||||
self._log.info('Skipping album with no mb_albumid: {0}',
|
||||
album_formatted)
|
||||
self._log.info(
|
||||
"Skipping album with no mb_albumid: {0}", album_formatted
|
||||
)
|
||||
continue
|
||||
|
||||
items = list(a.items())
|
||||
|
||||
# Do we have a valid MusicBrainz album ID?
|
||||
if not re.match(MBID_REGEX, a.mb_albumid):
|
||||
self._log.info('Skipping album with invalid mb_albumid: {0}',
|
||||
album_formatted)
|
||||
self._log.info(
|
||||
"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('Release ID {0} not found for album {1}',
|
||||
a.mb_albumid,
|
||||
album_formatted)
|
||||
self._log.info(
|
||||
"Release ID {0} not found for album {1}",
|
||||
a.mb_albumid,
|
||||
album_formatted,
|
||||
)
|
||||
continue
|
||||
|
||||
# Map release track and recording MBIDs to their information.
|
||||
@@ -132,8 +155,10 @@ class MBSyncPlugin(BeetsPlugin):
|
||||
# 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:
|
||||
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]
|
||||
@@ -143,13 +168,15 @@ class MBSyncPlugin(BeetsPlugin):
|
||||
# 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):
|
||||
if (
|
||||
c.medium_index == item.track
|
||||
and c.medium == item.disc
|
||||
):
|
||||
mapping[item] = c
|
||||
break
|
||||
|
||||
# Apply.
|
||||
self._log.debug('applying changes to {}', album_formatted)
|
||||
self._log.debug("applying changes to {}", album_formatted)
|
||||
with lib.transaction():
|
||||
autotag.apply_metadata(album_info, mapping)
|
||||
changed = False
|
||||
@@ -174,5 +201,5 @@ class MBSyncPlugin(BeetsPlugin):
|
||||
|
||||
# Move album art (and any inconsistent items).
|
||||
if move and lib.directory in util.ancestry(items[0].path):
|
||||
self._log.debug('moving album {0}', album_formatted)
|
||||
self._log.debug("moving album {0}", album_formatted)
|
||||
a.move()
|
||||
|
||||
Reference in New Issue
Block a user