2021-05-20 19:25:51 +08:00
|
|
|
|
import time
|
|
|
|
|
|
from services.log import logger
|
2021-06-30 19:50:55 +08:00
|
|
|
|
from utils.langconv import *
|
2021-05-20 19:25:51 +08:00
|
|
|
|
import aiohttp
|
2021-06-30 19:50:55 +08:00
|
|
|
|
from utils.user_agent import get_user_agent
|
2021-05-20 19:25:51 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def get_anime(anime: str) -> str:
|
|
|
|
|
|
s_time = time.time()
|
2021-07-30 21:21:51 +08:00
|
|
|
|
url = "https://api.trace.moe/search?anilistInfo&url={}".format(anime)
|
2021-05-20 19:25:51 +08:00
|
|
|
|
logger.debug("[info]Now starting get the {}".format(url))
|
|
|
|
|
|
try:
|
|
|
|
|
|
async with aiohttp.ClientSession(headers=get_user_agent()) as session:
|
|
|
|
|
|
async with session.get(url, timeout=45) as response:
|
|
|
|
|
|
if response.status == 200:
|
|
|
|
|
|
anime_json = await response.json()
|
2021-07-30 21:21:51 +08:00
|
|
|
|
if not anime_json["error"]:
|
|
|
|
|
|
if anime_json == "Error reading imagenull":
|
2021-07-06 17:27:59 +08:00
|
|
|
|
return "图像源错误,注意必须是静态图片哦"
|
|
|
|
|
|
repass = ""
|
|
|
|
|
|
# 拿到动漫 中文名
|
|
|
|
|
|
for anime in anime_json["result"][:5]:
|
2021-07-30 21:21:51 +08:00
|
|
|
|
synonyms = anime["anilist"]["synonyms"]
|
2021-07-06 17:27:59 +08:00
|
|
|
|
for x in synonyms:
|
|
|
|
|
|
_count_ch = 0
|
|
|
|
|
|
for word in x:
|
2021-07-30 21:21:51 +08:00
|
|
|
|
if "\u4e00" <= word <= "\u9fff":
|
2021-07-06 17:27:59 +08:00
|
|
|
|
_count_ch += 1
|
|
|
|
|
|
if _count_ch > 3:
|
|
|
|
|
|
anime_name = x
|
|
|
|
|
|
break
|
|
|
|
|
|
else:
|
2021-07-30 21:21:51 +08:00
|
|
|
|
anime_name = anime["anilist"]["title"]["native"]
|
2021-07-06 17:27:59 +08:00
|
|
|
|
episode = anime["episode"]
|
|
|
|
|
|
from_ = int(anime["from"])
|
|
|
|
|
|
m, s = divmod(from_, 60)
|
|
|
|
|
|
similarity = anime["similarity"]
|
2021-07-30 21:21:51 +08:00
|
|
|
|
putline = "[ {} ][{}][{}:{}] 相似度:{:.2%}".format(
|
|
|
|
|
|
Converter("zh-hans").convert(anime_name),
|
|
|
|
|
|
episode if episode else "?",
|
|
|
|
|
|
m,
|
|
|
|
|
|
s,
|
|
|
|
|
|
similarity,
|
|
|
|
|
|
)
|
|
|
|
|
|
repass += putline + "\n"
|
|
|
|
|
|
return f"耗时 {int(time.time() - s_time)} 秒\n" + repass[:-1]
|
2021-07-06 17:27:59 +08:00
|
|
|
|
else:
|
|
|
|
|
|
return f'访问错误 error:{anime_json["error"]}'
|
2021-05-20 19:25:51 +08:00
|
|
|
|
else:
|
2021-07-30 21:21:51 +08:00
|
|
|
|
return f"访问失败,请再试一次吧, status: {response.status}"
|
2021-07-06 17:27:59 +08:00
|
|
|
|
except Exception as e:
|
2021-07-30 21:21:51 +08:00
|
|
|
|
logger.error(f"识番发生错误 e:{e}")
|
|
|
|
|
|
return "发生了奇怪的错误,那就没办法了,再试一次?"
|