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

3
.gitignore vendored
View File

@@ -1,4 +1,5 @@
.ruff_cache
__pycache__
debug.py
videos
videos
config.test.json

View File

@@ -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"

17
entry.py Normal file
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())

10
main.py
View File

@@ -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()

View File

@@ -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()