[Python] 纯文本查看 复制代码
import requests
import os
import json
#进度条要的库
import time
import sys
headers = {
'authority': 'c.y.qq.com',
'method': 'GET',
'path': '/soso/fcgi-bin/client_search_cp?ct=24&qqmusic_ver=1298&new_json=1&remoteplace=txt.yqq.song&searchid=64361863091094477&t=0&aggr=1&cr=1&catZhida=1&lossless=0&flag_qc=0&p=1&n=10&w=%E7%A0%B4%E8%8C%A7&g_tk_new_20200303=57712389&g_tk=57712389&loginUin=0&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8¬ice=0&platform=yqq.json&needNewCode=0',
'scheme': 'https',
'accept': 'application/json, text/javascript, */*; q=0.01',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'zh-CN,zh;q=0.9',
'origin': 'https://y.qq.com',
'referer': 'https://y.qq.com/portal/search.html',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-site',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36'
}
def sj(): #读取搜索数据
ge = input('请输入歌名: ')
req = requests.get('https://c.y.qq.com/soso/fcgi-bin/client_search_cp?ct=24&qqmusic_ver=1298&new_json=1&remoteplace=txt.yqq.song&searchid=59254584347080480&t=0&aggr=1&cr=1&catZhida=1&lossless=0&flag_qc=0&p=1&n=10&w='+ge+'&g_tk_new_20200303=1646128606&g_tk=1646128606&loginUin=123456&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8¬ice=0&platform=yqq.json&needNewCode=0').json()
#print(req)
return req
def Time_1(): #进度条函数
for i in range(1,51):
sys.stdout.write('\r')
sys.stdout.write('{0}% |{1}'.format(int(i%51)*2,int(i%51)*'■'))
sys.stdout.flush()
time.sleep(0.125)
sys.stdout.write('\n')
def gd(list): #打印歌单
xh=1
print('\033[5;32m* 歌曲 歌手 专辑\033[0m')
for m in list:
print('%-3s'%xh,end='')
歌曲长度1=len(m['title'])
歌曲长度2=len(m['title'].encode('utf-8'))
歌曲长=int((歌曲长度2-歌曲长度1)/2+歌曲长度1)
歌曲格=int(52-歌曲长)
歌手长度1=len(m['singer'][0]['name'])
歌手长度2=len(m['singer'][0]['name'].encode('utf-8'))
歌手长=int((歌手长度2-歌手长度1)/2+歌手长度1)
歌手格=int(20-歌手长)
专辑长度1=len(m['album']['name'])
专辑长度2=len(m['album']['name'].encode('utf-8'))
专辑长=int((专辑长度2-专辑长度1)/2+专辑长度1)
print(m['title']+' '*歌曲格,end='')
print(m['singer'][0]['name']+' '*歌手格,end='')
print(m['album']['name'])
xh+=1
def cd(list): #获取菜单
while True:
print(' '*30+'\033[1;37;41m*\033[0m\033[1;37;41m—————————————====选择操作====—————————————\033[0m\033[1;37;41m*\033[0m\n'+' '*30+'\033[1;37;41m│\033[0m1.进行下载 2.进行播放 3.获取评论\033[1;37;41m│\033[0m\n'+' '*30+'\033[1;37;41m│\033[0m4.获取歌词 5.生成云图 6.重新开始\033[1;37;41m│\033[0m\n'+' '*30+'\033[1;37;41m*===============按任意键退出===============*\033[0m')
菜单=input('>>>>> ')
if 菜单==str(1):
xzyy(list)
elif 菜单==str(2):
bfyy(list)
elif 菜单==str(3):
hqpl(list)
elif 菜单==str(4):
hqgc(list)
elif 菜单==str(5):
print('想屁吃??')
elif 菜单==str(6):
req=sj()
gd(req['data']['song']['list'])
list=req['data']['song']['list']
continue
else:
break
print('----------操作已完成----------')
yhxz = input('请选择下次操作')
#读取单个音乐的mid和uid
def id(list):
选择歌曲=int(input("请输入歌曲的序号>>> "))-1
原id=str(list[选择歌曲]['id'])
return 原id
def id_mid(list):
选择歌曲=int(input("请输入歌曲的序号>>> "))-1
歌曲mid=str(list[选择歌曲]['mid'])
曲名 = str(list[选择歌曲]['title']+'——'+list[选择歌曲]['singer'][0]['name'])
return 歌曲mid,曲名
def id_uid(list):
选择歌曲=int(input("请输入歌曲的序号>>> "))-1
歌曲uid=str(list[选择歌曲]['id'])
return 歌曲uid
def xzyy(list): #下载音乐
歌曲mid,曲名=id_mid(list)
headers={
'authority':'u.y.qq.com',
'method':'GET',
'path':'/cgi-bin/musicu.fcg?format=json&data={%22req%22:{%22module%22:%22CDN.SrfCdnDispatchServer%22,%22method%22:%22GetCdnDispatch%22,%22param%22:{%22guid%22:%226503543428%22,%22calltype%22:0,%22userip%22:%22%22}},%22req_0%22:{%22module%22:%22vkey.GetVkeyServer%22,%22method%22:%22CgiGetVkey%22,%22param%22:{%22guid%22:%226503543428%22,%22songmid%22:[%22001Y7zXa1p0ryF%22],%22songtype%22:[0],%22uin%22:%220%22,%22loginflag%22:1,%22platform%22:%2220%22}},%22comm%22:{%22uin%22:0,%22format%22:%22json%22,%22ct%22:24,%22cv%22:0}}',
'scheme':'https',
'accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'accept-encoding':'gzip, deflate, br',
'accept-language':'zh-CN,zh;q=0.9',
'cache-control':'max-age=0',
'sec-fetch-dest':'document',
'sec-fetch-mode':'navigate',
'sec-fetch-site':'none',
'sec-fetch-user':'?1',
'upgrade-insecure-requests':'1',
'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36',
}
url='https://u.y.qq.com/cgi-bin/musicu.fcg?format=json&data={"req":{"module":"CDN.SrfCdnDispatchServer","method":"GetCdnDispatch","param":{"guid":"6503543428","calltype":0,"userip":""}},"req_0":{"module":"vkey.GetVkeyServer","method":"CgiGetVkey","param":{"guid":"6503543428","songmid":["'+歌曲mid+'"],"songtype":[0],"uin":"0","loginflag":1,"platform":"20"}},"comm":{"uin":0,"format":"json","ct":24,"cv":0}}'
音频数据=requests.get(url=url,headers=headers).json()
#print(音频数据)
网址前段 = 音频数据['req']['data']['freeflowsip'][0]
网址后段 = 音频数据['req_0']['data']['midurlinfo'][0]['purl']
全网址 = str(网址前段+网址后段)
全网址 =requests.get(全网址,headers=headers).content
下载路径 = os.getcwd() #返回程序的当前路径
try:
下载路径=os.path.join(下载路径,"qq音乐")
if not os.path.exists(下载路径):
os.mkdir(下载路径)
os.chdir(下载路径)
with open(曲名+'.m4a','wb') as f:
f.write(全网址)
print('下载中···')
Time_1()
print('下载成功')
except:
print('对不起,下载失败,可能无版权,或收费歌。')
def bfyy(list): #播放音乐
歌曲mid,曲名=id_mid(list)
headers={
'authority':'u.y.qq.com',
'method':'GET',
'path':'/cgi-bin/musicu.fcg?format=json&data={%22req%22:{%22module%22:%22CDN.SrfCdnDispatchServer%22,%22method%22:%22GetCdnDispatch%22,%22param%22:{%22guid%22:%226503543428%22,%22calltype%22:0,%22userip%22:%22%22}},%22req_0%22:{%22module%22:%22vkey.GetVkeyServer%22,%22method%22:%22CgiGetVkey%22,%22param%22:{%22guid%22:%226503543428%22,%22songmid%22:[%22001Y7zXa1p0ryF%22],%22songtype%22:[0],%22uin%22:%220%22,%22loginflag%22:1,%22platform%22:%2220%22}},%22comm%22:{%22uin%22:0,%22format%22:%22json%22,%22ct%22:24,%22cv%22:0}}',
'scheme':'https',
'accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'accept-encoding':'gzip, deflate, br',
'accept-language':'zh-CN,zh;q=0.9',
'cache-control':'max-age=0',
'sec-fetch-dest':'document',
'sec-fetch-mode':'navigate',
'sec-fetch-site':'none',
'sec-fetch-user':'?1',
'upgrade-insecure-requests':'1',
'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36',
}
url = 'https://u.y.qq.com/cgi-bin/musicu.fcg?format=json&data={"req":{"module":"CDN.SrfCdnDispatchServer","method":"GetCdnDispatch","param":{"guid":"6503543428","calltype":0,"userip":""}},"req_0":{"module":"vkey.GetVkeyServer","method":"CgiGetVkey","param":{"guid":"6503543428","songmid":["'+歌曲mid+'"],"songtype":[0],"uin":"0","loginflag":1,"platform":"20"}},"comm":{"uin":0,"format":"json","ct":24,"cv":0}}'
音频数据=requests.get(url=url,headers=headers).json()
#print(音频数据)
网址前段 = 音频数据['req']['data']['freeflowsip'][0]
网址后段 = 音频数据['req_0']['data']['midurlinfo'][0]['purl']
全网址 = str(网址前段+网址后段)
print('播放地址:'+全网址)
def hqpl(list): #获取评论
print('默认获取前15条热门评论')
歌曲uid=id_uid(list)
pl =requests.get('https://c.y.qq.com/base/fcgi-bin/fcg_global_comment_h5.fcg?g_tk_new_20200303=57712389&g_tk=57712389&loginUin=2079898547&hostUin=0&format=json&inCharset=utf8&outCharset=GB2312¬ice=0&platform=yqq.json&needNewCode=0&cid='+歌曲uid+'&reqtype=2&biztype=1&topid='+歌曲uid+'&cmd=6&needmusiccrit=0&pagenum=1&pagesize=15&lasthotcommentid=song_253511781_1762308377_1590307612_2634401830_1590308001&domain=qq.com&ct=24&cv=10101010').json()
pld = pl['comment']['commentlist']
#print(pld)
序号2 = 1
for plhq in pld:
if plhq['middlecommentcontent'] == None :
print(str(序号2)+': '+plhq['nick']+': '+plhq['rootcommentcontent']+'\n\n')
else:
print(str(序号2)+': '+plhq['nick']+'回复了'+plhq['middlecommentcontent'][0]['replyednick']+':'+plhq['rootcommentcontent']+'\n'+plhq['middlecommentcontent'][0]['subcommentcontent']+'\n\n')
序号2 +=1
def hqgc(list): #获取歌词
歌曲uid=id_uid(list)
url ='https://c.y.qq.com/lyric/fcgi-bin/fcg_query_lyric_yqq.fcg?nobase64=1&musicid='+歌曲uid+'&-=jsonp1&g_tk_new_20200303=57712389&g_tk=57712389&loginUin=2079898547&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8¬ice=0&platform=yqq.json&needNewCode=0'
gc =requests.get(url=url,headers=headers).json()
去杂=str(gc['lyric'])
纯1=去杂.replace('[','').replace(']','').replace('ti:','').replace(' (','————').replace(')][ar:','\n').replace('','\n').replace(' ',' ').replace(');','').replace('ar:','').replace('al:','').replace('by:\noffset:0','').replace(')','').replace('-','').replace('@',' ').replace('/C.vo/',' ').replace('','').replace(';','')
纯2=纯1.splitlines()
del 纯2[1]
del 纯2[1]
歌词引用序号=0
for 纯3 in 纯2:
if 歌词引用序号 >= 2:
纯3 = 纯3[16:]
print(纯3)
else:
print(纯3)
歌词引用序号+=1
if __name__ == "__main__":
req=sj()
list=req['data']['song']['list']
gd(list)
cd(list)
print('\033[1;31;43m感谢使用~\033[0m')