feat(local): 优化本地歌曲搜索和播放功能

- 修复删除歌曲时未删除对应歌词文件的问题
- 优化本地歌曲搜索功能,支持模糊搜索
- 添加双击播放歌曲的功能
- 重构部分代码以提高可读性和可维护性
This commit is contained in:
2025-03-23 14:28:05 +08:00
parent dd5551a8e6
commit 1fe94b488b

View File

@ -1,4 +1,5 @@
import time import time
from functools import total_ordering
from tkinter import * from tkinter import *
import threading import threading
import pygame import pygame
@ -146,9 +147,15 @@ def download_music(song_name, choose):
resp.close() resp.close()
id = resp.json()["data"]["rid"].replace("MUSIC_", "") id = resp.json()["data"]["rid"].replace("MUSIC_", "")
try: try:
resp_lrc = requests.get(f"https://api.jkyai.top/API/yyjhss.php?id={id}&type=kw") resp_lrc = requests.get(f"http://m.kuwo.cn/newh5/singles/songinfoandlrc?musicId={id}")
resp_lrc.close() resp_lrc.close()
lrc = resp_lrc.json()["data"]["lrc"] lrc = ""
lrclist = resp_lrc.json()["data"]["lrclist"]
for i in lrclist:
total_time = i["time"]
min = total_time // 60
sec = total_time % 60
lrc += f'[{min}:{sec}]{i["lineLyric"]}\n'
except Exception as e: except Exception as e:
print(f"download_kw_lrc: {e}") print(f"download_kw_lrc: {e}")
lrc = "" lrc = ""
@ -160,9 +167,9 @@ def download_music(song_name, choose):
resp.close() resp.close()
id = resp.json()["data"]["id"] id = resp.json()["data"]["id"]
try: try:
resp_lrc = requests.get(f"https://api.jkyai.top/API/yyjhss.php?id={id}&type=wy") 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()["data"]["lrc"] 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 = ""
@ -329,12 +336,17 @@ def refresh():
def delete(): def delete():
global music_dir global music_dir, listbox1, music_dir_without_endswith, filtered_list
global listbox1
try: try:
if tkinter.messagebox.askokcancel(lang.get(la, "mbox.title.delete"), lang.get(la, "mbox.text.delete", )): if tkinter.messagebox.askokcancel(lang.get(la, "mbox.title.delete"), lang.get(la, "mbox.text.delete", )):
abs_path = path + "/" + music_dir[listbox1.curselection()[0]] abs_path = path + "/" + music_dir[listbox1.curselection()[0]]
abs_path_lrc = path + "/" + music_dir_without_endswith[listbox1.curselection()[0]] + ".lrc"
music_dir_without_endswith.remove(filtered_list[listbox1.curselection()[0] - 1])
os.remove(abs_path) os.remove(abs_path)
try:
os.remove(abs_path_lrc)
except:
pass
music_dir.remove(music_dir[listbox1.curselection()[0]]) music_dir.remove(music_dir[listbox1.curselection()[0]])
listbox1.delete(listbox1.curselection()[0]) listbox1.delete(listbox1.curselection()[0])
except IndexError: except IndexError:
@ -363,13 +375,21 @@ def pausesound():
def playsound(*event): def playsound(*event):
global music_playing, music_file_name, music_file_without_endswith, lyric, song_list, song_list_limit global music_playing, music_file_name, music_file_without_endswith, lyric, song_list, song_list_limit, music_dir
if not music_player.get_finished(): if not music_player.get_finished():
music_player.stop_music() music_player.stop_music()
lyric = "" lyric = ""
music_dir_without_endswith = [os.path.splitext(file)[0] for file in os.listdir(path) if music_dir_without_endswith = [os.path.splitext(file)[0] for file in os.listdir(path) if
file.endswith(('.mp3', '.flac', '.ogg', '.m4a'))] file.endswith(('.mp3', '.flac', '.ogg', '.m4a'))]
music_dir = [file for file in os.listdir(path) if file.endswith(('.mp3', '.flac', '.ogg', '.m4a'))] music_dir = [file for file in os.listdir(path) if file.endswith(('.mp3', '.flac', '.ogg', '.m4a'))]
if var7.get() not in ["", lang.get(la, "ui.download.entry.song_name")]:
music_dir_filtered = []
for file in music_dir:
if search_words in os.path.splitext(file)[0]:
music_dir_filtered.append(file)
music_dir = music_dir_filtered
music_dir_without_endswith = [os.path.splitext(file)[0] for file in music_dir if
file.endswith(('.mp3', '.flac', '.ogg', '.m4a'))]
abs_path = path + "/" + music_dir[listbox1.curselection()[0]] abs_path = path + "/" + music_dir[listbox1.curselection()[0]]
if abs_path not in song_list: if abs_path not in song_list:
song_list.append(abs_path) song_list.append(abs_path)
@ -1187,7 +1207,7 @@ def download_version():
def search_local_song(): def search_local_song():
global music_dir_without_endswith, search_words global search_words, filtered_list
while True: while True:
if focused: if focused:
former_words = var7.get() former_words = var7.get()
@ -1196,6 +1216,10 @@ def search_local_song():
if former_words != current_words and entry1.get() != lang.get(la, "ui.download.entry.song_name"): if former_words != current_words and entry1.get() != lang.get(la, "ui.download.entry.song_name"):
search_words = current_words search_words = current_words
filtered_list = [song for song in music_dir_without_endswith if search_words in song] filtered_list = [song for song in music_dir_without_endswith if search_words in song]
if not filtered_list:
listbox1.unbind("<Double-1>")
else:
listbox1.bind("<Double-1>", playsound)
print(filtered_list) print(filtered_list)
listbox1.delete(0, END) listbox1.delete(0, END)
for song in filtered_list: for song in filtered_list:
@ -1204,7 +1228,7 @@ def search_local_song():
time.sleep(1) time.sleep(1)
version = "4.1.4" version = "4.1.5"
poem = "" poem = ""
appdata = os.getenv("APPDATA") appdata = os.getenv("APPDATA")
make_resource() make_resource()
@ -1396,6 +1420,7 @@ scrollbar1.config(command=listbox1.yview)
scrollbar1.place(x=227, y=0, height=292) scrollbar1.place(x=227, y=0, height=292)
refresh() refresh()
filtered_list = music_dir_without_endswith
labelframe_download = ttk.Labelframe(homepage_frame, text=lang.get(la, "frame.download"), height=354, width=510) labelframe_download = ttk.Labelframe(homepage_frame, text=lang.get(la, "frame.download"), height=354, width=510)
labelframe_download.place(x=280, y=20) labelframe_download.place(x=280, y=20)