Peach: Local-first video metadata scraper and media manager.
fix(metadata): 核对 FANZA 生写真特典的同号通常版
longmeidao4e1d246f3086
diff --git a/src/peach/javdb_identity.py b/src/peach/javdb_identity.pyindex 1f341cc96..61eecefa7 100644--- a/src/peach/javdb_identity.py+++ b/src/peach/javdb_identity.py@@ -6,6 +6,21 @@ import reRULE = 2+def fanza_regular_id(rows: list[dict]) -> str | None:+ """只识别原题仅相差 FANZA 生写真特典标记的两条发行记录。"""+ if len(rows) != 2:+ return None+ for special, regular in (rows, rows[::-1]):+ title = special.get('origin_title')+ ordinary = regular.get('origin_title')+ if not isinstance(title, str) or not isinstance(ordinary, str) or not ordinary:+ continue+ match = re.fullmatch(r'【FANZA限定】(.+) 生写真[1-9]\d?枚付き', title)+ if match and match[1] == ordinary and not ordinary.startswith('【FANZA限定】'):+ return regular['id']+ return None++def current_evidence(payload: object) -> bool:return (isinstance(payload, dict) and type(payload.get('javdb_rule')) is intand payload['javdb_rule'] == RULE)diff --git a/src/peach/javdb_public.py b/src/peach/javdb_public.pyindex f9d560a2e..4bafdda51 100644--- a/src/peach/javdb_public.py+++ b/src/peach/javdb_public.py@@ -448,8 +448,10 @@ class PublicAPI:"""番号对应的来源作品。来源去掉 MGS 的数字前缀(`259LUXU-891` 记作 `LUXU-891`),搜索词与身份核对都按 `release_identity` 认同一部作品。"""canonical = normalise_code_key(code)+ matches = []def identify():+ nonlocal matchesproduct = re.fullmatch(r'FC2-PPV-(\d+)', canonical)query = product[1] if product else release_identity(canonical) or canonicalrows = self._rows("/v2/search", "movies", {"q": query, "type": "movie", "page": 1})@@ -464,6 +466,15 @@ class PublicAPI:if source_id is None:identities = self._cached("movie-id:" + canonical, identify, refresh=refresh)+ if len(identities) == 2:+ # 旧快照保存的是 ID 列表;补取搜索原题后仍须核对两份详情。+ if not matches:+ identities = identify()+ self._remember('movie-id:' + canonical, identities)+ resolved = self._regular_edition(canonical, identities, matches, refresh=refresh)+ if resolved is not None:+ self._remember('movie-id:' + canonical, [resolved['id']])+ return resolvedif len(identities) != 1:raise PublicIdentityAmbiguous("同番号对应多个作品,请核对来源身份:" + '、'.join(identities))movie_id = identities[0]@@ -472,6 +483,31 @@ class PublicAPI:else:raise PublicSourceUnavailable("来源作品身份不匹配")+ return self._movie(canonical, movie_id, refresh=refresh)++ def _regular_edition(self, canonical, identities, matches, *, refresh):+ from .javdb_identity import fanza_regular_id+ rows = list({row['id']: row for row in matches}.values())+ regular = fanza_regular_id(rows)+ if regular is None or set(identities) != {row['id'] for row in rows}:+ return None+ movies = [self._movie(canonical, identity, refresh=refresh) for identity in identities]+ if fanza_regular_id(movies) != regular:+ return None+ performers = [{actor['id'] for actor in movie.get('actors') or []+ if isinstance(actor, dict) and isinstance(actor.get('id'), str)+ and actor_gender(actor) == 'female'} for movie in movies]+ duration = movies[0].get('duration')+ if (not performers[0] or performers[0] != performers[1]+ or type(duration) is not int or duration <= 0 or movies[1].get('duration') != duration):+ return None+ series = [movie.get('series_id') for movie in movies]+ if all(series) and series[0] != series[1]:+ return None+ return next(movie for movie in movies if movie['id'] == regular)++ def _movie(self, canonical, movie_id, *, refresh):+def fetch():movie = self._json("/v4/movies/" + movie_id).get("movie")if (not isinstance(movie, dict) or not same_release_code(canonical, movie.get("number"))diff --git a/src/peach/sources/javdb.py b/src/peach/sources/javdb.pyindex b5a3552f6..5d82a5a67 100644--- a/src/peach/sources/javdb.py+++ b/src/peach/sources/javdb.py@@ -37,8 +37,8 @@ JAVDB = SiteConfig(name="javdb", label="javdb", provider="javdb-page",#: 冷却按这个名字记);解析器名另起,候选与账本证据分得清是哪一种取法。接口自己的间隔、冷却与#: 请求预算在 `peach.javdb_public`,这一问不经会话的传输。JAVDB_PUBLIC = dataclasses.replace(JAVDB, provider="javdb-api")-#: 一问的请求上限:签名常量两次(一天一次)、搜索、详情,再留两次给换接口主机(ADR-0148)。-PUBLIC_REQUESTS = 6+#: 一问的请求上限:签名常量两次(一天一次)、搜索、最多两份发行详情,再留两次换接口主机。+PUBLIC_REQUESTS = 7#: 会话没有动作截止时刻时,一问最多等这么久。PUBLIC_SECONDS = 90.0diff --git a/tests/test_javdb_public.py b/tests/test_javdb_public.pyindex 129d24e7e..cd8630459 100644--- a/tests/test_javdb_public.py+++ b/tests/test_javdb_public.py@@ -24,6 +24,56 @@ from support.ledger import fresh_ledgerclass PublicProtocolTests(unittest.TestCase):+ @staticmethod+ def editions():+ common = {'number': 'EXM-001', 'duration': 120, 'series_id': 'Series',+ 'actors': [{'id': 'Actor', 'gender': 0}]}+ return [{**common, 'id': 'Special', 'origin_title': '【FANZA限定】サンプル作品 生写真3枚付き'},+ {**common, 'id': 'Regular', 'origin_title': 'サンプル作品'}]++ def test_fanza_photo_bonus_uses_the_regular_edition_after_detail_validation_in_both_orders(self):+ for rows in (self.editions(), self.editions()[::-1]):+ api = PublicAPI(sleeper=lambda _: None)+ details = {row['id']: {'movie': row} for row in rows}+ with patch.object(api, '_rows', return_value=rows), patch.object(+ api, '_json', side_effect=lambda path, details=details: details[path.rsplit('/', 1)[-1]]) as detail:+ movie = api.movie('EXM-001')+ self.assertEqual(movie['id'], 'Regular')+ self.assertEqual(api.movie('EXM-001'), movie)+ self.assertEqual(detail.call_count, 2)++ def test_fanza_missing_or_conflicting_work_evidence_keeps_the_identity_ambiguous(self):+ conflicts = ({'origin_title': '別の作品'}, {'duration': 121}, {'duration': None},+ {'actors': []}, {'actors': [{'id': 'Other', 'gender': 0}]},+ {'series_id': 'Other'}, {'origin_title': '【FANZA限定】サンプル作品 未知の特典'})+ for conflict in conflicts:+ with self.subTest(conflict=conflict):+ rows = self.editions()+ details = [dict(rows[0]), {**rows[1], **conflict}]+ api = PublicAPI(sleeper=lambda _: None)+ with patch.object(api, '_rows', return_value=rows), patch.object(+ api, '_json', side_effect=lambda path, details=details: {'movie': next(+ movie for movie in details if movie['id'] == path.rsplit('/', 1)[-1])}):+ with self.assertRaisesRegex(PublicSourceUnavailable, '多个作品'):+ api.movie('EXM-001')++ def test_cached_ambiguous_ids_can_be_resolved_without_invalidating_other_movies(self):+ from peach.javdb_identity import cache_key+ from peach.jav_cache import PublicSnapshots+ with tempfile.TemporaryDirectory() as temporary:+ root = Path(temporary).resolve()+ snapshots = PublicSnapshots(root)+ snapshots.save(cache_key('movie-id:EXM-001'), ['Special', 'Regular'], None)+ other = snapshots.save(cache_key('movie-id:EXM-002'), ['Other'], None)+ rows = self.editions()+ api = PublicAPI(cache_root=root, sleeper=lambda _: None)+ with patch.object(api, '_rows', return_value=rows), patch.object(+ api, '_json', side_effect=lambda path: {'movie': next(+ movie for movie in rows if movie['id'] == path.rsplit('/', 1)[-1])}):+ self.assertEqual(api.movie('EXM-001')['id'], 'Regular')+ self.assertEqual(snapshots.load(cache_key('movie-id:EXM-001')).value, ['Regular'])+ self.assertEqual(snapshots.load(cache_key('movie-id:EXM-002')), other)+def test_same_number_source_ids_are_ambiguous_in_both_orders(self):rows = [{'id': 'XeYkdb', 'number': 'MIKR-023', 'maker_name': 'MOODYZ', 'release_date': '2025-06-17','duration': 120, 'actors': [{'id': 'ActorA', 'name': '甲'}]},