Skip to content

Commit

Permalink
feat: 新增 on_message 处理消息方式
Browse files Browse the repository at this point in the history
refactor: 优化正则匹配,修改消息获取方式
  • Loading branch information
mengshouer committed Oct 19, 2024
1 parent fb5c672 commit 9285980
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 24 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ analysis_display_image = true # 是否显示封面 true/false
# 哪种类型需要显示封面,与上一项相冲突,上一项为true则全开 List[str]
analysis_display_image_list = ["video", "bangumi", "live", "article", "dynamic"]
analysis_enable_search = false # 是否开启搜视频功能 true/false example: "搜视频 123456"
analysis_use_on_message = false # 是否使用 on_message 处理消息,并且处理合并转发的信息,如果遇到 cpu 占用拉满的问题 #29,可以考虑打开此项
analysis_trust_env = false # 是否使用环境变量或者当前系统正在使用中的代理设置 true/false
Expand Down
38 changes: 28 additions & 10 deletions nonebot_plugin_analysis_bilibili/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import re
from typing import List, Union
from aiohttp import ClientSession
from nonebot import on_regex, logger, require
from nonebot import on_regex, logger, require, on_message
from nonebot.adapters import Event
from nonebot.rule import Rule
from nonebot.plugin import PluginMetadata
from nonebot.params import RegexStr
from .analysis_bilibili import config, b23_extract, bili_keyword, search_bili_by_title

require("nonebot_plugin_saa")
from nonebot_plugin_saa import (
from nonebot_plugin_saa import ( # noqa: E402
MessageFactory,
MessageSegmentFactory,
Text,
Expand All @@ -35,6 +34,7 @@
desc_blacklist = [str(i) for i in getattr(config, "analysis_desc_blacklist", [])]
trust_env = getattr(config, "analysis_trust_env", False)
enable_search = getattr(config, "analysis_enable_search", False)
use_on_message = getattr(config, "analysis_use_on_message", False)


async def is_enable_search() -> bool:
Expand Down Expand Up @@ -63,15 +63,22 @@ async def is_normal(event: Event) -> bool:
return True


pattern = (
r"^(?:(?:av|cv)\d+|BV[a-zA-Z0-9]{10})|"
r"(?:b23\.tv|bili(?:22|23|33|2233)\.cn|\.bilibili\.com|QQ小程序(?:]|]|\])哔哩哔哩).{0,500}"
)

analysis_bili = on_regex(
r"^(?:(av|cv)\d+|BV[a-zA-Z0-9]{10})|"
r".*(?:b23\.tv|bili(?:22|23|33|2233)\.cn|\.bilibili\.com|QQ小程序(?:]|]|\])哔哩哔哩).*",
flags=re.I,
pattern,
rule=is_normal,
block=False,
priority=11,
)

if use_on_message:
analysis_bili = on_message(rule=is_normal, block=False, priority=11)


rule = Rule(is_enable_search, is_normal)
search_bili = on_regex(r"^搜视频.*", rule=rule)

Expand Down Expand Up @@ -163,12 +170,23 @@ async def get_msg(


@analysis_bili.handle()
async def handle_analysis(event: Event, message=RegexStr()) -> None:
msg = await get_msg(event, message)
async def handle_analysis(event: Event) -> None:
message = event.get_message()
# on_message
if use_on_message:
# 不解析转发消息,可能会过长导致匹配时间过长
for segment in message:
if segment.type == "forward":
logger.debug("analysis_bilibili 忽略转发消息")
return
if not re.search(pattern, str(message)):
return
# on_regex
msg = await get_msg(event, str(message))
await send_msg(msg)


@search_bili.handle()
async def handle_search(event: Event, message=RegexStr()) -> None:
msg = await get_msg(event, message[3:].strip(), search=True)
async def handle_search(event: Event) -> None:
msg = await get_msg(event, str(event.get_message())[3:].strip(), search=True)
await send_msg(msg)
24 changes: 11 additions & 13 deletions nonebot_plugin_analysis_bilibili/analysis_bilibili.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import re
import nonebot

from time import time, localtime, strftime
from time import localtime, strftime
from typing import Dict, List, Optional, Tuple, Union
from aiohttp import ClientSession

Expand Down Expand Up @@ -73,7 +73,7 @@ async def bili_keyword(


async def b23_extract(text: str, session: ClientSession) -> str:
b23 = re.compile(r"b23.tv/(\w+)|(bili(22|23|33|2233).cn)/(\w+)", re.I).search(
b23 = re.compile(r"b23.tv/(\w+)|(bili(22|23|33|2233).cn)/(\w+)").search(
text.replace("\\", "")
)
url = f"https://{b23[0]}"
Expand All @@ -90,27 +90,25 @@ def extract(text: str) -> Tuple[str, Optional[str], Optional[str]]:
# 视频播放定位时间
time = re.compile(r"([?&]|&)t=\d+").search(text)
# 主站视频 av 号
aid = re.compile(r"av\d+", re.I).search(text)
aid = re.compile(r"av\d+").search(text)
# 主站视频 bv 号
bvid = re.compile(r"BV([A-Za-z0-9]{10})+", re.I).search(text)
bvid = re.compile(r"BV([A-Za-z0-9]{10})+").search(text)
# 番剧视频页
epid = re.compile(r"ep\d+", re.I).search(text)
epid = re.compile(r"ep\d+").search(text)
# 番剧剧集ssid(season_id)
ssid = re.compile(r"ss\d+", re.I).search(text)
ssid = re.compile(r"ss\d+").search(text)
# 番剧详细页
mdid = re.compile(r"md\d+", re.I).search(text)
mdid = re.compile(r"md\d+").search(text)
# 直播间
room_id = re.compile(r"live.bilibili.com/(blanc/|h5/)?(\d+)", re.I).search(text)
room_id = re.compile(r"live.bilibili.com/(blanc/|h5/)?(\d+)").search(text)
# 文章
cvid = re.compile(
r"(/read/(cv|mobile|native)(/|\?id=)?|^cv)(\d+)", re.I
).search(text)
cvid = re.compile(r"(/read/(cv|mobile|native)(/|\?id=)?|^cv)(\d+)").search(text)
# 动态
dynamic_id_type2 = re.compile(
r"(t|m).bilibili.com/(\d+)\?(.*?)(&|&)type=2", re.I
r"(t|m).bilibili.com/(\d+)\?(.*?)(&|&)type=2"
).search(text)
# 动态
dynamic_id = re.compile(r"(t|m).bilibili.com/(opus/)?(\d+)", re.I).search(text)
dynamic_id = re.compile(r"(t|m).bilibili.com/(opus/)?(\d+)").search(text)
if bvid:
url = f"https://api.bilibili.com/x/web-interface/view?bvid={bvid[0]}"
elif aid:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "nonebot-plugin-analysis-bilibili"
version = "2.7.7"
version = "2.8.0"
description = "nonebot2解析bilibili插件"
authors = ["mengshouer"]
license = "MIT"
Expand Down

0 comments on commit 9285980

Please sign in to comment.