Skip to content

Commit

Permalink
Merge pull request #366 from whipper-team/feature/issue-200-add-more-…
Browse files Browse the repository at this point in the history
…musicbrainz-identifiers

Add Track, Release Group, and Work MBIDs to ripped files
  • Loading branch information
JoeLametta authored Feb 13, 2019
2 parents 87b9460 + 364f024 commit 94b7001
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 11 deletions.
27 changes: 24 additions & 3 deletions whipper/common/mbngs.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class TrackMetadata(object):
mbid = None
sortName = None
mbidArtist = None
mbidRecording = None
mbidWorks = []


class DiscMetadata(object):
Expand All @@ -75,6 +77,7 @@ class DiscMetadata(object):
releaseType = None

mbid = None
mbidReleaseGroup = None
mbidArtist = None
url = None

Expand Down Expand Up @@ -144,6 +147,19 @@ def getIds(self):
joinString=";")


def _getWorks(recording):
"""Get "performance of" works out of a recording."""
works = []
valid_work_rel_types = [
u'a3005666-a872-32c3-ad06-98af558e99b0', # "Performance"
]
if 'work-relation-list' in recording:
for work in recording['work-relation-list']:
if work['type-id'] in valid_work_rel_types:
works.append(work['work']['id'])
return works


def _getMetadata(releaseShort, release, discid, country=None):
"""
@type release: C{dict}
Expand All @@ -165,7 +181,7 @@ def _getMetadata(releaseShort, release, discid, country=None):

discMD = DiscMetadata()

discMD.releaseType = releaseShort.get('release-group', {}).get('type')
discMD.releaseType = release['release-group']['type']
discCredit = _Credit(release['artist-credit'])

# FIXME: is there a better way to check for VA ?
Expand All @@ -188,6 +204,7 @@ def _getMetadata(releaseShort, release, discid, country=None):
discMD.release = release['date']

discMD.mbid = release['id']
discMD.mbidReleaseGroup = release['release-group']['id']
discMD.mbidArtist = discCredit.getIds()
discMD.url = 'https://musicbrainz.org/release/' + release['id']

Expand Down Expand Up @@ -229,7 +246,9 @@ def _getMetadata(releaseShort, release, discid, country=None):
track.mbidArtist = trackCredit.getIds()

track.title = t['recording']['title']
track.mbid = t['recording']['id']
track.mbid = t['id']
track.mbidRecording = t['recording']['id']
track.mbidWorks = _getWorks(t['recording'])

# FIXME: unit of duration ?
track.duration = int(t['recording'].get('length', 0))
Expand Down Expand Up @@ -304,7 +323,9 @@ def musicbrainz(discid, country=None, record=False):

res = musicbrainzngs.get_release_by_id(
release['id'], includes=["artists", "artist-credits",
"recordings", "discids", "labels"])
"recordings", "discids", "labels",
"recording-level-rels", "work-rels",
"release-groups"])
_record(record, 'release', release['id'], res)
releaseDetail = res['release']
formatted = json.dumps(releaseDetail, sort_keys=False, indent=4)
Expand Down
9 changes: 8 additions & 1 deletion whipper/common/program.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,15 +386,18 @@ def getTagList(self, number, mbdiscid):
releaseArtist = self.metadata.artist
disc = self.metadata.title
mbidRelease = self.metadata.mbid
mbidReleaseGroup = self.metadata.mbidReleaseGroup
mbidReleaseArtist = self.metadata.mbidArtist

if number > 0:
try:
track = self.metadata.tracks[number - 1]
trackArtist = track.artist
title = track.title
mbidRecording = track.mbid
mbidRecording = track.mbidRecording
mbidTrack = track.mbid
mbidTrackArtist = track.mbidArtist
mbidWorks = track.mbidWorks
except IndexError as e:
logger.error('no track %d found, %r', number, e)
raise
Expand All @@ -420,10 +423,14 @@ def getTagList(self, number, mbdiscid):
tags['DATE'] = self.metadata.release

if number > 0:
tags['MUSICBRAINZ_RELEASETRACKID'] = mbidTrack
tags['MUSICBRAINZ_TRACKID'] = mbidRecording
tags['MUSICBRAINZ_ARTISTID'] = mbidTrackArtist
tags['MUSICBRAINZ_ALBUMID'] = mbidRelease
tags['MUSICBRAINZ_RELEASEGROUPID'] = mbidReleaseGroup
tags['MUSICBRAINZ_ALBUMARTISTID'] = mbidReleaseArtist
if len(mbidWorks) > 0:
tags['MUSICBRAINZ_WORKID'] = mbidWorks

# TODO/FIXME: ISRC tag

Expand Down
55 changes: 55 additions & 0 deletions whipper/test/test_common_mbngs.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ def testNenaAndKimWildSingle(self):
self.assertEqual(track1.mbidArtist,
u'38bfaa7f-ee98-48cb-acd0-946d7aeecd76'
';4b462375-c508-432a-8c88-ceeec38b16ae')
self.assertEqual(track1.mbid,
u'1cc96e78-28ed-3820-b0b6-614c35b121ac')
self.assertEqual(track1.mbidRecording,
u'fde5622c-ce23-4ebb-975d-51d4a926f901')

track2 = metadata.tracks[1]

Expand All @@ -190,3 +194,54 @@ def testNenaAndKimWildSingle(self):
self.assertEqual(track2.mbidArtist,
u'38bfaa7f-ee98-48cb-acd0-946d7aeecd76'
';4b462375-c508-432a-8c88-ceeec38b16ae')
self.assertEqual(track2.mbid,
u'f16db4bf-9a34-3d5a-a975-c9375ab7a2ca')
self.assertEqual(track2.mbidRecording,
u'5f19758e-7421-4c71-a599-9a9575d8e1b0')

def testAllAvailableMetadata(self):
"""Check that all possible metadata gets assigned."""
# Using: David Rovics - The Other Side
# https://musicbrainz.org/release/6109ceed-7e21-490b-b5ad-3a66b4e4cfbb
filename = 'whipper.release.6109ceed-7e21-490b-b5ad-3a66b4e4cfbb.json'
path = os.path.join(os.path.dirname(__file__), filename)
handle = open(path, "rb")
response = json.loads(handle.read())
handle.close()
discid = "cHW1Uutl_kyWNaLJsLmTGTe4rnE-"

metadata = mbngs._getMetadata({}, response['release'], discid)
self.assertEqual(metadata.artist, u'David Rovics')
self.assertEqual(metadata.sortName, u'Rovics, David')
self.assertFalse(metadata.various)
self.assertIsInstance(metadata.tracks, list)
self.assertEqual(metadata.release, u'2015')
self.assertEqual(metadata.releaseTitle, u'The Other Side')
self.assertEqual(metadata.releaseType, u'Album')
self.assertEqual(metadata.mbid,
u'6109ceed-7e21-490b-b5ad-3a66b4e4cfbb')
self.assertEqual(metadata.mbidReleaseGroup,
u'99850b41-a06e-4fb8-992c-75c191a77803')
self.assertEqual(metadata.mbidArtist,
u'4d56eb9f-13b3-4f05-9db7-50195378d49f')
self.assertEqual(metadata.url,
u'https://musicbrainz.org/release'
'/6109ceed-7e21-490b-b5ad-3a66b4e4cfbb')
self.assertEqual(metadata.catalogNumber, u'[none]')
self.assertEqual(metadata.barcode, u'700261430249')

self.assertEqual(len(metadata.tracks), 16)

track1 = metadata.tracks[0]
self.assertEqual(track1.artist, u'David Rovics')
self.assertEqual(track1.title, u'Waiting for the Hurricane')
self.assertEqual(track1.duration, 176320)
self.assertEqual(track1.mbid,
u'4116eea3-b9c2-452a-8d63-92f1e585b225')
self.assertEqual(track1.sortName, u'Rovics, David')
self.assertEqual(track1.mbidArtist,
u'4d56eb9f-13b3-4f05-9db7-50195378d49f')
self.assertEqual(track1.mbidRecording,
u'b191794d-b7c6-4d6f-971e-0a543959b5ad')
self.assertEqual(track1.mbidWorks,
[u'90d5be68-0b29-45a3-ba01-c27ad78e3625'])
Loading

0 comments on commit 94b7001

Please sign in to comment.