Skip to content

armgit / peach

Public Imported from GitHub
0

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

fix(links): 写入端把改过名的站点收成现主机

longmeidaoee5f4a41ed11
diff --git a/scripts/install_entity_links.py b/scripts/install_entity_links.py
index 0168d0671..8c5831520 100644
--- a/scripts/install_entity_links.py
+++ b/scripts/install_entity_links.py
@@ -26,6 +26,7 @@ import httpx
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
from peach.review_csv import read_rows # noqa: E402
+from peach.social_links import canonical_url # noqa: E402
from peach.scripting import ( # noqa: E402
BACKUP_REQUIRED,
USER_AGENT,
@@ -39,17 +40,22 @@ FIELDS = ("entity_id", "kind", "name", "link_kind", "label", "url", "evidence")
def normalise_url(url: str) -> str:
- """补上 scheme 并去掉空白。
+ """补上 scheme、去掉空白,改过名的站点收成现主机。
复核表是人和脚本混写的,`moodyz.com` 和 `https://moodyz.com/` 都会出现。不统一的话
同一个站会因为写法不同绕过 `UNIQUE(entity_id,url)` 建出两行。
+
+ 主机别名要在这里收,不能只靠采集器各自记得。`UNIQUE(entity_id,url)` 和下面那句
+ 「已存在,跳过」都只认字面,`twitter.com/kouzaisaki` 和 `x.com/kouzaisaki` 在它们眼里
+ 是两条不同的链接——目录型来源抄的是 2023 年改名前的写法,一批就能建出几百条重复。
+ `normalize_link_hosts.py` 正是为清这个而写的;写入端不收,清完下一批又长回来。
"""
url = url.strip()
if not url:
return ""
if not urlsplit(url).scheme:
url = "https://" + url
- return url
+ return canonical_url(url)
def resolve_entity(connection: sqlite3.Connection, row: dict) -> tuple[int | None, str]:
diff --git a/tests/test_entity_link_install.py b/tests/test_entity_link_install.py
index 6171a0670..99d279aa3 100644
--- a/tests/test_entity_link_install.py
+++ b/tests/test_entity_link_install.py
@@ -96,6 +96,34 @@ class InstallTests(unittest.TestCase):
planned = self.module.plan(self.connection, [self.row(url="moodyz.com/")])
self.assertEqual(planned[0]["action"], "skip")
+ def test_the_old_host_of_a_renamed_site_does_not_become_a_second_row(self):
+ """目录型来源抄的是 2023 年改名前的写法,写入端要把它收成现主机。
+
+ `UNIQUE(entity_id,url)` 和「已存在,跳过」都只认字面,
+ `twitter.com/<handle>` 与 `x.com/<handle>` 在它们眼里是两条链接。minnano-av
+ 一批 710 条里就有 170 条是这个形态,全写进去等于把 `normalize_link_hosts.py`
+ 清掉的那 295 条重新长回来,而资料页会并排显示两枚一样的 X 图标。
+ """
+ self.module.install(
+ self.connection,
+ self.module.plan(self.connection, [self.row(url="https://x.com/kouzaisaki")]),
+ "review.csv")
+ planned = self.module.plan(
+ self.connection, [self.row(url="https://twitter.com/kouzaisaki")])
+ self.assertEqual(planned[0]["action"], "skip")
+ self.assertIn("已存在", planned[0]["reason"])
+ self.assertEqual(
+ self.connection.execute("SELECT count(*) FROM entity_link").fetchone()[0], 1)
+
+ def test_a_renamed_host_is_stored_in_its_current_form(self):
+ """收成现主机是写进库里,不只是拿来比对——否则库里仍是旧写法。"""
+ self.module.install(
+ self.connection,
+ self.module.plan(self.connection, [self.row(url="https://twitter.com/kouzaisaki")]),
+ "review.csv")
+ stored = self.connection.execute("SELECT url FROM entity_link").fetchone()[0]
+ self.assertEqual(stored, "https://x.com/kouzaisaki")
+
def test_an_entity_can_be_matched_by_alias(self):
planned = self.module.plan(self.connection, [self.row(name="プレステージ")])
self.assertEqual(planned[0]["entity_id"], 2)