diff --git a/zhenxun/plugins/search_image/__init__.py b/zhenxun/plugins/search_image/__init__.py index fce64046..f3eb1102 100644 --- a/zhenxun/plugins/search_image/__init__.py +++ b/zhenxun/plugins/search_image/__init__.py @@ -51,13 +51,13 @@ async def get_image_info(mod: str, url: str) -> str | list[Image | Text] | None: @_matcher.handle() -async def _(mode: Match[str], img: Match[alcImg]): +async def _(mode: Match[str], image: Match[alcImg]): if mode.available: _matcher.set_path_arg("mode", mode.result) else: _matcher.set_path_arg("mode", "saucenao") - if img.available: - _matcher.set_path_arg("image", img.result) + if image.available: + _matcher.set_path_arg("image", image.result) @_matcher.got_path("image", prompt="图来!") diff --git a/zhenxun/plugins/what_anime/__init__.py b/zhenxun/plugins/what_anime/__init__.py new file mode 100644 index 00000000..4881f6dc --- /dev/null +++ b/zhenxun/plugins/what_anime/__init__.py @@ -0,0 +1,58 @@ +from nonebot.plugin import PluginMetadata +from nonebot_plugin_alconna import Alconna, Args, Arparma +from nonebot_plugin_alconna import Image as alcImg +from nonebot_plugin_alconna import Match, on_alconna +from nonebot_plugin_saa import Image, Text +from nonebot_plugin_session import EventSession + +from zhenxun.configs.utils import PluginExtraData +from zhenxun.services.log import logger + +from .data_source import get_anime + +__plugin_meta__ = PluginMetadata( + name="识番", + description="以图识番", + usage=""" + usage: + api.trace.moe 以图识番 + 指令: + 识番 [图片] + """.strip(), + extra=PluginExtraData( + author="HibiKier", version="0.1", menu_type="一些工具" + ).dict(), +) + + +_matcher = on_alconna(Alconna("识番", Args["image?", alcImg]), block=True, priority=5) + + +@_matcher.handle() +async def _(image: Match[alcImg]): + if image.available: + _matcher.set_path_arg("image", image.result) + + +@_matcher.got_path("image", prompt="图来!") +async def _( + session: EventSession, + arparma: Arparma, + image: alcImg, +): + if not image.url: + await Text("图片url为空...").finish() + await Text("开始识别...").send() + anime_data_report = await get_anime(image.url) + if anime_data_report: + await Text(anime_data_report).send(reply=True) + logger.info( + f" 识番 {image.url} --> {anime_data_report}", + arparma.header_result, + session=session, + ) + else: + logger.info( + f"识番 {image.url} 未找到...", arparma.header_result, session=session + ) + await Text(f"没有寻找到该番剧,果咩..").send(reply=True) diff --git a/zhenxun/plugins/what_anime/data_source.py b/zhenxun/plugins/what_anime/data_source.py new file mode 100644 index 00000000..15801f62 --- /dev/null +++ b/zhenxun/plugins/what_anime/data_source.py @@ -0,0 +1,47 @@ +import time + +from zhenxun.services.log import logger +from zhenxun.utils.http_utils import AsyncHttpx + + +async def get_anime(anime: str) -> str: + s_time = time.time() + url = "https://api.trace.moe/search?anilistInfo&url={}".format(anime) + logger.debug("[info]Now starting get the {}".format(url)) + try: + anime_json = (await AsyncHttpx.get(url)).json() + if not anime_json["error"]: + if anime_json == "Error reading imagenull": + return "图像源错误,注意必须是静态图片哦" + repass = "" + # 拿到动漫 中文名 + for anime in anime_json["result"][:5]: + synonyms = anime["anilist"]["synonyms"] + for x in synonyms: + _count_ch = 0 + for word in x: + if "\u4e00" <= word <= "\u9fff": + _count_ch += 1 + if _count_ch > 3: + anime_name = x + break + else: + anime_name = anime["anilist"]["title"]["native"] + episode = anime["episode"] + from_ = int(anime["from"]) + m, s = divmod(from_, 60) + similarity = anime["similarity"] + putline = "[ {} ][{}][{}:{}] 相似度:{:.2%}".format( + anime_name, + episode if episode else "?", + m, + s, + similarity, + ) + repass += putline + "\n" + return f"耗时 {int(time.time() - s_time)} 秒\n" + repass[:-1] + else: + return f'访问错误 error:{anime_json["error"]}' + except Exception as e: + logger.error(f"识番发生错误", e=e) + return "发生了奇怪的错误,那就没办法了,再试一次?"