feat: 新增歌曲收藏功能 #87

This commit is contained in:
涵曦 2024-09-03 15:55:19 +00:00
parent d3895f2632
commit d1b869ae43
3 changed files with 62 additions and 0 deletions

View File

@ -22,6 +22,8 @@ def default_key_word_dict():
"分钟后关机": "stop_after_minute", "分钟后关机": "stop_after_minute",
"播放列表": "play_music_list", "播放列表": "play_music_list",
"刷新列表": "gen_music_list", "刷新列表": "gen_music_list",
"加入收藏": "add_to_favorites",
"取消收藏": "del_from_favorites",
} }
@ -50,6 +52,8 @@ def default_key_match_order():
"关机", "关机",
"刷新列表", "刷新列表",
"播放列表", "播放列表",
"加入收藏",
"取消收藏",
] ]
@ -96,6 +100,7 @@ class Config:
httpauth_password: str = os.getenv("XIAOMUSIC_HTTPAUTH_PASSWORD", "") httpauth_password: str = os.getenv("XIAOMUSIC_HTTPAUTH_PASSWORD", "")
music_list_url: str = os.getenv("XIAOMUSIC_MUSIC_LIST_URL", "") music_list_url: str = os.getenv("XIAOMUSIC_MUSIC_LIST_URL", "")
music_list_json: str = os.getenv("XIAOMUSIC_MUSIC_LIST_JSON", "") music_list_json: str = os.getenv("XIAOMUSIC_MUSIC_LIST_JSON", "")
custom_play_list_json: str = os.getenv("XIAOMUSIC_CUSTOM_PLAY_LIST_JSON", "")
disable_download: bool = ( disable_download: bool = (
os.getenv("XIAOMUSIC_DISABLE_DOWNLOAD", "false").lower() == "true" os.getenv("XIAOMUSIC_DISABLE_DOWNLOAD", "false").lower() == "true"
) )
@ -153,6 +158,7 @@ class Config:
def init_keyword(self): def init_keyword(self):
self.key_match_order = default_key_match_order() self.key_match_order = default_key_match_order()
self.key_word_dict = default_key_word_dict()
self.append_keyword(self.keywords_playlocal, "playlocal") self.append_keyword(self.keywords_playlocal, "playlocal")
self.append_keyword(self.keywords_play, "play") self.append_keyword(self.keywords_play, "play")
self.append_keyword(self.keywords_stop, "stop") self.append_keyword(self.keywords_stop, "stop")

View File

@ -12,6 +12,9 @@ $(function(){
append_op_button_name("下一首"); append_op_button_name("下一首");
append_op_button_name("关机"); append_op_button_name("关机");
append_op_button_name("加入收藏");
append_op_button_name("取消收藏");
$container.append($("<hr>")); $container.append($("<hr>"));
append_op_button_name("10分钟后关机"); append_op_button_name("10分钟后关机");

View File

@ -454,6 +454,8 @@ class XiaoMusic:
self.music_list["全部"] = list(self.all_music.keys()) self.music_list["全部"] = list(self.all_music.keys())
self._append_custom_play_list()
# 歌单排序 # 歌单排序
for _, play_list in self.music_list.items(): for _, play_list in self.music_list.items():
play_list.sort(key=custom_sort_key) play_list.sort(key=custom_sort_key)
@ -462,6 +464,16 @@ class XiaoMusic:
for device in self.devices.values(): for device in self.devices.values():
device.update_playlist() device.update_playlist()
def _append_custom_play_list(self):
if not self.config.custom_play_list_json:
return
try:
custom_play_list = json.loads(self.config.custom_play_list_json)
self.music_list["收藏"] = list(custom_play_list["收藏"])
except Exception as e:
self.log.exception(f"Execption {e}")
# 给歌单里补充网络歌单 # 给歌单里补充网络歌单
def _append_music_list(self): def _append_music_list(self):
if not self.config.music_list_json: if not self.config.music_list_json:
@ -715,6 +727,47 @@ class XiaoMusic:
minute = int(arg1) minute = int(arg1)
return await self.devices[did].stop_after_minute(minute) return await self.devices[did].stop_after_minute(minute)
# 添加歌曲到收藏列表
async def add_to_favorites(self, did="", arg1="", **kwargs):
name = arg1 if arg1 else self.playingmusic(did)
if not name:
return
favorites = self.music_list.get("收藏", [])
if name in favorites:
return
favorites.append(name)
self.save_favorites(favorites)
# 从收藏列表中移除
async def del_from_favorites(self, did="", arg1="", **kwargs):
name = arg1 if arg1 else self.playingmusic(did)
if not name:
return
favorites = self.music_list.get("收藏", [])
if name not in favorites:
return
favorites.remove(name)
self.save_favorites(favorites)
def save_favorites(self, favorites):
self.music_list["收藏"] = favorites
custom_play_list = {}
if self.config.custom_play_list_json:
custom_play_list = json.loads(self.config.custom_play_list_json)
custom_play_list["收藏"] = favorites
self.config.custom_play_list_json = json.dumps(
custom_play_list, ensure_ascii=False
)
self.save_cur_config()
# 更新每个设备的歌单
for device in self.devices.values():
device.update_playlist()
# 获取音量 # 获取音量
async def get_volume(self, did="", **kwargs): async def get_volume(self, did="", **kwargs):
return await self.devices[did].get_volume() return await self.devices[did].get_volume()