Skip to content

Commit

Permalink
beetsplug: cleanup for 'missing -a' code
Browse files Browse the repository at this point in the history
* Use full name for musicbrainzngs import
* Use beets internal logging facilities
* Match releases by release id
* Convert some strings to Unicode
* Remove unnecessary MB rate-limiting
* Remove unnecessary imports

Signed-off-by: Quentin Young <qlyoung@qlyoung.net>
  • Loading branch information
qlyoung committed Mar 18, 2017
1 parent 502b06b commit 25e3129
Showing 1 changed file with 19 additions and 25 deletions.
44 changes: 19 additions & 25 deletions beetsplug/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@
"""
from __future__ import division, absolute_import, print_function

import time
import beets
import musicbrainzngs as m
import sys
import musicbrainzngs

from musicbrainzngs.musicbrainz import MusicBrainzError
from beets.autotag import hooks
Expand Down Expand Up @@ -171,41 +168,38 @@ def _missing_albums(self, lib, args):
albums_by_artist[artist] = artistalbs

# build dict mapping artist to list of all albums
m.set_useragent('beets', beets.__version__, 'http://beets.io/')

for artist, albums in albums_by_artist.items():
if artist[1] is None or artist[1] == "":
print_(u"[!] No musicbrainz ID for artist {}, skipping."
.format(artist[0]), stream=sys.stderr)
self._log.info(
u"No musicbrainz ID for artist {}, skipping.",
artist[0]
)
continue

try:
releases = m.browse_releases(artist=artist[1])['release-list']
resp = musicbrainzngs.browse_release_groups(artist=artist[1])
release_groups = resp['release-group-list']
except MusicBrainzError as err:
print_(u"[!] Couldn't fetch info for artist '{}' ({}) - '{}'"
.format(artist[0], artist[1], err), stream=sys.stderr)
self._log.info(
u"Couldn't fetch info for artist '{}' ({}) - '{}'",
artist[0], artist[1], err
)
continue

missing = []
present = []
for release in releases:
missing.append(release)
for rg in release_groups:
missing.append(rg)
for alb in albums:
# compare by title, don't care about specific releases
if alb['album'].lower() == release['title'].lower():
missing.remove(release)
present.append(release)
if alb['mb_releasegroupid'] == rg['id']:
missing.remove(rg)
present.append(rg)
break

missing_titles = {release['title'] for release in missing}

prefix_miss = "{} - ".format(artist[0])

for release in missing_titles:
print_(u"{}{}".format(prefix_miss, release))
missing_titles = {rg['title'] for rg in missing}

# respect musicbrainz rate limits
time.sleep(1)
for release_title in missing_titles:
print_(u"{} - {}".format(artist[0], release_title))

def _missing(self, album):
"""Query MusicBrainz to determine items missing from `album`.
Expand Down

0 comments on commit 25e3129

Please sign in to comment.