From ae7ea2d379ec7fb0edb8e333145bb141106a2fd2 Mon Sep 17 00:00:00 2001 From: Kyomotoi <1172294279@qq.com> Date: Sat, 20 Feb 2021 08:19:41 +0800 Subject: =?UTF-8?q?=E2=9C=A8=20=E6=9B=B4=E6=96=B0=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=EF=BC=8C=E5=9F=8B=E4=B8=8Bbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 更新插件: - call-owner - code-runner - status - anime-search - tex(待修复) - 埋下bug: - service中limit作为机器人服务中的开关,目前写入文件亟待修复 --- ATRI/plugins/anime-search/__init__.py | 89 +++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 ATRI/plugins/anime-search/__init__.py (limited to 'ATRI/plugins/anime-search/__init__.py') diff --git a/ATRI/plugins/anime-search/__init__.py b/ATRI/plugins/anime-search/__init__.py new file mode 100644 index 0000000..61f523b --- /dev/null +++ b/ATRI/plugins/anime-search/__init__.py @@ -0,0 +1,89 @@ +import re +import json + +from nonebot.adapters.cqhttp.message import MessageSegment +from nonebot.plugin import on_command +from nonebot.adapters.cqhttp import Bot, MessageEvent +from nonebot.adapters.cqhttp.message import Message +from nonebot.typing import T_State + +from ATRI.rule import is_in_banlist, is_in_dormant +from ATRI.exceptions import RequestTimeOut +from ATRI.utils.request import get_bytes + +from .data_source import to_simple_string + + +URL = "https://trace.moe/api/search?url=" + + +anime_search = on_command( + "/anime", + rule=is_in_banlist() & is_in_dormant() +) + +@anime_search.handle() +async def _anime_search(bot: Bot, + event: MessageEvent, + state: T_State) -> None: + msg = str(event.message).strip() + if msg: + state["msg"] = msg + +@anime_search.got("msg", prompt="请发送咱一张图片~!") +async def _(bot: Bot, + event: MessageEvent, + state: T_State) -> None: + msg = state["msg"] + img = re.findall(r"url=(.*?)]", msg) + if not img: + await anime_search.reject("请发送图片而不是其它东西!!") + + try: + req = await get_bytes(URL + img[0]) + except RequestTimeOut: + raise RequestTimeOut("Request failed!") + + data = json.loads(req)["docs"] + try: + d = {} + for i in range(len(data)): + if data[i]["title_chinese"] in d.keys(): + d[data[i]["title_chinese"]][0] += data[i]["similarity"] + else: + m = data[i]["at"] / 60 + s = data[i]["at"] % 60 + + if not data[i]["episode"]: + n = 1 + else: + n = data[i]["episode"] + + d[to_simple_string(data[i]["title_chinese"])] = [ + data[i]["similarity"], + f"第{n}集", + f"{int(m)}分{int(s)}秒处" + ] + except Exception as err: + raise Exception(f"Invalid data.\n{err}") + + result = sorted( + d.items(), + key=lambda x:x[1], + reverse=True + ) + + t = 0 + + msg0 = f"{MessageSegment.at(event.user_id)}\nResult [{len(d)}]:" + for i in result: + t += 1 + s = "%.2f%%" % (i[1][0] * 100) + msg0 = msg0 + ( + "\n——————————\n" + f"({t}) Similarity: {s}\n" + f"Name: {i[0]}\n" + f"Time: {i[1][1]} {i[1][2]}" + ) + + await anime_search.finish(Message(msg0)) -- cgit v1.2.3