Skip to content

Commit 0a68299

Browse files
committed
#139 fix base62 issue
1 parent aeaf2a0 commit 0a68299

2 files changed

Lines changed: 21 additions & 21 deletions

File tree

librespot/metadata.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -91,28 +91,26 @@ def from_uri(uri: str) -> AlbumId:
9191
matcher = AlbumId.pattern.search(uri)
9292
if matcher is not None:
9393
album_id = matcher.group(1)
94-
return AlbumId(
95-
util.bytes_to_hex(AlbumId.base62.decode(album_id, 16)))
94+
return AlbumId(util.bytes_to_hex(AlbumId.base62.decode(album_id.encode())))
9695
raise TypeError("Not a Spotify album ID: {}.f".format(uri))
9796

9897
@staticmethod
9998
def from_base62(base62: str) -> AlbumId:
100-
return AlbumId(util.bytes_to_hex(AlbumId.base62.decode(base62, 16)))
99+
return AlbumId(util.bytes_to_hex(AlbumId.base62.decode(base62.encode(), 16)))
101100

102101
@staticmethod
103102
def from_hex(hex_str: str) -> AlbumId:
104103
return AlbumId(hex_str)
105104

106105
def to_mercury_uri(self) -> str:
107-
return "spotify:album:{}".format(
108-
AlbumId.base62.encode(util.hex_to_bytes(self.__hex_id)))
106+
return "hm://metadata/4/album/{}".format(self.__hex_id)
109107

110108
def hex_id(self) -> str:
111109
return self.__hex_id
112110

113111
def to_spotify_uri(self) -> str:
114112
return "spotify:album:{}".format(
115-
ArtistId.base62.encode(util.hex_to_bytes(self.__hex_id)))
113+
AlbumId.base62.encode(util.hex_to_bytes(self.__hex_id)).decode())
116114

117115

118116
class ArtistId(SpotifyId):
@@ -121,20 +119,20 @@ class ArtistId(SpotifyId):
121119
__hex_id: str
122120

123121
def __init__(self, hex_id: str):
124-
self.__hex_id = hex_id
122+
self.__hex_id = hex_id.lower()
125123

126124
@staticmethod
127125
def from_uri(uri: str) -> ArtistId:
128126
matcher = ArtistId.pattern.search(uri)
129127
if matcher is not None:
130128
artist_id = matcher.group(1)
131129
return ArtistId(
132-
util.bytes_to_hex(ArtistId.base62.decode(artist_id, 16)))
130+
util.bytes_to_hex(ArtistId.base62.decode(artist_id.encode(), 16)))
133131
raise TypeError("Not a Spotify artist ID: {}".format(uri))
134132

135133
@staticmethod
136134
def from_base62(base62: str) -> ArtistId:
137-
return ArtistId(util.bytes_to_hex(ArtistId.base62.decode(base62, 16)))
135+
return ArtistId(util.bytes_to_hex(ArtistId.base62.decode(base62.encode(), 16)))
138136

139137
@staticmethod
140138
def from_hex(hex_str: str) -> ArtistId:
@@ -145,7 +143,7 @@ def to_mercury_uri(self) -> str:
145143

146144
def to_spotify_uri(self) -> str:
147145
return "spotify:artist:{}".format(
148-
ArtistId.base62.encode(util.hex_to_bytes(self.__hex_id)))
146+
ArtistId.base62.encode(util.hex_to_bytes(self.__hex_id)).decode())
149147

150148
def hex_id(self) -> str:
151149
return self.__hex_id
@@ -164,13 +162,13 @@ def from_uri(uri: str) -> EpisodeId:
164162
if matcher is not None:
165163
episode_id = matcher.group(1)
166164
return EpisodeId(
167-
util.bytes_to_hex(PlayableId.base62.decode(episode_id, 16)))
165+
util.bytes_to_hex(PlayableId.base62.decode(episode_id.encode(), 16)))
168166
raise TypeError("Not a Spotify episode ID: {}".format(uri))
169167

170168
@staticmethod
171169
def from_base62(base62: str) -> EpisodeId:
172170
return EpisodeId(
173-
util.bytes_to_hex(PlayableId.base62.decode(base62, 16)))
171+
util.bytes_to_hex(PlayableId.base62.decode(base62.encode(), 16)))
174172

175173
@staticmethod
176174
def from_hex(hex_str: str) -> EpisodeId:
@@ -181,7 +179,7 @@ def to_mercury_uri(self) -> str:
181179

182180
def to_spotify_uri(self) -> str:
183181
return "Spotify:episode:{}".format(
184-
PlayableId.base62.encode(util.hex_to_bytes(self.__hex_id)))
182+
PlayableId.base62.encode(util.hex_to_bytes(self.__hex_id)).decode())
185183

186184
def hex_id(self) -> str:
187185
return self.__hex_id
@@ -203,12 +201,12 @@ def from_uri(uri: str) -> ShowId:
203201
matcher = ShowId.pattern.search(uri)
204202
if matcher is not None:
205203
show_id = matcher.group(1)
206-
return ShowId(util.bytes_to_hex(ShowId.base62.decode(show_id, 16)))
204+
return ShowId(util.bytes_to_hex(ShowId.base62.decode(show_id.encode(), 16)))
207205
raise TypeError("Not a Spotify show ID: {}".format(uri))
208206

209207
@staticmethod
210208
def from_base62(base62: str) -> ShowId:
211-
return ShowId(util.bytes_to_hex(ShowId.base62.decode(base62, 16)))
209+
return ShowId(util.bytes_to_hex(ShowId.base62.decode(base62.encode(), 16)))
212210

213211
@staticmethod
214212
def from_hex(hex_str: str) -> ShowId:
@@ -219,7 +217,7 @@ def to_mercury_uri(self) -> str:
219217

220218
def to_spotify_uri(self) -> str:
221219
return "spotify:show:{}".format(
222-
ShowId.base62.encode(util.hex_to_bytes(self.__hex_id)))
220+
ShowId.base62.encode(util.hex_to_bytes(self.__hex_id)).decode())
223221

224222
def hex_id(self) -> str:
225223
return self.__hex_id
@@ -238,19 +236,22 @@ def from_uri(uri: str) -> TrackId:
238236
if search is not None:
239237
track_id = search.group(1)
240238
return TrackId(
241-
util.bytes_to_hex(PlayableId.base62.decode(track_id, 16)))
239+
util.bytes_to_hex(PlayableId.base62.decode(track_id.encode(), 16)))
242240
raise RuntimeError("Not a Spotify track ID: {}".format(uri))
243241

244242
@staticmethod
245243
def from_base62(base62: str) -> TrackId:
246-
return TrackId(util.bytes_to_hex(PlayableId.base62.decode(base62, 16)))
244+
return TrackId(util.bytes_to_hex(PlayableId.base62.decode(base62.encode(), 16)))
247245

248246
@staticmethod
249247
def from_hex(hex_str: str) -> TrackId:
250248
return TrackId(hex_str)
251249

250+
def to_mercury_uri(self) -> str:
251+
return "hm://metadata/4/track/{}".format(self.__hex_id)
252+
252253
def to_spotify_uri(self) -> str:
253-
return "spotify:track:{}".format(self.__hex_id)
254+
return "spotify:track:{}".format(TrackId.base62.encode(util.hex_to_bytes(self.__hex_id)).decode())
254255

255256
def hex_id(self) -> str:
256257
return self.__hex_id

librespot/util.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ def decode(self, encoded: bytes, length: int = -1):
6464
def translate(self, indices: bytes, dictionary: bytes):
6565
translation = bytearray(len(indices))
6666
for i in range(len(indices)):
67-
translation[i] = dictionary[int.from_bytes(indices[i].encode(),
68-
"big")]
67+
translation[i] = dictionary[int.from_bytes(bytes([indices[i]]), "big")]
6968
return translation
7069

7170
def convert(self, message: bytes, source_base: int, target_base: int,

0 commit comments

Comments
 (0)