From 50da8a0554635a0e430f963af12b509ff22e2ea3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B6=B5=E6=9B=A6?= Date: Sun, 23 Jun 2024 02:49:13 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20#74=20=E9=85=8D=E7=BD=AE=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=E5=8F=AF=E4=BB=A5=E5=92=8C=E4=B8=8B=E8=BD=BD=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=E5=88=86=E5=BC=80=E9=85=8D=E7=BD=AE,=20=E6=96=B0?= =?UTF-8?q?=E5=A2=9EXIAOMUSIC=5FCONF=5FPATH=E7=94=A8=E6=9D=A5=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E9=85=8D=E7=BD=AE=E7=9B=AE=E5=BD=95=EF=BC=8C=E4=B8=8D?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=97=B6=E4=BD=BF=E7=94=A8=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- xiaomusic/config.py | 1 + xiaomusic/xiaomusic.py | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/xiaomusic/config.py b/xiaomusic/config.py index 44014a0..ae9e1a0 100644 --- a/xiaomusic/config.py +++ b/xiaomusic/config.py @@ -87,6 +87,7 @@ class Config: use_command: bool = False verbose: bool = os.getenv("XIAOMUSIC_VERBOSE", "").lower() == "true" music_path: str = os.getenv("XIAOMUSIC_MUSIC_PATH", "music") + conf_path: str = os.getenv("XIAOMUSIC_CONF_PATH", None) hostname: str = os.getenv("XIAOMUSIC_HOSTNAME", "192.168.2.5") port: int = int(os.getenv("XIAOMUSIC_PORT", "8090")) proxy: str | None = os.getenv("XIAOMUSIC_PROXY", None) diff --git a/xiaomusic/xiaomusic.py b/xiaomusic/xiaomusic.py index 5ac3e60..c6fbb60 100644 --- a/xiaomusic/xiaomusic.py +++ b/xiaomusic/xiaomusic.py @@ -57,6 +57,10 @@ class XiaoMusic: self.queue = queue.Queue() self.music_path = config.music_path + self.conf_path = config.conf_path + if not self.conf_path: + self.conf_path = config.music_path + self.hostname = config.hostname self.port = config.port self.proxy = config.proxy @@ -177,6 +181,8 @@ class XiaoMusic: f"cannot find did for hardware: {self.config.hardware} " "please set it via MI_DID env" ) + except Exception as e: + self.log.error(f"Execption init hardware {e}") def get_cookie(self): if self.config.cookie: @@ -716,9 +722,16 @@ class XiaoMusic: def getconfig(self): return self.config + # 获取设置文件 + def getsettingfile(self): + if not os.path.exists(self.conf_path): + os.makedirs(self.conf_path) + filename = os.path.join(self.conf_path, "setting.json") + return filename + def try_init_setting(self): try: - filename = os.path.join(self.music_path, "setting.json") + filename = self.getsettingfile() with open(filename) as f: data = json.loads(f.read()) self.update_config_from_setting(data) @@ -726,11 +739,13 @@ class XiaoMusic: self.log.info(f"The file {filename} does not exist.") except json.JSONDecodeError: self.log.warning(f"The file {filename} contains invalid JSON.") + except Exception as e: + self.log.error(f"Execption init setting {e}") # 保存配置并重新启动 async def saveconfig(self, data): # 默认暂时配置保存到 music 目录下 - filename = os.path.join(self.music_path, "setting.json") + 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)