#173 Implement PlaylistId

This commit is contained in:
kokarare1212
2023-01-03 10:16:51 +09:00
parent 29ff02882b
commit e65f7fc691
2 changed files with 37 additions and 2 deletions

View File

@@ -11,8 +11,8 @@ from librespot.audio.storage import ChannelManager
from librespot.cache import CacheManager from librespot.cache import CacheManager
from librespot.crypto import CipherPair, DiffieHellman, Packet from librespot.crypto import CipherPair, DiffieHellman, Packet
from librespot.mercury import MercuryClient, MercuryRequests, RawMercuryRequest from librespot.mercury import MercuryClient, MercuryRequests, RawMercuryRequest
from librespot.metadata import AlbumId, ArtistId, EpisodeId, ShowId, TrackId from librespot.metadata import AlbumId, ArtistId, EpisodeId, ShowId, TrackId, PlaylistId
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 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
from librespot.proto.ExplicitContentPubsub_pb2 import UserAttributesUpdate from librespot.proto.ExplicitContentPubsub_pb2 import UserAttributesUpdate
from librespot.structure import Closeable, MessageListener, RequestListener, SubListener from librespot.structure import Closeable, MessageListener, RequestListener, SubListener
import base64 import base64
@@ -154,6 +154,18 @@ class ApiClient(Closeable):
proto.ParseFromString(body) proto.ParseFromString(body)
return proto return proto
def get_playlist(self, _id: PlaylistId) -> Playlist4External.SelectedListContent:
response = self.send("GET",
"/playlist/v2/playlist/{}".format(_id.id()), None,
None)
ApiClient.StatusCodeException.check_status(response)
body = response.content
if body is None:
raise IOError()
proto = Playlist4External.SelectedListContent()
proto.ParseFromString(body)
return proto
def set_client_token(self, client_token): def set_client_token(self, client_token):
self.__client_token_str = client_token self.__client_token_str = client_token

View File

@@ -62,6 +62,29 @@ class PlayableId:
raise NotImplementedError raise NotImplementedError
class PlaylistId(SpotifyId):
base62 = Base62.create_instance_with_inverted_character_set()
pattern = re.compile(r"spotify:playlist:(.{22})")
__id: str
def __init__(self, _id: str):
self.__id = _id
@staticmethod
def from_uri(uri: str) -> PlaylistId:
matcher = PlaylistId.pattern.search(uri)
if matcher is not None:
playlist_id = matcher.group(1)
return PlaylistId(playlist_id)
raise TypeError("Not a Spotify playlist ID: {}.".format(uri))
def id(self) -> str:
return self.__id
def to_spotify_uri(self) -> str:
return "spotify:playlist:" + self.__id
class UnsupportedId(PlayableId): class UnsupportedId(PlayableId):
uri: str uri: str