mirror of
https://github.com/amtoaer/bili-sync.git
synced 2026-05-07 06:22:54 +08:00
feat: 优化配置和凭据构建
This commit is contained in:
32
credential.py
Normal file
32
credential.py
Normal file
@@ -0,0 +1,32 @@
|
||||
from bilibili_api import Credential
|
||||
from settings import settings
|
||||
|
||||
|
||||
class PersistedCredential(Credential):
|
||||
def __init__(self) -> None:
|
||||
super().__init__(
|
||||
settings.sessdata,
|
||||
settings.bili_jct,
|
||||
settings.buvid3,
|
||||
settings.dedeuserid,
|
||||
settings.ac_time_value,
|
||||
)
|
||||
|
||||
async def refresh(self) -> None:
|
||||
await super().refresh()
|
||||
(
|
||||
settings.sessdata,
|
||||
settings.bili_jct,
|
||||
settings.dedeuserid,
|
||||
settings.ac_time_value,
|
||||
) = (
|
||||
self.sessdata,
|
||||
self.bili_jct,
|
||||
self.dedeuserid,
|
||||
self.ac_time_value,
|
||||
)
|
||||
# 暂时使用同步调用
|
||||
settings.save()
|
||||
|
||||
|
||||
credential = PersistedCredential()
|
||||
52
settings.py
52
settings.py
@@ -2,45 +2,65 @@ from dataclasses import dataclass
|
||||
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):
|
||||
sessiondata: str
|
||||
sessdata: str
|
||||
bili_jct: str
|
||||
buvid3: str
|
||||
dedeuserid: str
|
||||
ac_time_value: str
|
||||
favorite_ids: list
|
||||
path_mapper: dict
|
||||
favorite_ids: list[int]
|
||||
path_mapper: dict[int, str]
|
||||
|
||||
@staticmethod
|
||||
def load(path: Path | str) -> Self:
|
||||
if isinstance(path, str):
|
||||
path = Path(path)
|
||||
def load(path: Path | None = None) -> Self:
|
||||
if not path:
|
||||
path = DEFAULT_CONFIG_PATH
|
||||
try:
|
||||
with path.open("r") as f:
|
||||
return Config.schema().loads(f.read())
|
||||
except Exception as e:
|
||||
raise ValueError(f"Failed to load config file: {path}") from e
|
||||
raise RuntimeError(f"Failed to load config file: {path}") from e
|
||||
|
||||
def save(self, path: Path | str) -> Self:
|
||||
if isinstance(path, str):
|
||||
path = Path(path)
|
||||
def save(self, path: Path | None = None) -> Self:
|
||||
if not path:
|
||||
path = DEFAULT_CONFIG_PATH
|
||||
try:
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
with path.open("w") as f:
|
||||
f.write(Config.schema().dumps(self))
|
||||
return self
|
||||
except Exception as e:
|
||||
raise PermissionError(f"Failed to save config file: {path}") from e
|
||||
return self
|
||||
raise RuntimeError(f"Failed to save config file: {path}") from e
|
||||
|
||||
|
||||
def init_settings() -> Config:
|
||||
if (Path(__file__).parent / "config.json").exists():
|
||||
return Config.load(Path(__file__).parent / "config.json")
|
||||
# TODO: 读取环境变量
|
||||
return Config().save(Path(__file__).parent / "config.json")
|
||||
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)
|
||||
)
|
||||
|
||||
|
||||
settings = init_settings()
|
||||
|
||||
Reference in New Issue
Block a user