mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-11 06:39:59 +08:00
refactor code
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
from nonebot import on_command
|
||||
from .data_source import from_anime_get_info
|
||||
from services.log import logger
|
||||
from nonebot.adapters.cqhttp import Bot, Event
|
||||
from nonebot.adapters.cqhttp import Bot, MessageEvent, GroupMessageEvent
|
||||
from nonebot.typing import T_State
|
||||
from configs.config import MAXINFO_GROUP_ANIME, MAXINFO_PRIVATE_ANIME
|
||||
from utils.utils import get_message_text, get_message_type, UserExistLimiter
|
||||
from utils.utils import get_message_text, UserExistLimiter
|
||||
|
||||
|
||||
__plugin_name__ = '搜番'
|
||||
__plugin_name__ = "搜番"
|
||||
__plugin_usage__ = r"""
|
||||
在群内使用此功能只返还5个结果,私聊返还 20 个结果(绝不能打扰老色批们看色图!)
|
||||
搜索动漫资源
|
||||
@@ -16,39 +16,43 @@ __plugin_usage__ = r"""
|
||||
""".strip()
|
||||
_ulmt = UserExistLimiter()
|
||||
|
||||
search_anime = on_command('搜番', aliases={'搜动漫'}, priority=5, block=True)
|
||||
search_anime = on_command("搜番", aliases={"搜动漫"}, priority=5, block=True)
|
||||
|
||||
|
||||
@search_anime.args_parser
|
||||
async def _(bot: Bot, event: Event, state: T_State):
|
||||
async def _(bot: Bot, event: MessageEvent, state: T_State):
|
||||
msg = get_message_text(event.json())
|
||||
if not msg:
|
||||
await search_anime.reject('番名番名番名呢?', at_sender=True)
|
||||
state['anime'] = msg
|
||||
await search_anime.reject("番名番名番名呢?", at_sender=True)
|
||||
state["anime"] = msg
|
||||
|
||||
|
||||
@search_anime.handle()
|
||||
async def _(bot: Bot, event: Event, state: T_State):
|
||||
async def _(bot: Bot, event: MessageEvent, state: T_State):
|
||||
if _ulmt.check(event.user_id):
|
||||
await search_anime.finish('您有动漫正在搜索,请稍等...', at_sender=True)
|
||||
await search_anime.finish("您有动漫正在搜索,请稍等...", at_sender=True)
|
||||
_ulmt.set_True(event.user_id)
|
||||
if get_message_text(event.json()):
|
||||
state['anime'] = get_message_text(event.json())
|
||||
state["anime"] = get_message_text(event.json())
|
||||
|
||||
|
||||
@search_anime.got('anime', prompt='是不是少了番名?')
|
||||
async def _(bot: Bot, event: Event, state: T_State):
|
||||
key_word = state['anime']
|
||||
await search_anime.send(f'开始搜番 {key_word}', at_sender=True)
|
||||
anime_report = await from_anime_get_info(key_word, MAXINFO_GROUP_ANIME if get_message_type(event.json()) in ['group', 'discuss'] else MAXINFO_PRIVATE_ANIME)
|
||||
@search_anime.got("anime", prompt="是不是少了番名?")
|
||||
async def _(bot: Bot, event: MessageEvent, state: T_State):
|
||||
key_word = state["anime"]
|
||||
await search_anime.send(f"开始搜番 {key_word}", at_sender=True)
|
||||
anime_report = await from_anime_get_info(
|
||||
key_word,
|
||||
MAXINFO_GROUP_ANIME
|
||||
if isinstance(event, GroupMessageEvent)
|
||||
else MAXINFO_PRIVATE_ANIME,
|
||||
)
|
||||
if anime_report:
|
||||
await search_anime.send(anime_report)
|
||||
logger.info(f"USER {event.user_id} GROUP"
|
||||
f" {event.group_id if event.message_type != 'private' else 'private'} 搜索番剧 {key_word} 成功")
|
||||
logger.info(
|
||||
f"USER {event.user_id} GROUP"
|
||||
f" {event.group_id if isinstance(event, GroupMessageEvent) else 'private'} 搜索番剧 {key_word} 成功"
|
||||
)
|
||||
else:
|
||||
logger.warning(f"未找到番剧 {key_word}")
|
||||
await search_anime.send(f"未找到番剧 {key_word}(也有可能是超时,再尝试一下?)")
|
||||
_ulmt.set_False(event.user_id)
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -7,41 +7,55 @@ import time
|
||||
from utils.utils import get_local_proxy
|
||||
|
||||
|
||||
async def from_anime_get_info(key_word: str, max: int) -> str:
|
||||
async def from_anime_get_info(key_word: str, max_: int) -> str:
|
||||
s_time = time.time()
|
||||
repass = ""
|
||||
url = 'https://share.dmhy.org/topics/rss/rss.xml?keyword=' + parse.quote(key_word)
|
||||
url = "https://share.dmhy.org/topics/rss/rss.xml?keyword=" + parse.quote(key_word)
|
||||
try:
|
||||
logger.debug("Now starting get the {}".format(url))
|
||||
repass = await get_repass(url, max)
|
||||
repass = await get_repass(url, max_)
|
||||
except Exception as e:
|
||||
logger.error("Timeout! {}".format(e))
|
||||
|
||||
return f"搜索 {key_word} 结果(耗时 {int(time.time() - s_time)} 秒):\n" + repass
|
||||
|
||||
|
||||
async def get_repass(url: str, max: int) -> str:
|
||||
async def get_repass(url: str, max_: int) -> str:
|
||||
putline = []
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get(url, proxy=get_local_proxy(), timeout=20) as response:
|
||||
d = feedparser.parse(await response.text())
|
||||
url_list = [e.link for e in d.entries][:max]
|
||||
url_list = [e.link for e in d.entries][:max_]
|
||||
for u in url_list:
|
||||
print(u)
|
||||
try:
|
||||
async with session.get(u, proxy=get_local_proxy(), timeout=20) as res:
|
||||
async with session.get(
|
||||
u, proxy=get_local_proxy(), timeout=20
|
||||
) as res:
|
||||
html = etree.HTML(await res.text())
|
||||
magent = html.xpath('.//a[@id="a_magnet"]/text()')[0]
|
||||
title = html.xpath('.//h3/text()')[0]
|
||||
item = html.xpath('//div[@class="info resource-info right"]/ul/li')
|
||||
class_a = item[0].xpath('string(.)')[5:].strip().replace("\xa0", "").replace("\t", "")
|
||||
size = item[3].xpath('string(.)')[5:].strip()
|
||||
title = html.xpath(".//h3/text()")[0]
|
||||
item = html.xpath(
|
||||
'//div[@class="info resource-info right"]/ul/li'
|
||||
)
|
||||
class_a = (
|
||||
item[0]
|
||||
.xpath("string(.)")[5:]
|
||||
.strip()
|
||||
.replace("\xa0", "")
|
||||
.replace("\t", "")
|
||||
)
|
||||
size = item[3].xpath("string(.)")[5:].strip()
|
||||
|
||||
putline.append("【{}】| {}\n【{}】| {}".format(class_a, title, size, magent))
|
||||
putline.append(
|
||||
"【{}】| {}\n【{}】| {}".format(class_a, title, size, magent)
|
||||
)
|
||||
except Exception as e:
|
||||
logger.warning(f'搜番超时 e:{e}')
|
||||
logger.warning(f"搜番超时 e:{e}")
|
||||
|
||||
repass = '\n\n'.join(putline)
|
||||
repass = "\n\n".join(putline)
|
||||
|
||||
return repass
|
||||
# print(asyncio.get_event_loop().run_until_complete(from_anime_get_info('进击的巨人', 1234556)))
|
||||
|
||||
|
||||
# print(asyncio.get_event_loop().run_until_complete(from_anime_get_info('进击的巨人', 1234556)))
|
||||
|
||||
Reference in New Issue
Block a user