diff options
Diffstat (limited to 'ATRI/plugins/saucenao')
-rw-r--r-- | ATRI/plugins/saucenao/__init__.py | 7 | ||||
-rw-r--r-- | ATRI/plugins/saucenao/data_source.py | 53 |
2 files changed, 26 insertions, 34 deletions
diff --git a/ATRI/plugins/saucenao/__init__.py b/ATRI/plugins/saucenao/__init__.py index b55f18c..468b001 100644 --- a/ATRI/plugins/saucenao/__init__.py +++ b/ATRI/plugins/saucenao/__init__.py @@ -16,7 +16,6 @@ _search_flmt_notice = choice(["慢...慢一..点❤", "冷静1下", "歇会歇� saucenao = SaouceNao().on_command("以图搜图", "透过一张图搜索可能的来源") - @saucenao.args_parser # type: ignore async def _get_img(bot: Bot, event: MessageEvent, state: T_State): msg = str(event.message).strip() @@ -28,18 +27,16 @@ async def _get_img(bot: Bot, event: MessageEvent, state: T_State): else: state["img"] = msg - @saucenao.handle() async def _ready_search(bot: Bot, event: MessageEvent, state: T_State): user_id = event.get_user_id() if not _search_flmt.check(user_id): await saucenao.finish(_search_flmt_notice) - + msg = str(event.message).strip() if msg: state["img"] = msg - @saucenao.got("img", "图呢?") async def _deal_search(bot: Bot, event: MessageEvent, state: T_State): user_id = event.get_user_id() @@ -47,7 +44,7 @@ async def _deal_search(bot: Bot, event: MessageEvent, state: T_State): img = findall(r"url=(.*?)]", msg) if not img: await saucenao.reject("请发送图片而不是其他东西!!") - + a = SaouceNao(SauceNAO.key) result = f"> {MessageSegment.at(user_id)}" + await a.search(img[0]) _search_flmt.start_cd(user_id) diff --git a/ATRI/plugins/saucenao/data_source.py b/ATRI/plugins/saucenao/data_source.py index 3f77118..4f38f2c 100644 --- a/ATRI/plugins/saucenao/data_source.py +++ b/ATRI/plugins/saucenao/data_source.py @@ -3,27 +3,23 @@ from random import choice from ATRI.service import Service from ATRI.rule import is_in_service from ATRI.exceptions import RequestError -from ATRI.utils import request, UbuntuPaste - +from ATRI.utils import request URL = "https://saucenao.com/search.php" - __doc__ = """ 以图搜图,仅限二刺螈 """ class SaouceNao(Service): - def __init__( - self, - api_key: str = None, - output_type=2, - testmode=1, - dbmaski=32768, - db=5, - numres=5, - ): + def __init__(self, + api_key: str = None, + output_type=2, + testmode=1, + dbmaski=32768, + db=5, + numres=5): Service.__init__(self, "以图搜图", __doc__, rule=is_in_service("以图搜图")) params = dict() @@ -50,26 +46,25 @@ class SaouceNao(Service): res = data["results"] result = list() - for i in range(len(res)): - data = res[i] + for i in range(3): + sim = res[i]["header"]["similarity"] + if float(sim) >= 80: + data = res[i] - _result = dict() - _result["similarity"] = data["header"]["similarity"] - _result["index_name"] = data["header"]["index_name"] - _result["url"] = choice(data["data"].get("ext_urls", ["None"])) - result.append(_result) + _result = dict() + _result["similarity"] = sim + _result["index_name"] = data[i]["header"]["index_name"] + _result["url"] = choice(data["data"].get("ext_urls", ["None"])) + result.append(_result) msg0 = str() for i in result: - msg0 += ( - "\n——————————\n" - f"Similarity: {i['similarity']}\n" - f"Name: {i['index_name']}\n" - f"URL: {i['url'].replace('https://', '')}" - ) + msg0 += ("\n——————————\n" + f"Similarity: {i['similarity']}\n" + f"Name: {i['index_name']}\n" + f"URL: {i['url'].replace('https://', '')}") - if len(res) <= 3: - return msg0 + if not result: + return "没有相似的结果呢..." else: - repo = f"\n详细请移步此处~\n{await UbuntuPaste(content=msg0).paste()}" - return repo + return msg0 |