Peach: Local-first video metadata scraper and media manager.
feat: 事务所标识按分辨率择优,并接受人工指定的社媒头像
longmeidao1a166ddf8bbc
diff --git a/scripts/harvest_studio_icons.py b/scripts/harvest_studio_icons.pyindex f6b184d45..828f9a357 100644--- a/scripts/harvest_studio_icons.py+++ b/scripts/harvest_studio_icons.py@@ -23,13 +23,16 @@ favicon 一律退回。实测七个 JAV 厂牌站,六个的 favicon 内容比`人像不是标识`。用户指定的来源不过这道闸——指定就是结论。模型取不到时整轮不设这道闸,统计里说明,不静默当成「都没有脸」。-**声明之外还有两个来源,按这个顺序。** 站点自己 header 里那张 `<img>`-(`site_logos.logo_images`),再是它挂在页面上的 X 账号头像-(`site_logos.x_profiles` + `avatar_tiers`)。bambi.ne.jp 是这条链的实测样本:它只声明-一枚 16×16 的 `favicon.ico`,按声明走只能得出「只有小图标」;header 那张-`header_logo.png` 是 187×57 的字标;而它 X 账号的头像是 119×119 的方标——三样里最能顶-小位的是最后那个。用户 2026-09-05 的口径:站点自己那几张都不清楚、头像反而清楚时,-头像既做 icon 也做 logo。+**声明之外还有三个来源,按这个顺序。** 站点自己 header 里那张 `<img>`+(`site_logos.logo_images`)、人在登录态解出来的社媒头像(`named_avatars`)、+它挂在页面上的 X 账号头像(`site_logos.x_profiles` + `avatar_tiers`)。bambi.ne.jp 是+这条链的实测样本:它只声明一枚 16×16 的 `favicon.ico`;header 那张 `header_logo.png`+是 187×57 的字标;X 头像 119×119;而它 Instagram 的头像是 1000×1000。用户 2026-09-05+的口径:站点自己那几张都不清楚、头像反而清楚时,头像既做 icon 也做 logo。++**够用就停,不够用就比大小。** 排在前面的来源更正式,但正式不等于清楚。取到的短边够+`GOOD_ENOUGH_SHORT_EDGE` 就不再敲别人的门;到不了就把这条链接的其余来源问完,取明显+更大的那枚(`BETTER_BY` 倍以上才算「明显」——114 和 119 谁大不该决定用谁的标)。两条实测逼出来的规矩:@@ -72,7 +75,7 @@ import httpxsys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))from peach import face_detect, images, link_marks, site_icons, site_logos # noqa: E402-from peach.config import GENERATED_DIR, REVIEW_DIR+from peach.config import GENERATED_DIR, REVIEW_DIR, STATE_DIRfrom peach.review_csv import write_rows@@ -103,6 +106,13 @@ MIN_SHORT_EDGE = 32#: `logo` 位是厂牌页那个 160 px 大位,2x 屏要 320 px;96 是「还能看」的下限,#: 低于它说明取到的是缩略图不是标识资产。MIN_LOGO_SHORT_EDGE = 96+#: 够用就停的线:公司格最宽 180 CSS px,2 倍屏要 360 实像素,再大在页面上没有分别。+#: 到不了这条线才把这条链接的其余来源问完——每多问一个来源就多敲一次别人的门。+GOOD_ENOUGH_SHORT_EDGE = 360+#: 后面的来源要大出这个倍数才顶掉前面的。声明的 114 对上头像的 119 谁大不该决定用谁的+#: 标:那点差别在页面上看不出来,而来源的正式程度有差。bambi 声明的 16×16 对上+#: 1000×1000 的头像才是该换的那种差距。+BETTER_BY = 1.5#: `logo` 位的指定来源,按 canonical_name。`icon` 位的覆盖在#: `site_icons.HOST_OVERRIDES`,两张表不能合并:那一张是「按主机发现图标」的例外,@@ -482,6 +492,41 @@ def first_mark(urls: list[str], fetch, policy: "SquareMark") -> tuple[bytes, strreturn None+def named_avatars(path: Path) -> dict[str, str]:+ """`{账本名: 头像地址}`;文件不在就是空的。++ Instagram 的头像地址只有登录态才拿得到:登出页的 `og:image` 是空的,+ `web_profile_info` 接口回 429(2026-09-05 实测)。所以这一份由人在登录的浏览器里+ 解出来写进文件,脚本只管取字节、过闸、和别的来源比大小。地址带签名会过期,+ 隔一段时间要重解一次;过期了就是取不回来,不会拿别的图顶替。++ 键用账本里的规范名而不是 handle:判断「这个账号是不是这家公司的」需要人看一眼,+ krone 官号叫 `krone_official__`,而 `miyu_krone` 是艺人的号,形状上分不开。+ """+ try:+ data = json.loads(path.read_text(encoding="utf-8"))+ except (OSError, ValueError):+ return {}+ found: dict[str, str] = {}+ for name, item in (data or {}).items():+ url = item.get("url") if isinstance(item, dict) else item+ if isinstance(url, str) and url.startswith("http"):+ found[str(name)] = url+ return found+++def _enough(found: list[tuple[bytes, str, str, str, str]]) -> bool:+ return any(_short_edge(item[3]) >= GOOD_ENOUGH_SHORT_EDGE for item in found)+++def _best(found: list[tuple[bytes, str, str, str, str]]+ ) -> tuple[bytes, str, str, str, str]:+ """这一趟收到的候选里用哪一枚:来源优先,除非后面有明显更大的。"""+ ranked = max(found, key=lambda item: _short_edge(item[3]))+ first = max(1, _short_edge(found[0][3]))+ return ranked if _short_edge(ranked[3]) >= first * BETTER_BY else found[0]++def _short_edge(size: str) -> int:"""`"187x57"` → 57;量不到就是 0。"""parts = str(size).split("x")@@ -606,13 +651,18 @@ def hunt_logo_row(safe: str, target: dict[str, str], entry: dict[str, str],def icon_row(safe: str, target: dict[str, str], entries: list[dict[str, str]],- fetch, candidate_dir: Path, faces=None+ fetch, candidate_dir: Path, faces=None, avatar: str = "") -> tuple[dict[str, object], dict[str, object] | None]:"""`icon` 位一行。一个厂牌可能挂多条链接,第一条做成就停。- 每条链接依次问三个来源:站点声明的图标、站点 header 里那张 `<img>`、站点挂着的- X 账号头像。三样都过同一道闸(方、够大、不是人像),所以先问哪一个不影响合不合格,- 只决定同样合格时用谁的——站点自己声明的最正式,排前面。+ 每条链接依次问四个来源:站点声明的图标、站点 header 里那张 `<img>`、人在登录态+ 解出来的社媒头像(`named_avatars`)、站点挂着的 X 账号头像。四样都过同一道闸+ (方、够大、不是人像),排序即正式程度。++ **够用就停,不够用就比大小。** 声明那一枚够 `GOOD_ENOUGH_SHORT_EDGE` 就不再敲别人+ 的门;到不了就把这条链接的其余来源问完,取明显更大的那枚(`_best`)。bambi 声明的+ 是 119×119,而它 Instagram 的头像是 1000×1000——按「第一枚合格的就用」会把那张+ 糊的装上去,页面上一眼就看得出来。第二个返回值是同一趟带出来的 `logo` 行:走字标补白时是那张补白图,做成方标时由`hunt_logo_row` 决定,都没有就是 None。@@ -626,31 +676,34 @@ def icon_row(safe: str, target: dict[str, str], entries: list[dict[str, str]],scope = EntityScope()for entry in entries:before = getattr(fetch, "fetched", 0)+ found: list[tuple[bytes, str, str, str, str]] = []made = site_icons.best_mark(entry["url"], fetch, policy,accept=scope if shares_its_host(entry["url"]) else None)- found = ((made, entry["url"], "官网声明的图标", policy.size, policy.aspect)- if made else None)- # 声明那一枚做成了、大位还空着时也要翻一遍首页:SO MODEL AGENT 声明的+ if made:+ found.append((made, entry["url"], "官网声明的图标", policy.size, policy.aspect))+ # 声明那一枚够大、大位又已经有图时才免掉首页那一趟:SO MODEL AGENT 声明的# favicon 只有 114×114,而 header 里挂着 600×150 的完整字标,那才是大位要的。- marks, profiles = (site_assets(entry["url"], fetch)- if found is None or not target["installed"] else ([], []))- if found is None:+ if not _enough(found) or not target["installed"]:+ marks, profiles = site_assets(entry["url"], fetch)own = first_mark(marks, fetch, policy)if own:- found = (own[0], own[1], "站点 header 里的 <img>",- policy.size, policy.aspect)- elif marks:- first_mark(marks, fetch, policy)- if found is None:+ found.append((own[0], own[1], "站点 header 里的 <img>",+ policy.size, policy.aspect))+ if avatar and not _enough(found):+ got = first_mark([avatar], fetch, policy)+ if got:+ found.append((got[0], got[1], "人指定的社媒头像",+ policy.size, policy.aspect))for profile in profiles:- avatar = first_mark(avatar_urls(profile, fetch), fetch, policy)- if avatar:- found = (avatar[0], avatar[1], f"{profile} 的头像",- policy.size, policy.aspect)+ if _enough(found):break+ got = first_mark(avatar_urls(profile, fetch), fetch, policy)+ if got:+ found.append((got[0], got[1], f"{profile} 的头像",+ policy.size, policy.aspect))if found:- return _made_rows(safe, target, entry, policy, found, candidate_dir)+ return _made_rows(safe, target, entry, policy, _best(found), candidate_dir)reachable = reachable or getattr(fetch, "fetched", 0) > beforeattempts.append(entry["url"])@@ -807,7 +860,8 @@ def wordmark_source_rows(safe: str, target: dict[str, str], fetch, candidate_dirdef harvest(targets: dict[str, dict[str, str]],links: dict[str, list[dict[str, str]]],- fetch, candidate_dir: Path, faces=None) -> list[dict[str, object]]:+ fetch, candidate_dir: Path, faces=None,+ avatars: dict[str, str] | None = None) -> list[dict[str, object]]:"""每个目标厂牌出一行 `icon`;有指定 logo 来源或走了字标补白的再出 `logo` 行。有指定字标来源的厂牌只走那一条,两行都从同一张方图出(`wordmark_source_rows`)。@@ -830,7 +884,10 @@ def harvest(targets: dict[str, dict[str, str]],if marked_logo is not None:rows.append(marked_logo)continue- icon, wordmark_logo = icon_row(safe, target, entries, fetch, candidate_dir, faces)+ name = (entries[0]["studio"] if entries+ else LOGO_SOURCE_NAMES.get(safe, safe.replace("_", " ")))+ icon, wordmark_logo = icon_row(safe, target, entries, fetch, candidate_dir, faces,+ (avatars or {}).get(name, ""))logo = logo_row(safe, target, entries, fetch, candidate_dir)if (logo is not None and logo["verdict"] == OKand icon["verdict"] not in INSTALLABLE):@@ -914,7 +971,7 @@ def run(args: argparse.Namespace) -> dict[str, object]:client = httpx.Client(trust_env=True, follow_redirects=True)try:rows = harvest(targets, links, Fetcher(client, args.timeout, args.interval),- args.candidate_dir.resolve(), faces)+ args.candidate_dir.resolve(), faces, named_avatars(args.avatars))finally:client.close()@@ -943,6 +1000,8 @@ def main(argv: list[str] | None = None) -> int:parser.add_argument("--output", type=Path,default=REVIEW_DIR / "studio-icons.csv")parser.add_argument("--logo-root", type=Path, default=GENERATED_DIR / "logos")+ parser.add_argument("--avatars", type=Path, default=STATE_DIR / "agency-avatars.json",+ help="人在登录态解出来的社媒头像地址,按账本名")parser.add_argument("--candidate-dir", type=Path,default=REVIEW_DIR / "studio-icons")parser.add_argument("--only", nargs="*", default=[],diff --git a/src/peach/site_logos.py b/src/peach/site_logos.pyindex aa2ea0bb7..fd7c6cc55 100644--- a/src/peach/site_logos.py+++ b/src/peach/site_logos.py@@ -15,9 +15,11 @@ bambi.ne.jp 就是(2026-09-05 实测),按声明走只能得出「只有小**og:image 不进这里。** 那一位通常是分享卡片用的横幅或首屏剧照(bambi.ne.jp 的`images/1/image.jpg` 连图都解不开),命中率低、错得响。要用得由人指定。-**社媒头像只认 X。** Instagram 登出页不给头像:`og:image` 是空的,HTML 里也没有-`profile_pic_url`(2026-09-05 对 `bambi.hajimero` 实测)。TikTok 的 oembed 头像只有-百来像素。X 登出页的 `og:image` 指向 `pbs.twimg.com/profile_images/...`,按+**自动发现的社媒头像只认 X。** Instagram 登出页不给头像:`og:image` 是空的,HTML 里+也没有 `profile_pic_url`(2026-09-05 对 `bambi.hajimero` 实测);登录态才有,而那要+浏览器里的会话,脚本拿不到,所以 Instagram 的头像地址由人解出来写进+`harvest_studio_icons.named_avatars` 读的那份文件。TikTok 的 oembed 头像只有百来像素。+X 登出页的 `og:image` 指向 `pbs.twimg.com/profile_images/...`,按`social_links.twimg_tiers` 退档就能拿到上传原图,这条路 `harvest_social_avatars`已经走通并用在女优头像上,这里复用同一条。diff --git a/tests/test_studio_icon_variants.py b/tests/test_studio_icon_variants.pyindex 931579988..4f93839b8 100644--- a/tests/test_studio_icon_variants.py+++ b/tests/test_studio_icon_variants.py@@ -1105,7 +1105,7 @@ class RunTests(unittest.TestCase):database=self.database, output=self.root / "studio-icons.csv",logo_root=self.logos, candidate_dir=self.root / "candidates",only=[], kind="studio", interval=0.0, timeout=1.0, install=install,- no_face_gate=True))+ no_face_gate=True, avatars=self.root / "avatars.json"))finally:MODULE.site_icons.best_mark = original@@ -1123,7 +1123,7 @@ class RunTests(unittest.TestCase):database=self.database, output=self.root / "agency-icons.csv",logo_root=self.logos, candidate_dir=self.root / "candidates",only=[], kind="agency", interval=0.0, timeout=1.0, install=False,- no_face_gate=True))+ no_face_gate=True, avatars=self.root / "avatars.json"))finally:MODULE.site_icons.best_mark = originalfrom peach.review_csv import read_rows@@ -1141,7 +1141,7 @@ class RunTests(unittest.TestCase):database=self.database, output=self.root / "agency-icons.csv",logo_root=self.logos, candidate_dir=self.root / "candidates",only=[], kind="agency", interval=0.0, timeout=1.0, install=False,- no_face_gate=True))+ no_face_gate=True, avatars=self.root / "avatars.json"))finally:MODULE.site_icons.best_mark = originalfrom peach.review_csv import read_rows@@ -1183,7 +1183,7 @@ class RunTests(unittest.TestCase):database=self.database, output=self.root / "studio-icons.csv",logo_root=self.logos, candidate_dir=self.root / "candidates",only=["HEYZO"], kind="studio", interval=0.0, timeout=1.0, install=False,- no_face_gate=True))+ no_face_gate=True, avatars=self.root / "avatars.json"))finally:MODULE.site_icons.best_mark = originalself.assertEqual(stats["复核行"], 1)@@ -1372,7 +1372,7 @@ class SiteOwnAssetTests(unittest.TestCase):pages.update(overrides)return Fetch(pages)- def harvest(self, fetch=None, faces=None, declared=None, targets=None):+ def harvest(self, fetch=None, faces=None, declared=None, targets=None, avatars=None):original = MODULE.site_icons.best_markdef stub(url, fetcher, policy, fallback=None, accept=None):@@ -1386,7 +1386,7 @@ class SiteOwnAssetTests(unittest.TestCase):try:return MODULE.harvest(self.targets if targets is None else targets,self.links, fetch or self.fetch(),- self.candidates, faces)+ self.candidates, faces, avatars)finally:MODULE.site_icons.best_mark = original@@ -1432,6 +1432,47 @@ class SiteOwnAssetTests(unittest.TestCase):with Image.open(io.BytesIO(Path(str(logo["candidate"])).read_bytes())) as image:self.assertEqual(image.size[0], image.size[1], "装盘的一律是方图")+ def test_a_much_bigger_avatar_beats_the_declared_icon(self):+ """bambi 声明的是 119×119,它 Instagram 的头像是 1000×1000。++ 「第一枚合格的就用」会把那张糊的装上去,而公司格是 180 px 宽、2 倍屏 360 实像素,+ 119 缩放上去一眼就看得出。+ """+ icon = self.harvest(+ fetch=self.fetch(**{self.AVATAR: block_png((1000, 1000))}),+ declared=block_png((119, 119)))[0]+ self.assertEqual(icon["mark_size"], "1000x1000")+ self.assertIn("的头像", icon["evidence"])++ def test_a_marginally_bigger_avatar_does_not_displace_the_declared_icon(self):+ """119 和 114 谁大不该决定用谁的标:那点差别在页面上看不出来。"""+ icon = self.harvest(declared=block_png((114, 114)))[0]+ self.assertEqual((icon["evidence"], icon["mark_size"]),+ ("官网声明的图标", "114x114"))++ def test_a_big_enough_declared_icon_stops_the_walk(self):+ """够大就不再敲别人的门。多问一个来源就多一次请求,而结果不会更好。"""+ fetch = self.fetch()+ self.harvest(fetch=fetch, declared=block_png((512, 512)),+ targets={"Bambi_Promotion": {"original_size": "187x57",+ "installed": "Bambi_Promotion.img"}})+ self.assertNotIn(self.PROFILE, fetch.asked)+ self.assertNotIn(self.AVATAR, fetch.asked)++ def test_a_named_avatar_is_tried_before_the_x_account(self):+ """人指定的那一个是判断过的:`krone_official__` 是公司号,`miyu_krone` 是艺人号,+ 形状上分不开,所以这一步不交给脚本猜。"""+ named = "https://scontent.cdninstagram.com/v/t51/bambi_1000.jpg"+ fetch = self.fetch(**{named: block_png((1000, 1000))})+ icon = self.harvest(fetch=fetch, avatars={"Bambi Promotion": named})[0]+ self.assertEqual(icon["url"], named)+ self.assertIn("人指定的社媒头像", icon["evidence"])++ def test_a_named_avatar_that_cannot_be_fetched_falls_back(self):+ """签名地址会过期。过期就是取不回来,不能拿别的图冒充它。"""+ icon = self.harvest(avatars={"Bambi Promotion": "https://scontent/expired.jpg"})[0]+ self.assertEqual(icon["url"], self.AVATAR)+def test_an_installed_image_keeps_the_hero_slot(self):"""`<safe>.img` 多半正是这家的完整字标,而 `.logo.img` 排在它前面。出这一行等于拿一枚 28 px 用的方标把大位上本来对的那张顶掉。"""@@ -1473,6 +1514,40 @@ class SiteOwnAssetTests(unittest.TestCase):self.assertNotIn("intent/tweet", " ".join(fetch.asked))+class NamedAvatarTests(unittest.TestCase):+ """人在登录态解出来的头像地址,按账本名读进来。"""++ def setUp(self):+ self.tmp = tempfile.TemporaryDirectory()+ self.addCleanup(self.tmp.cleanup)+ self.path = Path(self.tmp.name).resolve() / "agency-avatars.json"++ def write(self, text):+ self.path.write_text(text, encoding="utf-8")+ return MODULE.named_avatars(self.path)++ def test_a_missing_file_is_simply_empty(self):+ """这一份是可选的补充。没有它整轮照跑,不该报错也不该停。"""+ self.assertEqual(MODULE.named_avatars(self.path), {})++ def test_the_url_may_carry_a_note_beside_it(self):+ """地址是签名过的一长串,边上要能写清它是谁的号、什么时候解的。"""+ found = self.write('{"Bambi Promotion": {"url": "https://cdn/x.jpg",'+ ' "note": "instagram @bambi.hajimero 1000x1000"}}')+ self.assertEqual(found, {"Bambi Promotion": "https://cdn/x.jpg"})++ def test_a_bare_string_is_accepted_too(self):+ self.assertEqual(self.write('{"LINX": "https://cdn/linx.jpg"}'),+ {"LINX": "https://cdn/linx.jpg"})++ def test_junk_entries_are_dropped_instead_of_failing_the_run(self):+ found = self.write('{"A": {"note": "还没解"}, "B": "", "C": "https://cdn/c.jpg"}')+ self.assertEqual(found, {"C": "https://cdn/c.jpg"})++ def test_a_broken_file_does_not_take_the_harvest_down_with_it(self):+ self.assertEqual(self.write("{不是 JSON"), {})++class FetcherCacheTests(unittest.TestCase):"""同一份首页这一趟要被问三次:声明、header 里的 `<img>`、页面上的账号。"""