Peach: Local-first video metadata scraper and media manager.
feat(scripts): run_performer_profiles 一次补完库里女优的资料
longmeidaoeeafb9b663ec
diff --git a/scripts/run_performer_profiles.py b/scripts/run_performer_profiles.pynew file mode 100644index 000000000..438a9ac0c--- /dev/null+++ b/scripts/run_performer_profiles.py@@ -0,0 +1,103 @@+"""一次补完库里女优的资料:逐条跑补女优资料后继(ADR-0067),不等处理任务按名额排。++处理任务每轮只给存量 `STOCK_SHARE` 个名额,九百多位女优要一个多月才轮遍;这个脚本把+同一条后继在当前进程里挨个跑掉。取页、限流、冷却、双向核对、落库与判词 CSV 全是后继+自己那一套,这里只负责挑人和记进度:++- 挑人用后继的 `stock`:有路进得去、记号未到期的女优,作品多的在前。跑过又没变的由记号+ 跳过,所以中断之后原样重跑就是接着跑。+- 写入批次是 `auto:performer-profile@manual-<时间>`,+ `scripts/revert_auto_landing.py --source auto:performer-profile` 整批撤回。+- 连续 `STOP_AFTER_UNFETCHED` 位都「未取得」多半是站点冷却或断网,停下来报告,不把剩下+ 的女优全刷成「未取得」。++用法:+ python scripts/run_performer_profiles.py # dry-run,只报待跑人数+ python scripts/run_performer_profiles.py --apply --backup <路径> [--limit N]+"""+from __future__ import annotations++import argparse+import sys+import time+from pathlib import Path++sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))++from peach import performer_profile_followup as profiles # noqa: E402+from peach.followups import Attempts, attempts_root # noqa: E402+from peach.migrations import sqlite_backup # noqa: E402+from peach.scripting import (BACKUP_REQUIRED, add_ledger_write_args, open_readonly, # noqa: E402+ verify_after_write)++#: 连续几位「未取得」就停。站点冷却期内每一位都会立刻落成「未取得」,再跑只是刷判词。+STOP_AFTER_UNFETCHED = 5+++class Handle:+ """后继要的任务句柄只用到批次号与进度,这里把进度打到终端。"""++ def __init__(self, run_id: str):+ self.run_id = run_id++ def progress(self, **_kwargs) -> None:+ return None+++def pending(connection, generated: Path, limit: int | None) -> list:+ attempts = Attempts(attempts_root(generated))+ return profiles.stock(connection, attempts, limit=limit or 10**9)+++def sweep(contract, items, run_id: str, *, out=sys.stdout,+ stop_after: int = STOP_AFTER_UNFETCHED) -> dict:+ """挨个跑,返回各结论的计数;连续 `stop_after` 位未取得就停。"""+ handle, tally, streak = Handle(run_id), {}, 0+ for index, item in enumerate(items, 1):+ try:+ summary = profiles.run(contract, item.key, handle)+ except Exception as error: # 一位出错只算她自己,接着跑下一位。+ summary = {"outcome": f"出错:{type(error).__name__}: {error}"}+ outcome = str(summary.get("outcome", ""))+ head = outcome.split(";")[0] or "无结论"+ tally[head] = tally.get(head, 0) + 1+ print(f"{index}/{len(items)} {summary.get('name', item.label)}:{outcome}", file=out, flush=True)+ streak = streak + 1 if outcome == profiles.UNFETCHED else 0+ if streak >= stop_after:+ print(f"连续 {streak} 位未取得,停下:多半是站点冷却或网络断了,稍后原样重跑即可接着来",+ file=out, flush=True)+ tally["停在"] = index+ break+ return tally+++def main(argv=None) -> int:+ parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])+ add_ledger_write_args(parser)+ parser.add_argument("--limit", type=int, help="这一趟最多跑几位")+ args = parser.parse_args(argv)+ if args.apply and not args.backup:+ raise SystemExit(BACKUP_REQUIRED)+ from peach.web_state import WebContract+ contract = WebContract(args.db)+ with open_readonly(args.db) as connection:+ items = pending(connection, contract.candidate_root, args.limit)+ print(f"待补资料的女优:{len(items)} 位")+ if not args.apply:+ for item in items[:10]:+ print(f" {item.label}")+ return 0+ sqlite_backup(Path(args.db), Path(args.backup))+ print(f"备份:{args.backup}")+ run_id = f"manual-{time.strftime('%Y%m%dT%H%M%S')}"+ tally = sweep(contract, items, run_id)+ print("结论:" + ",".join(f"{key} {value}" for key, value in tally.items()))+ print(f"批次:{profiles.SOURCE}@{run_id};判词:{contract.candidate_root / profiles.REVIEW_FILE}")+ with open_readonly(args.db) as connection:+ integrity, violations = verify_after_write(connection)+ print(f"integrity_check:{integrity};外键违规:{violations}")+ return 0 if integrity == "ok" and not violations else 1+++if __name__ == "__main__":+ raise SystemExit(main())diff --git a/scripts/test_runner.py b/scripts/test_runner.pyindex 224abecc0..777766785 100644--- a/scripts/test_runner.py+++ b/scripts/test_runner.py@@ -180,7 +180,8 @@ AUTO_SCOPE_FILES: dict[str, tuple[str, ...]] = {"src/peach/studio_icons.py": ("catalog", "metadata", "web"),"src/peach/studio_sites.py": ("catalog", "metadata"),"scripts/revert_auto_landing.py": ("catalog", "metadata", "tooling"),- # 补女优资料后继的解析与读写层,测试都在 `test_performer_profile_followup.py`。+ # 补女优资料后继的解析与读写层、一次补完的脚本,测试都在 `test_performer_profile_followup.py`。+ "scripts/run_performer_profiles.py": ("metadata",),"src/peach/avwikidb.py": ("metadata",),"src/peach/performer_profiles.py": ("metadata",),# 女优页头的视图:主体测试在 `test_performer_header.py`(metadata),资料接口与骨架diff --git a/tests/test_performer_profile_followup.py b/tests/test_performer_profile_followup.pyindex c81860c7a..03d5e4d48 100644--- a/tests/test_performer_profile_followup.py+++ b/tests/test_performer_profile_followup.py@@ -527,5 +527,53 @@ class SchedulingTests(Case):self.assertEqual(keys, [followup.followup_key(new), followup.followup_key(old)])+def load_sweep():+ spec = importlib.util.spec_from_file_location(+ "run_performer_profiles_under_test", ROOT / "scripts" / "run_performer_profiles.py")+ module = importlib.util.module_from_spec(spec)+ spec.loader.exec_module(module)+ return module+++class SweepTests(unittest.TestCase):+ """一次补完的脚本只挑人、记进度;每一位怎么补是后继自己的事,这里换成替身。"""++ def items(self, count):+ return [SimpleNamespace(key=followup.followup_key(index), label=f"女优{index}")+ for index in range(1, count + 1)]++ def test_consecutive_unfetched_stops_the_sweep_instead_of_stamping_everyone(self):+ sweep = load_sweep()+ with mock.patch.object(followup, "run", return_value={"outcome": followup.UNFETCHED}) as run:+ tally = sweep.sweep(None, self.items(20), "manual-test", out=io.StringIO(), stop_after=3)+ self.assertEqual(run.call_count, 3)+ self.assertEqual(tally["停在"], 3)++ def test_one_failure_is_hers_alone_and_the_sweep_goes_on(self):+ sweep = load_sweep()+ outcomes = [RuntimeError("坏页"), {"outcome": "minnano-av 写入"}, {"outcome": "资料未过期"}]+ with mock.patch.object(followup, "run", side_effect=outcomes):+ tally = sweep.sweep(None, self.items(3), "manual-test", out=io.StringIO())+ self.assertEqual(tally, {"出错:RuntimeError: 坏页": 1, "minnano-av 写入": 1, "资料未过期": 1})++ def test_the_batch_carries_the_manual_run_id(self):+ sweep = load_sweep()+ seen = []+ with mock.patch.object(followup, "run",+ side_effect=lambda _c, _k, handle: seen.append(handle.run_id) or {}):+ sweep.sweep(None, self.items(2), "manual-20260925T120000", out=io.StringIO())+ self.assertEqual(seen, ["manual-20260925T120000"] * 2)++ def test_apply_without_backup_is_refused_before_anything_runs(self):+ sweep = load_sweep()+ with mock.patch.object(sweep, "pending") as pending, \+ mock.patch.object(followup, "run") as run, \+ self.assertRaises(SystemExit) as refused:+ sweep.main(["--db", "unused.db", "--apply"])+ self.assertEqual(str(refused.exception), sweep.BACKUP_REQUIRED)+ pending.assert_not_called()+ run.assert_not_called()++if __name__ == "__main__":unittest.main()