librespot-python/librespot/metadata.py

284 lines
8.2 KiB
Python
Raw Normal View History

2021-05-15 00:07:29 +02:00
from __future__ import annotations
2021-09-12 05:58:24 +02:00
from librespot import util
from librespot.proto.ContextTrack_pb2 import ContextTrack
2021-09-12 05:58:24 +02:00
from librespot.util import Base62
import re
2021-05-15 00:08:00 +02:00
2021-05-15 00:07:29 +02:00
class SpotifyId:
STATIC_FROM_URI = "fromUri"
STATIC_FROM_BASE62 = "fromBase62"
STATIC_FROM_HEX = "fromHex"
@staticmethod
def from_base62(base62: str):
2021-09-12 05:58:24 +02:00
raise NotImplementedError
2021-05-15 00:07:29 +02:00
@staticmethod
def from_hex(hex_str: str):
2021-09-12 05:58:24 +02:00
raise NotImplementedError
2021-05-15 00:07:29 +02:00
@staticmethod
def from_uri(uri: str):
2021-09-12 05:58:24 +02:00
raise NotImplementedError
2021-05-15 00:07:29 +02:00
def to_spotify_uri(self) -> str:
2021-09-12 05:58:24 +02:00
raise NotImplementedError
2021-05-15 00:07:29 +02:00
class SpotifyIdParsingException(Exception):
pass
class PlayableId:
2021-09-12 05:58:24 +02:00
base62 = Base62.create_instance_with_inverted_character_set()
2021-05-15 00:07:29 +02:00
@staticmethod
def from_uri(uri: str) -> PlayableId:
if not PlayableId.is_supported(uri):
return UnsupportedId(uri)
2021-09-12 05:58:24 +02:00
if TrackId.pattern.search(uri) is not None:
2021-05-15 00:07:29 +02:00
return TrackId.from_uri(uri)
if EpisodeId.pattern.search(uri) is not None:
2021-05-15 00:07:29 +02:00
return EpisodeId.from_uri(uri)
raise TypeError("Unknown uri: {}".format(uri))
2021-05-15 00:07:29 +02:00
@staticmethod
def is_supported(uri: str):
2021-05-15 00:08:08 +02:00
return (not uri.startswith("spotify:local:")
and not uri == "spotify:delimiter"
and not uri == "spotify:meta:delimiter")
2021-05-15 00:07:29 +02:00
@staticmethod
def should_play(track: ContextTrack):
return track.metadata_or_default
def get_gid(self) -> bytes:
2021-09-12 05:58:24 +02:00
raise NotImplementedError
2021-05-15 00:07:29 +02:00
def hex_id(self) -> str:
2021-09-12 05:58:24 +02:00
raise NotImplementedError
2021-05-15 00:07:29 +02:00
def to_spotify_uri(self) -> str:
2021-09-12 05:58:24 +02:00
raise NotImplementedError
2021-05-15 00:07:29 +02:00
2023-01-03 02:16:51 +01:00
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
2021-05-15 00:07:29 +02:00
class UnsupportedId(PlayableId):
uri: str
def __init__(self, uri: str):
self.uri = uri
def get_gid(self) -> bytes:
raise TypeError()
def hex_id(self) -> str:
raise TypeError()
def to_spotify_uri(self) -> str:
return self.uri
class AlbumId(SpotifyId):
2021-09-12 05:58:24 +02:00
base62 = Base62.create_instance_with_inverted_character_set()
pattern = re.compile(r"spotify:album:(.{22})")
__hex_id: str
2021-05-15 00:07:29 +02:00
def __init__(self, hex_id: str):
2021-09-12 05:58:24 +02:00
self.__hex_id = hex_id.lower()
2021-05-15 00:07:29 +02:00
@staticmethod
def from_uri(uri: str) -> AlbumId:
2021-09-12 05:58:24 +02:00
matcher = AlbumId.pattern.search(uri)
2021-05-15 00:07:29 +02:00
if matcher is not None:
album_id = matcher.group(1)
2023-01-03 02:22:23 +01:00
return AlbumId(util.bytes_to_hex(AlbumId.base62.decode(album_id.encode(), 16)))
2023-01-02 22:33:02 +01:00
raise TypeError("Not a Spotify album ID: {}.".format(uri))
2021-05-15 00:07:29 +02:00
@staticmethod
def from_base62(base62: str) -> AlbumId:
2022-07-12 00:42:25 +02:00
return AlbumId(util.bytes_to_hex(AlbumId.base62.decode(base62.encode(), 16)))
2021-05-15 00:07:29 +02:00
@staticmethod
def from_hex(hex_str: str) -> AlbumId:
return AlbumId(hex_str)
def to_mercury_uri(self) -> str:
2022-07-12 00:42:25 +02:00
return "hm://metadata/4/album/{}".format(self.__hex_id)
2021-05-15 00:07:29 +02:00
def hex_id(self) -> str:
2021-09-12 05:58:24 +02:00
return self.__hex_id
def to_spotify_uri(self) -> str:
return "spotify:album:{}".format(
2022-07-12 00:42:25 +02:00
AlbumId.base62.encode(util.hex_to_bytes(self.__hex_id)).decode())
2021-05-15 00:07:29 +02:00
class ArtistId(SpotifyId):
2021-09-12 05:58:24 +02:00
base62 = Base62.create_instance_with_inverted_character_set()
pattern = re.compile("spotify:artist:(.{22})")
__hex_id: str
2021-05-15 00:07:29 +02:00
def __init__(self, hex_id: str):
2022-07-12 00:42:25 +02:00
self.__hex_id = hex_id.lower()
2021-05-15 00:07:29 +02:00
@staticmethod
def from_uri(uri: str) -> ArtistId:
2021-09-12 05:58:24 +02:00
matcher = ArtistId.pattern.search(uri)
2021-05-15 00:07:29 +02:00
if matcher is not None:
artist_id = matcher.group(1)
2021-05-15 00:08:08 +02:00
return ArtistId(
2022-07-12 00:42:25 +02:00
util.bytes_to_hex(ArtistId.base62.decode(artist_id.encode(), 16)))
2021-05-15 00:07:29 +02:00
raise TypeError("Not a Spotify artist ID: {}".format(uri))
@staticmethod
def from_base62(base62: str) -> ArtistId:
2022-07-12 00:42:25 +02:00
return ArtistId(util.bytes_to_hex(ArtistId.base62.decode(base62.encode(), 16)))
2021-05-15 00:07:29 +02:00
@staticmethod
def from_hex(hex_str: str) -> ArtistId:
return ArtistId(hex_str)
def to_mercury_uri(self) -> str:
2021-09-12 05:58:24 +02:00
return "hm://metadata/4/artist/{}".format(self.__hex_id)
2021-05-15 00:07:29 +02:00
def to_spotify_uri(self) -> str:
return "spotify:artist:{}".format(
2022-07-12 00:42:25 +02:00
ArtistId.base62.encode(util.hex_to_bytes(self.__hex_id)).decode())
2021-05-15 00:07:29 +02:00
def hex_id(self) -> str:
2021-09-12 05:58:24 +02:00
return self.__hex_id
2021-05-15 00:07:29 +02:00
class EpisodeId(SpotifyId, PlayableId):
2021-09-12 05:58:24 +02:00
pattern = re.compile(r"spotify:episode:(.{22})")
__hex_id: str
2021-05-15 00:07:29 +02:00
def __init__(self, hex_id: str):
2021-09-12 05:58:24 +02:00
self.__hex_id = hex_id.lower()
2021-05-15 00:07:29 +02:00
@staticmethod
def from_uri(uri: str) -> EpisodeId:
2021-09-12 05:58:24 +02:00
matcher = EpisodeId.pattern.search(uri)
2021-05-15 00:07:29 +02:00
if matcher is not None:
episode_id = matcher.group(1)
return EpisodeId(
2022-07-12 00:42:25 +02:00
util.bytes_to_hex(PlayableId.base62.decode(episode_id.encode(), 16)))
2021-07-31 13:42:13 +02:00
raise TypeError("Not a Spotify episode ID: {}".format(uri))
2021-05-15 00:07:29 +02:00
@staticmethod
def from_base62(base62: str) -> EpisodeId:
2021-05-15 00:08:08 +02:00
return EpisodeId(
2022-07-12 00:42:25 +02:00
util.bytes_to_hex(PlayableId.base62.decode(base62.encode(), 16)))
2021-05-15 00:07:29 +02:00
@staticmethod
def from_hex(hex_str: str) -> EpisodeId:
return EpisodeId(hex_str)
def to_mercury_uri(self) -> str:
2021-09-12 05:58:24 +02:00
return "hm://metadata/4/episode/{}".format(self.__hex_id)
2021-05-15 00:07:29 +02:00
def to_spotify_uri(self) -> str:
return "Spotify:episode:{}".format(
2022-07-12 00:42:25 +02:00
PlayableId.base62.encode(util.hex_to_bytes(self.__hex_id)).decode())
2021-05-15 00:07:29 +02:00
def hex_id(self) -> str:
2021-09-12 05:58:24 +02:00
return self.__hex_id
2021-05-15 00:07:29 +02:00
def get_gid(self) -> bytes:
2021-09-12 05:58:24 +02:00
return util.hex_to_bytes(self.__hex_id)
2021-05-15 00:07:29 +02:00
class ShowId(SpotifyId):
2021-09-12 05:58:24 +02:00
base62 = Base62.create_instance_with_inverted_character_set()
pattern = re.compile("spotify:show:(.{22})")
__hex_id: str
2021-05-15 00:07:29 +02:00
def __init__(self, hex_id: str):
2021-09-12 05:58:24 +02:00
self.__hex_id = hex_id
2021-05-15 00:07:29 +02:00
@staticmethod
def from_uri(uri: str) -> ShowId:
2021-09-12 05:58:24 +02:00
matcher = ShowId.pattern.search(uri)
2021-05-15 00:07:29 +02:00
if matcher is not None:
show_id = matcher.group(1)
2022-07-12 00:42:25 +02:00
return ShowId(util.bytes_to_hex(ShowId.base62.decode(show_id.encode(), 16)))
2021-05-15 00:07:29 +02:00
raise TypeError("Not a Spotify show ID: {}".format(uri))
@staticmethod
def from_base62(base62: str) -> ShowId:
2022-07-12 00:42:25 +02:00
return ShowId(util.bytes_to_hex(ShowId.base62.decode(base62.encode(), 16)))
2021-05-15 00:07:29 +02:00
@staticmethod
def from_hex(hex_str: str) -> ShowId:
return ShowId(hex_str)
def to_mercury_uri(self) -> str:
2021-09-12 05:58:24 +02:00
return "hm://metadata/4/show/{}".format(self.__hex_id)
2021-05-15 00:07:29 +02:00
def to_spotify_uri(self) -> str:
return "spotify:show:{}".format(
2022-07-12 00:42:25 +02:00
ShowId.base62.encode(util.hex_to_bytes(self.__hex_id)).decode())
2021-05-15 00:07:29 +02:00
def hex_id(self) -> str:
2021-09-12 05:58:24 +02:00
return self.__hex_id
2021-05-15 00:07:29 +02:00
class TrackId(PlayableId, SpotifyId):
2021-09-12 05:58:24 +02:00
pattern = re.compile("spotify:track:(.{22})")
__hex_id: str
2021-05-15 00:07:29 +02:00
def __init__(self, hex_id: str):
2021-09-12 05:58:24 +02:00
self.__hex_id = hex_id.lower()
2021-05-15 00:07:29 +02:00
@staticmethod
def from_uri(uri: str) -> TrackId:
2021-09-12 05:58:24 +02:00
search = TrackId.pattern.search(uri)
2021-05-15 00:07:29 +02:00
if search is not None:
track_id = search.group(1)
2021-05-15 00:08:08 +02:00
return TrackId(
2022-07-12 00:42:25 +02:00
util.bytes_to_hex(PlayableId.base62.decode(track_id.encode(), 16)))
2021-05-15 00:07:29 +02:00
raise RuntimeError("Not a Spotify track ID: {}".format(uri))
@staticmethod
def from_base62(base62: str) -> TrackId:
2022-07-12 00:42:25 +02:00
return TrackId(util.bytes_to_hex(PlayableId.base62.decode(base62.encode(), 16)))
2021-05-15 00:07:29 +02:00
@staticmethod
def from_hex(hex_str: str) -> TrackId:
return TrackId(hex_str)
2022-07-12 00:42:25 +02:00
def to_mercury_uri(self) -> str:
return "hm://metadata/4/track/{}".format(self.__hex_id)
2021-05-15 00:07:29 +02:00
def to_spotify_uri(self) -> str:
2022-07-12 00:42:25 +02:00
return "spotify:track:{}".format(TrackId.base62.encode(util.hex_to_bytes(self.__hex_id)).decode())
2021-05-15 00:07:29 +02:00
def hex_id(self) -> str:
2021-09-12 05:58:24 +02:00
return self.__hex_id
2021-05-15 00:07:29 +02:00
def get_gid(self) -> bytes:
2021-09-12 05:58:24 +02:00
return util.hex_to_bytes(self.__hex_id)