summaryrefslogtreecommitdiff
path: root/ATRI/plugins/anime_search.py
diff options
context:
space:
mode:
Diffstat (limited to 'ATRI/plugins/anime_search.py')
-rw-r--r--ATRI/plugins/anime_search.py69
1 files changed, 39 insertions, 30 deletions
diff --git a/ATRI/plugins/anime_search.py b/ATRI/plugins/anime_search.py
index 1c7e5a3..68a5add 100644
--- a/ATRI/plugins/anime_search.py
+++ b/ATRI/plugins/anime_search.py
@@ -8,7 +8,7 @@ from nonebot.typing import T_State
from ATRI.service import Service as sv
from ATRI.rule import is_in_service
-from ATRI.exceptions import RequestTimeOut
+from ATRI.exceptions import RequestError
from ATRI.utils.request import get_bytes
from ATRI.utils.translate import to_simple_string
from ATRI.utils.ub_paste import paste
@@ -24,40 +24,45 @@ __doc__ = """
以图搜番 (pic)
"""
-anime_search = sv.on_command(cmd="以图搜番", docs=__doc__, rule=is_in_service("以图搜番"))
-
+anime_search = sv.on_command(
+ cmd="以图搜番",
+ docs=__doc__,
+ rule=is_in_service('以图搜番')
+)
@anime_search.args_parser # type: ignore
async def _load_anime(bot: Bot, event: MessageEvent, state: T_State) -> None:
msg = str(event.message)
- quit_list = ["算了", "罢了", "不搜了", "取消"]
+ quit_list = ['算了', '罢了', '不搜了', '取消']
if msg in quit_list:
- await anime_search.finish("好吧...")
+ await anime_search.finish('好吧...')
if not msg:
- await anime_search.reject("图呢?")
+ await anime_search.reject('图呢?')
else:
- state["pic_anime"] = msg
-
+ state['pic_anime'] = msg
@anime_search.handle()
-async def _anime_search(bot: Bot, event: MessageEvent, state: T_State) -> None:
+async def _anime_search(bot: Bot,
+ event: MessageEvent,
+ state: T_State) -> None:
msg = str(event.message).strip()
if msg:
- state["pic_anime"] = msg
-
+ state['pic_anime'] = msg
-@anime_search.got("pic_anime", prompt="图呢?")
-async def _deal_search(bot: Bot, event: MessageEvent, state: T_State) -> None:
- msg = state["pic_anime"]
+@anime_search.got('pic_anime', prompt='图呢?')
+async def _deal_search(bot: Bot,
+ event: MessageEvent,
+ state: T_State) -> None:
+ msg = state['pic_anime']
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!")
-
+ except RequestError:
+ raise RequestError("Request failed!")
+
data = json.loads(req)["docs"]
try:
d = dict()
@@ -67,24 +72,28 @@ async def _deal_search(bot: Bot, event: MessageEvent, state: T_State) -> None:
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)}秒处",
+ 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)
-
+
+ result = sorted(
+ d.items(),
+ key=lambda x:x[1],
+ reverse=True
+ )
+
t = 0
-
+
msg0 = f"> {event.sender.nickname}"
for i in result:
t += 1
@@ -95,15 +104,15 @@ async def _deal_search(bot: Bot, event: MessageEvent, state: T_State) -> None:
f"Name: {i[0]}\n"
f"Time: {i[1][1]} {i[1][2]}"
)
-
+
if len(result) == 2:
await anime_search.finish(Message(msg0))
else:
data = FormData()
- data.add_field("poster", "ATRI running log")
- data.add_field("syntax", "text")
- data.add_field("expiration", "day")
- data.add_field("content", msg0)
+ data.add_field('poster', 'ATRI running log')
+ data.add_field('syntax', 'text')
+ data.add_field('expiration', 'day')
+ data.add_field('content', msg0)
repo = f"> {event.sender.nickname}\n"
repo = repo + f"详细请移步此处~\n{await paste(data)}"