diff --git a/xiaomusic/config.py b/xiaomusic/config.py index c7bf4f318..56641ac1a 100644 --- a/xiaomusic/config.py +++ b/xiaomusic/config.py @@ -123,6 +123,7 @@ class Config: enable_force_stop: bool = ( os.getenv("XIAOMUSIC_ENABLE_FORCE_STOP", "false").lower() == "true" ) + play_type: int = int(os.getenv("XIAOMUSIC_PLAY_TYPE", "2")) def append_keyword(self, keys, action): for key in keys.split(","): diff --git a/xiaomusic/xiaomusic.py b/xiaomusic/xiaomusic.py index fc09c8ab0..9e61bdf49 100644 --- a/xiaomusic/xiaomusic.py +++ b/xiaomusic/xiaomusic.py @@ -9,6 +9,7 @@ import time import traceback import urllib.parse +from dataclasses import asdict from logging.handlers import RotatingFileHandler from pathlib import Path @@ -64,8 +65,6 @@ def __init__(self, config: Config): # 下载对象 self.download_proc = None - # 单曲循环,全部循环 - self.play_type = PLAY_TYPE_RND self.cur_music = "" self._next_timer = None self._timeout = 0 @@ -102,6 +101,8 @@ def __init__(self, config: Config): self.log.info(f"Startup OK. {debug_config}") def init_config(self): + # 单曲循环,全部循环 + self.play_type = self.config.play_type self.music_path = self.config.music_path self.conf_path = self.config.conf_path if not self.conf_path: @@ -854,18 +855,21 @@ async def play_next(self, **kwargs): # 单曲循环 async def set_play_type_one(self, **kwargs): self.play_type = PLAY_TYPE_ONE + self.save_play_type() await self.do_tts("已经设置为单曲循环") # 全部循环 async def set_play_type_all(self, **kwargs): self.play_type = PLAY_TYPE_ALL self._gen_play_list() + self.save_play_type() await self.do_tts("已经设置为全部循环") # 随机播放 async def random_play(self, **kwargs): self.play_type = PLAY_TYPE_RND self._gen_play_list() + self.save_play_type() await self.do_tts("已经设置为随机播放") # 刷新列表 @@ -897,7 +901,8 @@ def find_real_music_list_name(self, list_name): if real_name: self.log.info(f"根据【{list_name}】找到播放列表【{real_name}】") list_name = real_name - self.log.info(f"没找到播放列表【{list_name}】") + else: + self.log.info(f"没找到播放列表【{list_name}】") return list_name # 播放一个播放列表 @@ -917,8 +922,6 @@ async def play_music_list(self, **kwargs): music_name = "" if len(parts) > 1: music_name = parts[1] - else: - music_name = self.get_next_music() await self.play(arg1=music_name) async def stop(self, **kwargs): @@ -1014,12 +1017,30 @@ def try_init_setting(self): # 保存配置并重新启动 async def saveconfig(self, data): + # 配置文件落地 + self.do_saveconfig(data) + # 更新配置 + self.update_config_from_setting(data) + # 重新初始化 + await self.call_main_thread_function(self.reinit) + + # 配置文件落地 + def do_saveconfig(self, data): # 默认暂时配置保存到 music 目录下 filename = self.getsettingfile() with open(filename, "w", encoding="utf-8") as f: json.dump(data, f, ensure_ascii=False, indent=4) - self.update_config_from_setting(data) - await self.call_main_thread_function(self.reinit) + + # 把当前配置落地 + def save_cur_config(self): + data = asdict(self.config) + self.do_saveconfig(data) + self.log.info("save_cur_config ok") + + # 播放类型落地 + def save_play_type(self): + self.config.play_type = self.play_type + self.save_cur_config() def update_config_from_setting(self, data): # 兼容旧配置:一段时间后清理这里的旧代码