Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSPF Track identifiers should be a list #140

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading