librespot-python/librespot/metadata/TrackId.py
deepsource-autofix[bot] 45e69da1a8
Format code with yapf
This commit fixes the style issues introduced in 3df41f0 according to the output
from yapf.

Details: https://deepsource.io/gh/kokarare1212/librespot-python/transform/8943cbb4-879c-4a5f-8f77-e14c8c5ac178/
2021-04-09 23:16:05 +00:00

42 lines
1.2 KiB
Python

from __future__ import annotations
from librespot.common import Utils
from librespot.metadata import SpotifyId
from librespot.metadata.PlayableId import PlayableId
import re
class TrackId(PlayableId, SpotifyId):
_PATTERN = re.compile("spotify:track:(.{22})")
_hexId: str
def __init__(self, hex_id: str):
self._hexId = hex_id.lower()
@staticmethod
def from_uri(uri: str) -> TrackId:
search = TrackId._PATTERN.search(uri)
if search is not None:
track_id = search.group(1)
return TrackId(
Utils.bytes_to_hex(PlayableId.BASE62.decode(track_id, 16)))
else:
raise RuntimeError("Not a Spotify track ID: {}".format(uri))
@staticmethod
def from_base62(base62: str) -> TrackId:
return TrackId(Utils.bytes_to_hex(PlayableId.BASE62.decode(base62,
16)))
@staticmethod
def from_hex(hex_str: str) -> TrackId:
return TrackId(hex_str)
def to_spotify_uri(self) -> str:
return "spotify:track:{}".format(self._hexId)
def hex_id(self) -> str:
return self._hexId
def get_gid(self) -> bytes:
return Utils.hex_to_bytes(self._hexId)