diff options
-rw-r--r-- | ATRI/plugins/anime_search.py | 38 |
1 files changed, 20 insertions, 18 deletions
diff --git a/ATRI/plugins/anime_search.py b/ATRI/plugins/anime_search.py index 482776e..c6a72f8 100644 --- a/ATRI/plugins/anime_search.py +++ b/ATRI/plugins/anime_search.py @@ -13,7 +13,7 @@ from ATRI.utils.limit import FreqLimiter from ATRI.exceptions import RequestError -URL = "https://api.trace.moe/search?url=" +URL = "https://api.trace.moe/search?anilistInfo=true&url=" _anime_flmt = FreqLimiter(10) _anime_flmt_notice = choice(["慢...慢一..点❤", "冷静1下", "歇会歇会~~"]) @@ -24,9 +24,10 @@ __doc__ = """ class Anime(Service): + def __init__(self): Service.__init__(self, "以图搜番", __doc__, rule=is_in_service("以图搜番")) - + @staticmethod async def _request(url: str) -> dict: aim = URL + url @@ -36,31 +37,34 @@ class Anime(Service): raise RequestError("Request failed!") result = await res.json() return result - + @classmethod async def search(cls, url: str) -> str: data = await cls._request(url) - data = data["docs"] - + data = data["result"] + d = dict() for i in range(len(data)): - if data[i]["title_chinese"] in d.keys(): - d[data[i]["title_chinese"]][0] += data[i]["similarity"] + if data[i]["anilist"]["title"]["native"] in d.keys(): + d[data[i]["anilist"]["title"]["native"]][0] += data[i]["similarity"] else: - m = data[i]["at"] / 60 - s = data[i]["at"] % 60 + from_m = data[i]["from"] / 60 + from_s = data[i]["from"] % 60 + + to_m = data[i]["to"] / 60 + to_s = data[i]["to"] % 60 if not data[i]["episode"]: n = 1 else: n = data[i]["episode"] - d[Translate(data[i]["title_chinese"]).to_simple()] = [ + d[Translate(data[i]["anilist"]["title"]["native"]).to_simple()] = [ data[i]["similarity"], f"第{n}集", - f"{int(m)}分{int(s)}秒处", + f"约{int(from_m)}min{int(from_s)}s至{int(to_m)}min{int(to_s)}s处", ] - + result = sorted(d.items(), key=lambda x: x[1], reverse=True) t = 0 msg0 = str() @@ -73,7 +77,7 @@ class Anime(Service): f"Name: {i[0]}\n" f"Time: {i[1][1]} {i[1][2]}" ) - + if len(result) == 2: return msg0 else: @@ -89,7 +93,6 @@ class Anime(Service): anime_search = Anime().on_command("以图搜番", "发送一张图以搜索可能的番剧") - @anime_search.args_parser # type: ignore async def _get_anime(bot: Bot, event: MessageEvent, state: T_State): msg = str(event.message).strip() @@ -101,18 +104,16 @@ async def _get_anime(bot: Bot, event: MessageEvent, state: T_State): else: state["anime"] = msg - @anime_search.handle() async def _ready_sear(bot: Bot, event: MessageEvent, state: T_State): user_id = event.get_user_id() if not _anime_flmt.check(user_id): await anime_search.finish(_anime_flmt_notice) - + msg = str(event.message).strip() if msg: state["anime"] = msg - @anime_search.got("anime", "图呢?") async def _deal_sear(bot: Bot, event: MessageEvent, state: T_State): user_id = event.get_user_id() @@ -120,7 +121,8 @@ async def _deal_sear(bot: Bot, event: MessageEvent, state: T_State): img = re.findall(r"url=(.*?)]", msg) if not img: await anime_search.reject("请发送图片而不是其它东西!!") - + + await bot.send(event, "别急,在找了") a = await Anime().search(img[0]) result = f"> {MessageSegment.at(user_id)}\n" + a _anime_flmt.start_cd(user_id) |