import json
import os
from tkinter import *
from urllib.request import urlretrieve
import requests
from urllib.parse import quote
def song_load(music_name, music_url):
# 创建文件夹
os.makedirs('downloaded_music', exist_ok=True)
path = 'downloaded_music\{}.mp4'.format(music_name)
text.insert(END, '文件:{},正在下载...'.format(music_name))
# 文本框滚动
text.see(END)
# 更新
text.update()
urlretrieve(music_url, path)
text.insert(END, '下载完毕:{}'.format(music_name))
# 文本框滚动
text.see(END)
# 更新
text.update()
def huoqu_music():
try:
text.delete(0, END)
music_info = [] # 存储已获取的音乐ID
for i in range(0, 55):
key_word = quote(entry.get())
url = f'http://www.kuwo.cn/api/www/search/searchMusicBykeyWord?key={key_word}&pn={i}&rn=20&httpsStatus=1&reqId=273867a0-2c5e-11ee-b1b5-db033bb09d98&plat=web_www&from='
headers = {
"Accept": "application/json, text/plain, */*",
"Accept-Language": "zh-CN,zh;q=0.9",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"Cookie": "Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1690445552; _ga=GA1.2.492811842.1690445552; _gid=GA1.2.1407149096.1690445552; _gat=1; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1690449315; Hm_Iuvt_cdb524f42f0ce19b169b8072123a4727=ATRHZ5WEnw4jJMmQwYEzJcr8QKCPBXjE; _ga_ETPBRPM9ML=GS1.2.1690449316.2.1.1690449339.37.0.0",
"Pragma": "no-cache",
"Referer": "http://www.kuwo.cn/search/list?key=%E5%91%A8%E6%9D%B0%E4%BC%A6",
"Secret": "1f082824a0b1ba4cd04cd92aeeabf4ac4aecbcf2f78e4eb329374f81e6e17d7e004c83b3",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36",
"Accept-Encoding": "deflate, gzip"
}
res = requests.get(url, headers=headers).text
dic = json.loads(res)
for item in dic["data"]["list"]:
if item["isListenFee"] == False:
music_name = item["name"]
music_id = item["rid"]
music_art = item["artist"]
music_free = item["isListenFee"]
# 过滤重复的music_id
if music_id not in {m['music_id'] for m in music_info}:
music_info.append({'music_name': music_name, 'music_id': music_id})
# 成功获取音乐信息后,调用下载音乐的函数
down_music(music_info)
except requests.exceptions.RequestException as e:
text.insert(END, f'API 请求发生错误: {e}')
except json.JSONDecodeError as e:
text.insert(END, f'JSON 解析错误: {e}')
def down_music(music_info):
os.makedirs('downloaded_music', exist_ok=True)
for music in music_info:
music_id = music['music_id']
music_name = music['music_name']
url = f"http://www.kuwo.cn/api/v1/www/music/playUrl?mid={music_id}&type=music&httpsStatus=1&reqId=c46eeee0-2c59-11ee-aecd-8d89c636063c&plat=web_www&from="
headers = {
"Accept": "application/json, text/plain, */*",
"Accept-Language": "zh-CN,zh;q=0.9",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"Cookie": "Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1690445552; _ga=GA1.2.492811842.1690445552; _gid=GA1.2.1407149096.1690445552; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1690447147; _ga_ETPBRPM9ML=GS1.2.1690445552.1.1.1690447148.60.0.0; Hm_Iuvt_cdb524f42f0ce19b169b8072123a4727=jD6p4jscTSwTH4dRmRYQ68YtPyPHWmMm",
"Pragma": "no-cache",
"Referer": "http://www.kuwo.cn/search/list?key=^%^E5^%^BC^%^A0^%^E9^%^9F^%^B6^%^E6^%^B6^%^B5",
"Secret": "34184c1cceee9e6aea689a14ecd2fdaf50e7a0d98bd565ff28055c99f3d45a5602562d24",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36",
"Accept-Encoding": "deflate, gzip"
}
res = requests.get(url, headers=headers).json()
music_url = res['data']['url']
#print(music_name,music_url)
# 下载
music = requests.get(music_url, headers=headers).content
with open(f"downloaded_music/{music_name}.mp4", mode="wb") as f:
song_load(music_name, music_url)
f.write(music)
#print(f"下载完成:{music_name}")
root = Tk()
# 2.添加标题
root.title('音乐下载器')
# 3.设置窗口大小
root.geometry('560x450+400+200')
# 4.标签控件
label = Label(root, text='请输入歌手:', font=('华文行楷', 20))
# 5.定位
label.grid()
# 6.输入框
entry = Entry(root, font=('隶书', 20))
# 7.定位
entry.grid(row=0, column=1)
# 8.列表框
text = Listbox(root, font=('楷书', 16), width=50, heigh=15)
# 9.定位 columnspan 组件横跨的列数
text.grid(row=3, columnspan=2)
# 点击下载按钮
button = Button(root, text='开始下载', font=('隶书', 15), command=huoqu_music)
# 定位 sticky 对齐方式 W E N S 东南西北
button.grid(row=4, column=0, sticky=W)
# 退出程序的按钮
button1 = Button(root, text='退出程序', font=('隶书', 15), command=root.quit)
# 定位 sticky 对齐方式 W E N S 东南西北
button1.grid(row=4, column=1, sticky=E)
# 显示界面
root.mainloop()