Skip to content

Commit

Permalink
fix: #74 配置目录可以和下载目录分开配置, 新增XIAOMUSIC_CONF_PATH用来设置配置目录,不配置时使用下载目录
Browse files Browse the repository at this point in the history
  • Loading branch information
hanxi committed Jun 23, 2024
1 parent 42b5978 commit 50da8a0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions xiaomusic/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 17 additions & 2 deletions xiaomusic/xiaomusic.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ def __init__(self, config: Config):
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
Expand Down Expand Up @@ -177,6 +181,8 @@ async def _init_data_hardware(self):
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:
Expand Down Expand Up @@ -716,21 +722,30 @@ def playingmusic(self):
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)
except FileNotFoundError:
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)
Expand Down

0 comments on commit 50da8a0

Please sign in to comment.