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 cleanup #138

Merged
merged 6 commits into from
May 28, 2024
Merged
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
29 changes: 16 additions & 13 deletions troi/playlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import requests
import spotipy

from troi import Recording, Playlist, PipelineError, Element, Artist, Release
from troi import Recording, Playlist, PipelineError, Element, Artist, ArtistCredit, Release
from troi.operations import is_homogeneous
from troi.print_recording import PrintRecordingList
from troi.tools.spotify_lookup import submit_to_spotify
Expand All @@ -25,11 +25,6 @@
PLAYLIST_TRACK_EXTENSION_URI = "https://musicbrainz.org/doc/jspf#track"
SUBSONIC_URI_PREFIX = "https://subsonic.org/entity/song/"

# TODO: When resolving a playlist, insert "location" into existing playlist, don't create a new one
# And recording lookup needs to be replace with metadata lookup. retire the labs API endpoint!
# Artist.mbids is totatlly stupid (see ^^). We need [artists] with "join_phrase" in musicbrainz hash.
# All this for the next PR.


def _serialize_to_jspf(playlist, created_for=None, track_count=None):
"""
Expand All @@ -51,8 +46,7 @@ def _serialize_to_jspf(playlist, created_for=None, track_count=None):
data["annotation"] = playlist.description

if created_for:
# TODO: This element is in the wrong location!
data["created_for"] = created_for
data["extension"][PLAYLIST_EXTENSION_URI]["created_for"] = created_for

if playlist.additional_metadata:
data["extension"][PLAYLIST_EXTENSION_URI]["additional_metadata"] = playlist.additional_metadata
Expand Down Expand Up @@ -114,14 +108,23 @@ def _deserialize_from_jspf(data) -> Playlist:
recordings = []

for track in data["track"]:
recording = Recording(name=track["title"], mbid=track["identifier"].split("/")[-1])
identifier = track["identifier"]

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

recording = Recording(name=track["title"], mbid=mbid)
if track.get("creator"):
artist = Artist(name=track["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")]
artist.mbids = artist_mbids
recording.artist = artist
artists = [Artist(mbid) for mbid in artist_mbids]
else:
artists = None
recording.artist_credit = ArtistCredit(name=track["creator"], artists=artists)

if track.get("album"):
recording.release = Release(name=track["album"])
Expand Down Expand Up @@ -427,7 +430,7 @@ class PlaylistMakerElement(Element):
:param desc: The description of the playlist
:param patch-slug: The patch slug (URL-safe short name) of the patch that created the playlist. Optional.
:param max_num_recordings: The maximum number of recordings to have in the playlist. Extras are discarded. Optional argument, and the default is to keep all recordings
:param max_artist_occurence: The number of times and artist is allowed to appear in the playlist. Any recordings that exceed this count ared discarded. Optional, default is to keep all recordings.
:param max_artist_occurrence: The number of times and artist is allowed to appear in the playlist. Any recordings that exceed this count ared discarded. Optional, default is to keep all recordings.
:param shuffle: If True, the playlist will be shuffled before being truncated. Optional. Default: False
:param is_april_first: If True, do something very sneaky. Default: False.
'''
Expand Down
Loading