Skip to content

armgit / peach

Public Imported from GitHub
0

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

feat(follow): 隔天重传按作品专属的毫秒时长归进同一组

longmeidao21df858390b1
diff --git a/docs/adr/0077-follow-reuploads-join-on-a-work-specific-length.md b/docs/adr/0077-follow-reuploads-join-on-a-work-specific-length.md
new file mode 100644
index 000000000..d8340a6c8
--- /dev/null
+++ b/docs/adr/0077-follow-reuploads-join-on-a-work-specific-length.md
@@ -0,0 +1,65 @@
+# ADR-0077:隔天重传按作品专属的毫秒时长归组
+
+- 状态:Accepted
+- 日期:2026-09-26
+- 修订:ADR-0058「后果」里保持分开的隔天重传
+
+## 背景
+
+Memz 在 rule34.xxx 上的 Grace Ashcroft 动画分成了两张卡:18148361 于 07-18 上传,
+18193355 与 18193376 于 07-23 上传。三帖是同一段 60.054 秒的动画,连发判据接不住:
+
+- 相隔 4 天多,超出 3 小时的连发窗口。
+- 后两帖多挂了 `resident_evil_9:_requiem`,身份标签集合不同。
+
+能拿来当证据的只剩时长。2026-09-26 在真实 ledger 上只读核对过另外两条路:
+
+| 判据 | 结果 |
+| --- | --- |
+| 文件 MD5(`content_hash`) | 三帖各不相同,重传时重新编码过 |
+| 缩略图画面签名 | 两对 dHash 相差 12 与 14、色块差 55 与 67:站上生成缩略图取了不同的帧 |
+
+## 决策
+
+`follow_store._align_reuploads` 排在连发判据之后,四个条件同时成立才把两帖的组合并:
+
+1. 同一来源,时长到毫秒相同,而且这个时长在这个来源里只出现在一种角色组合上。整秒的
+ 时长不认。
+2. 角色标签集合相同,作品标签不看。
+3. 一般标签 Jaccard 重合度不低于 0.5,与连发判据同一个阈值。
+4. 发布时间相隔 3 小时以上;同一时段里的几帖归连发判据管。
+
+合并的是整组的键。有出处或共用 `group_hint` 的组只当锚,一次合并只能有一个锚。第 1 条的
+固定导出长度按筛选前的全部条目认(`FollowStore.group` 的 `population`),不按当前这一页。
+
+## 依据
+
+只读试算,16649 条可见条目,11957 组:
+
+| 判据 | 新并出的组 | 抽查 |
+| --- | --- | --- |
+| 同来源、同角色、同毫秒时长、重合 ≥ 0.5 | 60 | LazyProcrastinator 一小时内的 43 帖 Raven 串成一张卡 |
+| 再要求相隔 3 小时以上 | 38 | 20.02、31.232、45.045 秒的组占一半,画面多数不同 |
+| **再要求时长只属于一种角色组合(采用)** | 14 | 拼图逐组看过,Memz 在内;没有拆开任何旧组 |
+
+固定导出长度的分布没有中间地带:被并组的时长在同一来源里要么出现在 55 到 98 种角色
+组合上(LazyProcrastinator 的 20.02 秒是 29.97 帧率下的 600 帧),要么只有 1 到 3 种。
+
+重合度阈值:缩略图完全一致的三对重传,重合度分别是 0.96、0.69、0.47;抽查到的同一来源
+同时长的不同动画最高 0.40。
+
+## 拒绝方案
+
+- 按缩略图画面合并。重传的缩略图取帧不同,Memz 这对都判不出来。
+- 按文件哈希合并。重传都重新编码;同一个哈希分在两张卡的 293 个文件里,大多是 pawchive
+ 每月帖子共用的同一张横幅图。
+- 时长加容差。60.054 与 60.075 只差 0.021 秒,却是两位作者的两种导出长度。
+
+## 后果
+
+- 重合度 0.47 的那对(InitialA 的 7657264 与 7664287)仍然分开。
+- 用固定导出长度的重传仍然分开:LazyProcrastinator 的 17799100 与 17809026 缩略图完全
+ 一致,时长是 20.02 秒。
+- 两帖各写了不同出处的重传仍然分开,比如 Pantsushi3D 隔 392 天发的两条推。
+- 作者只用一种角色组合时,他的固定导出长度也会被认成专属时长;同一角色隔几天的两部
+ 时长逐毫秒相同、标签又重合过半时会并到一起。
diff --git a/src/peach/follow_store.py b/src/peach/follow_store.py
index 047f5cfb0..967213573 100644
--- a/src/peach/follow_store.py
+++ b/src/peach/follow_store.py
@@ -702,16 +702,22 @@ class FollowStore:
@staticmethod
def group(items: tuple[FollowItemRow, ...],
authors: dict[int, tuple[str, frozenset[str]]] | None = None,
+ population: tuple[FollowItemRow, ...] | None = None,
) -> tuple[ReleaseGroup, ...]:
"""把条目折叠成作品分组。
先按来源自带的 `group_hint` 合并(booru 的 `parent_id` 比标题可靠),
再按标题推出的 `release_key` 合并,最后同一组里选主条目。booru 上没有标题也
没有出处的帖子,按角色、上传时间与标签重合度认出同一作品的连发(ADR-0046);
- 站内 id 连号上传的同一批短片按角色认成一包(ADR-0045)。
+ 站内 id 连号上传的同一批短片按角色认成一包(ADR-0045);角色、毫秒时长与标签
+ 都对得上的隔天重传也归进同一组(ADR-0077)。
`authors` 把来源 id 映射到(作者键, 这位作者的全部名字写法)。给了它,标题里
夹着的作者名在分组前剥掉,相似标题的版本也跨同一作者的各个来源去对。
+
+ `population` 是筛选前的全部条目,只用来认作者的固定导出长度(见
+ `_align_reuploads`);不给就按 `items` 认。按状态或标签筛过的一页里其他角色
+ 可能正好都不在,同一个时长就会被错认成只属于一部片子。
"""
# 先按标题判据拆开同站撞车,再按来源自带的关系合并:来源自己声明过的关系
# 优先,绝不能被标题判据拆散。
@@ -729,9 +735,9 @@ class FollowStore:
linked = _hint_linked(stripped)
# 连发判据排在出处对齐之后:没有出处的帖子要挂到同批有出处的那组上,得先让
# 那组共用一个键。
- aligned = _align_tag_bursts(_align_by_group_hint(_align_upload_packs(
+ aligned = _align_reuploads(_align_tag_bursts(_align_by_group_hint(_align_upload_packs(
_align_title_families(_split_ambiguous_works(stripped, linked), authors))),
- linked)
+ linked), linked, _template_lengths(population if population is not None else items))
primaries = group_duplicates(aligned)
buckets: dict[int, tuple[FollowItemRow, list[FollowItemRow]]] = {}
for item, primary in zip(aligned, primaries):
@@ -1337,7 +1343,7 @@ def _align_tag_bursts(items: tuple[FollowItemRow, ...],
if not identity:
continue
general = frozenset(tag for tag, kind in tag_types.items() if kind == "general")
- anchor = bool(metadata.get("source")) or (item.provider, item.external_id) in linked
+ anchor = _declares_origin(item, linked)
buckets.setdefault((item.source_id, identity), []).append(
(moment, general, anchor, item))
renamed: dict[int, str] = {}
@@ -1412,6 +1418,119 @@ def _overlap(left: frozenset[str], right: frozenset[str]) -> float:
return len(left & right) / len(union) if union else 0.0
+def _align_reuploads(items: tuple[FollowItemRow, ...],
+ linked: frozenset[tuple[str, str]] = frozenset(),
+ templates: frozenset[tuple[int, float]] | None = None,
+ ) -> tuple[FollowItemRow, ...]:
+ """同一段片子隔几天重传的几帖归到一个键下(ADR-0077)。
+
+ Memz 的 Grace Ashcroft 动画 07-18 传过一帖,07-23 又传了两帖:文件各自重新编码,MD5
+ 不同,站上生成的缩略图也取了不同的帧,只有时长逐毫秒相同(60.054 秒)。连发判据的
+ 3 小时窗口接不住,作品标签也多挂了一个 `resident_evil_9:_requiem`。
+
+ 条件同时成立才算重传:
+
+ 1. 同一来源,时长到毫秒相同,而且这个时长在这个来源里只出现在一种角色组合上。作者常用
+ 固定的导出长度:LazyProcrastinator 的 20.02 秒用在 98 种角色组合上,InitialA 的
+ 31.232 秒用在 60 种上,这种时长相同说明不了是同一段。整秒的时长也不认,那多半是
+ 站点报的取整值。
+ 2. 角色标签的集合相同;作品标签不看,各帖写法不一。
+ 3. 一般标签的 Jaccard 重合度不低于 `_BURST_MIN_OVERLAP`。
+ 4. 发布时间相隔 `_BURST_GAP` 以上。同一时段里连着传的几帖归连发判据管,这里不拿
+ 时长把它按标签切开的几段重新串起来。
+
+ 合并的是整组的键,不是单帖:一帖已经在连发组里,整组跟着它走。有出处的组只当锚,
+ 一次合并里只能有一个锚:两帖各写了不同的出处,是来源自己说的两次发布,不并。
+
+ `templates` 是第 1 条里的固定导出长度(`_template_lengths`),不给就按 `items` 算。
+ """
+ if templates is None:
+ templates = _template_lengths(items)
+ buckets: dict[tuple, list[tuple[frozenset[str], datetime, frozenset[str],
+ FollowItemRow]]] = {}
+ for item in items:
+ found = _reupload_bucket(item)
+ if found is not None and found[0] not in templates and found[1]:
+ buckets.setdefault((found[0], found[1]), []).append((*found[1:], item))
+ anchored = {item.release_key for item in items
+ if item.release_key and _declares_origin(item, linked)}
+ parent: dict[str, str] = {}
+ anchors: dict[str, frozenset[str]] = {}
+
+ def root(key: str) -> str:
+ while parent.get(key, key) != key:
+ key = parent[key]
+ return key
+
+ for members in buckets.values():
+ for item, other in _reupload_pairs(members):
+ left, right = root(item.release_key), root(other.release_key)
+ if left == right:
+ continue
+ joined = (anchors.get(left, frozenset({left} & anchored))
+ | anchors.get(right, frozenset({right} & anchored)))
+ if len(joined) > 1:
+ continue
+ target = next(iter(joined)) if joined else min(left, right)
+ parent[left] = parent[right] = target
+ parent.pop(target, None)
+ anchors[target] = joined
+ if not parent:
+ return items
+ return tuple(
+ FollowItemRow(**{**item.__dict__, "release_key": root(item.release_key)})
+ if item.release_key and root(item.release_key) != item.release_key else item
+ for item in items
+ )
+
+
+def _template_lengths(items: tuple[FollowItemRow, ...]) -> frozenset[tuple[int, float]]:
+ """作者的固定导出长度:同一来源里出现在两种以上角色组合上的毫秒时长。"""
+ seen: dict[tuple, set[frozenset[str]]] = {}
+ for item in items:
+ found = _reupload_bucket(item)
+ if found is not None:
+ seen.setdefault(found[0], set()).add(found[1])
+ return frozenset(key for key, characters in seen.items() if len(characters) > 1)
+
+
+def _reupload_pairs(members: list) -> list[tuple[FollowItemRow, FollowItemRow]]:
+ """同一来源、同一毫秒时长、同一组角色的一桶里,够得上重传的成对。"""
+ return [(item, other)
+ for index, (_, moment, general, item) in enumerate(members)
+ for _, other_moment, other_general, other in members[index + 1:]
+ if abs(moment - other_moment) > _BURST_GAP
+ and _overlap(general, other_general) >= _BURST_MIN_OVERLAP]
+
+
+def _reupload_bucket(item: FollowItemRow) -> tuple[
+ tuple, frozenset[str], datetime, frozenset[str]] | None:
+ """重传判据的桶键(来源、毫秒时长)、角色集合、发布时间与一般标签;
+ 不参与判断的条目给 None。"""
+ metadata = item.metadata or {}
+ tag_types = metadata.get("tag_types")
+ if (metadata.get("title_from") != "tags" or not isinstance(tag_types, dict)
+ or not item.release_key or not item.duration or item.duration <= 0):
+ return None
+ seconds = round(float(item.duration), 3)
+ try:
+ moment = datetime.fromisoformat(str(item.published_at).replace("Z", "+00:00"))
+ except (TypeError, ValueError):
+ return None
+ if seconds.is_integer():
+ return None
+ characters = frozenset(tag for tag, kind in tag_types.items()
+ if kind == "character" and tag not in _BURST_PSEUDO_CHARACTERS)
+ general = frozenset(tag for tag, kind in tag_types.items() if kind == "general")
+ return (item.source_id, seconds), characters, moment, general
+
+
+def _declares_origin(item: FollowItemRow, linked: frozenset[tuple[str, str]]) -> bool:
+ """来源自己声明了这一帖属于哪次发布:写了出处,或与别的条目共用 `group_hint`。"""
+ return (bool((item.metadata or {}).get("source"))
+ or (item.provider, item.external_id) in linked)
+
+
#: 一包短片的边界:相邻两条站内 id 至多差多少、时长至多差几秒,以及标题开头的
#: 角色名至多几个词。
_PACK_ID_GAP = 100
diff --git a/src/peach/web_follow.py b/src/peach/web_follow.py
index 0d5cda156..81ab69866 100644
--- a/src/peach/web_follow.py
+++ b/src/peach/web_follow.py
@@ -1712,7 +1712,8 @@ def q_follow(contract, args) -> dict:
# 只剩两三条,得反复点「加载更多」才凑出一屏;按条目切页再分组的话,同一作品
# 的几份上传落在相邻两页,页面上就是两张卡。所以整批分组,`limit` 数的是组。
page = tuple(item for item in counted if not statuses or item.status in statuses)
- ranked = _sorted_groups(store.group(page, by_author), sort, direction, seed)
+ ranked = _sorted_groups(store.group(page, by_author, everything),
+ sort, direction, seed)
has_more = len(ranked) > offset + limit
ranked = ranked[offset:offset + limit]
# 翻卡与封面计数要知道组里哪几张是同一个画面,判据与缓存见 `follow_faces`。
diff --git a/tests/test_follow_store.py b/tests/test_follow_store.py
index 4fceb2ac1..efc423865 100644
--- a/tests/test_follow_store.py
+++ b/tests/test_follow_store.py
@@ -532,6 +532,69 @@ class GroupingTests(_StoreCase):
replace(stale, group_hint="rule34xxx:post:2"))
self.assertEqual(len(groups), 1)
+ def _upload(self, external_id, hours, *, duration=60.054, character="grace_ashcroft",
+ general=("athletic_female", "blender", "cowgirl_position", "nude"), **extra):
+ return replace(self._booru_post(external_id, hours * 60, character=character,
+ general=general, **extra), duration=duration)
+
+ def _members(self, groups):
+ return sorted(sorted(m.external_id for m in (g.primary, *g.variants, *g.duplicates))
+ for g in groups)
+
+ def test_the_same_clip_uploaded_again_days_later_joins_the_first_upload(self):
+ # Memz 的 Grace 动画:07-18 一帖带出处,07-23 又传两帖。重新编码过,只有时长逐毫秒
+ # 相同;后两帖多挂一个作品标签,一般标签也不完全一样。
+ later = {"resident_evil_9:_requiem": "copyright", "grace_ashcroft": "character",
+ "memz": "artist", "athletic_female": "general", "blender": "general",
+ "cowgirl_position": "general", "nude": "general", "3d_animation": "general"}
+ groups = self._record_booru(
+ self._upload("18148361", 0, source="https://danbooru.donmai.us/posts/11817280"),
+ self._upload("18193355", 104, tag_types=later),
+ self._upload("18193376", 104, tag_types=later))
+ self.assertEqual(self._members(groups), [["18148361", "18193355", "18193376"]])
+
+ def test_an_export_length_shared_by_other_characters_is_not_evidence(self):
+ # LazyProcrastinator 的 20.02 秒用在上百种角色组合上:同一角色隔几天的两帖是两部。
+ groups = self._record_booru(
+ self._upload("1", 0, duration=20.02, character="reika_(doa)"),
+ self._upload("2", 24 * 100, duration=20.02, character="reika_(doa)"),
+ self._upload("3", 24 * 50, duration=20.02, character="jote_(ffxvi)"))
+ self.assertEqual(len(groups), 3)
+
+ def test_a_filtered_page_still_knows_an_export_length(self):
+ # 只看未读时,别的角色那几帖可能都不在这一页;认固定长度要按筛选前的全部条目。
+ self._record_booru(
+ self._upload("1", 0, duration=20.02, character="reika_(doa)"),
+ self._upload("2", 24 * 100, duration=20.02, character="reika_(doa)"),
+ self._upload("3", 24 * 50, duration=20.02, character="jote_(ffxvi)"))
+ everything = self.store.items()
+ page = tuple(item for item in everything if item.external_id != "3")
+ self.assertEqual(len(self.store.group(page, population=everything)), 2)
+
+ def test_uploads_within_one_session_are_left_to_the_burst_rule(self):
+ # 同一小时里时长相同的几帖是同一场景的不同体位,连发判据按相邻帖的标签切开了:
+ # 1 与 3 标签相近,中间隔着体位不同的 2,这里不拿时长把 1 和 3 重新串起来。
+ handjob = ("beach", "handjob", "duo", "pov")
+ groups = self._record_booru(
+ self._upload("1", 0, general=handjob),
+ self._upload("2", 0.25, general=("beach", "ass_focus", "doggy", "wall")),
+ self._upload("3", 0.5, general=(*handjob[:3], "kneeling")))
+ self.assertEqual(len(groups), 3)
+
+ def test_reuploads_need_overlapping_tags_and_a_millisecond_length(self):
+ groups = self._record_booru(
+ self._upload("1", 0), self._upload("2", 48, general=("standing", "hat", "wall", "nude")),
+ self._upload("3", 0, duration=45.0, character="nyotengu"),
+ self._upload("4", 48, duration=45.0, character="nyotengu"))
+ self.assertEqual(len(groups), 4)
+
+ def test_two_declared_releases_are_not_joined_as_a_reupload(self):
+ # Pantsushi3D 发过两条推:来源自己说是两次发布,缩略图再像也不并。
+ groups = self._record_booru(
+ self._upload("4704518", 0, source="https://twitter.com/Pantsushi3D/status/1"),
+ self._upload("6178800", 24 * 392, source="https://twitter.com/Pantsushi3D/status/2"))
+ self.assertEqual(len(groups), 2)
+
def _clip(self, video_id, title, *, duration=20.0, character="angel (kof)",
work="King of Fighters", **extra_tags):
tag_types = {"beach": "general", character: "general", work: "copyright",