2023-10-14 11:50:32 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
|
|
import json
|
|
|
|
import os
|
2024-06-14 01:58:10 +00:00
|
|
|
from dataclasses import dataclass
|
2023-10-14 11:50:32 +00:00
|
|
|
from xiaomusic.utils import validate_proxy
|
|
|
|
|
2024-06-26 17:34:23 +00:00
|
|
|
# 默认口令
|
|
|
|
DEFAULT_KEY_WORD_DICT = {
|
2023-10-14 11:50:32 +00:00
|
|
|
"播放歌曲": "play",
|
2024-06-26 17:34:23 +00:00
|
|
|
"播放本地歌曲": "playlocal",
|
|
|
|
"关机": "stop",
|
2023-10-14 11:50:32 +00:00
|
|
|
"下一首": "play_next",
|
2024-01-27 12:56:07 +00:00
|
|
|
"单曲循环": "set_play_type_one",
|
|
|
|
"全部循环": "set_play_type_all",
|
2024-01-27 15:00:17 +00:00
|
|
|
"随机播放": "random_play",
|
2024-01-28 10:17:45 +00:00
|
|
|
"分钟后关机": "stop_after_minute",
|
2024-06-12 17:12:07 +00:00
|
|
|
"播放列表": "play_music_list",
|
2024-06-12 17:21:09 +00:00
|
|
|
"刷新列表": "gen_music_list",
|
2024-01-29 15:10:16 +00:00
|
|
|
"set_volume#": "set_volume",
|
2024-05-16 22:39:05 +00:00
|
|
|
"get_volume#": "get_volume",
|
2023-10-14 11:50:32 +00:00
|
|
|
}
|
|
|
|
|
2024-01-28 10:17:45 +00:00
|
|
|
# 命令参数在前面
|
|
|
|
KEY_WORD_ARG_BEFORE_DICT = {
|
|
|
|
"分钟后关机": True,
|
|
|
|
}
|
|
|
|
|
2024-06-26 17:34:23 +00:00
|
|
|
# 口令匹配优先级
|
|
|
|
DEFAULT_KEY_MATCH_ORDER = [
|
2024-01-29 15:10:16 +00:00
|
|
|
"set_volume#",
|
2024-05-16 22:39:05 +00:00
|
|
|
"get_volume#",
|
2024-01-28 10:17:45 +00:00
|
|
|
"分钟后关机",
|
|
|
|
"播放歌曲",
|
|
|
|
"下一首",
|
|
|
|
"单曲循环",
|
|
|
|
"全部循环",
|
|
|
|
"随机播放",
|
|
|
|
"关机",
|
2024-06-12 17:21:09 +00:00
|
|
|
"刷新列表",
|
2024-06-12 17:12:07 +00:00
|
|
|
"播放列表",
|
2024-01-28 10:17:45 +00:00
|
|
|
]
|
|
|
|
|
2023-10-14 11:50:32 +00:00
|
|
|
@dataclass
|
|
|
|
class Config:
|
2023-10-16 10:07:07 +00:00
|
|
|
hardware: str = os.getenv("MI_HARDWARE", "L07A")
|
2023-10-14 11:50:32 +00:00
|
|
|
account: str = os.getenv("MI_USER", "")
|
|
|
|
password: str = os.getenv("MI_PASS", "")
|
|
|
|
mi_did: str = os.getenv("MI_DID", "")
|
|
|
|
cookie: str = ""
|
2024-06-14 15:46:47 +00:00
|
|
|
verbose: bool = os.getenv("XIAOMUSIC_VERBOSE", "").lower() == "true"
|
2023-10-15 02:58:53 +00:00
|
|
|
music_path: str = os.getenv("XIAOMUSIC_MUSIC_PATH", "music")
|
2024-06-23 02:49:13 +00:00
|
|
|
conf_path: str = os.getenv("XIAOMUSIC_CONF_PATH", None)
|
2023-10-15 02:58:53 +00:00
|
|
|
hostname: str = os.getenv("XIAOMUSIC_HOSTNAME", "192.168.2.5")
|
|
|
|
port: int = int(os.getenv("XIAOMUSIC_PORT", "8090"))
|
2023-10-16 14:40:21 +00:00
|
|
|
proxy: str | None = os.getenv("XIAOMUSIC_PROXY", None)
|
2024-02-02 12:55:51 +00:00
|
|
|
search_prefix: str = os.getenv(
|
|
|
|
"XIAOMUSIC_SEARCH", "ytsearch:"
|
|
|
|
) # "bilisearch:" or "ytsearch:"
|
2024-02-24 04:49:17 +00:00
|
|
|
ffmpeg_location: str = os.getenv("XIAOMUSIC_FFMPEG_LOCATION", "./ffmpeg/bin")
|
2024-06-26 17:41:38 +00:00
|
|
|
active_cmd: str = os.getenv("XIAOMUSIC_ACTIVE_CMD", "play,random_play,playlocal")
|
2024-06-16 06:14:33 +00:00
|
|
|
exclude_dirs: str = os.getenv("XIAOMUSIC_EXCLUDE_DIRS", "@eaDir")
|
2024-06-23 03:30:32 +00:00
|
|
|
music_path_depth: int = int(os.getenv("XIAOMUSIC_MUSIC_PATH_DEPTH", "10"))
|
2024-06-23 07:06:42 +00:00
|
|
|
disable_httpauth: bool = (
|
|
|
|
os.getenv("XIAOMUSIC_DISABLE_HTTPAUTH", "true").lower() == "true"
|
|
|
|
)
|
|
|
|
httpauth_username: str = os.getenv("XIAOMUSIC_HTTPAUTH_USERNAME", "admin")
|
|
|
|
httpauth_password: str = os.getenv("XIAOMUSIC_HTTPAUTH_PASSWORD", "admin")
|
2024-06-25 11:13:46 +00:00
|
|
|
music_list_url: str = os.getenv("XIAOMUSIC_MUSIC_LIST_URL", "")
|
|
|
|
music_list_json: str = os.getenv("XIAOMUSIC_MUSIC_LIST_JSON", "")
|
2024-06-26 16:14:41 +00:00
|
|
|
disable_download: bool = (
|
|
|
|
os.getenv("XIAOMUSIC_DISABLE_DOWNLOAD", "false").lower() == "true"
|
|
|
|
)
|
2024-06-26 17:34:23 +00:00
|
|
|
key_word_dict = DEFAULT_KEY_WORD_DICT.copy()
|
|
|
|
key_match_order = DEFAULT_KEY_MATCH_ORDER.copy()
|
2024-06-27 05:27:28 +00:00
|
|
|
use_music_api: bool = (
|
|
|
|
os.getenv("XIAOMUSIC_USE_MUSIC_API", "false").lower() == "true"
|
|
|
|
)
|
2024-06-26 17:34:23 +00:00
|
|
|
|
|
|
|
def append_keyword(self, keys, action):
|
|
|
|
for key in keys.split(","):
|
|
|
|
self.key_word_dict[key] = action
|
|
|
|
if key not in self.key_match_order:
|
|
|
|
self.key_match_order.append(key)
|
2023-10-15 02:58:53 +00:00
|
|
|
|
|
|
|
def __post_init__(self) -> None:
|
|
|
|
if self.proxy:
|
|
|
|
validate_proxy(self.proxy)
|
2024-06-26 17:34:23 +00:00
|
|
|
keywords_playlocal = os.getenv(
|
|
|
|
"XIAOMUSIC_KEYWORDS_PLAYLOCAL", "播放本地歌曲,本地播放歌曲"
|
|
|
|
)
|
|
|
|
self.append_keyword(keywords_playlocal, "playlocal")
|
|
|
|
keywords_play = os.getenv("XIAOMUSIC_KEYWORDS_PLAY", "播放歌曲,放歌曲")
|
|
|
|
self.append_keyword(keywords_play, "play")
|
|
|
|
keywords_stop = os.getenv("XIAOMUSIC_KEYWORDS_STOP", "关机,暂停,停止")
|
|
|
|
self.append_keyword(keywords_stop, "stop")
|
2023-10-14 11:50:32 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_options(cls, options: argparse.Namespace) -> Config:
|
|
|
|
config = {}
|
|
|
|
if options.config:
|
|
|
|
config = cls.read_from_file(options.config)
|
|
|
|
for key, value in vars(options).items():
|
|
|
|
if value is not None and key in cls.__dataclass_fields__:
|
|
|
|
config[key] = value
|
|
|
|
return cls(**config)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def read_from_file(cls, config_path: str) -> dict:
|
|
|
|
result = {}
|
|
|
|
with open(config_path, "rb") as f:
|
|
|
|
config = json.load(f)
|
|
|
|
for key, value in config.items():
|
|
|
|
if value is not None and key in cls.__dataclass_fields__:
|
|
|
|
result[key] = value
|
|
|
|
return result
|