-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathAlternativeMethodForPy2.py
61 lines (47 loc) · 1.63 KB
/
AlternativeMethodForPy2.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
import json
import sys
import urllib
import httplib
import urllib2
# using API provided by dongyonghui(https://github.com/mrdong916)
# instruction page: github.com/mrdong916/DAPI
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
# transCode 020111 assigns to retrieve playlist from 163 music
postContent = {"TransCode":"020111","OpenId":"123456789","Body":{"SongListId":playlistId}}
encodedContent = json.dumps(postContent)
reqUrl = "https://api.hibai.cn/api/index/index"
req = urllib2.Request(reqUrl)
req.add_header('Content-Type', 'application/json')
req.add_header('Accept', 'application/json')
response = urllib2.urlopen(req, encodedContent).read()
data = json.loads(response)
output = ""
if data["ErrCode"] != "OK":
print(data["ErrCode"])
print(errRetrive)
print(help)
continue
body = data["Body"]
playlistName = body["name"]
tracks = body["songs"]
for track in tracks:
trackName = track["title"]
artist = track["author"]
output += trackName + " - " + artist + "\n"
with open(playlistName + ".txt", "w", encoding="utf-8") as file:
file.write(output.encode("utf8"))
print("===== Success =====\nCheck the directory of this file and find the .txt file!")
print