fix: #74 配置目录可以和下载目录分开配置, 新增XIAOMUSIC_CONF_PATH用来设置配置目录,不配置时使用下载目录

This commit is contained in:
涵曦 2024-06-23 02:49:13 +00:00
parent 42b5978d89
commit 50da8a0554
2 changed files with 18 additions and 2 deletions

View File

@ -87,6 +87,7 @@ class Config:
use_command: bool = False use_command: bool = False
verbose: bool = os.getenv("XIAOMUSIC_VERBOSE", "").lower() == "true" verbose: bool = os.getenv("XIAOMUSIC_VERBOSE", "").lower() == "true"
music_path: str = os.getenv("XIAOMUSIC_MUSIC_PATH", "music") 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") hostname: str = os.getenv("XIAOMUSIC_HOSTNAME", "192.168.2.5")
port: int = int(os.getenv("XIAOMUSIC_PORT", "8090")) port: int = int(os.getenv("XIAOMUSIC_PORT", "8090"))
proxy: str | None = os.getenv("XIAOMUSIC_PROXY", None) proxy: str | None = os.getenv("XIAOMUSIC_PROXY", None)

View File

@ -57,6 +57,10 @@ class XiaoMusic:
self.queue = queue.Queue() self.queue = queue.Queue()
self.music_path = config.music_path 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.hostname = config.hostname
self.port = config.port self.port = config.port
self.proxy = config.proxy self.proxy = config.proxy
@ -177,6 +181,8 @@ class XiaoMusic:
f"cannot find did for hardware: {self.config.hardware} " f"cannot find did for hardware: {self.config.hardware} "
"please set it via MI_DID env" "please set it via MI_DID env"
) )
except Exception as e:
self.log.error(f"Execption init hardware {e}")
def get_cookie(self): def get_cookie(self):
if self.config.cookie: if self.config.cookie:
@ -716,9 +722,16 @@ class XiaoMusic:
def getconfig(self): def getconfig(self):
return self.config 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): def try_init_setting(self):
try: try:
filename = os.path.join(self.music_path, "setting.json") filename = self.getsettingfile()
with open(filename) as f: with open(filename) as f:
data = json.loads(f.read()) data = json.loads(f.read())
self.update_config_from_setting(data) self.update_config_from_setting(data)
@ -726,11 +739,13 @@ class XiaoMusic:
self.log.info(f"The file {filename} does not exist.") self.log.info(f"The file {filename} does not exist.")
except json.JSONDecodeError: except json.JSONDecodeError:
self.log.warning(f"The file {filename} contains invalid JSON.") 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): async def saveconfig(self, data):
# 默认暂时配置保存到 music 目录下 # 默认暂时配置保存到 music 目录下
filename = os.path.join(self.music_path, "setting.json") filename = self.getsettingfile()
with open(filename, "w", encoding="utf-8") as f: with open(filename, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=4) json.dump(data, f, ensure_ascii=False, indent=4)
self.update_config_from_setting(data) self.update_config_from_setting(data)