Peach: Local-first video metadata scraper and media manager.
fix(links): 存档链接的原站给不出合格圆标时用存档站自己的图标
longmeidao75b849a74236
diff --git a/src/peach/routes_media.py b/src/peach/routes_media.pyindex 2898beac5..6d4ca2d74 100644--- a/src/peach/routes_media.py+++ b/src/peach/routes_media.py@@ -952,9 +952,15 @@ def _site_mark_response(state, url: str, root: Path):return Nonereturn upstream.content, upstream.headers.get("content-type", "")- # 两条通道都不适用时退回原样缩图:糊一点也好过露出地球图标。+ # 两条通道都不适用时退回原样缩图:糊一点也好过露出地球图标。存档链接例外:原站+ # 给不出合格的圆标就用存档站自己的图标,点过去本来就是存档。原站那张不合格的图+ # 原样缩进来是变形的:ONE'S DOUBLE 只留下 7:1 的横排字标。+ archived = page != urlmade = site_icons.best_mark(page, fetch, link_marks.render_mark,- fallback=link_marks.plain_mark)+ fallback=None if archived else link_marks.plain_mark)+ if made is None and archived:+ made = site_icons.best_mark(url, fetch, link_marks.render_mark,+ fallback=link_marks.plain_mark)if made:root.mkdir(parents=True, exist_ok=True)cached.write_bytes(made)diff --git a/src/peach/site_icons.py b/src/peach/site_icons.pyindex 44ce5b421..3f448f12f 100644--- a/src/peach/site_icons.py+++ b/src/peach/site_icons.py@@ -246,12 +246,6 @@ HOST_OVERRIDES: dict[str, tuple[str, ...]] = {"https://images-assets-ht.project1content.com/BangBros/MA/Subsites/""6480a83860e9c0.34870333.png",),- # ONE'S DOUBLE 2022 年关门,官网只剩存档;快照里的 favicon.ico 是 WordPress 的默认- # 图标,不是这家公司的。页脚那枚 SVG 标识与事务所页的字标是同一份(`studio_icons`)。- "ones-double.com": (- "https://web.archive.org/web/20220130000436id_/"- "https://ones-double.com/wp/assets/img/footer-logo.svg",- ),}diff --git a/tests/test_fastapi_api.py b/tests/test_fastapi_api.pyindex 6adeaad1b..088e0283e 100644--- a/tests/test_fastapi_api.py+++ b/tests/test_fastapi_api.py@@ -845,8 +845,8 @@ class FastApiContractTests(unittest.IsolatedAsyncioTestCase):await self.client.get("/site-mark?t=secret&domain=kemono.cr")self.assertTrue(any(host.endswith("kemono.cr") for host in hosts[reached:]), hosts)- async def test_archived_links_of_two_sites_get_two_marks(self):- """存档链接的圆标按快照保存的原站缓存,两家关了门的公司不共用 web.archive.org 那一枚。"""+ def _archive_links(self, colour_of):+ """两条存档链接;上游按 `colour_of(url)` 给一枚圆形图标,给 None 就 404。返回请求过的地址。"""from PIL import Image, ImageDrawfrom peach import routes_media@@ -855,19 +855,16 @@ class FastApiContractTests(unittest.IsolatedAsyncioTestCase):routes_media.GENERATED_DIR = self.root / "generated"asked = []- def icon(colour):+ def upstream(request):+ asked.append(str(request.url))+ colour = colour_of(str(request.url)) if request.url.path.endswith(".png") else None+ if colour is None:+ return httpx.Response(404, request=request)buffer = io.BytesIO()image = Image.new("RGBA", (180, 180), (255, 255, 255, 0))ImageDraw.Draw(image).ellipse((20, 20, 160, 160), fill=colour)image.save(buffer, format="PNG")- return buffer.getvalue()-- def upstream(request):- asked.append(str(request.url))- if not request.url.path.endswith(".png"):- return httpx.Response(404, request=request)- colour = "red" if "ones-double.com" in str(request.url) else "blue"- return httpx.Response(200, content=icon(colour), request=request,+ return httpx.Response(200, content=buffer.getvalue(), request=request,headers={"content-type": "image/png"})self._swap_http_client(upstream)with closing(sqlite3.connect(self.db)) as connection, connection:@@ -876,6 +873,11 @@ class FastApiContractTests(unittest.IsolatedAsyncioTestCase):" VALUES(?,1,'official','官网存档(2022-05)',?,'web.archive.org')",[(901, "https://web.archive.org/web/20220512171500/https://ones-double.com/people/a/"),(902, "https://web.archive.org/web/20221013093626/https://all-p.jp/talent/")])+ return asked++ async def test_archived_links_of_two_sites_get_two_marks(self):+ """存档链接的圆标按快照保存的原站缓存,两家关了门的公司不共用 web.archive.org 那一枚。"""+ asked = self._archive_links(lambda url: "red" if "ones-double.com" in url else "blue")answers = [await self.client.get(f"/link-mark?t=secret&id={link_id}")for link_id in (901, 902)]self.assertEqual([answer.status_code for answer in answers], [200, 200])@@ -883,6 +885,17 @@ class FastApiContractTests(unittest.IsolatedAsyncioTestCase):self.assertEqual(len(list((self.root / "generated" / "link-marks").iterdir())), 2)self.assertTrue(all(url.startswith("https://web.archive.org/") for url in asked), asked)+ async def test_an_archived_site_without_a_usable_icon_shows_the_archive_one(self):+ """原站在快照里给不出合格的圆标,就用存档站自己的:点过去本来就是存档。"""+ from PIL import Image++ self._archive_links(lambda url: None if "id_/" in url else "green")+ answer = await self.client.get("/link-mark?t=secret&id=901")+ self.assertEqual(answer.status_code, 200)+ # 单色透明的图标做成品牌色圆底白字,圆底那一圈就是存档站给的绿。+ red, green, blue, _alpha = Image.open(io.BytesIO(answer.content)).convert("RGBA").getpixel((64, 6))+ self.assertGreater(green, max(red, blue))+async def test_unauthorized_keeps_three_shapes_grouped_by_route_class(self):"""401 三种形态按路由类分组,收敛到 Depends 之后也不许并成一种。diff --git a/tests/test_site_icons.py b/tests/test_site_icons.pyindex 3913d302b..bb91e12be 100644--- a/tests/test_site_icons.py+++ b/tests/test_site_icons.py@@ -261,7 +261,7 @@ class ArchiveViewTests(unittest.TestCase):def test_an_address_already_inside_the_archive_is_fetched_as_is(self):_, locate = social_links.archive_view(self.SNAPSHOT)- pinned = site_icons.overrides_for("https://ones-double.com/")[0].url+ pinned = "https://web.archive.org/web/20220130000436id_/https://ones-double.com/logo.svg"self.assertEqual(locate(pinned), pinned)def test_a_live_link_is_left_alone(self):