Skip to content

Commit e65f7fc

Browse files
committed
#173 Implement PlaylistId
1 parent 29ff028 commit e65f7fc

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

librespot/core.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
from librespot.cache import CacheManager
1212
from librespot.crypto import CipherPair, DiffieHellman, Packet
1313
from librespot.mercury import MercuryClient, MercuryRequests, RawMercuryRequest
14-
from librespot.metadata import AlbumId, ArtistId, EpisodeId, ShowId, TrackId
15-
from librespot.proto import Authentication_pb2 as Authentication, ClientToken_pb2 as ClientToken, Connect_pb2 as Connect, Connectivity_pb2 as Connectivity, Keyexchange_pb2 as Keyexchange, Metadata_pb2 as Metadata
14+
from librespot.metadata import AlbumId, ArtistId, EpisodeId, ShowId, TrackId, PlaylistId
15+
from librespot.proto import Authentication_pb2 as Authentication, ClientToken_pb2 as ClientToken, Connect_pb2 as Connect, Connectivity_pb2 as Connectivity, Keyexchange_pb2 as Keyexchange, Metadata_pb2 as Metadata, Playlist4External_pb2 as Playlist4External
1616
from librespot.proto.ExplicitContentPubsub_pb2 import UserAttributesUpdate
1717
from librespot.structure import Closeable, MessageListener, RequestListener, SubListener
1818
import base64
@@ -154,6 +154,18 @@ def get_metadata_4_show(self, show: ShowId) -> Metadata.Show:
154154
proto.ParseFromString(body)
155155
return proto
156156

157+
def get_playlist(self, _id: PlaylistId) -> Playlist4External.SelectedListContent:
158+
response = self.send("GET",
159+
"/playlist/v2/playlist/{}".format(_id.id()), None,
160+
None)
161+
ApiClient.StatusCodeException.check_status(response)
162+
body = response.content
163+
if body is None:
164+
raise IOError()
165+
proto = Playlist4External.SelectedListContent()
166+
proto.ParseFromString(body)
167+
return proto
168+
157169
def set_client_token(self, client_token):
158170
self.__client_token_str = client_token
159171

librespot/metadata.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,29 @@ def to_spotify_uri(self) -> str:
6262
raise NotImplementedError
6363

6464

65+
class PlaylistId(SpotifyId):
66+
base62 = Base62.create_instance_with_inverted_character_set()
67+
pattern = re.compile(r"spotify:playlist:(.{22})")
68+
__id: str
69+
70+
def __init__(self, _id: str):
71+
self.__id = _id
72+
73+
@staticmethod
74+
def from_uri(uri: str) -> PlaylistId:
75+
matcher = PlaylistId.pattern.search(uri)
76+
if matcher is not None:
77+
playlist_id = matcher.group(1)
78+
return PlaylistId(playlist_id)
79+
raise TypeError("Not a Spotify playlist ID: {}.".format(uri))
80+
81+
def id(self) -> str:
82+
return self.__id
83+
84+
def to_spotify_uri(self) -> str:
85+
return "spotify:playlist:" + self.__id
86+
87+
6588
class UnsupportedId(PlayableId):
6689
uri: str
6790

0 commit comments

Comments
 (0)