Enable mblookup to take release id as argument
To make mblookup able to look up data based on release id, RegExp is used to detect whether the input is release id or disc id and behaves differently according to that. The input is now also tripped before being passed down. Signed-off-by: ABCbum <kimlong221002@gmail.com>
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
from whipper.command.basecommand import BaseCommand
|
||||
from whipper.common.mbngs import musicbrainz
|
||||
from whipper.common.mbngs import musicbrainz, getReleaseMetadata
|
||||
|
||||
import re
|
||||
|
||||
|
||||
class MBLookup(BaseCommand):
|
||||
@@ -12,35 +14,58 @@ Example disc id: KnpGsLhvH.lPrNc1PBL21lb9Bg4-"""
|
||||
|
||||
def add_arguments(self):
|
||||
self.parser.add_argument(
|
||||
'mbdiscid', action='store', help="MB disc id to look up"
|
||||
'mbid', action='store', help="MB disc id or release id to look up"
|
||||
)
|
||||
|
||||
def _printMetadata(self, md):
|
||||
"""
|
||||
Print out metadata received in a sensible way.
|
||||
|
||||
:param md: MusicBrainz's metadata about the disc
|
||||
:type md: `DiscMetadata`
|
||||
"""
|
||||
print(' Artist: %s' % md.artist.encode('utf-8'))
|
||||
print(' Title: %s' % md.title.encode('utf-8'))
|
||||
print(' Type: %s' % str(md.releaseType).encode('utf-8'))
|
||||
print(' URL: %s' % md.url)
|
||||
print(' Tracks: %d' % len(md.tracks))
|
||||
if md.catalogNumber:
|
||||
print(' Cat no: %s' % md.catalogNumber)
|
||||
if md.barcode:
|
||||
print(' Barcode: %s' % md.barcode)
|
||||
|
||||
for j, track in enumerate(md.tracks):
|
||||
print(' Track %2d: %s - %s' % (
|
||||
j + 1, track.artist.encode('utf-8'),
|
||||
track.title.encode('utf-8')
|
||||
))
|
||||
|
||||
def do(self):
|
||||
try:
|
||||
discId = str(self.options.mbdiscid)
|
||||
mbid = str(self.options.mbid.strip())
|
||||
except IndexError:
|
||||
print('Please specify a MusicBrainz disc id.')
|
||||
print('Please specify a MusicBrainz disc id or release id.')
|
||||
return 3
|
||||
|
||||
metadatas = musicbrainz(discId)
|
||||
releaseIdMatch = re.match(
|
||||
r'^[\dA-Fa-f]{8}-(?:[\dA-Fa-f]{4}-){3}[\dA-Fa-f]{12}$',
|
||||
mbid
|
||||
)
|
||||
discIdMatch = re.match(
|
||||
r'^[\dA-Za-z._]{27}-$',
|
||||
mbid
|
||||
)
|
||||
|
||||
print('%d releases' % len(metadatas))
|
||||
for i, md in enumerate(metadatas):
|
||||
print('- Release %d:' % (i + 1, ))
|
||||
print(' Artist: %s' % md.artist.encode('utf-8'))
|
||||
print(' Title: %s' % md.title.encode('utf-8'))
|
||||
print(' Type: %s' % str(md.releaseType).encode('utf-8')) # noqa: E501
|
||||
print(' URL: %s' % md.url)
|
||||
print(' Tracks: %d' % len(md.tracks))
|
||||
if md.catalogNumber:
|
||||
print(' Cat no: %s' % md.catalogNumber)
|
||||
if md.barcode:
|
||||
print(' Barcode: %s' % md.barcode)
|
||||
|
||||
for j, track in enumerate(md.tracks):
|
||||
print(' Track %2d: %s - %s' % (
|
||||
j + 1, track.artist.encode('utf-8'),
|
||||
track.title.encode('utf-8')
|
||||
))
|
||||
# see https://musicbrainz.org/doc/MusicBrainz_Identifier
|
||||
if releaseIdMatch:
|
||||
md = getReleaseMetadata(releaseIdMatch.group(0))
|
||||
if md:
|
||||
self._printMetadata(md)
|
||||
elif discIdMatch:
|
||||
metadatas = musicbrainz(discIdMatch.group(0))
|
||||
|
||||
print('%d releases' % len(metadatas))
|
||||
for i, md in enumerate(metadatas):
|
||||
print('- Release %d:' % (i + 1, ))
|
||||
self._printMetadata(md)
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user