Skip to content

armgit / peach

Public Imported from GitHub
0

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

fix: 重问所属带上日文别名,并让连接抖动可重试

longmeidao43441501ac69
diff --git a/scripts/resync_performer_agency.py b/scripts/resync_performer_agency.py
index d34271ca8..13f75a505 100644
--- a/scripts/resync_performer_agency.py
+++ b/scripts/resync_performer_agency.py
@@ -26,6 +26,7 @@ from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
from peach.config import REVIEW_DIR, STATE_DIR # noqa: E402
+from peach.entities import name_chain # noqa: E402
from peach.http import HttpRequest, HttpxTransport # noqa: E402
from peach.jobs import job_main # noqa: E402
from peach.minnano_av import actress_id, profile_text, search_url # noqa: E402
@@ -50,12 +51,25 @@ EXTRA_COUNTS = {
def targets(connection, agencies: list[str], only: list[str]) -> list[dict[str, object]]:
- """要问的人:(entity_id, 规范名, 账本里现在写着的事务所)。
+ """要问的人:id、规范名、账本里现在写着的事务所,以及可以拿去检索的名字链。
两个条件是并集而不是交集:`--agency LIGHT --only 北野未奈` 要问的是这一家加上
这个人,而不是「这一家里叫这个名字的」——移籍时人已经不在原来那一家名下了。
+
+ 检索必须带上别名。账本里 performer 的规范名是简体中文(`宫西光`),minnano-av
+ 只认日文写法(`宮西ひかる`),只拿规范名去问,14 位里 11 位落在「未取得」,
+ 看起来像是站点查不到这些人,实际上是我们从没用她的日文名查过。
"""
found: dict[int, dict[str, object]] = {}
+
+ def take(row) -> None:
+ aliases = [item["alias"] for item in connection.execute(
+ "SELECT alias FROM entity_alias WHERE entity_id=?"
+ " ORDER BY confidence DESC, alias", (row["id"],))]
+ found[row["id"]] = {"entity_id": row["id"], "performer": row["canonical_name"],
+ "current_agency": row["agency"] or "",
+ "chain": name_chain(row["canonical_name"], aliases)}
+
for name in agencies:
for row in connection.execute(
"SELECT e.id, e.canonical_name,"
@@ -63,37 +77,46 @@ def targets(connection, agencies: list[str], only: list[str]) -> list[dict[str,
" FROM entity e JOIN entity_membership m ON m.member_id=e.id"
" JOIN entity a ON a.id=m.agency_id"
" WHERE a.kind='agency' AND a.canonical_name=?", (name,)):
- found[row["id"]] = {"entity_id": row["id"], "performer": row["canonical_name"],
- "current_agency": row["agency"] or ""}
+ take(row)
for name in only:
for row in connection.execute(
"SELECT id, canonical_name,"
" json_extract(metadata_json,'$.agency.name') AS agency"
" FROM entity WHERE kind='performer' AND canonical_name=?", (name,)):
- found[row["id"]] = {"entity_id": row["id"], "performer": row["canonical_name"],
- "current_agency": row["agency"] or ""}
+ take(row)
return [found[key] for key in sorted(found)]
-def ask(http, name: str, timeout: float) -> tuple[str, str, str]:
+def ask(http, name: str, timeout: float,
+ tries: int = 3, pause: float = 3.0) -> tuple[str, str, str]:
"""问站点:这个人现在的「所属事務所」写的是什么。返回(事务所, actress_id, 说明)。
只采信重定向。检索页正文里那一堆 `actressNNN.html` 是「相关女优」,按正文解析
会把别人的事务所安到这个人头上。
+
+ 连接层抖一下要重试。2026-09-05 这一趟 14 位里有一位撞上 `SSL: UNEXPECTED_EOF`,
+ 写出来的那一行和「站点查不到这个人」长得一模一样,而它只是这一秒没连上。回了
+ HTTP 状态或者检索页没重定向则不重试:那两种再问一遍还是同一个答案。
"""
- try:
- response = http(HttpRequest("GET", search_url(name), {"User-Agent": USER_AGENT}),
- timeout, 4 << 20)
- except Exception as error: # noqa: BLE001 取不到就是取不到,写进复核件等重跑
- return "", "", f"未取得:{type(error).__name__}"
- if response.status != 200:
- return "", "", f"未取得:HTTP {response.status}"
- found_id = actress_id(response.url or "")
- if not found_id:
- return "", "", "未取得:检索页未唯一命中,需人工消歧"
- html = response.body.decode("utf-8", "replace")
- return (profile_text(html, "所属事務所"), found_id,
- f"minnano-av actress{found_id} 资料表「所属事務所」")
+ note = "未取得:一次都没问出去"
+ for attempt in range(max(1, tries)):
+ try:
+ response = http(HttpRequest("GET", search_url(name), {"User-Agent": USER_AGENT}),
+ timeout, 4 << 20)
+ except Exception as error: # noqa: BLE001 传输层什么都可能抛,一律当抖动
+ note = f"未取得:{type(error).__name__}"
+ if attempt + 1 < max(1, tries):
+ time.sleep(pause)
+ continue
+ if response.status != 200:
+ return "", "", f"未取得:HTTP {response.status}"
+ found_id = actress_id(response.url or "")
+ if not found_id:
+ return "", "", "未取得:检索页未唯一命中,需人工消歧"
+ html = response.body.decode("utf-8", "replace")
+ return (profile_text(html, "所属事務所"), found_id,
+ f"minnano-av actress{found_id} 资料表「所属事務所」")
+ return "", "", note
def plan(connection, http, args) -> list[dict[str, object]]:
@@ -101,12 +124,22 @@ def plan(connection, http, args) -> list[dict[str, object]]:
people = targets(connection, args.agency, args.only)
print(f"待问 {len(people)} 位 performer")
last = 0.0
- for person in people:
+
+ def paced(name: str) -> tuple[str, str, str]:
+ """间隔按请求算,不按人算:一个人有两个名字就是两次往返。"""
+ nonlocal last
wait = args.interval - (time.monotonic() - last)
if wait > 0:
time.sleep(wait)
last = time.monotonic()
- shown, _, note = ask(http, str(person["performer"]), args.timeout)
+ return ask(http, name, args.timeout)
+
+ for person in people:
+ shown, note = "", "未取得:没有可检索的名字"
+ for candidate in person["chain"]:
+ shown, _, note = paced(candidate)
+ if note.startswith("minnano-av"):
+ break
if not note.startswith("minnano-av"):
verdict = MISSING
elif not shown:
@@ -115,7 +148,8 @@ def plan(connection, http, args) -> list[dict[str, object]]:
verdict = SAME
else:
verdict = MOVED
- rows.append({**person, "site_agency": shown, "verdict": verdict, "evidence": note})
+ rows.append({**{key: value for key, value in person.items() if key != "chain"},
+ "site_agency": shown, "verdict": verdict, "evidence": note})
print(f"{str(person['performer'])[:12]:<12} {verdict} {shown}")
return rows
diff --git a/tests/test_performer_agency_resync.py b/tests/test_performer_agency_resync.py
index 8ba6ad4ad..07a307393 100644
--- a/tests/test_performer_agency_resync.py
+++ b/tests/test_performer_agency_resync.py
@@ -11,7 +11,7 @@ from pathlib import Path
from peach.entities import normalize_entity_name
from peach.migrations import upgrade
from scripts.resync_performer_agency import (
- GONE, MISSING, MOVED, SAME, apply_rows, ask, plan, targets,
+ GONE, MISSING, MOVED, SAME, apply_rows, ask, plan, search_url, targets,
)
ROOT = Path(__file__).resolve().parents[1]
@@ -52,7 +52,6 @@ class AskTests(unittest.TestCase):
"""站点那一问只有三种结果:命中、没这一格、取不到。"""
def page(self, shown: str) -> Http:
- from scripts.resync_performer_agency import search_url
return Http({search_url("松本一香"): Response(
200, "https://www.minnano-av.com/actress21661.html", profile_html(shown))})
@@ -74,10 +73,35 @@ class AskTests(unittest.TestCase):
def __call__(self, *args):
raise TimeoutError("connect")
- shown, _, note = ask(Broken(), "松本一香", 1.0)
+ shown, _, note = ask(Broken(), "松本一香", 1.0, tries=1)
self.assertEqual(shown, "")
self.assertIn("未取得", note)
+ def test_a_blip_on_the_first_try_does_not_become_a_verdict(self):
+ """一次 SSL EOF 写出来的行,和「站点查不到这个人」长得一模一样。"""
+ page = self.page("EST(エスト)旧・LIGHT")
+
+ class Flaky:
+ def __init__(self):
+ self.calls = 0
+
+ def __call__(self, request, timeout, limit):
+ self.calls += 1
+ if self.calls == 1:
+ raise ConnectionError("SSL: UNEXPECTED_EOF_WHILE_READING")
+ return page(request, timeout, limit)
+
+ flaky = Flaky()
+ shown, _, note = ask(flaky, "松本一香", 1.0, pause=0.0)
+ self.assertEqual(shown, "EST(エスト)旧・LIGHT")
+ self.assertEqual(flaky.calls, 2)
+
+ def test_a_search_page_that_never_redirects_is_not_retried(self):
+ """再问一遍还是同一个答案,重试只是多敲别人的门。"""
+ http = self.page("EST")
+ ask(http, "查无此人", 1.0, pause=0.0)
+ self.assertEqual(len(http.asked), 1)
+
class LedgerFixture(unittest.TestCase):
"""一家事务所、三位成员,外加一位没有归属的人。"""
@@ -129,6 +153,14 @@ class TargetTests(LedgerFixture):
def test_the_same_person_named_twice_is_asked_once(self):
self.assertEqual(len(targets(self.con, ["LIGHT"], ["松本一香"])), 3)
+ def test_the_japanese_alias_leads_the_search_chain(self):
+ """站点只认日文写法,规范名是简体中文;不带别名去问就查不到这个人。"""
+ self.con.execute(
+ "INSERT INTO entity_alias(entity_id,alias,normalized_alias,source)"
+ " VALUES(11,'松本いちか',?,'test')", (normalize_entity_name("松本いちか"),))
+ found = targets(self.con, [], ["松本一香"])
+ self.assertEqual(found[0]["chain"], ["松本いちか", "松本一香"])
+
class PlanTests(LedgerFixture):
@@ -137,7 +169,6 @@ class PlanTests(LedgerFixture):
return types.SimpleNamespace(**{**base, **overrides})
def http(self, answers: dict[str, str]) -> Http:
- from scripts.resync_performer_agency import search_url
return Http({search_url(name): Response(
200, f"https://www.minnano-av.com/actress{2000 + index}.html",
profile_html(shown))
@@ -159,6 +190,28 @@ class PlanTests(LedgerFixture):
rows = self.run_plan({})
self.assertEqual({str(row["verdict"]) for row in rows}, {MISSING})
+ def with_alias(self):
+ self.con.execute(
+ "INSERT INTO entity_alias(entity_id,alias,normalized_alias,source)"
+ " VALUES(11,'松本いちか',?,'test')", (normalize_entity_name("松本いちか"),))
+
+ def test_the_chain_stops_at_the_first_name_that_lands(self):
+ """日文名排在链首,它命中了就不该再为简体名多敲一次门。"""
+ self.with_alias()
+ http = self.http({"松本いちか": "EST(エスト)旧・LIGHT"})
+ with contextlib.redirect_stdout(io.StringIO()):
+ rows = plan(self.con, http, self.args(agency=[], only=["松本一香"]))
+ self.assertEqual(str(rows[0]["verdict"]), MOVED)
+ self.assertEqual(http.asked, [search_url("松本いちか")])
+
+ def test_a_miss_on_the_first_name_tries_the_next(self):
+ self.with_alias()
+ http = self.http({"松本一香": "EST(エスト)旧・LIGHT"})
+ with contextlib.redirect_stdout(io.StringIO()):
+ rows = plan(self.con, http, self.args(agency=[], only=["松本一香"]))
+ self.assertEqual(str(rows[0]["verdict"]), MOVED)
+ self.assertEqual(len(http.asked), 2)
+
class ApplyTests(LedgerFixture):