-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathximalaya.py
81 lines (60 loc) · 1.99 KB
/
ximalaya.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
#!/usr/bin/env python3
import requests
import html
import json
import sys
import os
from threading import Thread
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
def main():
albumId = sys.argv[1]
getAlbum(albumId)
def getAlbum(albumId):
pageNum = 1
pageSize = 30
fileList = []
while True:
url = f'https://www.ximalaya.com/revision/play/album?albumId={albumId}&pageNum={pageNum}&sort=-1&pageSize={pageSize}'
print(url)
r = requests.get(url, headers={'User-Agent': 'curl'})
if r.status_code != 200:
print(f'Error, request status code: {r.status_code}')
return
jsonstr = html.unescape(r.content.decode('utf-8'))
album = json.loads(jsonstr)
hasMore = album['data']['hasMore']
tracks = album['data']['tracksAudioPlay']
if not tracks:
print('Error, no result')
return
albumName = tracks[0]['albumName']
if not os.path.exists(albumName):
os.makedirs(albumName)
for t in tracks:
src = t['src']
if src != None:
fileList.append((t['trackName'], src))
if not hasMore:
break
pageNum += 1
i = 1
total = len(fileList)
for name, link in fileList:
# ximalaya has limit on max connection numbers at same time
# so we use single thread here
print(f'[{i}/{total}] {name} - {link}')
downloadFile(link, albumName, name, '.m4a')
i += 1
print('Download complete!')
def downloadFile(url, folder, fileName, ext='.mp3'):
session = requests.Session()
adapter = HTTPAdapter(max_retries=Retry(connect=3, backoff_factor=3))
session.mount('http://', adapter)
session.mount('https://', adapter)
r = session.get(url)
if r.status_code == 200:
with open(f'{folder}/{fileName}{ext}', 'wb') as f:
f.write(r.content)
if __name__ == '__main__':
main()