feat: switch config to JSONC + split secrets.json
Load business config as JSONC (comments + trailing commas) via an in-repo stripper then stdlib json — no new deps. Default path prefers config.jsonc, falls back to legacy config.json. Auth write-back targets only sibling secrets.json so JSONC comments are never wiped; secrets overlay merges on load. Examples, compose, Docker, docs, favsort and tools all share the same loader. Phase A only — no DB settings migration.
This commit was merged in pull request #4.
This commit is contained in:
1 parent
b4f0f45c4e
commit
8e7ead1669
21 files changed
+652
-182
No files matched your search
+23
-14
@@ -6,7 +6,7 @@ bili-fav / favsort —— B 站收藏夹批量重排序工具
|
||||
1) 同一个 upper 的视频必须连续(聚成一组),组内按发布时间倒序(并列按 avid 升序)
|
||||
2) 组间按"该 upper 在本收藏夹中最早视频的发布时间"倒序(并列按该组最小 avid 升序)
|
||||
3) 失效/异常资源置于末尾,保持原相对顺序
|
||||
4) config.json 里的 overrides 可把指定视频强制归入另一个 upper(代发等场景)
|
||||
4) 配置里的 overrides 可把指定视频强制归入另一个 upper(代发等场景)
|
||||
|
||||
指令
|
||||
python favsort.py help [子命令]
|
||||
@@ -36,11 +36,16 @@ sys.stdout.reconfigure(encoding="utf-8")
|
||||
BASE = "https://api.bilibili.com"
|
||||
ROOT = os.path.dirname(os.path.abspath(__file__))
|
||||
DATA_DIR = os.path.join(ROOT, "data")
|
||||
CONFIG_PATH = os.path.join(ROOT, "config.json")
|
||||
|
||||
from reaction_board.bilibili.auth import ( # noqa: E402
|
||||
make_headers,
|
||||
)
|
||||
from reaction_board.config import ( # noqa: E402
|
||||
ConfigError,
|
||||
default_config_path,
|
||||
load_config as load_app_config,
|
||||
)
|
||||
|
||||
CONFIG_PATH = str(default_config_path(ROOT))
|
||||
|
||||
CHUNK = 20 # infos 接口单次 resources 数量(实测 20 稳定)
|
||||
API = {
|
||||
@@ -70,18 +75,19 @@ def load_config(require_auth=True):
|
||||
if not os.path.exists(CONFIG_PATH):
|
||||
raise SystemExit(
|
||||
f"缺少 {CONFIG_PATH}\n"
|
||||
f"请复制 config.example.json 为 config.json 并填入 cookie / csrf / mid。"
|
||||
f"请复制 config.example.jsonc 为 config.jsonc,并复制 secrets.example.json 为 secrets.json 后填入凭据\n"
|
||||
f"(或运行 python app.py auth --account write)。"
|
||||
)
|
||||
with open(CONFIG_PATH, encoding="utf-8") as f:
|
||||
raw = json.load(f)
|
||||
bilibili = raw.get("bilibili")
|
||||
if not isinstance(bilibili, dict) or not isinstance(bilibili.get("write"), dict):
|
||||
raise SystemExit("config.json 必须配置 bilibili.write(扫码请运行 python app.py auth --account write)")
|
||||
write = bilibili["write"]
|
||||
try:
|
||||
app_cfg = load_app_config(CONFIG_PATH, require_auth=False)
|
||||
except ConfigError as exc:
|
||||
raise SystemExit(str(exc)) from exc
|
||||
write = app_cfg.bilibili.write
|
||||
raw = app_cfg.raw
|
||||
cfg = {
|
||||
"mid": write.get("mid", 0),
|
||||
"csrf": write.get("csrf", ""),
|
||||
"cookie": write.get("cookie", ""),
|
||||
"mid": write.mid,
|
||||
"csrf": write.csrf,
|
||||
"cookie": write.cookie,
|
||||
"overrides": raw.get("overrides", {}),
|
||||
"sleep": raw.get("sleep", {}),
|
||||
}
|
||||
@@ -90,7 +96,10 @@ def load_config(require_auth=True):
|
||||
for k, v in DEFAULT_CONFIG["sleep"].items():
|
||||
cfg["sleep"].setdefault(k, v)
|
||||
if require_auth and (not cfg["cookie"] or not cfg["csrf"]):
|
||||
raise SystemExit("config.json 中的 bilibili.write.cookie / csrf 不能为空(可先运行 python app.py auth --account write)")
|
||||
raise SystemExit(
|
||||
"bilibili.write.cookie / csrf 不能为空"
|
||||
f"(写入 {app_cfg.secrets_path.name},或运行 python app.py auth --account write)"
|
||||
)
|
||||
return cfg
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user