diff --git a/troi/patches/lb_radio_classes/playlist.py b/troi/patches/lb_radio_classes/playlist.py index 56e350b4..52401d3a 100755 --- a/troi/patches/lb_radio_classes/playlist.py +++ b/troi/patches/lb_radio_classes/playlist.py @@ -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 @@ -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 diff --git a/troi/playlist.py b/troi/playlist.py index b8d385dd..06f50829 100755 --- a/troi/playlist.py +++ b/troi/playlist.py @@ -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: @@ -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) diff --git a/troi/print_recording.py b/troi/print_recording.py index b7da1234..2e63eb5b 100755 --- a/troi/print_recording.py +++ b/troi/print_recording.py @@ -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