Peach: Local-first video metadata scraper and media manager.
fix(catalog): 同番号下文件名一样的两份算同一条重复
longmeidaofd0078cd5d98
diff --git a/src/peach/catalog_rules.py b/src/peach/catalog_rules.pyindex cd2d567c0..cf1db1282 100644--- a/src/peach/catalog_rules.py+++ b/src/peach/catalog_rules.py@@ -10,6 +10,7 @@ web 层——`repository`(数据层)取 `is_jav_code`,`taste_history` 取from __future__ import annotationsimport re+from pathlib import PureWindowsPathLENGTH_TAGS = {"短片-2分内", "中片-10分内", "长片-30分内", "超长片-30分上"}@@ -990,7 +991,40 @@ def duration_clusters(items: list[dict]) -> list[list[dict]]:else:clusters.append([item])clusters.extend([item] for item in items if not (item.get("duration") or 0) > 0)- return clusters+ return _merged_by_filename(clusters)+++def _merged_by_filename(clusters: list[list[dict]]) -> list[list[dict]]:+ """同番号下文件名一样的那些,并回同一组。++ 时长是唯一判据时,同一版的两份会被分开:一份原盘一份转码,容器里写的时长+ 差出十几秒就超了容差;一份根本没探过时长,那更是自己单独一组。两组各只有+ 一个,重复清单于是一条都不提示——而这两份恰恰是最该提示的那种。++ 番号已经把范围收在同一发行上,这一层只看文件名:同番号下名字一样的就是同+ 一版。分卷不受影响,`ABC-001-A` 和 `ABC-001-B` 本来就不同名。+ """+ home = list(range(len(clusters)))++ def root(index: int) -> int:+ while home[index] != index:+ home[index] = home[home[index]]+ index = home[index]+ return index++ first: dict[str, int] = {}+ for index, cluster in enumerate(clusters):+ for item in cluster:+ stem = PureWindowsPath(str(item.get("name") or "")).stem.casefold()+ if not stem:+ continue+ known = first.setdefault(stem, index)+ if root(known) != root(index):+ home[root(index)] = root(known)+ merged: dict[int, list[dict]] = {}+ for index, cluster in enumerate(clusters):+ merged.setdefault(root(index), []).extend(cluster)+ return [merged[key] for key in sorted(merged)]def dir_expr(alias: str = "a.") -> str:diff --git a/tests/test_rm_web.py b/tests/test_rm_web.pyindex fedc6a8da..56be36c80 100644--- a/tests/test_rm_web.py+++ b/tests/test_rm_web.py@@ -2458,6 +2458,31 @@ class DuplicateDetectionTests(unittest.TestCase):self.assertEqual(result["total"], 1)self.assertEqual(result["groups"][0]["count"], 2)+ def test_two_files_with_the_same_name_are_one_duplicate_whatever_the_clock_says(self):+ """同番号下同名的两份是同一版,时长差多少都算重复。++ 一份原盘一份转码,容器里写的时长能差出十几秒,超出容差就各成一组,+ 两组各只有一个,清单上一条都不提示——而这正是最该提示的那种。探时长+ 失败的那份(`duration` 为空)同理。+ """+ kept = self.add("SSIS-001", 7200, 9_000_000_000, name="SSIS-001.mp4")+ drifted = self.add("SSIS-001", 7380, 4_000_000_000, drive="R", name="SSIS-001.mp4")+ unprobed = self.add("SSIS-001", None, 3_000_000_000, drive="A", name="SSIS-001.mp4")+ result = self.groups()+ self.assertEqual(result["total"], 1)+ self.assertEqual({item["id"] for item in result["groups"][0]["files"]},+ {kept, drifted, unprobed})++ def test_a_shared_name_does_not_drag_in_the_other_parts(self):+ """并的是同名那几份,别的簇不跟着合。"""+ self.add("PPT-019", 6000, 5_000_000_000, name="PPT-019-A.mp4")+ self.add("PPT-019", 6000, 4_000_000_000, drive="R", name="PPT-019-A.mp4")+ self.add("PPT-019", 9000, 5_000_000_000, name="PPT-019-B.mp4")+ self.add("PPT-019", 9000, 4_000_000_000, drive="R", name="PPT-019-B.mp4")+ result = self.groups()+ self.assertEqual(result["total"], 2)+ self.assertTrue(all(group["count"] == 2 for group in result["groups"]))+def test_multi_part_releases_never_collapse_into_one_cluster(self):# PPT-018 实测:109.2/175.2/196.4 分各两份。按番号只留最大会删掉两个部分。for minutes in (109.2, 175.2, 196.4):