Skip to content

armgit / peach

Public Imported from GitHub
0

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

fix(follow): 按名字查找 rule34.xxx 时前缀命中的标签也列为候选

longmeidao9063a86d017b
diff --git a/src/peach/follow_discovery.py b/src/peach/follow_discovery.py
index feaf7c234..b02307423 100644
--- a/src/peach/follow_discovery.py
+++ b/src/peach/follow_discovery.py
@@ -265,32 +265,53 @@ def _rule34xxx_candidates(term: str, transport, credential: Credential | None) -
先前这里把手柄逐字当标签查一遍,写法差一个分隔符就是零命中:`Ria_neearts`
什么都查不到,站上写作 `ria-neearts`,248 件作品。补全接口不需要凭据,
因此发现阶段也不再需要——凭据仍然是**抓取**这条订阅的前提。
+
+ 补全按字面前缀返回,而敲进查找框的常常是名字的开头。抹掉分隔符后与输入
+ 相同的写法是精确命中,只列它们;一个精确命中都没有时,前缀命中的标签也是
+ 站上真实存在的写法,照列出来、证据写明以该词开头,由人分辨是不是同一个人。
"""
wanted = identity_key(term)
if not wanted:
return []
connector = Rule34XxxConnector(transport=transport, credential=credential, max_items=1)
- picked: list[Candidate] = []
- seen: set[str] = set()
+ exact: list[tuple[str, int]] = []
+ partial: list[tuple[str, int]] = []
for probe in spelling_variants(term):
for tag, count in connector.autocomplete(probe):
- canonical = canonical_source_ref("rule34xxx", tag)
- if identity_key(tag) != wanted or canonical in seen:
+ key = identity_key(tag)
+ if not key.startswith(wanted):
continue
- seen.add(canonical)
- picked.append(Candidate(
- "rule34xxx", canonical, _rule34xxx_tag_url(canonical),
- canonical.replace("_", " "), "work",
- f"站内标签 {canonical} 下有 {count} 件作品" if count
- else f"站内存在标签 {canonical}"))
- if len(picked) >= MAX_CANDIDATES_PER_SOURCE:
- return picked
- if picked:
- return picked
- if credential is None:
+ (exact if key == wanted else partial).append((tag, count))
+ # 精确写法有命中就不必再试其余分隔符;前缀相似的命中要等所有写法都问完,
+ # 才知道真的没有精确的那一个。
+ if exact:
+ break
+ # 精确命中就是这个人,只列它们;一个精确命中都没有时,才把前缀相似的
+ # 写法列出来当候选,证据写明以该词开头。
+ hits = exact if exact else partial
+ prefix_hit = not exact
+ picked: list[Candidate] = []
+ seen: set[str] = set()
+ for tag, count in hits:
+ canonical = canonical_source_ref("rule34xxx", tag)
+ if canonical in seen:
+ continue
+ seen.add(canonical)
+ if prefix_hit:
+ evidence = (f"站内标签以该词开头:{canonical} 下有 {count} 件作品" if count
+ else f"站内存在以该词开头的标签 {canonical}")
+ else:
+ evidence = (f"站内标签 {canonical} 下有 {count} 件作品" if count
+ else f"站内存在标签 {canonical}")
+ picked.append(Candidate(
+ "rule34xxx", canonical, _rule34xxx_tag_url(canonical),
+ canonical.replace("_", " "), "work", evidence))
+ if len(picked) >= MAX_CANDIDATES_PER_SOURCE:
+ break
+ if credential is None or picked:
return picked
# 补全一次只回十条,热门前缀会把完整写法挤出去。有凭据时再按原样查一次标签,
- # 这条路径不受那个上限影响。
+ # 这条路径不受那个上限影响;只有补全一个精确命中都没给到时才轮到它。
tag = canonical_source_ref("rule34xxx", re.sub(r"\s+", "_", term.strip()))
if not connector.fetch(tag).candidates:
return picked
diff --git a/tests/test_follow_discovery.py b/tests/test_follow_discovery.py
index a552a3f2f..ccf4470a9 100644
--- a/tests/test_follow_discovery.py
+++ b/tests/test_follow_discovery.py
@@ -294,11 +294,44 @@ class DiscoverTests(_DiscoveryCase):
self.assertEqual([c.ref for c in found.candidates], ["ria-neearts"])
self.assertEqual(found.failures, {})
- def test_a_tag_that_merely_starts_with_the_term_is_not_a_hit(self):
- # 补全按前缀返回,`riahri` 也在结果里。名字不同就不是这个人,不能拿来登记。
+ def test_an_exact_hit_leaves_out_prefix_neighbors(self):
+ # 精确写法就是这个人;名字相似但不同的标签不再并列出现,候选列表只留给
+ # 真正命中的那一个。
+ routes = {"autocomplete.php": HttpResponse(200, {}, b"["
+ b"{\"label\": \"ria (12)\", \"value\": \"ria\"},"
+ b"{\"label\": \"ria-neearts (248)\", \"value\": \"ria-neearts\"},"
+ b"{\"label\": \"riahri (156)\", \"value\": \"riahri\"}]")}
+ found = self._discover("ria", routes, providers=("rule34xxx",))
+ self.assertEqual([c.ref for c in found.candidates], ["ria"])
+ self.assertEqual(found.candidates[0].evidence, "站内标签 ria 下有 12 件作品")
+
+ def test_a_prefix_hit_is_a_candidate_when_no_spelling_matched_exactly(self):
+ # 敲名字的开头、站上的标签比它长时,精确判据一这条也找不到。前缀命中的
+ # 写法仍是站上真实存在的标签,照列出来、证据写明以该词开头,由人分辨。
routes = {"autocomplete.php": HttpResponse(200, {}, R34_AUTOCOMPLETE_HIT)}
found = self._discover("ria", routes, providers=("rule34xxx",))
- self.assertEqual(found.candidates, ())
+ self.assertEqual([c.ref for c in found.candidates], ["ria-neearts", "riahri"])
+ self.assertEqual(found.candidates[0].evidence,
+ "站内标签以该词开头:ria-neearts 下有 248 件作品")
+ self.assertEqual(found.candidates[1].evidence,
+ "站内标签以该词开头:riahri 下有 156 件作品")
+ self.assertIn("tags=ria-neearts", found.candidates[0].url)
+
+ def test_a_prefix_hit_without_a_count_still_states_itself(self):
+ routes = {"autocomplete.php": HttpResponse(200, {}, b"["
+ b"{\"label\": \"forceball_fx\", \"value\": \"forceball_fx\"}]")}
+ found = self._discover("forceball", routes, providers=("rule34xxx",))
+ self.assertEqual([c.ref for c in found.candidates], ["forceball_fx"])
+ self.assertEqual(found.candidates[0].evidence,
+ "站内存在以该词开头的标签 forceball_fx")
+
+ def test_prefix_neighbors_do_not_shadow_the_variant_that_matched_exactly(self):
+ # 补全按字面前缀返回:`Ria_neearts` 查不出 `ria-neearts`,要换分隔符写法
+ # 再问一次。前一种写法带回来的相似标签不能盖住后一种写法的精确命中。
+ routes = {"autocomplete.php?q=Ria_neearts": HttpResponse(200, {}, R34_AUTOCOMPLETE_HIT),
+ "autocomplete.php?q=Ria-neearts": HttpResponse(200, {}, R34_AUTOCOMPLETE_HIT)}
+ found = self._discover("Ria_neearts", routes, providers=("rule34xxx",))
+ self.assertEqual([c.ref for c in found.candidates], ["ria-neearts"])
def test_an_f95_collection_thread_is_found_by_the_forum_search(self):
# `latest_data.php` 只索引 Latest Updates,艺术家的 Collection 帖不在里面。