Skip to content

Commit

Permalink
Merge pull request #140 from metabrainz/identifier-fix
Browse files Browse the repository at this point in the history
JSPF Track identifiers should be a list
  • Loading branch information
amCap1712 authored May 28, 2024
2 parents 9dc5882 + 2c41fa4 commit 2b29c99
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
19 changes: 16 additions & 3 deletions troi/patches/lb_radio_classes/playlist.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import troi
from random import randint, shuffle
from random import shuffle

import requests
from urllib.parse import quote

from troi import Recording
from troi import TARGET_NUMBER_OF_RECORDINGS
Expand Down Expand Up @@ -40,7 +39,21 @@ def read(self, entities):
self.local_storage["data_cache"]["element-descriptions"].append(f"playlist {self.mbid}")

# Fetch the recordings, then shuffle
mbid_list = [r["identifier"][34:] for r in r.json()["playlist"]["track"]]
mbid_list = []
for recording in r.json()["playlist"]["track"]:
identifiers = recording["identifier"]
if isinstance(identifiers, str):
identifiers = [identifiers]

mbid = None
for identifier in identifiers:
if identifier.startswith("https://musicbrainz.org/recording/") or \
identifier.startswith("http://musicbrainz.org/recording/"):
mbid = identifier.split("/")[-1]
break

mbid_list.append(mbid)

shuffle(mbid_list)

# Select and convert the first n MBIDs into Recordings
Expand Down
19 changes: 11 additions & 8 deletions troi/playlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def _serialize_to_jspf(playlist, created_for=None, track_count=None):
track["creator"] = e.artist_credit.name if e.artist_credit else ""

track["title"] = e.name
track["identifier"] = "https://musicbrainz.org/recording/" + str(e.mbid)
track["identifier"] = ["https://musicbrainz.org/recording/" + str(e.mbid)]

loc = e.musicbrainz.get("filename", None)
if loc is not None:
Expand Down Expand Up @@ -108,20 +108,23 @@ def _deserialize_from_jspf(data) -> Playlist:
recordings = []

for track in data["track"]:
identifier = track["identifier"]
identifiers = track["identifier"]
if isinstance(identifiers, str):
identifiers = [identifiers]

if identifier.startswith("https://musicbrainz.org/recording/") or \
identifier.startswith("http://musicbrainz.org/recording/"):
mbid = identifier.split("/")[-1]
else:
mbid = None
mbid = None
for identifier in identifiers:
if identifier.startswith("https://musicbrainz.org/recording/") or \
identifier.startswith("http://musicbrainz.org/recording/"):
mbid = identifier.split("/")[-1]
break

recording = Recording(name=track["title"], mbid=mbid)
if track.get("creator"):
extension = track["extension"][PLAYLIST_TRACK_EXTENSION_URI]
if extension.get("artist_identifiers"):
artist_mbids = [url.split("/")[-1] for url in extension.get("artist_identifiers")]
artists = [Artist(mbid) for mbid in artist_mbids]
artists = [Artist(mbid=mbid) for mbid in artist_mbids]
else:
artists = None
recording.artist_credit = ArtistCredit(name=track["creator"], artists=artists)
Expand Down
2 changes: 1 addition & 1 deletion troi/print_recording.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def _print_recording(self, recording, year=False, popularity=False, listen_count

if recording.artist_credit is not None:
if recording.artist_credit.artists is not None:
text += " %-20s" % ",".join([ a.mbid[:5] for a in recording.artist_credit.artists ])
text += " %-20s" % ",".join([a.mbid[:5] for a in recording.artist_credit.artists])
if recording.artist_credit.artist_credit_id is not None:
text += " %8d" % recording.artist_credit.artist_credit_id

Expand Down

0 comments on commit 2b29c99

Please sign in to comment.