diff --git a/.gitignore b/.gitignore index e5cb8b6..a8b30e6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .ruff_cache __pycache__ debug.py -videos \ No newline at end of file +videos +config.test.json \ No newline at end of file diff --git a/constants.py b/constants.py index 96f2264..89f8acc 100644 --- a/constants.py +++ b/constants.py @@ -1,6 +1,10 @@ from pathlib import Path +import os - -DEFAULT_CONFIG_PATH = Path(__file__).parent / "config.json" +DEFAULT_CONFIG_PATH = ( + Path(__file__).parent / "config.json" + if not os.getenv("TESTING") + else Path(__file__).parent / "config.test.json" +) FFMPEG_COMMAND = "ffmpeg" diff --git a/entry.py b/entry.py new file mode 100644 index 0000000..7f69e0f --- /dev/null +++ b/entry.py @@ -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()) diff --git a/main.py b/main.py index b078aeb..9e5fcc0 100644 --- a/main.py +++ b/main.py @@ -1,10 +1,4 @@ -import asyncio -from processor import process - - -async def main() -> None: - await process() - +from entry import start if __name__ == "__main__": - asyncio.run(main()) + start() diff --git a/settings.py b/settings.py index 3bfe0f0..4e2da42 100644 --- a/settings.py +++ b/settings.py @@ -1,21 +1,27 @@ -from dataclasses import dataclass +from dataclasses import dataclass, field, fields from dataclasses_json import DataClassJsonMixin from pathlib import Path from typing import Self -import os from constants import DEFAULT_CONFIG_PATH @dataclass class Config(DataClassJsonMixin): - sessdata: str - bili_jct: str - buvid3: str - dedeuserid: str - ac_time_value: str - favorite_ids: list[int] - path_mapper: dict[int, str] + sessdata: str = "" + bili_jct: str = "" + buvid3: str = "" + dedeuserid: str = "" + ac_time_value: str = "" + interval: int = 20 + 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 def load(path: Path | None = None) -> Self: @@ -33,7 +39,7 @@ class Config(DataClassJsonMixin): try: path.parent.mkdir(parents=True, exist_ok=True) with path.open("w") as f: - f.write(Config.schema().dumps(self)) + f.write(Config.schema().dumps(self, indent=4)) return self except Exception as e: raise RuntimeError(f"Failed to save config file: {path}") from e @@ -41,26 +47,10 @@ class Config(DataClassJsonMixin): def init_settings() -> Config: if DEFAULT_CONFIG_PATH.exists(): - return Config.load(DEFAULT_CONFIG_PATH) - if os.getenv("TESTING"): - from debug import debug_config - - return debug_config - return ( - Config.schema() - .load( - { - "sessdata": "", - "bili_jct": "", - "buvid3": "", - "dedeuserid": "", - "ac_time_value": "", - "favorite_ids": [], - "path_mapper": {}, - } - ) - .save(DEFAULT_CONFIG_PATH) - ) + conf = Config.load(DEFAULT_CONFIG_PATH) + else: + conf = Config() + return conf.save(DEFAULT_CONFIG_PATH).validate() settings = init_settings()