refactor(NFLmusic): 重构音乐搜索和播放功能

- 移除酷我音乐、酷狗音乐和聚合搜索支持
- 更新网易云音乐API接口和数据结构
- 更新QQ音乐歌词获取方式
- 修改API请求参数和响应数据解析逻辑
- 统一错误处理和异常捕获机制
- 更新版本号从4.3.5到4.3.6
This commit is contained in:
2026-01-31 21:15:50 +08:00
parent 4e55f0521f
commit 240f380026

View File

@ -140,89 +140,33 @@ def download_music(song_name, choose):
style='success.Striped.Horizontal.TProgressbar') style='success.Striped.Horizontal.TProgressbar')
progressbar.place(x=10, y=300) progressbar.place(x=10, y=300)
br = br_dict[var.get()] br = br_dict[var.get()]
if choice == "KUWO": if choice == "WANGYIYUN":
resp = requests.get(f"{url}?msg={song_name}&n={choose}&br={br}&num=60&type=json&key=Dragon5B887C2DC41AD03C93F06BAF4B7888C3") resp = requests.get(f"{url}?name={song_name}&n={choose}&br={br}&limit=60&type=json&key=oiapi-e3329f47-ccbc-2279-14b6-eabd4c7286ac")
resp.close() resp.close()
id = resp.json()["link"].rsplit("/", 1)[1] songid = resp.json()["data"]["id"]
try: try:
resp_lrc = requests.get(f"http://m.kuwo.cn/newh5/singles/songinfoandlrc?musicId={id}") resp_lrc = requests.get(f"https://music.163.com/api/song/lyric?id={songid}&lv=-1&kv=-1&tv=-1")
resp_lrc.close()
lrc = ""
lrclist = resp_lrc.json()["data"]["lrclist"]
for i in lrclist:
total_time = i["time"]
min = float(total_time) // 60
sec = float(total_time) % 60
lrc += f'[{int(min)}:{sec}]{i["lineLyric"]}\n'
except Exception as e:
print(f"download_kw_lrc: {e}")
lrc = ""
music_name = resp.json()["song_name"]
singer = resp.json()["song_singer"]
music_url = resp.json()["flac_url"]
elif choice == "WANGYIYUN":
resp = requests.get(f"{url}?gm={song_name}&n={choose}&br={br}&num=60&type=json&key=Dragon5B887C2DC41AD03C93F06BAF4B7888C3")
resp.close()
id = resp.json()["link"].rsplit("=", 1)[1]
try:
resp_lrc = requests.get(f"https://music.163.com/api/song/lyric?id={id}&lv=-1&kv=-1&tv=-1")
resp_lrc.close() resp_lrc.close()
lrc = resp_lrc.json()["lrc"]["lyric"] lrc = resp_lrc.json()["lrc"]["lyric"]
except Exception as e: except Exception as e:
print(f"download_wyy_lrc: {e}") print(f"download_wyy_lrc: {e}")
lrc = "" lrc = ""
music_name = resp.json()["title"] music_name = resp.json()["data"]["name"]
singer = resp.json()["singer"] singer = resp.json()["data"]["singers"][0]["name"]
music_url = resp.json()["music_url"] music_url = resp.json()["data"]["url"]
elif choice == "QQ": elif choice == "QQ":
resp = requests.get(f"{url}?msg={song_name}&n={choose}&num=60&type=json&br={br}&key=Dragon5B887C2DC41AD03C93F06BAF4B7888C3") resp = requests.get(f"{url}?msg={song_name}&n={choose}&limit=60&type=json&br={br}&key=oiapi-e3329f47-ccbc-2279-14b6-eabd4c7286ac")
resp.close() resp.close()
songid = resp.json()["data"]["songid"]
resp1 = requests.get(f"https://oiapi.net/api/QQMusicLyric?id={songid}")
try: try:
lrc = resp.json()["data"]["lyric"] lrc = resp1.json()["message"]
except Exception as e: except Exception as e:
print(f"download_qq_lrc: {e}") print(f"download_qq_lrc: {e}")
lrc = "" lrc = ""
music_name = resp.json()["data"]["song_name"] music_name = resp.json()["data"]["song"]
singer = resp.json()["data"]["song_singer"]
music_url = resp.json()["data"]["music_url"]
elif choice == "KUGOU":
song_name = song_name.replace(" ", "")
resp = requests.get(f"{url}?msg={song_name}&n={choose}&num=60&type=json&br={br}&key=Dragon5B887C2DC41AD03C93F06BAF4B7888C3")
resp.close()
try:
lrc = resp.json()["lyrics"]
except Exception as e:
print(f"download_kg_lrc: {e}")
lrc = ""
music_name = resp.json()["title"]
singer = resp.json()["singer"]
music_url = resp.json()["music_url"]
elif choice == "JUHE":
song_name = song_name.replace(" ", "")
resp = requests.get(f"{url}?msg={song_name}&n={choose}&num=60&type=json&key=Dragon5B887C2DC41AD03C93F06BAF4B7888C3")
resp.close()
try:
lrc = resp.json()["data"]["lyric"]
print(lrc)
# 正则表达式匹配 [mm:ss]
def convert_timestamp(match):
mm_ss = match.group(1)
minutes, seconds = mm_ss.split(':')
# 转换为浮点秒数并保留一位小数(虽然这里是整数)
total_seconds = int(minutes) * 60 + int(seconds)
# 格式化回 mm:ss.s 形式
new_minutes = total_seconds // 60
new_seconds = total_seconds % 60
return f"[{new_minutes:02d}:{new_seconds:02d}.0]"
# 替换所有 [mm:ss] 为 [mm:ss.0]
lrc = re.sub(r'\[(\d{2}:\d{2})\]', convert_timestamp, lrc)
except Exception as e:
print(f"download_kg_lrc: {e}")
lrc = ""
music_name = resp.json()["data"]["title"]
singer = resp.json()["data"]["singer"] singer = resp.json()["data"]["singer"]
music_url = resp.json()["data"]["url"] music_url = resp.json()["data"]["music"]
else: else:
lrc = "" lrc = ""
lrc = lrc.replace("\\n", "\n") lrc = lrc.replace("\\n", "\n")
@ -286,7 +230,8 @@ def download_music(song_name, choose):
{"_singer_": f"{singer}", "_music_": f"{music_name}"})) {"_singer_": f"{singer}", "_music_": f"{music_name}"}))
downloading = False downloading = False
except KeyError: except KeyError as e:
print(e, type(e))
downloading = False downloading = False
progressbar.destroy() progressbar.destroy()
if song_name == "": if song_name == "":
@ -329,41 +274,23 @@ def get_data_without_blocking(song_name):
try: try:
button0["state"] = "disabled" button0["state"] = "disabled"
songlist.delete(*songlist.get_children()) songlist.delete(*songlist.get_children())
if choice == "KUWO": if choice == "WANGYIYUN":
url1 = f"{url}?msg={song_name}&num=60&type=json&key=Dragon5B887C2DC41AD03C93F06BAF4B7888C3" url1 = f"{url}?name={song_name}&limit=60&type=json&key=oiapi-e3329f47-ccbc-2279-14b6-eabd4c7286ac"
elif choice == "WANGYIYUN":
url1 = f"{url}?gm={song_name}&num=60&type=json&key=Dragon5B887C2DC41AD03C93F06BAF4B7888C3"
elif choice == "QQ": elif choice == "QQ":
url1 = f"{url}?msg={song_name}&num=60&type=json&key=Dragon5B887C2DC41AD03C93F06BAF4B7888C3" url1 = f"{url}?msg={song_name}&limit=60&type=json&key=oiapi-e3329f47-ccbc-2279-14b6-eabd4c7286ac"
elif choice == "KUGOU":
url1 = f"{url}?msg={song_name.replace(' ', '')}&num=60&type=json&key=Dragon5B887C2DC41AD03C93F06BAF4B7888C3"
elif choice == "JUHE":
url1 = f"{url}?msg={song_name}&num=60&type=json&key=Dragon5B887C2DC41AD03C93F06BAF4B7888C3"
resp = requests.get(url1) resp = requests.get(url1)
jsondata = resp.json() jsondata = resp.json()
resp.close() resp.close()
last_search_target = song_name last_search_target = song_name
for index in range(len(jsondata["data"] if isinstance(jsondata, dict) else jsondata)): for index in range(len(jsondata["data"] if isinstance(jsondata, dict) else jsondata)):
if choice == "KUWO": if choice == "WANGYIYUN":
full_name = jsondata["data"][index]["songname"] full_name = jsondata["data"][index]["name"]
artist = jsondata["data"][index]["singer"] artist = jsondata["data"][index]["singers"][0]["name"]
album = jsondata["data"][index]["song_rid"] album = jsondata["data"][index]["id"]
elif choice == "WANGYIYUN": elif choice == "QQ":
full_name = jsondata["data"][index]["title"] full_name = jsondata["data"][index]["song"]
artist = jsondata["data"][index]["singer"] artist = jsondata["data"][index]["singer"]
album = jsondata["data"][index]["songid"] album = jsondata["data"][index]["songid"]
elif choice == "QQ":
full_name = jsondata["data"][index]["song_title"]
artist = jsondata["data"][index]["song_singer"]
album = ""
elif choice == "KUGOU":
full_name = jsondata["data"][index]["title"]
artist = jsondata["data"][index]["singer"]
album = ""
elif choice == "JUHE":
full_name = jsondata[index]["title"]
artist = jsondata[index]["singer"]
album = jsondata[index]["app"]
songlist.insert("", "end", values=(full_name, artist, album)) songlist.insert("", "end", values=(full_name, artist, album))
except requests.exceptions.JSONDecodeError: except requests.exceptions.JSONDecodeError:
resp_text = resp.text resp_text = resp.text
@ -1327,7 +1254,7 @@ def search_local_song():
time.sleep(1) time.sleep(1)
version = "4.3.5" version = "4.3.6"
poem = "" poem = ""
appdata = os.getenv("APPDATA") appdata = os.getenv("APPDATA")
make_resource() make_resource()
@ -1696,11 +1623,8 @@ themeLabel.place(x=10, y=25)
# 基本设置 # 基本设置
repo_dict = { repo_dict = {
"酷我音乐": "KUWO",
"QQ音乐": "QQ", "QQ音乐": "QQ",
"网易云音乐": "WANGYIYUN", "网易云音乐": "WANGYIYUN",
"酷狗音乐": "KUGOU",
"聚合搜索": "JUHE"
} }
repo_dict_reverse = {} repo_dict_reverse = {}
for repo in repo_dict: for repo in repo_dict: