[Asm] 纯文本查看 复制代码
import requests,os
import time,re
import urllib.parse
import tkinter as tk
from tkinter import ttk
from tkinter.filedialog import askdirectory
import threading
headers={
"authority": "www.kugou.com",
"method": "GET",
"path": "/yy/rank/home/1-6666.html?from=rank",
"scheme": "https",
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,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": "no-cache",
"cookie": "kg_mid=628fb7a3fc8e7dab426f2846c32b409f; kg_dfid=3dpnap3DlhYI2QcUiN4BVBki; KuGooRandom=66411627561045001; kg_dfid_collect=d41d8cd98f00b204e9800998ecf8427e; Hm_lvt_aedee6983d4cfc62f509129360d6bb3d=1627561043,1627564661,1630060779,1630066172; ACK_SERVER_10016=%7B%22list%22%3A%5B%5B%22gzreg-user.kugou.com%22%5D%5D%7D; ACK_SERVER_10017=%7B%22list%22%3A%5B%5B%22gzverifycode.service.kugou.com%22%5D%5D%7D; ACK_SERVER_10015=%7B%22list%22%3A%5B%5B%22gzlogin-user.kugou.com%22%5D%5D%7D; kg_mid_temp=628fb7a3fc8e7dab426f2846c32b409f; Hm_lpvt_aedee6983d4cfc62f509129360d6bb3d=1630066414",
"pragma": "no-cache",
'sec-ch-ua': '" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"',
"sec-ch-ua-mobile": "?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; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.106 Safari/537.36",
}
def start_music():
ui.t.delete('1.0', 'end')
name = ui.path1.get()
ui.t.insert('insert', "正在搜索歌曲:" + name)
ui.t.insert('insert', "\n")
name1 = urllib.parse.quote(name)
t = int(time.time()*1000)
url = f"http://songsearch.kugou.com/song_search_v2?callback=&keyword={name1}&page=1&pagesize=30&userid=-1&clientver=&platform=WebFilter&tag=em&filter=2&iscorrection=1&privilege_filter=0&_={t}"
print(url)
resp1 = html_respense(url)
y=resp1.json()
hashs = y["data"]["lists"]
hash=[hash['FileHash'] for hash in hashs]
AlbumID=[hash['AlbumID'] for hash in hashs]
FileName=[hash['FileName'] for hash in hashs]
indexs=[]
i=0
for index in zip(hash,AlbumID,FileName):
i+=1
ui.t.insert('insert', f"第{i}首 {index[2]}")
ui.t.insert('insert', "\n")
ui.t.see('end') #随着text的文本增加,焦点(光标),view始终在行尾
indexs.append(index)
return indexs
# 请求函数
def html_respense(url):
for i in range(3):
try:
resp= requests.get(url,headers=headers,timeout=20)
resp.encoding = 'utf-8'
if resp.status_code==200:
return resp
except:
time.sleep(1)
print(f"正在尝试重新请求第{i}次")
continue
else:
print("请求失败,请检查")
#下载
def music_down(indexs):
title=ui.vv.get()
print(title)
if not os.path.exists(title):
os.makedirs(title)
down_fails = []
for index in indexs:
pattern = re.compile(r"[\/\\\:\*\?\"\<\>\|]") # 文件名不能包括/\:*?<>"|
songname = re.sub(pattern, "_", index[2])
print(songname)
try:
params = {
"r": "play/getdata",
"hash": index[0],
"dfid": "3dpnap3DlhYI2QcUiN4BVBki",
"mid": "628fb7a3fc8e7dab426f2846c32b409f",
"platid": "4",
"album_id": index[1],
"_": int(time.time() * 1000),
}
ui.t.insert('insert', f"歌曲----{index[2]}正在下载")
ui.t.insert('insert', "\n")
resp_2 = requests.get(url=callback_url, headers=headers, params=params).json()
music_down_url = resp_2['data']['play_url']
print(music_down_url)
resp_3 = html_respense(music_down_url)
path = title +"\\" + songname + ".mp3"
with open(path, 'wb') as f:
f.write(resp_3.content)
print(songname, "下载完成")
ui.t.insert('insert', f"歌曲{songname}下载完成")
ui.t.insert('insert', "\n")
ui.t.see('end')
except Exception:
down_fails.append(songname)
ui.t.insert('insert', f"歌曲{songname}下载失败")
ui.t.insert('insert', "\n")
for root, dirs, files in os.walk(title):
t = songname + ".mp3"
for file in files:
if file == t:
os.remove(root + "\\" + t)
print(t, "删除成功****************************************")
music_down_fails(down_fails)
def music_down_fails(down_fails):
ui.t.delete('1.0', 'end')
for songname in down_fails:
ui.t.insert('insert', f"歌曲{songname}下载失败")
ui.t.insert('insert', "\n")
ui.t.see('end')
def main():
ui.t.delete('1.0', 'end')
start = time.time()
indexs = start_music()
ui.t.delete('1.0', 'end')
thread = threading.Thread(target=music_down, args=(indexs,))
thread.start()
tim = time.time() - start
tim = round(tim, 2)
print("一共耗时", tim)
ui.t.insert('insert', f"一共耗时{tim}秒")
ui.t.insert('insert', "\n")
ui.t.see('end')
class music_ui():
def __init__(self,root):
self.root =root
self.vv=tk.StringVar()
self.path1 = tk.StringVar()
def ui_cuaw(self):
# 标题
self.root.title("音乐下载2.0")
# 画布大小
self.root.geometry("600x480")
#标签
tk.Label(self.root,text="酷狗歌曲下载",font="楷体").place(x=250,y=10)
# 备注
tk.Label(self.root, text="可以搜索歌曲名或者歌手名支持模糊搜索").place(x=40, y=30)
#搜索按钮
ttk.Button(self.root,text="搜索",command=start_music).place(x=40,y=50)
#输入框
ttk.Entry(self.root,width=60,textvariable=self.path1).place(x=140,y=52)
#选择存放地址
ttk.Button(self.root, text="选择存放地址",command=self.storage).place(x=40, y=80)
ttk.Entry(self.root, width=60,textvariable=self.vv).place(x=140, y=82)
#状态文本框
tk.Label(self.root, text="下载状态").place(x=240, y=110)
ttk.Button(self.root, text="歌曲下载",command=main).place(x=40, y=110)
self.t=tk.Text(self.root, width=75,height=24)
self.t.place(x=40, y=140)
def storage(self):
path = askdirectory()
music_down_path=path.replace("/","\\\\")
print(music_down_path)
self.vv1= self.vv.set(music_down_path)
if __name__ == '__main__':
callback_url = 'https://wwwapi.kugou.com/yy/index.php?'
#画布
root= tk.Tk()
#ui类
ui = music_ui(root)
ui.ui_cuaw()
#循环
root.mainloop()
#++====代码优化 去除几个小bug =====
链接: https://pan.baidu.com/s/1I2TNWbILhHL8W2uWRy5Ebw 提取码: 42ym 复制这段内容后打开百度网盘手机App,操作更方便哦