Peach: Local-first video metadata scraper and media manager.
fix(follow): 没写出处的连发帖并进同批有出处的那组,连号放宽到 100
longmeidao64e29937933d
diff --git a/docs/adr/0058-follow-bursts-anchor-on-declared-origins.md b/docs/adr/0058-follow-bursts-anchor-on-declared-origins.mdnew file mode 100644index 000000000..0aeac3cb3--- /dev/null+++ b/docs/adr/0058-follow-bursts-anchor-on-declared-origins.md@@ -0,0 +1,71 @@+# ADR-0058:连发帖挂到同批有出处的那组,连发与连号都看站内 id++- 状态:Accepted+- 日期:2026-09-24+- 修订:ADR-0046 决策(只处理无出处条目、身份只看作品与角色标签)、ADR-0045 决策第三条(id 间隔)++## 背景++关注页上有三处同一作者、同一天的卡片没合成一张,逐条查了 ledger:++| 样本 | 卡在哪一条 | 实际数值 |+| --- | --- | --- |+| InitialA 在 rule34.paheal 的 Lily Artemis II | 连发判据只收无出处帖;paheal 不给标签分类,身份集合为空 | 同批 9 帖,8 帖出处是 subscribestar 2685233,最后一帖没写,晚 25 秒 |+| LazyProcrastinator 在 rule34video 的 Barney's Mom | 连号间隔超过 30 | `Riding` 4608531 与下一条 4608595 相差 64;时长同为 20 秒 |+| Rekin3D 在 rule34.xxx 的两帖 | 身份多一个 `horseguy`;一帖有出处 | 站上上传是 05-01 与 05-20,id 相差 198043 |++第三个样本不是漏合并。两帖落库时间都是 09-14 03:0x,是 dapi 的 `change`(最后修改时间):+09-14 有人一起改了这两帖的标签。上传时间取详情页 Posted 的修正(a6ff2138)之前落库、+已经补齐过分类的行不会再进第二阶段,`published_at` 就一直是改标签那一刻。++这也说明 ADR-0046 只看时间不够:一批旧帖被一起改标签,几个月前各自上传的帖子就成了+「几分钟内连发」。++## 决策++连发判据(`follow_store._align_tag_bursts`):++1. 有出处或与别的条目共用 `group_hint` 的帖子参与判断,但只当锚,键不动。没出处的一串+ 帖子前后 3 小时内、按同样的身份与重合度能对上的锚全在同一组时,这一串归进那一组;+ 对上两组以上,只在这一串里互相归。+2. 这一步排到 `_align_by_group_hint` 之后,锚所在的那组已经共用一个键。+3. 相邻两帖的站内 id 相差不超过 3000,锚与帖之间同样。id 不是纯数字的站只看时间。+4. `tag_types` 里全是 general 的站(rule34.paheal)身份取整组标签(小写),整组相同才算。++连号判据(`follow_store._align_upload_packs`):相邻两条的 id 间隔从 30 放到 100,其余+条件不变。++## 依据++2026-09-24 在真实 ledger 上只读试算,16019 条可见条目,分组数 11745:++| 判据 | 新并出的组 | 并入条目 | 拆开的旧组 | 抽查结论 |+| --- | --- | --- | --- | --- |+| 连发挂锚,只看时间 | 16 | 23 | 0 | 8 组 id 相差 2 万到 97 万;取得上传时间的 7 组全部相隔 4 天到 3 个月 |+| **连发挂锚,加 id 上限 3000(采用)** | 8 | 14 | 1 | 8 组都是同一批;拆开的那组 id 差 776581,站上 02-14 与 04-24 上传 |+| **再把连号间隔放到 100(采用)** | 再加 2 | 再加 2 | 0 | Barney's Mom 一包、Taka84 同一片的三个字幕版 |++最终分组数 11730,按来源:rule34.xxx 5 组、rule34.paheal 2 组、rule34video 2 组、+混合组 1 组(主条目在 pawchive)。逐组记录、缩略图地址与判断在主检出+`build/fgm-audit.csv`。++id 上限的取值:现有连发串的 349 对相邻帖里,347 对 id 差不超过 1000,一对 1383(3 分钟内+的同一批图),一对 776581(即上面拆开的那组)。rule34.xxx 实测约每天 1 万个新 id,3000 大致+是 7 小时,比 3 小时的时间窗宽,只拦改标签造成的假连发。++连号间隔的取值:除 id 外其他条件都满足的相邻对,id 差 44、48、64 的全在同一天;往上+第一对是 263,已经跨天(`Ashley (black, full ver.)` 与 `(white, full ver.)`),再往上的+`Lavinia-cowgirl` 与 `Lavinia-doggystyle` 相差 300、隔一天,是同一角色的两段新片。++## 拒绝方案++- 把改标签的时间修回上传时间再判。那要重新请求每条旧帖的详情页,是抓取侧的回填,+ 不能靠读时判据完成;读时判据先用 id 挡住,回填另议。+- 锚对上两组以上时挂最近的那组。来源自己把它们分成了几个作品,挂哪组都是猜。+- 连号间隔放到 300 以上。隔天的同角色新片会并进来,用户定过隔几天的新作不并。++## 后果++同一批里没写出处的那一帖跟进有出处的那组,paheal 的连发也能认出来。仍然漏的是同一段+片子隔几天重传的情况(InitialA 的 11127566 与 11176396 时长逐毫秒相同,站上隔 6 天上传),+按隔天不并的边界保持分开。落库时间是 `change` 的旧行,卡片上的日期仍是改标签那天。diff --git a/src/peach/follow_store.py b/src/peach/follow_store.pyindex b7dc8a7e5..047f5cfb0 100644--- a/src/peach/follow_store.py+++ b/src/peach/follow_store.py@@ -727,9 +727,11 @@ class FollowStore:)stripped = _strip_author_names(split_posts, authors or {})linked = _hint_linked(stripped)- aligned = _align_by_group_hint(_align_upload_packs(_align_tag_bursts(- _align_title_families(_split_ambiguous_works(stripped, linked), authors),- linked)))+ # 连发判据排在出处对齐之后:没有出处的帖子要挂到同批有出处的那组上,得先让+ # 那组共用一个键。+ aligned = _align_tag_bursts(_align_by_group_hint(_align_upload_packs(+ _align_title_families(_split_ambiguous_works(stripped, linked), authors))),+ linked)primaries = group_duplicates(aligned)buckets: dict[int, tuple[FollowItemRow, list[FollowItemRow]]] = {}for item, primary in zip(aligned, primaries):@@ -1291,9 +1293,10 @@ def _published_near(left: FollowItemRow, right: FollowItemRow) -> bool:return abs(gap) <= _FAMILY_WINDOW-#: 标签连发归组的边界:相邻两条至多隔多久、一般标签至少重合多少,以及哪些角色标签-#: 不算身份——rule34.xxx 把 POV 视角里的观众标成角色 `you`,几乎每条都有。+#: 标签连发归组的边界:相邻两条至多隔多久、站内 id 至多差多少、一般标签至少重合多少,+#: 以及哪些角色标签不算身份——rule34.xxx 把 POV 视角里的观众标成角色 `you`,几乎每条都有。_BURST_GAP = timedelta(hours=3)+_BURST_ID_GAP = 3000_BURST_MIN_OVERLAP = 0.5_BURST_PSEUDO_CHARACTERS = frozenset({"you"})@@ -1307,23 +1310,26 @@ def _align_tag_bursts(items: tuple[FollowItemRow, ...],rule34.xxx 上把 Angel (KOF) 的同一段动画按横屏、竖屏和几个机位拆成 6 帖,几分钟内连着发,除了角色和标签之外没有任何字段把它们连起来。- 四个条件同时成立才归组:同一来源;作品与角色标签的集合完全相同;按发布时间排开,- 相邻两条相隔不超过 `_BURST_GAP`;一般标签与组里某一条的 Jaccard 重合度不低于- `_BURST_MIN_OVERLAP`。只处理标签拼标题、没有出处、来源也没声明同组的条目:- 出处与父帖是来源自己给的关系,比这里的推断可靠,不拿推断去改它。+ 四个条件同时成立才算连发:同一来源;身份标签的集合完全相同(见 `_burst_identity`);+ 按发布时间排开,相邻两条相隔不超过 `_BURST_GAP`;一般标签与组里某一条的 Jaccard+ 重合度不低于 `_BURST_MIN_OVERLAP`。++ 只改没有出处、来源也没声明同组的条目的键:出处与父帖是来源自己给的关系,比这里的+ 推断可靠,不拿推断去改它。有出处的条目只当锚:InitialA 在 rule34.paheal 上一口气+ 传了 9 帖,8 帖写着同一个 subscribestar 出处,剩下一帖没写。没出处的一串帖子前后+ `_BURST_GAP` 内、按同样的身份与重合度能对上的锚全在同一组时,这一串归进那一组;+ 对上两组以上说明来源自己把它们分成了几个作品,挂哪一组都是猜,只在这一串里互相归。+ 这一步排在 `_align_by_group_hint` 之后,锚所在的那组已经共用一个键。"""- buckets: dict[tuple[int, frozenset[str]], list[tuple[datetime, frozenset[str],+ buckets: dict[tuple[int, frozenset[str]], list[tuple[datetime, frozenset[str], bool,FollowItemRow]]] = {}for item in items:metadata = item.metadata or {}tag_types = metadata.get("tag_types")- if (metadata.get("title_from") != "tags" or metadata.get("source")- or not isinstance(tag_types, dict) or not item.release_key- or (item.provider, item.external_id) in linked):+ if (metadata.get("title_from") != "tags" or not isinstance(tag_types, dict)+ or not item.release_key):continue- identity = frozenset(- tag for tag, kind in tag_types.items()- if kind in ("copyright", "character") and tag not in _BURST_PSEUDO_CHARACTERS)+ identity = _burst_identity(tag_types)try:moment = datetime.fromisoformat(str(item.published_at).replace("Z", "+00:00"))except (TypeError, ValueError):@@ -1331,21 +1337,24 @@ def _align_tag_bursts(items: tuple[FollowItemRow, ...],if not identity:continuegeneral = frozenset(tag for tag, kind in tag_types.items() if kind == "general")- buckets.setdefault((item.source_id, identity), []).append((moment, general, item))+ anchor = bool(metadata.get("source")) or (item.provider, item.external_id) in linked+ buckets.setdefault((item.source_id, identity), []).append(+ (moment, general, anchor, item))renamed: dict[int, str] = {}for members in buckets.values():- members.sort(key=lambda entry: (entry[0], entry[2].external_id))- run = [members[0]]- for entry in [*members[1:], None]:- if entry is not None and entry[0] - run[-1][0] <= _BURST_GAP and any(- _overlap(entry[1], other[1]) >= _BURST_MIN_OVERLAP for other in run):- run.append(entry)+ members.sort(key=lambda entry: (entry[0], entry[3].external_id))+ anchors = [entry for entry in members if entry[2]]+ for run in _burst_runs([entry for entry in members if not entry[2]]):+ keys = {anchor[3].release_key for anchor in anchors if any(+ _burst_near(anchor, entry)+ and _overlap(anchor[1], entry[1]) >= _BURST_MIN_OVERLAP for entry in run)}+ if len(keys) == 1:+ key = keys.pop()+ elif len(run) > 1:+ key = min(entry[3].release_key for entry in run)+ else:continue- if len(run) > 1:- key = min(member[2].release_key for member in run)- renamed.update((member[2].id, key) for member in run)- if entry is not None:- run = [entry]+ renamed.update((entry[3].id, key) for entry in run)if not renamed:return itemsreturn tuple(@@ -1355,6 +1364,49 @@ def _align_tag_bursts(items: tuple[FollowItemRow, ...],)+def _burst_identity(tag_types: dict) -> frozenset[str]:+ """连发判据里的身份:作品与角色标签的集合。++ rule34.paheal 不给标签分类,`tag_types` 里全是 general,身份就退到整组标签(小写):+ 那里一帖只有几个标签(中位数 7 个),作者、角色、作品和 `animated` 这类形态词都在里面,+ 整组相同才算同一作品,比按词形猜哪个是角色更窄。+ """+ kinds = set(tag_types.values())+ if kinds == {"general"}:+ return frozenset(str(tag).lower() for tag in tag_types)+ return frozenset(+ tag for tag, kind in tag_types.items()+ if kind in ("copyright", "character") and tag not in _BURST_PSEUDO_CHARACTERS)+++def _burst_runs(members: list) -> list[list]:+ """按时间排好的条目切成连发串:相邻两条时间与站内 id 都挨着,标签与串里某一条够重合。"""+ runs: list[list] = []+ for entry in members:+ if runs and _burst_near(entry, runs[-1][-1]) and any(+ _overlap(entry[1], other[1]) >= _BURST_MIN_OVERLAP for other in runs[-1]):+ runs[-1].append(entry)+ else:+ runs.append([entry])+ return runs+++def _burst_near(left: tuple, right: tuple) -> bool:+ """两帖的发布时间相隔不超过 `_BURST_GAP`,站内 id 相差不超过 `_BURST_ID_GAP`。++ 时间不够:rule34.xxx 早年落库的行,`published_at` 是 dapi 的 `change`(最后修改时间)。+ 有人一次改一批旧帖的标签,几个月前各自上传的帖子就「在同一分钟发布」——Rekin3D 的+ 17361475(05-01 上传)与 17559518(05-20 上传)都记成 09-14 03:0x。帖子 id 是全站+ 递增的上传序号,改标签不动它,所以连发还要 id 挨着。id 不是纯数字的站只看时间。+ """+ if abs(left[0] - right[0]) > _BURST_GAP:+ return False+ try:+ return abs(int(left[3].external_id) - int(right[3].external_id)) <= _BURST_ID_GAP+ except (TypeError, ValueError):+ return True++def _overlap(left: frozenset[str], right: frozenset[str]) -> float:union = left | rightreturn len(left & right) / len(union) if union else 0.0@@ -1362,7 +1414,7 @@ def _overlap(left: frozenset[str], right: frozenset[str]) -> float:#: 一包短片的边界:相邻两条站内 id 至多差多少、时长至多差几秒,以及标题开头的#: 角色名至多几个词。-_PACK_ID_GAP = 30+_PACK_ID_GAP = 100_PACK_DURATION_SLACK = 1.0_PACK_NAME_MAX_WORDS = 3_PACK_NAME_SEPARATOR_RE = re.compile(r"\s+[-–—|:]\s+|[-–—]|[\[(『【|:]")diff --git a/tests/test_follow_store.py b/tests/test_follow_store.pyindex 8307dea55..4fceb2ac1 100644--- a/tests/test_follow_store.py+++ b/tests/test_follow_store.py@@ -472,13 +472,57 @@ class GroupingTests(_StoreCase):self._booru_post("2", 1, general=("standing_sex", "against_wall", "hat", "nude")))self.assertEqual(len(groups), 2)- def test_a_post_with_a_declared_origin_is_not_pulled_into_a_burst(self):- # 出处是来源自己给的关系,推断出来的连发不去改它。+ def test_a_post_without_an_origin_joins_the_declared_work_it_was_uploaded_with(self):+ # LazyProcrastinator 的 Aerith 一批 6 帖,5 帖写了出处,夹在中间的一帖没写。+ origin = "https://www.fanbox.cc/@lazy/posts/12304831"groups = self._record_booru(- self._booru_post("1", 0),- self._booru_post("2", 1, source="https://www.fanbox.cc/@lazy/posts/1"))+ self._booru_post("15101361", 0, source=origin),+ self._booru_post("15101385", 3),+ self._booru_post("15101390", 4, source=origin))+ self.assertEqual(len(groups), 1)+ self.assertEqual(len(groups[0].variants), 2)++ def test_two_declared_works_are_not_joined_through_a_post_between_them(self):+ # 来源把两帖分成了两个作品,中间那帖挂哪一组都是猜,三帖各自成卡。+ groups = self._record_booru(+ self._booru_post("1", 0, source="https://www.fanbox.cc/@lazy/posts/12304831"),+ self._booru_post("2", 1),+ self._booru_post("3", 2, source="https://www.fanbox.cc/@lazy/posts/12304832"))+ self.assertEqual(len(groups), 3)++ def test_posts_retagged_together_are_not_a_burst(self):+ # Rekin3D 的两帖分别在 05-01 与 05-20 上传,09-14 一起被改了标签,落库时间都是+ # 改标签那一分钟。帖子 id 相差近 20 万,不是连着传的。+ groups = self._record_booru(+ self._booru_post("17361475", 1), self._booru_post("17559518", 0))self.assertEqual(len(groups), 2)+ def test_an_untagged_booru_post_joins_its_batch_by_the_whole_tag_set(self):+ # rule34.paheal 不给标签分类。InitialA 一口气传的 Lily 动画里,8 帖写着同一个+ # subscribestar 出处,最后一帖没写;整组标签相同,归进那一组。+ source_id = self._source(provider="rule34paheal", ref="initiala", label="initiala")+ tags = {tag: "general" for tag in+ ("InitialA", "Lily_Artemis_II", "Stellar_Blade", "animated", "blender")}++ def post(external_id, seconds, **extra):+ return FollowCandidate(+ provider="rule34paheal", external_id=external_id,+ title="lily artemis ii · stellar blade", title_is_name=False,+ published_at=(MOMENT + timedelta(seconds=seconds)).isoformat(),+ group_hint="subscribestar:2685233" if "source" in extra+ else f"rule34paheal:post:{external_id}",+ extra={"title_from": "tags", "tag_types": tags, **extra})++ origin = "https://subscribestar.adult/posts/2685233"+ self.store.record(source_id, _fetch([+ post("7452299", 0, source=origin), post("7452300", 1, source=origin),+ post("7452301", 25),+ post("7452302", 30, tag_types={**tags, "Eve": "general"}),+ ], provider="rule34paheal", ref="initiala"), moment=MOMENT)+ groups = sorted(sorted(m.external_id for m in (g.primary, *g.variants, *g.duplicates))+ for g in self.store.group(self.store.items()))+ self.assertEqual(groups, [["7452299", "7452300", "7452301"], ["7452302"]])+def test_a_stored_hint_is_recomputed_from_the_recorded_source(self):# 落库时缺协议头的出处没认出来,键退回了帖子自己的 id;读时按出处重算后并组。stale = self._booru_post("2", 1, character="ahri",@@ -512,6 +556,23 @@ class GroupingTests(_StoreCase):self.assertEqual(groups, [["Angel-handjob", "Angel-missionary", "Angel-riding hugging"],["Mai-blowjob", "Mai-doggy"]])+ def test_a_clip_a_few_dozen_uploads_earlier_joins_its_pack(self):+ # Barney's Mom 一包 8 条 20 秒短片,Riding 比下一条早 64 个 id,中间夹着别人的上传。+ freyja = {"freyja (final fantasy)": "general"}+ groups = self._record_clips(+ self._clip(4608531, "Barney's Mom - Riding", work="Final Fantasy", **freyja),+ self._clip(4608595, "Barney's Mom - Doggy 3", work="Final Fantasy", **freyja),+ self._clip(4608599, "Barney's Mom - Sitting Blowjob", work="Final Fantasy",+ **freyja))+ self.assertEqual(len(groups), 1)++ def test_the_same_character_uploaded_the_next_day_stays_apart(self):+ # LazyProcrastinator 的 Lavinia 两条隔天上传,id 相差 300,是两部。+ groups = self._record_clips(+ self._clip(4600000, "Lavinia-cowgirl", character="lavinia (zzz)"),+ self._clip(4600300, "Lavinia-doggystyle", character="lavinia (zzz)"))+ self.assertEqual(len(groups), 2)+def test_clips_far_apart_or_of_another_length_stay_apart(self):groups = self._record_clips(self._clip(4608755, "Angel-riding hugging"), self._clip(4609755, "Angel-missionary"),