Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

fix(follow): rule34.xxx 补齐脚本带上账号凭据

longmeidao2389fb952e44
diff --git a/scripts/backfill_rule34_details.py b/scripts/backfill_rule34_details.py
index 568d71ded..432aefe46 100644
--- a/scripts/backfill_rule34_details.py
+++ b/scripts/backfill_rule34_details.py
@@ -39,7 +39,8 @@ SRC_DIR = PROJECT_ROOT / "src"
if str(SRC_DIR) not in sys.path:
sys.path.insert(0, str(SRC_DIR))
-from peach.config import GENERATED_DIR
+from peach.config import GENERATED_DIR, SECRETS_DIR, SHARED_CREDENTIAL_ROOT
+from peach.follow_secrets import CredentialError, credential_store_for
from peach.follow_sources import FollowSourceError, build_connector
from peach.review_csv import read_rows, write_rows
from peach.scripting import (
@@ -227,6 +228,19 @@ def run(args: argparse.Namespace) -> int:
print(f"rule34xxx 共 {total} 条,已写入 {len(done)} 条,本趟待问 {len(rows)} 条", flush=True)
if not rows:
return 0
+ # dapi 要账号自己的 user_id + api_key;只读文件头不需要。缺凭据在备份与写入之前就停。
+ credential = None
+ if not args.duration_only:
+ try:
+ credential = credential_store_for(
+ SECRETS_DIR, shared_root=SHARED_CREDENTIAL_ROOT).load("rule34xxx")
+ except CredentialError as error:
+ print(f"rule34xxx 凭据不可用:{error}", file=sys.stderr)
+ return 2
+ if credential is None:
+ print("rule34xxx 需要 user_id 与 api_key;请把它们写进 "
+ "peach-data/secrets/follow/rule34xxx.json", file=sys.stderr)
+ return 2
writer: sqlite3.Connection | None = None
before = 0
@@ -238,7 +252,7 @@ def run(args: argparse.Namespace) -> int:
print(f"备份:{args.backup}", flush=True)
before = writer.execute("SELECT COUNT(*) FROM follow_item").fetchone()[0]
- connector = build_connector("rule34xxx")
+ connector = build_connector("rule34xxx", credential=credential)
log: list[dict] = []
staged: list[tuple[int, dict]] = []
staged_logs: list[dict] = []
diff --git a/tests/test_scripts.py b/tests/test_scripts.py
index 8b40736b7..7bbca59c4 100644
--- a/tests/test_scripts.py
+++ b/tests/test_scripts.py
@@ -1778,6 +1778,24 @@ class OperationalScriptTests(unittest.TestCase):
self.assertEqual(json.loads(rows[2]), {"tags": []})
self.assertEqual(backfill.settled_ids(out), {1})
+ def test_rule34_backfill_without_credentials_stops_before_the_backup(self):
+ """分类与上传时间要问 dapi,dapi 要账号凭据;缺凭据就在备份与写入之前停。"""
+ backfill = load_script("backfill_rule34_details")
+ database = self.tmp_ledger()
+ backup = database.parent / "b.db"
+ store = mock.Mock()
+ store.load.return_value = None
+ args = backfill.build_parser().parse_args(
+ ["--db", str(database), "--apply", "--backup", str(backup),
+ "--out", str(database.parent / "r.csv"), "--delay", "0"])
+ with mock.patch.object(backfill, "credential_store_for", return_value=store), \
+ mock.patch.object(backfill, "build_connector") as build, \
+ redirect_stdout(io.StringIO()), redirect_stderr(io.StringIO()):
+ self.assertEqual(backfill.run(args), 2)
+ store.load.assert_called_once_with("rule34xxx")
+ build.assert_not_called()
+ self.assertFalse(backup.exists())
+
def test_follow_image_dims_backfill_reads_archives_first_and_probes_headers_for_the_rest(self):
"""图片墙的比例占位要每张图都有宽高;存量行里只有 fanbox 记过。