Peach: Local-first video metadata scraper and media manager.
fix(avatars): 挑图先分来源档,作品封面只比缩略图强一档
longmeidao3fa3a8237b8b
diff --git a/scripts/harvest_social_avatars.py b/scripts/harvest_social_avatars.pyindex 948c7a455..f6cf7c41c 100644--- a/scripts/harvest_social_avatars.py+++ b/scripts/harvest_social_avatars.py@@ -619,14 +619,59 @@ def measure_faces(candidates: list[dict], probe) -> None:candidate["face_width"] = face_px_width(record)-def rank_key(row: dict) -> tuple[int, int, int, int]:- """挑图的唯一判据:脸的像素宽先说话,画布只是它没得比时的退路。+#: 挑图的第一层判据:这张图是拍来做人像的,还是拍来卖片的。脸有多少像素是第二层。+#:+#: 作品封面是这里唯一被单独压一档的来源。它不是拍来做人像的:JAV 封面普遍是双联版式,+#: 左半幅整块留给标题和宣传文字,右半幅那张脸还常被角标和腰封压住。把它放大成一个+#: 64–160 px 的圆头像,圆框里出现的往往是半行日文标题,而不是这个人。像素再多也换不来+#: 一张能认人的脸——这一档差的是构图,不是清晰度,所以必须在比脸宽之前就分开。+#: 缩略图在它下面:那是同一张图被裁小的一份,除了没有别的可用之外没有任何理由选它。+PORTRAIT_TIER = 2+COVER_TIER = 1+THUMBNAIL_TIER = 0+SOURCE_TIERS = {+ # 候选行用的是 source_kind+ "official_profile": PORTRAIT_TIER, # 本人社媒头像,脸是自己挑的+ "official_directory": PORTRAIT_TIER, # 事务所与名录的宣材人像+ "official_mirror": PORTRAIT_TIER,+ "external_media_library": PORTRAIT_TIER, # 媒体库整版人像+ "single_performer_cover": COVER_TIER,+ "thumbnail": THUMBNAIL_TIER,+ # 盘上那张只留下 provenance 里的 provider,两套词汇在这里并成一张表+ "social-web": PORTRAIT_TIER,+ "jae": PORTRAIT_TIER,+ "babepedia": PORTRAIT_TIER,+ "verified-photo-page": PORTRAIT_TIER,+ "cover-fallback": COVER_TIER,+}+++def source_tier(row: dict) -> int:+ """认不出来源就当人像档。++ 这个默认方向是有意的:认不出来的多半是别的管线装的整版人像(gfriends 那 434 张、+ Stash 那 14 张的 provenance 里根本没有 provider 字段),把它们当封面会让本脚本那些+ 400×400 的社媒头像一路把它们顶掉——正是 2026-09-06 那趟 `--force` 的教训。+ """+ for key in (row.get("source_kind"), row.get("provider")):+ if key in SOURCE_TIERS:+ return SOURCE_TIERS[key]+ return PORTRAIT_TIER+++def rank_key(row: dict) -> tuple[int, int, int, int, int]:+ """先看这张图是不是拍来做人像的,再看脸的像素宽,画布只是它们都没得比时的退路。++ 来源档在最前面,所以一张 400×400 社媒头像胜过 800×538 的作品封面,哪怕封面上+ 那张脸更大:封面上和脸抢地方的是标题文字,那不是清晰度问题。同一档之内才轮到+ 脸宽说话。检不出脸的候选脸宽是 0,自然排到有脸的后面;全都检不出(侧脸、低头、模型缺席)时整批都是 0,排序原样落回画布口径:先比短边再比长边——竖构图人像与方图头像用同一把尺会偏袒长边。"""- return (row.get("face_width") or 0,+ return (source_tier(row),+ row.get("face_width") or 0,min(row["width"], row["height"]),max(row["width"], row["height"]),row["width"] * row["height"])@@ -659,6 +704,19 @@ def select_winner(candidates: list[dict]) -> tuple[dict | None, list[dict]]:return ranked[-1], ranked[:-1]+def incumbent_provider(destination: Path) -> str:+ """盘上那张是谁装的。只有 provenance 记得住这件事,图本身不带来源。++ 没有 provenance 的按认不出处理,`source_tier` 会把它当人像档——434 张 gfriends+ 和 14 张 Stash 头像的 provenance 里就没有 provider 这个字段。+ """+ try:+ record = json.loads(Path(f"{destination}.provenance.json").read_text(encoding="utf-8"))+ except (OSError, ValueError):+ return ""+ return str(record.get("provider") or "")++def incumbent_row(destination: Path, probe) -> dict | None:"""盘上那张的选图口径,用来和赢家比。没有图、读不出都返回 None。@@ -676,7 +734,8 @@ def incumbent_row(destination: Path, probe) -> dict | None:if not (width > 0 and height > 0):return Nonereturn {"width": width, "height": height,- "face_width": face_px_width(record), "face_record": record}+ "face_width": face_px_width(record), "face_record": record,+ "provider": incumbent_provider(destination)}def outranks_incumbent(winner: dict, incumbent: dict | None) -> bool:diff --git a/scripts/vendor_web_dependencies.mjs b/scripts/vendor_web_dependencies.mjsindex bffe4ed3f..274e2f3a0 100644--- a/scripts/vendor_web_dependencies.mjs+++ b/scripts/vendor_web_dependencies.mjs@@ -67,7 +67,7 @@ const lucideIcons = new Map([["refresh-cw", "refresh-cw"], ["user-round", "user-round"], ["tags", "tags"],["list-filter", "list-filter"], ["chart", "chart-no-axes-column"],["settings", "settings"], ["gauge", "gauge"],- ["history", "history"], ["sparkles", "sparkles"],+ ["history", "history"], ["sparkles", "sparkles"], ["star", "star"],["x", "x"], ["folder-open", "folder-open"], ["info", "info"],["hard-drive", "hard-drive"], ["globe", "globe"], ["rss", "rss"],["thumbs-up", "thumbs-up"], ["thumbs-down", "thumbs-down"], ["eye", "eye"],diff --git a/tests/test_social_avatar_harvest.py b/tests/test_social_avatar_harvest.pyindex 747860173..589867ce1 100644--- a/tests/test_social_avatar_harvest.py+++ b/tests/test_social_avatar_harvest.py@@ -222,8 +222,9 @@ class SelectionTests(unittest.TestCase):self.module = load_module()@staticmethod- def candidate(width, height, sha, evidence="图", face_width=None):- row = {"provider": "social-web", "source_kind": "official_profile",+ def candidate(width, height, sha, evidence="图", face_width=None,+ source_kind="official_profile", provider="social-web"):+ row = {"provider": provider, "source_kind": source_kind,"source_url": "https://example.invalid/a.jpg", "external_id": "x","width": width, "height": height, "mime_type": "image/jpeg","sha256": sha, "object_path": Path("/tmp/x"), "matched": "釈アリス",@@ -232,6 +233,50 @@ class SelectionTests(unittest.TestCase):row["face_width"] = face_widthreturn row+ def test_a_cover_loses_to_any_portrait_however_big_its_face_is(self):+ """作品封面差的是构图,不是清晰度,所以在比脸宽之前就先输一档。++ JAV 封面普遍是双联版式:左半幅整块留给标题和宣传文字,右半幅那张脸还常被+ 角标和腰封压住。放进 64–160 px 的圆头像里,圆框中央经常是半行日文标题而不是+ 这个人。像素再多也换不来一张能认人的脸。+ """+ cover = self.candidate(800, 538, hashlib.sha256(b"cover").hexdigest(),+ face_width=240, source_kind="single_performer_cover",+ provider="cover-fallback")+ portrait = self.candidate(400, 400, hashlib.sha256(b"x").hexdigest(),+ face_width=120)+ winner, runners_up = self.module.select_winner([cover, portrait])+ self.assertEqual(winner["width"], 400)+ self.assertEqual([row["width"] for row in runners_up], [800])++ def test_a_cover_still_beats_a_thumbnail(self):+ """封面只压一档:它上面是人像,下面是缩略图——同一张图被裁小的那一份,+ 除了没有别的可用之外没有任何选它的理由。"""+ cover = self.candidate(800, 538, hashlib.sha256(b"c").hexdigest(),+ face_width=40, source_kind="single_performer_cover")+ thumb = self.candidate(1200, 1200, hashlib.sha256(b"t").hexdigest(),+ face_width=300, source_kind="thumbnail")+ winner, _ = self.module.select_winner([cover, thumb])+ self.assertEqual(winner["width"], 800)++ def test_covers_still_rank_against_each_other_by_face_width(self):+ """同一档之内,判据原样是脸的像素宽。"""+ small = self.candidate(1600, 1076, hashlib.sha256(b"a").hexdigest(),+ face_width=60, source_kind="single_performer_cover")+ big = self.candidate(800, 538, hashlib.sha256(b"b").hexdigest(),+ face_width=210, source_kind="single_performer_cover")+ winner, _ = self.module.select_winner([small, big])+ self.assertEqual(winner["width"], 800)++ def test_an_unknown_source_counts_as_a_portrait(self):+ """认不出来源就当人像档:434 张 gfriends 和 14 张 Stash 头像的 provenance+ 里根本没有 provider 字段,把它们当封面会让 400×400 的社媒头像一路顶掉。"""+ self.assertEqual(self.module.source_tier({}), self.module.PORTRAIT_TIER)+ self.assertEqual(self.module.source_tier({"provider": "gfriends"}),+ self.module.PORTRAIT_TIER)+ self.assertEqual(self.module.source_tier({"provider": "cover-fallback"}),+ self.module.COVER_TIER)+def test_the_bigger_canvas_loses_to_the_bigger_face(self):"""`performer-8711` 的真实两张:640×960 的全身照画布更大,脸只有 67 px;540×810 的半身照画布小 15%,脸有 160 px。头像圆框里认不认得出人,看的是@@ -342,6 +387,38 @@ class InstallGateTests(unittest.TestCase):self.assertTrue(self.module.outranks_incumbent({"width": 400, "height": 400, "face_width": 10}, None))+ def test_a_portrait_replaces_an_installed_cover_even_with_a_smaller_face(self):+ """盘上那张的来源从 provenance 读回来,所以闸门也认得出它是张封面。++ 闸门比的仍是同一把尺 `rank_key`,只是尺的第一格现在是来源档:装着的封面+ 脸再大也在下一档,任何人像都能顶掉它。+ """+ with tempfile.TemporaryDirectory() as tmp:+ directory = Path(tmp).resolve()+ destination = directory / "performer-1.img"+ self.install(directory, "performer-1.img", b"cover",+ self.record(800, 538, 240))+ Path(f"{destination}.provenance.json").write_text(+ json.dumps({"provider": "cover-fallback"}), encoding="utf-8")+ incumbent = self.module.incumbent_row(destination, lambda p: None)+ self.assertEqual(incumbent["provider"], "cover-fallback")+ portrait = {"width": 400, "height": 400, "face_width": 120,+ "source_kind": "official_profile"}+ self.assertTrue(self.module.outranks_incumbent(portrait, incumbent))++ def test_an_installed_portrait_without_provenance_is_not_treated_as_a_cover(self):+ """没有 provenance 的按认不出处理,仍是人像档,脸更小的候选顶不掉它。"""+ with tempfile.TemporaryDirectory() as tmp:+ directory = Path(tmp).resolve()+ destination = directory / "performer-1.img"+ self.install(directory, "performer-1.img", b"portrait",+ self.record(1000, 1500, 400))+ incumbent = self.module.incumbent_row(destination, lambda p: None)+ self.assertEqual(incumbent["provider"], "")+ self.assertFalse(self.module.outranks_incumbent(+ {"width": 400, "height": 400, "face_width": 120,+ "source_kind": "official_profile"}, incumbent))+def test_a_smaller_face_never_overwrites_the_installed_one(self):"""2026-09-06 实测:一趟 --force 换掉 350 张,282 张脸更小。本脚本的候选池(X 的 400×400、jae 的 320×500、作品封面)未必比别的管线装的强。"""