准备重构
This commit is contained in:
68
legacy/auxiliary.py
Normal file
68
legacy/auxiliary.py
Normal file
@@ -0,0 +1,68 @@
|
||||
#!/usr/bin/env python3
|
||||
import time
|
||||
import pathlib
|
||||
import toml
|
||||
import typing
|
||||
from playsound import playsound
|
||||
import threading
|
||||
import hashlib
|
||||
import edge_tts as tts
|
||||
|
||||
class ConfigFile:
|
||||
def __init__(self, path: str):
|
||||
self.path = pathlib.Path(path)
|
||||
if not self.path.exists():
|
||||
self.path.touch()
|
||||
self.data = dict()
|
||||
self._load()
|
||||
|
||||
def _load(self):
|
||||
"""从文件加载配置数据"""
|
||||
with open(self.path, 'r') as f:
|
||||
try:
|
||||
self.data = toml.load(f)
|
||||
except toml.TomlDecodeError:
|
||||
self.data = {}
|
||||
|
||||
def modify(self, key: str, value: typing.Any):
|
||||
"""修改配置值并保存"""
|
||||
self.data[key] = value
|
||||
self.save()
|
||||
|
||||
def save(self, path: typing.Union[str, pathlib.Path] = ""):
|
||||
"""保存配置到文件"""
|
||||
save_path = pathlib.Path(path) if path else self.path
|
||||
with open(save_path, 'w') as f:
|
||||
toml.dump(self.data, f)
|
||||
|
||||
def get(self, key: str, default: typing.Any = None) -> typing.Any:
|
||||
"""获取配置值,如果不存在返回默认值"""
|
||||
return self.data.get(key, default)
|
||||
|
||||
def action_play_voice(content):
|
||||
config = ConfigFile("config.toml")
|
||||
if config.get("auto_voice", False):
|
||||
return
|
||||
def play():
|
||||
communicate = tts.Communicate(
|
||||
content,
|
||||
"zh-CN-YunjianNeural",
|
||||
)
|
||||
communicate.save_sync(
|
||||
f"./cache/voice/{content}.wav"
|
||||
)
|
||||
playsound(f"./cache/voice/{content}.wav")
|
||||
threading.Thread(target=play).start()
|
||||
|
||||
def get_daystamp() -> int:
|
||||
"""获取当前日戳(以天为单位的整数时间戳)"""
|
||||
config = ConfigFile("config.toml")
|
||||
time_override = config.get("time_override", -1)
|
||||
|
||||
if time_override != -1:
|
||||
return int(time_override)
|
||||
|
||||
return int((time.time()+config.get("timezone_offset")) // (24 * 3600))
|
||||
|
||||
def get_md5(text):
|
||||
return hashlib.md5(text.encode('utf-8')).hexdigest()
|
Reference in New Issue
Block a user