2021-02-24 00:46:59 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-04-10 01:16:20 +02:00
|
|
|
import re
|
|
|
|
|
2021-04-10 01:16:25 +02:00
|
|
|
from librespot.common import Base62
|
|
|
|
from librespot.common import Utils
|
2021-02-24 00:46:59 +01:00
|
|
|
from librespot.metadata import SpotifyId
|
|
|
|
|
|
|
|
|
|
|
|
class AlbumId(SpotifyId.SpotifyId):
|
|
|
|
_PATTERN = re.compile(r"spotify:album:(.{22})")
|
|
|
|
_BASE62 = Base62.create_instance_with_inverted_character_set()
|
|
|
|
_hexId: str
|
|
|
|
|
|
|
|
def __init__(self, hex_id: str):
|
|
|
|
self._hexId = hex_id.lower()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def from_uri(uri: str) -> AlbumId:
|
|
|
|
matcher = AlbumId._PATTERN.search(uri)
|
|
|
|
if matcher is not None:
|
|
|
|
album_id = matcher.group(1)
|
2021-04-10 01:16:32 +02:00
|
|
|
return AlbumId(
|
|
|
|
Utils.bytes_to_hex(AlbumId._BASE62.decode(album_id, 16)))
|
2021-02-24 00:46:59 +01:00
|
|
|
else:
|
|
|
|
raise TypeError("Not a Spotify album ID: {}.f".format(uri))
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def from_base62(base62: str) -> AlbumId:
|
2021-04-10 01:16:05 +02:00
|
|
|
return AlbumId(Utils.bytes_to_hex(AlbumId._BASE62.decode(base62, 16)))
|
2021-02-24 00:46:59 +01:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def from_hex(hex_str: str) -> AlbumId:
|
|
|
|
return AlbumId(hex_str)
|
|
|
|
|
|
|
|
def to_mercury_uri(self) -> str:
|
|
|
|
return "spotify:album:{}".format(
|
2021-04-10 01:16:32 +02:00
|
|
|
AlbumId._BASE62.encode(Utils.hex_to_bytes(self._hexId)))
|
2021-02-24 00:46:59 +01:00
|
|
|
|
|
|
|
def hex_id(self) -> str:
|
|
|
|
return self._hexId
|