-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathpython3.py
More file actions
47 lines (39 loc) · 1.24 KB
/
python3.py
File metadata and controls
47 lines (39 loc) · 1.24 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
46
47
# -*- coding: utf-8 -*-
import json
import sys, io
from urllib.request 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 = 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
with urlopen(urladd) as url:
response = url.read().decode('utf-8')
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', encoding='utf-8') as file:
file.write(output)
print('=== Success ===\nCheck the directory of this file and find the .txt file!')
print