有问题请留言
import requests,os,math
class Song(dict):
url="https://u.y.qq.com/cgi-bin/musicu.fcg"
downloadUrl="http://mobileoc.music.tc.qq.com/M500"
headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36",}
date={
"req_0":{"module":"vkey.GetVkeyServer",
"method":"CgiGetVkey",
"param":{"guid":"ffffffff82def4af4b12b3cd9337d5e7",
"songmid":["002DwM9K2ZPpog"],
"songtype":[0],
"uin":"346897220",
}
}
}
def __init__(self,raw:dict,file:str):
dict.__init__(self,raw)
self.date['req_0']['param']['songmid']=["002DwM9K2ZPpog"]
self.downloadFile=file
c=self.__getVkey();
def __getHash(self):
return self['file']['media_mid']
def __getVkey(self):
params={"data":str(self.date).replace("'",'"').replace(" ",""),}
return requests.get(self.url,params).json()['req_0']['data']['midurlinfo'][0]['vkey']
def download(self):
url=self.downloadUrl+self.__getHash()+".mp3"
params={
"guid": "ffffffff82def4af4b12b3cd9337d5e7",
"vkey": self.__getVkey(),
"uin": "346897220",
"fromtag": "53",
}
music=requests.get(url,params,headers=self.headers,timeout=3)
try:
file=self.downloadFile+self.getName()+'.mp3'
if(not os.path.exists(file)):
with open(file,"wb") as f:
f.write(music.content)
f.close()
else:
return False
except i:
return False;
return True;
def getName(self):
return self['name']+' - '+self['singer'][0]['name'];
class QQMusic:
searchUrl="https://c.y.qq.com/soso/fcgi-bin/client_search_cp"
page_size=60
searchParams={
"new_json":"1", #"flag_qc":"1",
"p":"", #页码
"n":"", #每页歌曲数,上限为60
"w":"", #关键字
"format":"json",
}
def __init__(self,file:str):
self.downloadFile=file
self.searchParams['n']=str(self.page_size)
if(not os.path.exists(file)):
os.makedirs(file)
def __searchByPage(self,key:str,index:int):
self.searchParams['w']=key
self.searchParams['p']=str(int)
return requests.get(self.searchUrl,self.searchParams).json()['data']['song']
def search(self,key:str):
songList=[]
front=self.__searchByPage(key,1);
songList.extend(Song(i,self.downloadFile) for i in front['list'])
for page in range(2,math.ceil(front['totalnum']/self.page_size)+1):
songList.extend(Song(i,self.downloadFile) for i in self.__searchByPage(key,page)['list'])
return songList
def searchPage(self,key:str,page:int):
return (Song(i,self.downloadFile) for i in self.__searchByPage(key,page)['list'])
#演示
m=QQMusic("d:/test/") #以下载路径创建
for song in m.searchPage("Wasted Love",1): #搜索第一页
if(song.download()):
print("已下载——",song.getName())
else:
print("文件已存在或下载失败")
print(t)