Skip to content

armgit / peach

Public Imported from GitHub
0

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

feat(review): 补空自动落库放开到 community 源,official 已确认的值不再被 community 挑战

longmeidao1562f23b933a
diff --git a/src/peach/web_review.py b/src/peach/web_review.py
index 31cdca649..13f2b291d 100644
--- a/src/peach/web_review.py
+++ b/src/peach/web_review.py
@@ -354,10 +354,11 @@ def _review_rows(contract: ReviewContract, category: str) -> tuple[list[dict], s
rows = [row for row in rows if _metadata_row_adds_information(connection, row)]
# 韩国 MIB 的番号不适用 JAV 规则,`allows_code` 已经拦在刮削入口。但候选件是
# 历史产物,闸门只管以后不再生成,管不了已经落盘的那些:2026-09-04 实测队列里
- # 还有 49 条(AR 39、JI 10,覆盖 title/studio/series/performers/release_date)。
+ # 还有 214 条(title 51、studio 51、release_date 51、performers 39、series 22)。
# 这些值全是 JAV 目录站按错番号返回的别的作品,没有一条值得占用人的注意力。
rows = [row for row in rows
if not is_korean_mib_code(str(row.get("code") or ""))]
+ rows = _drop_community_challenges_to_official(connection, rows)
elif category == "performer_avatars":
# 候选 CSV 里的 `current_name` 是抓取来源给的罗马音;账本早就有更好的
# 规范名(`Alice Shaku` 的规范名是 `释爱丽丝`),罗马音本身也已经登记
@@ -481,6 +482,54 @@ def _metadata_row_adds_information(connection, row: dict) -> bool:
return any(str(c.get("display_value") or "").strip() != current for c in candidates)
+def _drop_community_challenges_to_official(connection, rows: list[dict]) -> list[dict]:
+ """community 源推不翻 official 源已确认的值,这种行不该进队列。
+
+ 用户 2026-09-04 定的口径:按官方来。实测 26 条发行日期「冲突」里,账本现值全部由
+ official 源写入(r18dev 10、aventertainment 9、libredmm 1),挑战方无一例外是
+ javbus。它们不是「两个日期二选一」,而是 community 源要覆盖 official 源——项目自己的
+ `SOURCE_SPECS` 早就排好了序,让人再判一遍等于把已经定好的信任模型丢回给人。
+
+ 判据只认账本里留下的落库记录:`review_decision` 的 note 记着当初是哪个来源写的。
+ 没有记录的行照常进队列——那说明现值来路不明(早期导入、文件名推断),community
+ 源的异议就有意义,拦掉它才是真的丢信息。
+ """
+ keyed = {str(row.get("item_key") or ""): row for row in rows
+ if str(row.get("current_value") or "").strip()}
+ if not keyed:
+ return rows
+ official_values: dict[str, str] = {}
+ marks = ",".join("?" * len(keyed))
+ for item_key, note in connection.execute(
+ f"SELECT item_key,note FROM review_decision WHERE category='metadata_fields' "
+ f"AND status='approved' AND item_key IN ({marks})", list(keyed)):
+ try:
+ parsed = json.loads(str(note or "{}"))
+ except (TypeError, ValueError):
+ continue
+ spec = SOURCE_SPECS.get(str(parsed.get("source") or "").strip())
+ if spec is not None and spec.official:
+ official_values[str(item_key)] = str(parsed.get("value") or "")
+ if not official_values:
+ return rows
+
+ def keep(row: dict) -> bool:
+ item_key = str(row.get("item_key") or "")
+ if item_key not in official_values:
+ return True
+ # 现值被别的动作改过就不能再算「official 写的那一个」,交回人工。
+ recorded = official_values[item_key].strip()
+ if recorded and recorded != str(row.get("current_value") or "").strip():
+ return True
+ return any(
+ (spec := SOURCE_SPECS.get(str(c.get("source") or "").strip())) is not None
+ and spec.official
+ for c in row.get("candidates") or []
+ )
+
+ return [row for row in rows if keep(row)]
+
+
def _use_canonical_entity_names(connection, rows: list[dict]) -> None:
"""把候选行的显示名换成账本规范名,来源写法留在 `source_name`。"""
ids = [int(row["entity_id"]) for row in rows
@@ -504,22 +553,37 @@ def _use_canonical_entity_names(connection, rows: list[dict]) -> None:
AUTO_APPLY_FIELDS = frozenset({"release_date"})
+def _auto_apply_rule(candidate: dict) -> str:
+ """这条自动落库该记在哪条规则名下。
+
+ official 与 community 两类补空在 `review_decision` 里必须分得开:出了问题要回溯的
+ 是「哪些值是 community 源补的」,而 note 是唯一留着这个区别的地方。
+ """
+ spec = SOURCE_SPECS.get(str(candidate.get("source") or "").strip())
+ kind = "official" if spec is not None and spec.official else "community"
+ return f"adr-0018-empty-field-single-{kind}-source"
+
+
def metadata_auto_apply_candidate(connection, row: dict) -> dict | None:
"""这一行能否不经复核直接落库;不能就返回 None。
- 四项必须同时成立,缺一项就仍然走人工:
+ 三项必须同时成立,缺一项就仍然走人工:
1. 目标字段当前为空——只补空,永不覆盖既有真相字段;
2. 只有一个候选——有第二个值就存在取舍,那正是复核要做的事;
- 3. 来源在当前 policy 下是 official / official_mirror;
- 4. 番号在该番号名下**每一条**资产的文件名里逐字出现。
+ 3. 番号在该番号名下**每一条**资产的文件名里逐字出现。
- 第 4 条是这条捷径唯一的身份保证。刮削按番号取值,番号错则值错;文件名里
+ 来源是不是 official 不在其中(用户 2026-09-04 决定)。补空不覆盖任何东西,唯一的
+ 风险是「这个值属不属于这部片」,而那由第 3 条管,与来源可信度无关。卡住 official
+ 这条的代价是实测 76 条 javbus 补空候选全部滞留人工,它们补的都是账本里空着的发行
+ 日期——没有可判断项,却要人逐条点过。落库时按来源实际级别记规则名,回溯得出来。
+
+ 第 3 条是这条捷径唯一的身份保证。刮削按番号取值,番号错则值错;文件名里
逐字出现是本机可核验的证据,而复核界面其实给不了这个保证——它只并排显示
番号和日期,并不告诉你番号跟这个文件对不对得上。
- `official` 一律按当前 policy 解析,不读候选 CSV 里的同名字段:那是抓取当时
- 的快照,实测 r18dev 在 CSV 里写着 False,而现行 policy 认它是 official_mirror。
+ 来源级别一律按当前 policy 解析,不读候选 CSV 里的同名字段:那是抓取当时的
+ 快照,实测 r18dev 在 CSV 里写着 False,而现行 policy 认它是 official_mirror。
"""
if str(row.get("field") or "").strip() not in AUTO_APPLY_FIELDS:
return None
@@ -529,8 +593,7 @@ def metadata_auto_apply_candidate(connection, row: dict) -> dict | None:
if len(candidates) != 1:
return None
candidate = candidates[0]
- spec = SOURCE_SPECS.get(str(candidate.get("source") or "").strip())
- if spec is None or not spec.official:
+ if str(candidate.get("source") or "").strip() not in SOURCE_SPECS:
return None
code = str(row.get("code") or "").strip()
query = str(row.get("query") or code).strip()
@@ -1023,7 +1086,10 @@ def w_review_auto_apply(contract: ReviewContract, _body=None):
"ON CONFLICT(category,item_key) DO UPDATE SET status=excluded.status,"
"note=excluded.note,updated_at=excluded.updated_at",
(item_key, json.dumps({
- "auto_applied": True, "rule": "adr-0018-empty-field-single-official-source",
+ "auto_applied": True,
+ # 规则名记来源的实际级别,不写死 official:补空对 community 源同样
+ # 成立,但两者日后要分开回溯时,note 是唯一还留着这个区别的地方。
+ "rule": _auto_apply_rule(candidate),
"candidate_key": candidate.get("candidate_key"),
"source": candidate.get("source"),
"value": candidate.get("display_value"),
diff --git a/tests/test_web_review.py b/tests/test_web_review.py
index 787ded7c4..3403eb22c 100644
--- a/tests/test_web_review.py
+++ b/tests/test_web_review.py
@@ -225,7 +225,12 @@ class ReviewQueueTests(unittest.TestCase):
self.assertEqual(self._auto()["applied"], 0)
self.assertEqual(sorted(self.queue_keys("metadata_fields")), ["AAA", "BBB"])
- def test_auto_apply_ignores_community_sources_and_other_fields(self):
+ def test_auto_apply_fills_empty_fields_from_community_sources_too(self):
+ """补空不覆盖任何东西,唯一的风险由「番号在文件名里」那条管,与来源级别无关。
+
+ 卡住 official 的代价是实测 76 条 javbus 补空候选全部滞留人工,补的都是账本里
+ 空着的发行日期——没有可判断项,却要人逐条点过。白名单之外的字段仍然不走这条路。
+ """
self._asset(94, "CCC-3", "CCC-3.mp4")
self._asset(95, "DDD-4", "DDD-4.mp4")
self.write_metadata_rows([
@@ -235,7 +240,48 @@ class ReviewQueueTests(unittest.TestCase):
{"item_key": "DDD", "field": "performers", "current": "",
"candidates": ["某人"], "code": "DDD-4"},
])
- self.assertEqual(self._auto()["applied"], 0)
+ self.assertEqual(self._auto()["applied"], 1)
+ con = sqlite3.connect(self.db_path)
+ try:
+ self.assertEqual(
+ con.execute("SELECT release_date FROM asset WHERE id=94").fetchone()[0],
+ "2015-02-20")
+ note = con.execute(
+ "SELECT note FROM review_decision WHERE item_key='CCC'").fetchone()[0]
+ finally:
+ con.close()
+ # 规则名要留下来源级别,否则日后回溯不出哪些值是 community 源补的。
+ self.assertEqual(json.loads(note)["rule"],
+ "adr-0018-empty-field-single-community-source")
+
+ def test_community_candidate_never_challenges_an_official_written_value(self):
+ """按官方来:community 源推不翻 official 源已确认的值,这种行不进队列。
+
+ 实测 26 条发行日期「冲突」里,账本现值全部由 official 源写入(r18dev 10、
+ aventertainment 9、libredmm 1),挑战方无一例外是 javbus。让人再判一遍等于把
+ `SOURCE_SPECS` 早就排好的信任模型丢回给人。
+ """
+ self._asset(96, "EEE-5", "EEE-5.mp4")
+ self._asset(97, "FFF-6", "FFF-6.mp4")
+ con = sqlite3.connect(self.db_path)
+ try:
+ con.execute(
+ "INSERT INTO review_decision(category,item_key,status,note,updated_at) "
+ "VALUES('metadata_fields','EEE','approved',?,'2026-08-30T00:00:00Z')",
+ (json.dumps({"auto_applied": True, "source": "r18dev",
+ "value": "2015-05-30"}),))
+ con.commit()
+ finally:
+ con.close()
+ self.write_metadata_rows([
+ # 现值由 r18dev 写入,javbus 想改成别的日期:按信任模型直接不进队列。
+ {"item_key": "EEE", "field": "release_date", "current": "2015-05-30",
+ "candidates": ["2015-08-30"], "code": "EEE-5", "source": "javbus"},
+ # 现值来路不明(没有落库记录)时,community 的异议仍然有意义。
+ {"item_key": "FFF", "field": "release_date", "current": "2014-01-01",
+ "candidates": ["2014-03-03"], "code": "FFF-6", "source": "javbus"},
+ ])
+ self.assertEqual(self.queue_keys("metadata_fields"), ["FFF"])
def test_metadata_candidates_that_repeat_the_current_value_never_queue(self):
"""复核的成本是注意力:和现值一模一样的行会把真正要判的淹掉。