Skip to content

Commit 3117ad3

Browse files
committed
refactor(saucenao): 重构对搜索结果的处理逻辑
1 parent 42e75fa commit 3117ad3

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

YetAnotherPicSearch/saucenao.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from aiohttp import ClientSession
66
from PicImageSearch import SauceNAO
7+
from yarl import URL
78

89
from .ascii2d import ascii2d_search
910
from .config import config
@@ -40,7 +41,6 @@ async def saucenao_search(
4041
final_res = []
4142
if res and res.raw:
4243
selected_res = res.raw[0]
43-
ext_urls = selected_res.origin["data"].get("ext_urls")
4444
# 如果结果为 pixiv ,尝试找到原始投稿,避免返回盗图者的投稿
4545
if selected_res.index_id == saucenao_db["pixiv"]:
4646
pixiv_res_list = list(
@@ -57,29 +57,25 @@ async def saucenao_search(
5757
key=lambda x: int(re.search(r"\d+", x.url).group()), # type: ignore
5858
)
5959
# 如果地址有多个,优先取 danbooru
60-
elif ext_urls and len(ext_urls) > 1:
61-
for i in ext_urls:
60+
elif len(selected_res.ext_urls) > 1:
61+
for i in selected_res.ext_urls:
6262
if "danbooru" in i:
6363
selected_res.url = i
6464
_hide_img = hide_img or selected_res.hidden
6565
if selected_res.similarity < config.saucenao_low_acc:
6666
_hide_img = _hide_img or config.hide_img_when_low_acc
6767
thumbnail = await handle_img(selected_res.thumbnail, _hide_img)
68-
if _source := selected_res.origin["data"].get("source", ""):
69-
source = await shorten_url(_source)
70-
else:
71-
source = await shorten_url(await get_source(selected_res.url))
72-
# 如果结果为 doujin ,尝试返回日文标题而不是英文标题
73-
if selected_res.index_id in saucenao_db["doujin"]: # type: ignore
74-
if title := (
75-
selected_res.origin["data"].get("jp_name")
76-
or selected_res.origin["data"].get("eng_name")
77-
):
78-
selected_res.title = title
68+
source = selected_res.source
69+
if source and source == selected_res.title:
70+
source = ""
71+
if not source and selected_res.url:
72+
source = await get_source(selected_res.url)
73+
if source and URL(source).host:
74+
source = await shorten_url(source)
7975
_url = await shorten_url(selected_res.url)
8076
author = selected_res.author
81-
if author and selected_res.index_id == saucenao_db["pixiv"]:
82-
author = f'[{author}](https://pixiv.net/u/{selected_res.origin["data"]["member_id"]})'
77+
if author and selected_res.author_url:
78+
author = f"[{author}]({await shorten_url(selected_res.author_url)})"
8379
res_list = [
8480
f"SauceNAO ({selected_res.similarity}%)",
8581
thumbnail,

0 commit comments

Comments
 (0)