forked from sirodeneko/NeteaseCloudMusicApiWithGo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_api.py
126 lines (91 loc) · 2.77 KB
/
test_api.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
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import datetime
import requests
import sys
from simplejson import loads, dumps
HOST = 'http://localhost:3333'
SONG_URL = 'https://music.163.com/#/song?id=%s'
def login():
url = HOST + '/login/cellphone'
resp = requests.get(url, {
'phone': '17600083957',
'password': 'hit6886033'
})
data = resp.json()
def refresh_login():
pass
def search(q):
print 'search:' + q
url = HOST + '/search?keywords=' + q
resp = requests.get(url)
data = resp.json()
code = data.get('code')
if code != 200:
return
result = data['result']
song_count = result.get('songCount', 0)
print '搜索结果数量:', song_count
song_list = result.get('songs', [])
privileges = batch_get_privileges([str(s['id']) for s in song_list])
for i, s in enumerate(song_list):
id = s['id']
name = s['name']
fee = s['fee']
artists = s['artists']
duration = s['duration']
artists_names = ', '.join([c['name'] for c in artists])
idx = i + 1
copyright_id = s['copyrightId']
vip = privileges[i]['cp'] and '免费' or '付费VIP歌曲'
print idx, id, name, fee, 'duration:', duration_str(duration), '艺术家:', artists_names, 'copyright', copyright_id, SONG_URL % id, s['status'], vip
def duration_str(d):
sec = d / 1000
s = str(datetime.timedelta(seconds=int(sec)))
ss = s.split(':')
if len(ss) == 3:
return ':'.join(ss[1:])
return ss
def batch_get_privileges(ids):
url = HOST + '/song/detail?ids=' + ','.join(ids)
resp = requests.get(url)
data = resp.json()
code = data.get('code')
if code != 200:
return
privileges = data['privileges']
return privileges
def get_song(id):
url = HOST + '/song/detail?ids=' + str(id)
resp = requests.get(url)
data = resp.json()
code = data.get('code')
if code != 200:
return
song_list = data.get('songs', [])
for i, s in enumerate(song_list):
id = s['id']
name = s['name']
fee = s['fee']
artists = s['ar']
duration = s['dt']
artists_names = ', '.join([c['name'] for c in artists])
idx = i + 1
print idx, id, name, fee, 'duration:', duration_str(duration), '艺术家:', artists_names, 'copyright', s['copyright'], SONG_URL % id, s['st']
#print dumps(s)
# import ipdb
# ipdb.set_trace()
if __name__ == '__main__':
args = sys.argv
if len(args) == 1:
id = 168091 # 蓝莲花VIP
get_song(id)
id = 167908 # 有版权
get_song(id)
id = 25706282
get_song(id)
id = 185694 # 无版权
get_song(id)
else:
q = sys.argv[1]
search(q)