Skip to content

Commit

Permalink
Merge pull request #30 from Cypas/test
Browse files Browse the repository at this point in the history
feat:增加配装查询功能
  • Loading branch information
Cypas authored Jul 8, 2024
2 parents 013e97c + 9917170 commit 5a501b8
Show file tree
Hide file tree
Showing 10 changed files with 259 additions and 52 deletions.
95 changes: 83 additions & 12 deletions nonebot_plugin_splatoon3_schedule/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async def _(bot: Bot, event: Event):
plain_text = event.get_message().extract_plain_text().strip()
# 触发关键词 替换.。\/ 等前缀触发词
plain_text = multiple_replace(plain_text, dict_keyword_replace)
logger.info("同义文本替换后触发词为:" + plain_text + "\n")
logger.info("同义文本替换后触发词为:" + plain_text)
# 判断是否满足进一步正则
num_list = []
contest_match = None
Expand Down Expand Up @@ -71,7 +71,7 @@ async def _(bot: Bot, event: Event):
# 传递函数指针
func = get_stages_image
# 获取图片
img = get_save_temp_image(plain_text, func, num_list, contest_match, rule_match)
img = await get_save_temp_image(plain_text, func, num_list, contest_match, rule_match)
# 发送消息
await send_msg(bot, event, img)

Expand Down Expand Up @@ -167,7 +167,7 @@ async def _(bot: Bot, event: Event, re_tuple: Tuple = RegexGroup()):
# 传递函数指针
func = get_stages_image
# 获取图片
img = get_save_temp_image(plain_text, func, num_list, contest_match, rule_match)
img = await get_save_temp_image(plain_text, func, num_list, contest_match, rule_match)
# 发送消息
await send_msg(bot, event, img)

Expand All @@ -185,19 +185,90 @@ async def _(bot: Bot, event: Event):
plain_text = event.get_message().extract_plain_text().strip()
# 触发关键词 替换.。\/ 等前缀触发词
plain_text = multiple_replace(plain_text, dict_keyword_replace)
logger.info("同义文本替换后触发词为:" + plain_text + "\n")
logger.info("同义文本替换后触发词为:" + plain_text)
# 判断是否满足进一步正则
_all = False
if "全部" in plain_text:
_all = True
# 传递函数指针
func = get_coop_stages_image
# 获取图片
img = get_save_temp_image(plain_text, func, _all)
img = await get_save_temp_image(plain_text, func, _all)
# 发送消息
await send_msg(bot, event, img)


# 配装 触发器
matcher_build = on_regex(
"^[\\/.,,。]?配装\s?([\\u4e00-\\u9fa5a-zA-Z0-9]{2,20})?\s?(区域|区|推塔|抢塔|塔楼|塔|蛤蜊|蛤|抢鱼|鱼虎|鱼|涂地|涂涂|涂)?$",
priority=8,
block=True,
)


# 配装 触发器处理
@matcher_build.handle(parameterless=[Depends(_permission_check)])
async def _(bot: Bot, event: Event, re_tuple: Tuple = RegexGroup()):
re_list = []
for k, v in enumerate(re_tuple):
# 遍历正则匹配字典进行替换文本
if k == 0:
# 武器名
if v:
v = v.upper()
v = multiple_replace(v, dict_builds_pre_replace)
re_list.append(v)
if k == 1:
# 模式
value = dict_keyword_replace.get(v, v)
re_list.append(value)
logger.info("同义文本替换后触发参数为:" + json.dumps(re_list, ensure_ascii=False))

if not re_list[0]:
msg = "请携带需要查询的武器或模式信息作为参数,若为贴牌需要加上贴牌二字,如:\n/配装 小绿\n指定模式查询:\n/配装 贴牌碳刷 塔楼"
await send_msg(bot, event, msg)
return

# 整理参数
is_deco = False
mode = None
weapon_name = re_list[0]
if "贴牌" in weapon_name:
is_deco = True
weapon_name = weapon_name.replace("贴牌", "")

# 查询对应武器
build_info = db_image.get_build_info(weapon_name, is_deco)
if not build_info:
msg = "该关键词未查询到对应武器,请试试使用官方中文武器名称或其他常用名称后再试"
await send_msg(bot, event, msg)
return
zh_name: str = build_info.get("zh_name")
sendou_name: str = build_info.get("sendou_name")

plain_text = "配装"
re_list[0] = f'{zh_name.replace(" ", "")}'
plain_text = f"{plain_text} {re_list[0]}"
if re_list[1]:
plain_text = f"{plain_text} {re_list[1]}"

# mode
if re_list[1]:
mode = re_list[1]
mode_str = mode
else:
mode_str = "全部"
msg = f"正在获取武器 {re_list[0]}{mode_str}模式下的配装推荐,请稍等..."
await send_msg(bot, event, msg)

# 传递函数指针
func = get_build_image
# 获取图片
img = await get_save_temp_image(plain_text, func, sendou_name, mode)
# 发送图片
await send_msg(bot, event, img)


# 其他命令 触发器
matcher_else = on_regex("^[\\/.,,。]?(帮助|help|nso帮助|(随机武器).*|装备|衣服|祭典|活动)[ ]?$", priority=8, block=True)

Expand All @@ -209,7 +280,7 @@ async def _(bot: Bot, event: Event):
# 触发关键词 替换.。\/ 等前缀触发词
plain_text = multiple_replace(plain_text, dict_prefix_replace)
plain_text = plain_text.replace("help", "帮助")
logger.info("同义文本替换后触发词为:" + plain_text + "\n")
logger.info("同义文本替换后触发词为:" + plain_text)
# 判断是否满足进一步正则
# 随机武器
if re.search("^随机武器.*$", plain_text):
Expand All @@ -219,14 +290,14 @@ async def _(bot: Bot, event: Event):
msg = "请机器人管理员先发送 更新武器数据 更新本地武器数据库后,才能使用随机武器功能"
await send_msg(bot, event, msg)
else:
img = image_to_bytes(get_random_weapon_image(plain_text))
img = image_to_bytes(await get_random_weapon_image(plain_text))
# 发送消息
await send_msg(bot, event, img)
elif re.search("^祭典$", plain_text):
# 传递函数指针
func = get_festival_image
# 获取图片
img = get_save_temp_image(plain_text, func)
img = await get_save_temp_image(plain_text, func)
if img is None:
msg = "近期没有任何祭典"
await send_msg(bot, event, msg)
Expand All @@ -237,7 +308,7 @@ async def _(bot: Bot, event: Event):
# 传递函数指针
func = get_events_image
# 获取图片
img = get_save_temp_image(plain_text, func)
img = await get_save_temp_image(plain_text, func)
if img is None:
msg = "近期没有任何活动比赛"
await send_msg(bot, event, msg)
Expand All @@ -249,7 +320,7 @@ async def _(bot: Bot, event: Event):
# 传递函数指针
func = get_help_image
# 获取图片
img = get_save_temp_image(plain_text, func)
img = await get_save_temp_image(plain_text, func)
# 发送图片
await send_msg(bot, event, img)
# 当优先帮助打开时,额外发送nso帮助
Expand All @@ -260,12 +331,12 @@ async def _(bot: Bot, event: Event):
# 传递函数指针
func = get_nso_help_image
# 获取图片
img = get_save_temp_image(plain_text, func)
img = await get_save_temp_image(plain_text, func)
# 发送图片
await send_msg(bot, event, img)

elif re.search("^装备$", plain_text):
img = await get_screenshot(shot_url="https://splatoon3.ink/gear")
img = await get_screenshot(shot_url="https://splatoon3.ink/gear", mode="mobile")
# 发送图片
await send_msg(bot, event, img)

Expand Down
32 changes: 24 additions & 8 deletions nonebot_plugin_splatoon3_schedule/data/data_source.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from playwright.async_api import Browser, async_playwright
from playwright.sync_api import FloatRect

from .db_image import db_image
from ..utils import *
Expand Down Expand Up @@ -273,22 +274,37 @@ async def get_browser() -> Browser:
return _browser


async def get_screenshot(shot_url, shot_path=None):
async def get_screenshot(
shot_url,
mode="pc",
selector=None,
shot_path=None,
):
"""通过 browser 获取 shot_url 中的网页截图"""
# playwright 要求不能有多个 browser 被同时唤起
browser = await get_browser()
context = await browser.new_context(viewport={"width": 500, "height": 2000}, locale="zh-CH")
if mode == "pc":
context = await browser.new_context(viewport={"width": 1920, "height": 1080}, locale="zh-CH")
elif mode == "mobile":
context = await browser.new_context(viewport={"width": 500, "height": 2000}, locale="zh-CH")
page = await context.new_page()
await page.goto(shot_url, wait_until="networkidle")

try:
if shot_path is None:
return await page.screenshot()
await page.goto(shot_url, wait_until="load", timeout=300000)
await page.wait_for_timeout(1500)
if selector:
# 元素选择器
await page.wait_for_selector(selector)
element = await page.query_selector(selector)
screenshot = await element.screenshot(path=shot_path)
img = screenshot
else:
await page.screenshot(path=shot_path)
img = await page.screenshot(path=shot_path)

await page.wait_for_timeout(5000)
return img
except Exception as e:
logger.error("Screenshot failed" + str(e))
return await page.screenshot(full_page=True)
# return await page.screenshot(full_page=True)
raise e
finally:
await context.close()
20 changes: 20 additions & 0 deletions nonebot_plugin_splatoon3_schedule/data/db_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,5 +277,25 @@ def get_weapon_image(self, name, type_name) -> dict:
self.conn.commit()
return result

def get_build_info(self, keyword, is_deco) -> dict:
"""取配装数据"""
if not is_deco:
sql = f"select * from BUILDS where keywords LIKE '%|{keyword}|%'"
else:
sql = f"select * from BUILDS where keywords LIKE '%|{keyword}|%' AND is_deco=1"
c = self.conn.cursor()
c.execute(
sql,
)
# 单行查询结果
row = c.fetchone()
if row is not None:
# 查询有结果时将查询结果转换为字典
result = dict(zip([column[0] for column in c.description], row))
else:
result = None
self.conn.commit()
return result


db_image = DBIMAGE()
Loading

0 comments on commit 5a501b8

Please sign in to comment.