-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathspotify.py
620 lines (528 loc) · 20.2 KB
/
spotify.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
import glob
import json
import logging
import subprocess
import time
from os import mkdir, remove
from os.path import exists, join, getsize
from urllib.parse import urlparse
import requests
import wget
from Crypto.Cipher import AES
from Crypto.Util import Counter
from pywidevine import Device, PSSH, Cdm
from pywidevine.exceptions import InvalidSession, InvalidLicenseMessage, InvalidContext, SignatureMismatch
from playplay_pb2 import PlayPlayLicenseRequest, PlayPlayLicenseResponse, AUDIO_TRACK, Interactivity
from id_type import IDType
from token_manager import TokenManager
class Spotify:
MP4_FORMATS = [
'MP4_128',
'MP4_128_DUAL',
'MP4_256',
'MP4_256_DUAL'
]
OGG_FORMATS = [
'OGG_VORBIS_320',
'OGG_VORBIS_160',
'OGG_VORBIS_96'
]
def __init__(
self,
url_id: str,
token_manager: TokenManager,
quality: str,
output_folder: str
):
"""
Spotify Downloader
Author: github.com/DevLARLEY
"""
self.id, self.id_type = self.get_id_type(url_id)
self.token_manager = token_manager
self.quality = quality
self.output_folder = output_folder
self.client_bases = self._request_client_bases()
self.license_url = self._build_license_url(self.client_bases)
if not exists(self.output_folder):
mkdir(self.output_folder)
@staticmethod
def _get_cdms() -> list[str]:
if not (cdms := glob.glob(join("cdm", "*.wvd"))):
logging.error("No CDMs found")
exit(1)
return cdms
@staticmethod
def _request_client_bases():
request = requests.get(
url="https://apresolve.spotify.com",
params={
"type": "spclient"
}
)
if request.status_code != 200:
logging.error(f'Unable to request client bases ({request.status_code}): {request.text}')
return
def format_endpoint(endpoint: str):
domain, port = endpoint.split(':', 1)
match port:
case "80":
return f'http://{domain}'
case "443":
return f'https://{domain}'
return list(
map(
format_endpoint,
request.json().get('spclient')
)
)
@staticmethod
def _build_license_url(client_bases: list) -> str | None:
if client_bases:
return f'{client_bases[0]}/widevine-license/v1/audio/license'
@staticmethod
def get_id_type(url_id: str) -> tuple[str, IDType]:
if url_id.startswith('http'):
url_parse = urlparse(url_id)
if url_parse.netloc != 'open.spotify.com':
raise Exception("Invalid domain")
split = url_parse.path.split('/')
if len(split) < 3:
raise Exception("Invalid URL path")
return split[2], IDType(split[1])
elif url_id.startswith('spotify:'):
split = url_id.split(':')
return split[2], IDType(split[1])
return url_id, IDType.TRACK
def _request_album(
self,
album_id: str,
offset: int,
initial_tracks: list
) -> list[tuple[str, IDType]]:
album_request = requests.get(
url=f'https://api.spotify.com/v1/albums/{album_id}/tracks',
params={
"offset": offset,
"limit": 50
},
headers={
"Authorization": f'Bearer {self.token_manager.get_access_token()}'
}
)
if album_request.status_code != 200:
logging.error(f"Unable to request album ({album_request.status_code}): {album_request.text}")
return []
album_json = album_request.json()
tracks = list(
map(
lambda track: (track.get('id'), IDType.TRACK),
album_json.get('items')
)
)
if album_json.get('total') > 50:
if (tracks_length := len(tracks)) > 0:
return self._request_album(
album_id=album_id,
offset=offset + tracks_length,
initial_tracks=initial_tracks + tracks
)
else:
return initial_tracks + tracks
return tracks
def _request_playlist(
self,
playlist_id: str,
offset: int,
initial_tracks: list
) -> list[tuple[str, IDType]]:
playlist_request = requests.get(
url=f'https://api.spotify.com/v1/playlists/{playlist_id}/tracks',
params={
"offset": offset,
"limit": 100
},
headers={
"Authorization": f'Bearer {self.token_manager.get_access_token()}'
}
)
if playlist_request.status_code != 200:
logging.error(f"Unable to request playlist ({playlist_request.status_code}): {playlist_request.text}")
return []
album_json = playlist_request.json()
tracks = list(
map(
lambda item: (item.get('track', {}).get('id'), IDType.TRACK),
album_json.get('items')
)
)
if album_json.get('total') > 100:
if (tracks_length := len(tracks)) > 0:
return self._request_playlist(
playlist_id=playlist_id,
offset=offset + tracks_length,
initial_tracks=initial_tracks + tracks
)
else:
return initial_tracks + tracks
return tracks
def _request_show(
self,
show_id: str,
offset: int,
initial_tracks: list
) -> list[tuple[str, IDType]]:
show_request = requests.get(
url=f'https://api.spotify.com/v1/shows/{show_id}/episodes',
params={
"offset": offset,
"limit": 50
},
headers={
"Authorization": f'Bearer {self.token_manager.get_access_token()}'
}
)
if show_request.status_code != 200:
logging.error(f"Unable to request show ({show_request.status_code}): {show_request.text}")
return []
show_json = show_request.json()
tracks = list(
map(
lambda track: (track.get('id'), IDType.EPISODE),
show_json.get('items')
)
)
if show_json.get('total') > 50:
if (tracks_length := len(tracks)) > 0:
return self._request_show(
show_id=show_id,
offset=offset + tracks_length,
initial_tracks=initial_tracks + tracks
)
else:
return initial_tracks + tracks
return tracks
def get_tracks(self) -> list[tuple[str, IDType]]:
match self.id_type:
case IDType.ALBUM:
return self._request_album(
album_id=self.id,
offset=0,
initial_tracks=[]
)
case IDType.PLAYLIST:
return self._request_playlist(
playlist_id=self.id,
offset=0,
initial_tracks=[]
)
case IDType.SHOW:
return self._request_show(
show_id=self.id,
offset=0,
initial_tracks=[]
)
case _:
return [(self.id, self.id_type)]
@staticmethod
def _spotify_id_to_hex(spotify_id: str) -> str:
id_bytes = spotify_id.encode()
alphabet = b'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
dictionary = bytearray(256)
for i in range(62):
dictionary[alphabet[i]] = i & 0xff
big = bytearray(22)
for i in range(22):
big[i] = dictionary[int.from_bytes(id_bytes[i].to_bytes(1, 'big'))]
out = bytearray()
while len(big) > 0:
quotient = bytearray()
remainder = 0
for b in big:
accumulator = int(b & 0xff) + remainder * 62
digit = int((accumulator - (accumulator % 256)) / 256)
remainder = int(accumulator % 256)
if len(quotient) > 0 or digit > 0:
quotient += bytes([digit])
out += bytes([remainder])
big = quotient
out.reverse()
return out.hex()
def _select_from_quality(
self,
file_entries: list
) -> str | None:
for entry in file_entries:
if entry.get('format') == self.quality:
return entry.get('file_id', entry.get('fileId'))
# desired quality wasn't found, get best
logging.warning('Unable to find desired quality. Falling back to best.')
if (best := list(
filter(
lambda e: e.get('format') in [*self.MP4_FORMATS, *self.OGG_FORMATS],
sorted(
file_entries,
key=lambda e: sorted(e.get('format').split('_')),
reverse=True
)
)
)):
return best[0].get('file_id', best[0].get('fileId'))
def _get_track_metadata(
self,
track_id: str
) -> tuple[str, str, str] | None:
metadata_request = requests.get(
url=f"https://spclient.wg.spotify.com/metadata/4/track/{self._spotify_id_to_hex(track_id)}",
headers={
"Accept": "application/json",
"Authorization": f'Bearer {self.token_manager.get_access_token()}'
}
)
if metadata_request.status_code != 200:
logging.error(f"Unable to request track metadata ({metadata_request.status_code}): {metadata_request.text}")
return
meta_json = metadata_request.json()
logging.debug(meta_json)
title, artist = meta_json.get('name'), meta_json.get('artist', [{}])[0].get('name')
files = meta_json.get('file')
if not files:
files = meta_json.get('alternative', [{}])[0].get('file')
if not files:
logging.error(f"'{title}' is not available")
return
return title, artist, self._select_from_quality(files)
def _get_episode_metadata(
self,
track_id: str
) -> tuple[str, str, str] | None:
metadata_request = requests.get(
url="https://api-partner.spotify.com/pathfinder/v1/query",
headers={
"Accept": "application/json",
"Authorization": f'Bearer {self.token_manager.get_access_token()}',
},
params={
'operationName': 'getEpisodeOrChapter',
'variables': json.dumps({
"uri": f"spotify:episode:{track_id}"
}),
'extensions': json.dumps({
"persistedQuery": {
"version": 1,
"sha256Hash": "9697538fe993af785c10725a40bb9265a20b998ccd2383bd6f586e01303824e9"
}
})
}
)
if metadata_request.status_code != 200:
logging.error(
f"Unable to request episode metadata ({metadata_request.status_code}): {metadata_request.text}")
return
meta_json = metadata_request.json()
episode = meta_json.get('data', {}).get('episodeUnionV2')
title, artist = episode.get('name'), episode.get('creator')
items = episode.get('audio', {}).get('items', [])
if not items:
logging.error(f"'{title}' is not available")
return
return title, artist, self._select_from_quality(items)
@staticmethod
def _request_pssh(pssh_id: str) -> str | None:
pssh_request = requests.get(
url=f"https://seektables.scdn.co/seektable/{pssh_id}.json"
)
if pssh_request.status_code != 200:
logging.error(f"Unable to request PSSH ({pssh_request.status_code}): {pssh_request.text}")
return
if not (pssh := pssh_request.json().get('pssh')):
logging.error(f"Unable to locate PSSH in file manifest: {pssh_request.text}")
return
return pssh
def _get_keys(
self,
pssh: str,
license_url: str
) -> list[str] | None:
device = Device.load(self._get_cdms()[0])
cdm = Cdm.from_device(device)
session_id = cdm.open()
challenge = cdm.get_license_challenge(session_id, PSSH(pssh))
license_request = requests.post(
url=license_url,
headers={
"Authorization": f'Bearer {self.token_manager.get_access_token()}',
},
data=challenge
)
if license_request.status_code != 200:
logging.error(f"Unable to request license ({license_request.status_code}): {license_request.text}")
return
try:
cdm.parse_license(session_id, license_request.content)
except InvalidSession | InvalidLicenseMessage | InvalidContext | SignatureMismatch:
logging.error(f"Unable to parse license: {license_request.text}")
return
keys = list(
map(
lambda key: f"{key.kid.hex}:{key.key.hex()}",
filter(
lambda key: key.type == 'CONTENT',
cdm.get_keys(session_id)
)
)
)
cdm.close(session_id)
return keys
def _request_cdn_url(
self,
file_id: str
) -> str | None:
cdn_request = requests.get(
url=f"https://gew4-spclient.spotify.com/storage-resolve/files/audio/interactive/{file_id}",
params={
"alt": "json"
},
headers={
'Authorization': f'Bearer {self.token_manager.get_access_token()}'
}
)
if cdn_request.status_code != 200:
logging.error(f"Unable to request CDN URL ({cdn_request.status_code}): {cdn_request.text}")
return
cdn_json = cdn_request.json()
if not (cdn_url := cdn_json.get('cdnurl', [None])[0]):
logging.error("No CDN URL found")
return
return cdn_url
@staticmethod
def _clean_name(file_name: str) -> str:
allowed = " !#$%&'()+,-.0123456789;=@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{}~"
return ''.join(
filter(
lambda c: c in allowed,
file_name
)
)
@staticmethod
def _wget_download(url: str) -> str:
output_file = wget.download(
url=url,
bar=lambda c, t, _: print(
f'\r[INFO]: {round(c / t * 100)}% [{"#" * round(c / t * 100) + " " * (100 - round(c / t * 100))}] {round(c / 1000000, 2)}MB ',
end=''
)
)
print()
return output_file
def download(
self,
track_info: tuple[str, IDType]
):
track_id, track_type = track_info
match track_type:
case IDType.TRACK:
if metadata := self._get_track_metadata(track_id):
title, artist, file_id = metadata
else:
return
case IDType.EPISODE:
if metadata := self._get_episode_metadata(track_id):
title, artist, file_id = metadata
else:
return
case _:
return
if self.quality in self.MP4_FORMATS:
if not (pssh := self._request_pssh(file_id)):
return
logging.debug(f"PSSH: {pssh}")
if not (keys := self._get_keys(
pssh=pssh,
license_url=self.license_url
)):
return
logging.debug(f"Keys: {keys}")
if not (cdn_url := self._request_cdn_url(file_id)):
return
if exists(file_id):
remove(file_id)
downloaded = self._wget_download(cdn_url)
if not exists(downloaded):
logging.error("Downloaded file doesn't exist")
return
clean = self._clean_name(f'{title}{f" - {artist}" if artist else ""}')
output_file = f"{join(self.output_folder, clean)}.m4a"
if exists(output_file):
remove(output_file)
subprocess.run(
command := [
"mp4decrypt/mp4decrypt",
*sum([['--key', i] for i in keys], []),
downloaded,
output_file
],
shell=False
)
logging.debug(' '.join(command))
if getsize(output_file) == 0:
logging.error(f"Unable to decrypt {downloaded}")
return
elif self.quality in self.OGG_FORMATS:
playplay_license_request = PlayPlayLicenseRequest(
version=2,
token=bytes.fromhex("01e132cae527bd21620e822f58514932"),
interactivity=Interactivity.DOWNLOAD,
content_type=AUDIO_TRACK
)
# TODO: move to method
playplay_request = requests.post(
url=f"https://gew4-spclient.spotify.com/playplay/v1/key/{file_id}",
headers={
"Authorization": f"Bearer {self.token_manager.get_access_token()}",
},
data=playplay_license_request.SerializeToString()
)
playplay_license_response = PlayPlayLicenseResponse()
playplay_license_response.ParseFromString(playplay_request.content)
output = subprocess.check_output(
[
'playplay/playplay',
file_id,
obfuscated := playplay_license_response.obfuscated_key.hex()
],
shell=False
)
key = bytes.fromhex(output.strip().decode('utf-8'))
logging.debug(f'obfuscated: {obfuscated}, deobfuscated: {key.hex()}')
if not key:
logging.error('Unable to decrypt key')
return
if not (cdn_url := self._request_cdn_url(file_id)):
return
if exists(file_id):
remove(file_id)
downloaded = self._wget_download(cdn_url)
cipher = AES.new(
key=key,
mode=AES.MODE_CTR,
counter=Counter.new(
128,
initial_value=int.from_bytes(b'r\xe0g\xfb\xdd\xcb\xcfw\xeb\xe8\xbcd?c\r\x93', "big")
)
)
decrypted_buffer = cipher.decrypt(
open(downloaded, 'rb').read()
)
if not decrypted_buffer.startswith(b"OggS"):
logging.error("Decryption failed! The cause of this is still unknown, but 'OGG_VORBIS_160' should work.")
return
clean = self._clean_name(f'{title}{f" - {artist}" if artist else ""}')
output_file = f"{join(self.output_folder, clean)}.ogg"
with open(output_file, 'wb') as fw:
fw.write(decrypted_buffer[167:])
else:
return
remove(downloaded)
logging.info(f'Finished: {clean}')
time.sleep(1) # anti-timeout