import math
import random
import codecs
import requests
import json
id=1
headers='''
accept: */*
accept-encoding: gzip, deflate, br
accept-language: zh-CN,zh;q=0.9,en;q=0.8
content-length: 201
content-type: application/x-www-form-urlencoded; charset=UTF-8
cookie: pgv_pvid=860409964; pgv_info=ssid=s283563460; tvfe_boss_uuid=7df2cac8bd6193a0; RK=iBBlJzZwNG; ptcz=1b235f9fcb71c931519073ed89b7d0e1bf4fd7a37630b3e2112aa5ebe841ba9e; pgv_pvi=8337976320; pgv_si=s4545376256; qz_gdt=5zt7mw7oaaahtx4vdusq; user_id=null; session_id=null; mobileUV=1_16796a94224_9f543; sd_userid=21341547794618409; sd_cookie_crttime=1547794618409; eas_sid=P1k5D4s919o3T798f0w8c2Y371; TGLoginJSCurDomain=tgideas.qq.com; qqmusic_uin=; qqmusic_key=; _qpsvr_localtk=0.7566170047720839; ptisp=cm; FTN5K=89b569ab; rv2=80DF8075170C27D2FBF99E2F84C9FFB75D5BCA8BD598786AB6; property20=5083A8E64673E26FE991BFB43C3B54CBCBF3E93ED1C1C3394A648935FEC5AB663DA23B12282236EC; ts_uid=3083466358; p_uin=o1026091321; pt4_token=D0GB8D5DXf9zY83tBIFfMSIsKxnD6y7qqKcyOLDufr4_; p_skey=b7sz55F8Ei21tjLcQ95WaMf3vq2Ihyyo4hz1MT6J4pY_; p_luin=o1026091321; p_lskey=000400002d26246df8c78585a3180a4cf6ba01ee74e283cd46d42226de92c040e398b5780ee7d5d00ea5f75e; o_cookie=1026091321; verifysession=h011ca7b768a6475e7a39d2e1fd4e45fdb73f928d1eb8a0b611ac442689e51938f5021a6e8743544aee; qqmusic_fromtag=66; pac_uid=1_1026091321; userAction=1; yqq_stat=0; ts_refer=www.cnblogs.com/; yq_playschange=0; yq_playdata=; player_exist=1; yq_index=0; yplayer_open=1; ts_last=y.qq.com/portal/player.html
origin: https://y.qq.com
referer: https://y.qq.com/portal/player.html
user-agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 Safari/537.36
'''
mp3headers='''
accept: application/json, text/javascript, */*; q=0.01
accept-encoding: gzip, deflate, br
accept-language: zh-CN,zh;q=0.9,en;q=0.8
cookie: pgv_pvid=860409964; pgv_info=ssid=s283563460; tvfe_boss_uuid=7df2cac8bd6193a0; RK=iBBlJzZwNG; ptcz=1b235f9fcb71c931519073ed89b7d0e1bf4fd7a37630b3e2112aa5ebe841ba9e; pgv_pvi=8337976320; pgv_si=s4545376256; qz_gdt=5zt7mw7oaaahtx4vdusq; user_id=null; session_id=null; mobileUV=1_16796a94224_9f543; sd_userid=21341547794618409; sd_cookie_crttime=1547794618409; eas_sid=P1k5D4s919o3T798f0w8c2Y371; TGLoginJSCurDomain=tgideas.qq.com; qqmusic_uin=; qqmusic_key=; _qpsvr_localtk=0.7566170047720839; ptisp=cm; FTN5K=89b569ab; rv2=80DF8075170C27D2FBF99E2F84C9FFB75D5BCA8BD598786AB6; property20=5083A8E64673E26FE991BFB43C3B54CBCBF3E93ED1C1C3394A648935FEC5AB663DA23B12282236EC; ts_uid=3083466358; p_uin=o1026091321; pt4_token=D0GB8D5DXf9zY83tBIFfMSIsKxnD6y7qqKcyOLDufr4_; p_skey=b7sz55F8Ei21tjLcQ95WaMf3vq2Ihyyo4hz1MT6J4pY_; p_luin=o1026091321; p_lskey=000400002d26246df8c78585a3180a4cf6ba01ee74e283cd46d42226de92c040e398b5780ee7d5d00ea5f75e; o_cookie=1026091321; verifysession=h011ca7b768a6475e7a39d2e1fd4e45fdb73f928d1eb8a0b611ac442689e51938f5021a6e8743544aee; qqmusic_fromtag=66; pac_uid=1_1026091321; userAction=1; yqq_stat=0; ts_refer=www.cnblogs.com/; yq_playschange=0; yq_playdata=; player_exist=1; yplayer_open=1; ts_last=y.qq.com/portal/player.html; yq_index=1
origin: https://y.qq.com
referer: https://y.qq.com/portal/player.html
user-agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 Safari/537.36'''
downheader='''
Accept-Encoding: identity;q=1, *;q=0
chrome-proxy: frfr
Range: bytes=0-
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 Safari/537.36
guid: 860409964
vkey: 9F1451C2E1D71EAB3A28FBB17FCCE9424C870E2945D4A4FDF156848EA8ECE8714F28D85512C0D1302410E2C051DBC0DBBD22BF489D67325E
uin: 2361
fromtag: 66
'''
def gen_headers(s):
ls = s.split('
')
lsl = []
ls = ls[1:-1]
headers = {}
for l in ls:
l = l.split(': ')
lsl.append(l)
for x in lsl:
headers[str(x[0]).strip(' ')] = x[1]
return headers
headers=gen_headers(headers)
data={"comm":{"uin":"1026091321","ct":"24","cv":"0","gzip":"0","mcc":"460","mnc":"1"},"data_id":{"module":"track_info.UniformRuleCtrlServer","method":"GetTrackInfo","param":{"ids":[id],"types":[0]}}}
res=requests.post('https://u.y.qq.com/cgi-bin/musicu.fcg?g_tk=1111',json=data,headers=headers)
text=json.loads(res.text)
mid=text['data_id']['data']['tracks'][0]['mid']
mp3name=text['data_id']['data']['tracks'][0]['name']
mp3headers=gen_headers(mp3headers)
mp3url='https://u.y.qq.com/cgi-bin/musicu.fcg?-=getplaysongvkey03622942490798797&g_tk=231911714&loginUin=1026091321&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8¬ice=0&platform=yqq.json&needNewCode=0&data={"req":{"module":"CDN.SrfCdnDispatchServer","method":"GetCdnDispatch","param":{"guid":"860409964","calltype":0,"userip":""}},"req_0":{"module":"vkey.GetVkeyServer","method":"CgiGetVkey","param":{"guid":"860409964","songmid":["'+mid+'"],"songtype":[0],"uin":"1026091321","loginflag":1,"platform":"20"}},"comm":{"uin":1026091321,"format":"json","ct":24,"cv":0}}'
mp3=requests.get(mp3url,headers=mp3headers)
text=json.loads(mp3.text)
downheader=gen_headers(downheader)
url='http://124.193.230.144/amobile.music.tc.qq.com/'+text['req_0']['data']['midurlinfo'][0]['purl']
mp3=requests.get(url,headers=downheader)
mp3=mp3.content
with open('./'+mp3name+'.mp3','wb') as f:
f.write(mp3)
f.close()
print(mp3name)