Skip to content

Commit

Permalink
Merge pull request #113 from metabrainz/fix-empty-album-string
Browse files Browse the repository at this point in the history
Do not emit an album key in JSPF if we don't have album info.
  • Loading branch information
mayhem committed Oct 30, 2023
2 parents 661232b + 137eed2 commit a0ca6f9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
17 changes: 12 additions & 5 deletions troi/musicbrainz/recording_lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import requests
import ujson

from troi import Element, Artist, PipelineError, Recording, Playlist
from troi import Element, Artist, PipelineError, Recording, Playlist, Release


class RecordingLookupElement(Element):
Expand Down Expand Up @@ -81,15 +81,22 @@ def read(self, inputs):
output.append(r)
continue

if not r.artist:
if r.artist:
r.artist.name = row['artist_credit_name']
r.artist.mbids = row.get('[artist_credit_mbids]', [])
r.artist.artist_credit_id = row['artist_credit_id']
else:
a = Artist(name=row['artist_credit_name'],
mbids=row.get('[artist_credit_mbids]', []),
artist_credit_id=row['artist_credit_id'])
r.artist = a

if r.release:
r.release.name = row["release_name"]
r.release.mbid = row["release_mbid"]
else:
r.artist.name = row['artist_credit_name']
r.artist.mbids = row.get('[artist_credit_mbids]', [])
r.artist.artist_credit_id = row['artist_credit_id']
r.release = Release(name=row["release_name"],
mbid=row["release_mbid"])

r.name = row['recording_name']
r.length = row['length']
Expand Down
8 changes: 8 additions & 0 deletions troi/musicbrainz/tests/test_recording_lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"length": 253000,
"recording_mbid": "a96bf3b6-651d-49f4-9a89-eee27cecc18e",
"recording_name": "Sour Times",
"release_name": "Dummy",
"release_mbid": "cf2a6e34-b4b7-4164-a464-dbb3dbf09c28",
"original_recording_mbid": "a96bf3b6-651d-49f4-9a89-eee27cecc18e",
"canonical_recording_mbid": "a96bf3b6-651d-49f4-9a89-eee27cecc18e",
},
Expand All @@ -34,6 +36,8 @@
"length": 275333,
"recording_mbid": "cfa47c9b-f12f-4f9c-a6da-22a9355d6125",
"recording_name": "Blue Angel",
"release_name": "Hot",
"release_mbid": "70fc4df9-1a86-4357-aac7-0694d4248aed",
"original_recording_mbid": "ec5b8aa9-7483-4791-a185-1f599a0cdc35",
"canonical_recording_mbid": "cfa47c9b-f12f-4f9c-a6da-22a9355d6125",
}
Expand Down Expand Up @@ -62,9 +66,13 @@ def test_read(self, req):
assert entities[0].artist.name == "Portishead"
assert entities[0].artist.artist_credit_id == 65
assert entities[0].mbid == "a96bf3b6-651d-49f4-9a89-eee27cecc18e"
assert entities[0].release.mbid == "cf2a6e34-b4b7-4164-a464-dbb3dbf09c28"
assert entities[0].release.name == "Dummy"

assert entities[1].name == "Blue Angel"
assert entities[1].length == 275333
assert entities[1].artist.name == "Squirrel Nut Zippers"
assert entities[1].artist.artist_credit_id == 11
assert entities[1].mbid == "cfa47c9b-f12f-4f9c-a6da-22a9355d6125"
assert entities[1].release.mbid == "70fc4df9-1a86-4357-aac7-0694d4248aed"
assert entities[1].release.name == "Hot"
10 changes: 9 additions & 1 deletion troi/playlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ def _serialize_to_jspf(playlist, created_for=None, track_count=None):
artist_mbids = [str(mbid) for mbid in e.artist.mbids or []]
track["creator"] = e.artist.name if e.artist else ""

track["album"] = e.release.name if e.release else ""
track["title"] = e.name
track["identifier"] = "https://musicbrainz.org/recording/" + str(e.mbid)
if artist_mbids:
Expand All @@ -68,6 +67,15 @@ def _serialize_to_jspf(playlist, created_for=None, track_count=None):
"artist_identifiers": artist_mbids,
}
}

if e.release is not None:
if e.release.name is not None and e.release.name != "":
track["album"] = e.release.name

if e.release.mbid is not None and e.release.mbid != "":
track["extension"][PLAYLIST_TRACK_EXTENSION_URI]["release_identifier"] = \
PLAYLIST_RELEASE_URI_PREFIX + e.release.mbid

tracks.append(track)

data['track'] = tracks
Expand Down

0 comments on commit a0ca6f9

Please sign in to comment.