-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathpython2.py
More file actions
45 lines (37 loc) · 1.22 KB
/
Copy pathpython2.py
File metadata and controls
45 lines (37 loc) · 1.22 KB
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
import json
import sys
from urllib import urlopen
welcome = '===== Get tracks of your 163 playlist ====='
enterId = 'Enter the playlist id (Enter ? to get help):'
help = 'To get the id of the playlist, go to the page\
of it and look at the address bar.\
\nPlaylist id is the numbers after \
"http://music.163.com/#/playlist?id="'
errRetrive = 'No data retrived. \nPlease check the playlist id again.'
while 1:
print(welcome)
playlistId = raw_input(enterId)
#change the playlistId variable
if playlistId == '?':
print(help)
continue
urladd = "http://music.163.com/api/playlist/detail?id="\
+ str(playlistId)
# Your code where you can use urlopen
response = urlopen(urladd).read()
data = json.loads(response)
output = ""
if "result" not in data:
print(errRetrive)
print(help)
continue
tracks = data["result"]["tracks"]
for track in tracks:
trackName = track["name"]
artist = track["artists"][0]["name"]
output += trackName + ' - ' + artist + '\n'
playlistName = data["result"]["name"]
with open(playlistName + '.txt', 'w') as file:
file.write(output.encode('utf8'))
print('===== Success =====\nCheck the directory of this file and find the .txt file!')
print