feat: 简化配置,支持 daemon 运行

This commit is contained in:
amtoaer
2023-11-22 21:53:37 +08:00
parent 6f8b4afa1c
commit 5cc429e353
5 changed files with 47 additions and 41 deletions
+2 -1
View File
@@ -1,4 +1,5 @@
.ruff_cache .ruff_cache
__pycache__ __pycache__
debug.py debug.py
videos videos
config.test.json
+6 -2
View File
@@ -1,6 +1,10 @@
from pathlib import Path from pathlib import Path
import os
DEFAULT_CONFIG_PATH = (
DEFAULT_CONFIG_PATH = Path(__file__).parent / "config.json" Path(__file__).parent / "config.json"
if not os.getenv("TESTING")
else Path(__file__).parent / "config.test.json"
)
FFMPEG_COMMAND = "ffmpeg" FFMPEG_COMMAND = "ffmpeg"
+17
View File
@@ -0,0 +1,17 @@
from processor import process
import sys
import asyncio
from settings import settings
async def entry() -> None:
if any("once" in _ for _ in sys.argv):
# 单次运行
await process()
while True:
await process()
await asyncio.sleep(settings.interval)
def start() -> None:
asyncio.run(entry())
+2 -8
View File
@@ -1,10 +1,4 @@
import asyncio from entry import start
from processor import process
async def main() -> None:
await process()
if __name__ == "__main__": if __name__ == "__main__":
asyncio.run(main()) start()
+20 -30
View File
@@ -1,21 +1,27 @@
from dataclasses import dataclass from dataclasses import dataclass, field, fields
from dataclasses_json import DataClassJsonMixin from dataclasses_json import DataClassJsonMixin
from pathlib import Path from pathlib import Path
from typing import Self from typing import Self
import os
from constants import DEFAULT_CONFIG_PATH from constants import DEFAULT_CONFIG_PATH
@dataclass @dataclass
class Config(DataClassJsonMixin): class Config(DataClassJsonMixin):
sessdata: str sessdata: str = ""
bili_jct: str bili_jct: str = ""
buvid3: str buvid3: str = ""
dedeuserid: str dedeuserid: str = ""
ac_time_value: str ac_time_value: str = ""
favorite_ids: list[int] interval: int = 20
path_mapper: dict[int, str] favorite_ids: list[int] = field(default_factory=list)
path_mapper: dict[int, str] = field(default_factory=dict)
def validate(self) -> Self:
"""所有值必须被设置"""
if not all(getattr(self, f.name) for f in fields(self)):
raise ValueError("Some config values are not set.")
return self
@staticmethod @staticmethod
def load(path: Path | None = None) -> Self: def load(path: Path | None = None) -> Self:
@@ -33,7 +39,7 @@ class Config(DataClassJsonMixin):
try: try:
path.parent.mkdir(parents=True, exist_ok=True) path.parent.mkdir(parents=True, exist_ok=True)
with path.open("w") as f: with path.open("w") as f:
f.write(Config.schema().dumps(self)) f.write(Config.schema().dumps(self, indent=4))
return self return self
except Exception as e: except Exception as e:
raise RuntimeError(f"Failed to save config file: {path}") from e raise RuntimeError(f"Failed to save config file: {path}") from e
@@ -41,26 +47,10 @@ class Config(DataClassJsonMixin):
def init_settings() -> Config: def init_settings() -> Config:
if DEFAULT_CONFIG_PATH.exists(): if DEFAULT_CONFIG_PATH.exists():
return Config.load(DEFAULT_CONFIG_PATH) conf = Config.load(DEFAULT_CONFIG_PATH)
if os.getenv("TESTING"): else:
from debug import debug_config conf = Config()
return conf.save(DEFAULT_CONFIG_PATH).validate()
return debug_config
return (
Config.schema()
.load(
{
"sessdata": "",
"bili_jct": "",
"buvid3": "",
"dedeuserid": "",
"ac_time_value": "",
"favorite_ids": [],
"path_mapper": {},
}
)
.save(DEFAULT_CONFIG_PATH)
)
settings = init_settings() settings = init_settings()