Skip to content

armgit / peach

Public Imported from GitHub
0

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

fix(downloads): 资源搜索一条都没留下时说明各筛选原因的条数

longmeidao3414e8cc10f0
diff --git a/src/peach/resource_search.py b/src/peach/resource_search.py
index ed25788eb..b8df3f761 100644
--- a/src/peach/resource_search.py
+++ b/src/peach/resource_search.py
@@ -13,7 +13,7 @@ import json
import re
import threading
import time
-from collections import OrderedDict
+from collections import Counter, OrderedDict
from collections.abc import Callable
from dataclasses import dataclass
from urllib.parse import urlsplit
@@ -150,16 +150,28 @@ def torrent_info_hash(data: bytes) -> str:
return hashlib.sha1(bencode2.bencode(info)).hexdigest()
+#: 条目被筛掉的原因。搜索结果为空时据此分开「索引器里没有」和「有,但都不合格」。
+REJECT_REASONS = {"code": "番号不符", "size": "体积不在区间", "seeders": "没有人做种", "hash": "取不到 infohash"}
+
+
def candidate(item, indexer: dict, code: str, filters: Filters,
- fetch: Callable[[str], str | None] | None = None) -> dict | None:
- """`fetch` 把索引器给的种子下载地址换成 infohash;条目自带磁力或 infohash 时不调用。"""
+ fetch: Callable[[str], str | None] | None = None,
+ rejected: Counter | None = None) -> dict | None:
+ """`fetch` 把索引器给的种子下载地址换成 infohash;条目自带磁力或 infohash 时不调用。
+
+ 筛掉的条目按 `REJECT_REASONS` 的键计入 `rejected`。
+ """
+ def reject(reason: str) -> None:
+ if rejected is not None:
+ rejected[reason] += 1
+
title = str(item.title or "")[:500]
if not same_release_code(scan_code(title), code):
- return None
+ return reject("code")
if item.size is None or not filters.min_size <= item.size <= filters.max_size:
- return None
+ return reject("size")
if item.seeders is None or item.seeders <= 0:
- return None
+ return reject("seeders")
info_hash = None
for raw in (item.magnet_url, item.link, item.guid, item.infohash):
if raw:
@@ -171,7 +183,7 @@ def candidate(item, indexer: dict, code: str, filters: Filters,
if info_hash is None and fetch and item.link:
info_hash = fetch(item.link)
if info_hash is None:
- return None
+ return reject("hash")
# 候选只携带规范 hash,索引器返回的 tracker 与 URL 可能含私有 passkey。
quality = qualities(title, item.tags, code)
return {"id": info_hash, "info_hash": info_hash,
@@ -275,6 +287,7 @@ class Search:
def _search(self, indexers: list[dict], code: str, filters: Filters) -> dict:
found: dict[str, dict] = {}
warnings = []
+ rejected: Counter = Counter()
successful = 0
deadline = time.monotonic() + 45
query = code.split("-")[-1] if code.startswith("FC2-") else code
@@ -297,7 +310,7 @@ class Search:
budget[0] -= 1
return _torrent_hash(client, indexer, link, deadline)
for item in items[:MAX_ITEMS]:
- row = candidate(item, indexer, code, filters, fetch)
+ row = candidate(item, indexer, code, filters, fetch, rejected)
if row is None:
continue
prior = found.get(row["info_hash"])
@@ -313,6 +326,11 @@ class Search:
def rank(row):
target = row[filters.goal] if filters.goal in ("chinese", "uncensored") else row["resolution"]
return (-int(target), -row["resolution"], -row["seeders"], row["size"], row["info_hash"])
+ if not found and rejected:
+ # 一条都没留下时说清是哪种「没有」:索引器里有、只是没过筛选,换体积区间或换来源还有救。
+ counts = "、".join(f"{label} {rejected[key]} 条" for key, label in REJECT_REASONS.items() if rejected[key])
+ warnings.append(f"索引器返回了 {sum(rejected.values())} 条结果,都没通过筛选:{counts}")
return {"ok": successful > 0, "state": "ready" if successful else "error",
"items": sorted(found.values(), key=rank), "warnings": warnings,
+ "rejected": dict(rejected),
"error": "" if successful else "所有索引器都未能完成搜索"}
diff --git a/tests/test_downloads_resource_search.py b/tests/test_downloads_resource_search.py
index 5d3482007..0fe6b0508 100644
--- a/tests/test_downloads_resource_search.py
+++ b/tests/test_downloads_resource_search.py
@@ -140,6 +140,14 @@ class SearchTests(unittest.TestCase):
filters=Filters(min_size=100), blocked=[HASH])
self.assertEqual(result["items"], [])
+ def test_when_nothing_passes_the_filters_the_reasons_are_counted(self):
+ result = self.search(feed(item(title="ABC-124"), item(title="ABC-125"), item(seeders=0), item(size=1)),
+ filters=Filters(min_size=100))
+ self.assertEqual(result["items"], [])
+ self.assertEqual(result["rejected"], {"code": 2, "seeders": 1, "size": 1})
+ self.assertEqual(result["warnings"],
+ ["索引器返回了 4 条结果,都没通过筛选:番号不符 2 条、体积不在区间 1 条、没有人做种 1 条"])
+
def test_quality_goal_precedes_size_and_only_five_candidates_are_returned(self):
rows = [item(title=f"ABC-123 1080p {'中字' if at == 6 else ''}",
info_hash=f"{at:040x}", size=(at + 1) * 1024 ** 3) for at in range(7)]