forked from Superheroff/musicapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusicapi.py
406 lines (370 loc) · 15.4 KB
/
musicapi.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# -*- coding: utf-8 -*-
"""
@Time : 2023/3/25 18:52
@Author : superhero
@Email : 838210720@qq.com
@File : musicapi.py
@IDE: PyCharm
"""
import base64
import codecs
import hashlib
import json
import random
import time
import execjs
import requests
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad
from lxml import etree
class kugou(object):
def __init__(self, tid: str):
"""
初始化
:param tid: 歌单id
"""
self.mid = '239526275778893399526700786998289824956'
self.userid = '0'
self.tid = tid
self.data_list = [] # 组装歌单列表
self.ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36'
def kugou_signature(self, url):
"""
计算酷狗sign
:param url:
:return:
"""
uri = url.split('?')[1]
uri_list = uri.split('&')
ordered_list = sorted(uri_list)
uri = 'OIlwieks28dk2k092lksi2UIkp' + "".join(ordered_list) + 'OIlwieks28dk2k092lksi2UIkp'
return hashlib.md5(uri.encode(encoding='utf-8')).hexdigest()
def kugou_url(self, hash):
"""
获取酷狗音乐源地址
:param hash:
:return:
"""
hax = hash + '57ae12eb6890223e355ccfcb74edf70d1005' + self.mid + self.userid
str_md5 = hashlib.md5(hax.encode(encoding='utf-8')).hexdigest()
url = f'https://gateway.kugou.com/i/v2/?dfid=&pid=2&mid={self.mid}&cmd=26&token=&hash={hash}&area_code=1&behavior=play&appid=1005&module=&vipType=6&ptype=1&userid=0&mtype=1&album_id=&pidversion=3001&key={str_md5}&version=10209&album_audio_id=&with_res_tag=1'
header = {
'Host': 'gateway.kugou.com',
'x-router': 'tracker.kugou.com',
'User-Agent': 'Android511-AndroidPhone-10209-14-0-NetMusic-wifi'
}
ret = requests.get(url, headers=header).text
# print(ret)
ret = ret.replace('<!--KG_TAG_RES_START-->', '')
ret = ret.replace('<!--KG_TAG_RES_END-->', '')
json_data = json.loads(ret)
return json_data['url'][0]
def kugou_lrc(self, hash):
"""
获取酷狗歌词
:param hash:
:return:
"""
url = f'http://krcs.kugou.com/search?ver=1&man=yes&client=mobi&keyword=&duration=&hash={hash}&album_audio_id='
res = requests.get(url)
ret = 'null'
if res.status_code == 200:
json_data = res.json()
url = f'https://lyrics.kugou.com/download?ver=1&client=pc&id={json_data["candidates"][0]["id"]}&accesskey={json_data["candidates"][0]["accesskey"]}&fmt=lrc&charset=utf8'
res = requests.get(url).json()
ret = base64.b64decode(res['content']).decode("utf-8")
return ret
def kugou_list(self):
"""
获取酷狗歌单列表
:return:
"""
url = f'http://gatewayretry.kugou.com/v2/get_other_list_file?specialid={self.tid}&need_sort=1&module=CloudMusic' \
'&clientver=11239&pagesize=300&specalidpgc' \
f'={self.tid}&userid=0&page=1&type=0&area_code=1&appid=1005'
header = {
'User-Agent': 'Android9-AndroidPhone-11239-18-0-playlist-wifi',
'Host': 'gatewayretry.kugou.com',
'x-router': 'pubsongscdn.kugou.com',
'mid': '239526275778893399526700786998289824956', # 设备id
'dfid': '-',
'clienttime': str(time.time()).split('.')[0]
}
signature = self.kugou_signature(url)
url = url + '&signature=' + signature
ret = requests.get(url, headers=header)
if ret.status_code == 200:
json_data = ret.json()
self.data_list.clear()
for i in json_data['data']['info']:
name = i['name'].split(' - ')
song_id = i['hash']
self.data_list.append(
{'title': name[1], 'author': name[0], 'url': 'http://api2.52jan.com/kugou/%s' % song_id,
'pic': i['cover'].replace('/{size}', ''),
'lrc': 'http://api2.52jan.com/kugou/lrc/%s.lrc' % song_id})
else:
pass
# print(self.data_list)
return self.data_list
def to_16(self, key):
while len(key) % 16 != 0:
key += '\0'
return str.encode(key)
def AES_encrypt(self, text, key, iv):
bs = AES.block_size
pad2 = lambda s: s + (bs - len(s) % bs) * chr(bs - len(s) % bs)
encryptor = AES.new(self.to_16(key), AES.MODE_CBC, self.to_16(iv))
pd2 = pad(str.encode(pad2(text)), 16)
encrypt_aes = encryptor.encrypt(pd2)
encrypt_text = str(base64.encodebytes(encrypt_aes), encoding='utf-8')
return encrypt_text
def RSA_encrypt(self, text, pubKey, modulus):
text = text[::-1]
rs = int(codecs.encode(text.encode('utf-8'), 'hex_codec'), 16) ** int(pubKey, 16) % int(modulus, 16)
return format(rs, 'x').zfill(256)
class wyymusic(kugou):
def __init__(self, tid: str):
super(wyymusic, self).__init__(tid)
get_i = execjs.compile(r"""
function a(a) {
var d, e, b = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", c = "";
for (d = 0; a > d; d += 1)
e = Math.random() * b.length,
e = Math.floor(e),
c += b.charAt(e);
return c
}
""")
# csrf_token、cookie和MUSIC_U需要抓包获取,如果你的账号开通了会员将支持会员歌曲
self.csrf_token = ''
self.cookie = {}
self.MUSIC_U = ''
self.g = '0CoJUm6Qyw8W8jud'
self.b = "010001"
self.c = '00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7'
self.i = get_i.call('a', 16)
self.iv = "0102030405060708"
def get_encSecKey(self):
return self.RSA_encrypt(self.i, self.b, self.c)
def wyy_discover(self):
"""
获取歌单详情
:param list_id: 列表id
:return:
"""
# 这里要添加自己的cookie
url = "http://music.163.com/api/v6/playlist/detail"
ret = requests.post(url, data={"s": "0", "id": list_id, "n": "1000", "t": "0"},
headers={"User-Agent": self.ua, "Cookie": self.cookie}).json()
ids = ''
for i in ret['playlist']['trackIds']:
ids += str(i['id']) + ','
# ret = requests.get("https://music.163.com/playlist?id=" + list_id,
# headers={"User-Agent": self.ua, "Cookie": self.cookie}).text
# soup = etree.HTML(ret)
# m_id = soup.xpath('//ul[@class="f-hide"]/li/a/@href')
# ids = ''
# for i in m_id:
# ids += i.replace('/song?id=', '') + ','
self.get_music_info(ids[:-1])
return self.data_list
def wyy_music_list(self, t_ids):
"""
根据歌曲id拼接成想要的歌曲列表
:param t_ids: 歌单列表id数组
:return:
"""
url = f'http://music.163.com/api/song/detail/?id={t_ids}&ids=%5B{t_ids}%5D'
ret = requests.get(url).json()
self.data_list.clear()
for i in ret['songs']:
song_id = i['id']
author = ''
for n in i['artists']:
author += n['name'] + '/'
self.data_list.append({'title': i['name'], 'author': author[:-1],
'url': 'http://api2.52jan.com/wyy/%s' % song_id,
'pic': i['album']['picUrl'],
'lrc': 'http://api2.52jan.com/wyy/lrc/%s.lrc' % song_id})
def wyy_url(self, m_id: str):
"""
获取网易音乐源地址
:param m_id: 音乐id
:return:
"""
url = 'https://music.163.com/weapi/song/enhance/player/url/v1?csrf_token=' + self.csrf_token
encText = str(
{'ids': "[" + str(music_id) + "]", 'encodeType': 'aac', 'csrf_token': self.csrf_token, 'level': 'standard'})
params = AES_encrypt(AES_encrypt(encText, self.g, self.iv), self.i, self.iv)
data = {
'params': params,
'encSecKey': self.get_encSecKey()
}
headeer = {
'User-Agent': self.ua,
'Referer': 'https://music.163.com/',
'Content-Type': 'application/x-www-form-urlencoded',
'Cookie': self.cookie
}
ret = requests.post(url, headers=headeer, data=data).json()
download_url = ret['data'][0]['url']
if download_url is None:
download_url = self.get_wyy_playurl2(music_id)
return download_url
def wyy_url2(self, m_id: str):
"""
获取网易音乐源地址2
:param m_id: 音乐id
:return:
"""
url = f'https://music.163.com/api/song/enhance/player/url?id={music_id}&ids=%5B{music_id}%5D&br=3200000'
ret = requests.get(url, headers={'cookie': self.cookie}).json()
download_url = ret['data'][0]['url']
if download_url:
msg = download_url
else:
msg = {'msg': '出现了错误,错误位置:获取音乐源'}
return msg
def wyy_lrc(self, m_id: str):
"""
获取歌词
:param m_id: 音乐id
:return:
"""
url = f'https://music.163.com/api/song/lyric?id={m_id}&lv=1&kv=1&tv=-1'
ret = requests.get(url).json()
lrc = ret['lrc']['lyric']
if lrc == '':
lrc = ret['klyric']['lyric']
return lrc
class qqmusic(wyymusic):
def __init__(self, tid: str):
super(qqmusic, self).__init__(tid)
self.header = {
'User-Agent': self.ua,
'referer': 'https://y.qq.com/'
}
def encrypt(self, param):
"""
QQ音乐sign
:param param:
:return:
"""
k1 = {"0": 0, "1": 1, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8, "9": 9, "A": 10, "B": 11, "C": 12,
"D": 13, "E": 14, "F": 15}
l1 = [212, 45, 80, 68, 195, 163, 163, 203, 157, 220, 254, 91, 204, 79, 104, 6]
t = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
text = json.dumps(param, separators=(',', ':'))
md5 = hashlib.md5(text.encode()).hexdigest().upper()
t1 = ''.join([md5[i] for i in [21, 4, 9, 26, 16, 20, 27, 30]])
t3 = ''.join([md5[i] for i in [18, 11, 3, 2, 1, 7, 6, 25]])
ls2 = []
for i in range(16):
x1 = k1[md5[i * 2]]
x2 = k1[md5[i * 2 + 1]]
x3 = ((x1 * 16) ^ x2) ^ l1[i]
ls2.append(x3)
ls3 = []
for i in range(6):
if i == 5:
ls3.append(t[ls2[-1] >> 2])
ls3.append(t[(ls2[-1] & 3) << 4])
else:
x4 = ls2[i * 3] >> 2
x5 = (ls2[i * 3 + 1] >> 4) ^ ((ls2[i * 3] & 3) << 4)
x6 = (ls2[i * 3 + 2] >> 6) ^ ((ls2[i * 3 + 1] & 15) << 2)
x7 = 63 & ls2[i * 3 + 2]
ls3.extend(t[x4] + t[x5] + t[x6] + t[x7])
t2 = ''.join(ls3).replace('[\\/+]', '')
sign = 'zzb' + (t1 + t2 + t3).lower()
return sign
def qq_lrc(self, mid):
"""
QQ音乐歌词接口
:param mid: 歌曲id
"""
url = "https://c.y.qq.com/lyric/fcgi-bin/fcg_query_lyric_new.fcg"
params = dict(format="json", songmid=f"{mid}")
res = requests.get(url, params=params, headers=self.header).json()
data = base64.b64decode(res["lyric"]).decode("utf-8")
# print(data)
if data == '':
data = '纯音乐,请欣赏'
return data
def get_music_list(self):
"""
获取歌单列表
:return:
"""
url = 'https://c.y.qq.com/qzone/fcg-bin/fcg_ucc_getcdinfo_byids_cp.fcg?type=1&json=1&utf8=1&onlysong=0' \
'&disstid=%s&format=jsonp&g_tk=5381&jsonpCallback=playlistinfoCallback&loginUin=0&hostUin=0&inCharset' \
'=utf8&outCharset=utf-8?ice=0&platform=yqq&needNewCode=0' % self.tid
ret = requests.get(url=url, headers=self.header).text
res = json.loads(ret[21:-1])
self.data_list.clear()
for i in res['cdlist'][0]['songlist']:
author = ''
song_id = i['songmid']
pic = 'https://y.qq.com/music/photo_new/T002R300x300M000%s.jpg' % i['albummid']
for x in i['singer']:
author += x['name'] + '/'
self.data_list.append({'title': i['songname'], 'author': author[:-1],
'url': 'https://api2.52jan.com/qqmusic/%s' % song_id,
'pic': ''.join(pic),
'lrc': 'https://api2.52jan.com/qqmusic/lrc/%s.lrc' % song_id})
# print(json.dumps(self.data_list))
return self.data_list
def get_random(self, len):
return ''.join(str(random.choice(range(10))) for _ in range(len))
def get_music_vkey(self, mid):
"""
获取歌曲播放地址
:param mid: 001GLG5B45uLhI
:return:
"""
i = 1
req = 'req_' + str(i)
# guid会影响成功率,所以要随机
while True:
param = {"comm": {"cv": 4747474, "ct": 24, "format": "json", "inCharset": "utf-8", "outCharset": "utf-8",
"notice": 0, "platform": "yqq.json", "needNewCode": 1, "uin": 838210720,
"g_tk_new_20200303": 744448821, "g_tk": 744448821},
req: {"module": "vkey.GetVkeyServer", "method": "CgiGetVkey",
"param": {"guid": "794" + self.get_random(7), "songmid": [mid], "songtype": [0],
"uin": "838210720",
"loginflag": 1, "platform": "20"}}}
url = f'https://u.y.qq.com/cgi-bin/musics.fcg?_={round(time.time() * 1000)}&sign={self.encrypt(param)}'
self.header['cookie'] = ''
ret = requests.post(url=url, data=json.dumps(param, separators=(',', ':')), headers=self.header).json()
# print(json.dumps(ret))
code = ret['code']
if code == 2000:
i += 1
req = 'req_' + str(i)
if i > 7:
break
else:
break
purl = ret[req]['data']['midurlinfo'][0]['purl']
uri = random.choice(ret[req]['data']['sip']) + purl if code == 0 and purl != '' else 'vip歌曲'
# print(uri)
return uri
if __name__ == '__main__':
tid = "6222311"
musicapi = qqmusic(tid)
music_list = musicapi.kugou_list()
print("酷狗歌单信息:" + json.dumps(music_list))
# 获取歌曲源地址
# musicapi.kugou_url('0DBB2B56582BE6CB062F79D1D13FE21E')
musicapi.tid = "7480897649"
music_list = musicapi.wyy_discover()
print("网易歌单信息:" + json.dumps(music_list))
# 获取歌曲源地址
# musicapi.wyy_playurl('1413464902')
# musicapi.wyy_playurl2('1413464902')
musicapi.tid = "8672698451"
music_list = musicapi.get_music_list()
print("QQ歌单信息:" + json.dumps(music_list))
# 获取歌曲源地址
# musicapi.get_music_vkey('003XT6Ef4H6X66')