本次提交对核心模块进行了深度重构,引入 Pydantic 增强配置管理的类型安全性,并全面优化了插件管理系统。 主要变更详情: 1. 核心架构与配置 - 重构配置加载模块:引入 Pydantic 模型 (`core/config_models.py`),提供严格的配置项类型检查、验证及默认值管理。 - 统一模块结构:规范化模块导入路径,移除冗余的 `__init__.py` 文件,提升项目结构的清晰度。 - 性能优化:集成 Redis 缓存支持 (`RedisManager`),有效降低高频 API 调用开销,提升响应速度。 2. 插件系统升级 - 实现热重载机制:新增插件文件变更监听功能,支持开发过程中自动重载插件,提升开发效率。 - 优化生命周期管理:改进插件加载与卸载逻辑,支持精确卸载指定插件及其关联的命令、事件处理器和定时任务。 3. 功能特性增强 - 新增媒体 API:引入 `MediaAPI` 模块,封装图片、语音等富媒体资源的获取与处理接口。 - 完善权限体系:重构权限管理系统,实现管理员与操作员的分级控制,支持更细粒度的命令权限校验。 4. 代码质量与稳定性 - 全面类型修复:解决 `mypy` 静态类型检查发现的大量类型错误(包括 `CommandManager`、`EventFactory` 及 `Bot` API 签名不匹配问题)。 - 增强错误处理:优化消息处理管道的异常捕获机制,完善关键路径的日志记录,提升系统运行稳定性。
220 lines
8.0 KiB
Python
220 lines
8.0 KiB
Python
# -*- coding: utf-8 -*-
|
||
import re
|
||
import json
|
||
import httpx
|
||
from bs4 import BeautifulSoup
|
||
from typing import Optional, Dict, Any
|
||
from cachetools import TTLCache
|
||
|
||
from core.utils.logger import logger
|
||
from core.managers.command_manager import matcher
|
||
from models.events.message import MessageEvent, MessageSegment
|
||
|
||
# 创建一个TTL缓存,最大容量100,缓存时间60秒
|
||
processed_messages: TTLCache[Any, bool] = TTLCache(maxsize=100, ttl=60)
|
||
|
||
__plugin_meta__ = {
|
||
"name": "bili_parser",
|
||
"description": "自动解析B站分享卡片,提取视频封面和播放量等信息。",
|
||
"usage": "(自动触发)当检测到B站小程序分享卡片时,自动发送视频信息。",
|
||
}
|
||
|
||
HEADERS = {
|
||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
|
||
}
|
||
|
||
# 创建可复用的异步HTTP客户端
|
||
async_client = httpx.AsyncClient(headers=HEADERS, follow_redirects=False, timeout=10)
|
||
|
||
|
||
def format_count(num: int) -> str:
|
||
if not isinstance(num, int):
|
||
return str(num)
|
||
if num < 10000:
|
||
return str(num)
|
||
return f"{num / 10000:.1f}万"
|
||
|
||
|
||
def format_duration(seconds: int) -> str:
|
||
"""将秒数格式化为 MM:SS 的形式"""
|
||
if not isinstance(seconds, int) or seconds < 0:
|
||
return "滚木"
|
||
minutes, seconds = divmod(seconds, 60)
|
||
return f"{minutes:02d}:{seconds:02d}"
|
||
|
||
|
||
async def get_real_url(short_url: str) -> Optional[str]:
|
||
try:
|
||
response = await async_client.head(short_url)
|
||
if response.status_code == 302:
|
||
return response.headers.get('Location')
|
||
except httpx.RequestError as e:
|
||
logger.error(f"获取真实URL失败: {e}")
|
||
return None
|
||
|
||
async def parse_video_info(video_url: str) -> Optional[Dict[str, Any]]:
|
||
try:
|
||
response = await async_client.get(video_url, follow_redirects=True)
|
||
response.raise_for_status()
|
||
soup = BeautifulSoup(response.text, 'html.parser')
|
||
|
||
script_tag = soup.find('script', text=re.compile('window.__INITIAL_STATE__'))
|
||
if not script_tag:
|
||
return None
|
||
|
||
script_tag_content = script_tag.string
|
||
if not script_tag_content:
|
||
return None
|
||
|
||
match = re.search(r'window\.__INITIAL_STATE__\s*=\s*(\{.*?\});', script_tag_content)
|
||
if not match:
|
||
return None
|
||
json_str = match.group(1)
|
||
data = json.loads(json_str)
|
||
|
||
video_data = data.get('videoData', {})
|
||
up_data = data.get('upData', {})
|
||
stat = video_data.get('stat', {})
|
||
owner = video_data.get('owner', {})
|
||
|
||
cover_url = video_data.get('pic', '')
|
||
if cover_url:
|
||
cover_url = cover_url.split('@')[0]
|
||
if cover_url.startswith('//'):
|
||
cover_url = 'https:' + cover_url
|
||
|
||
owner_avatar = owner.get('face', '')
|
||
if owner_avatar:
|
||
if owner_avatar.startswith('//'):
|
||
owner_avatar = 'https:' + owner_avatar
|
||
owner_avatar = owner_avatar.split('@')[0]
|
||
|
||
return {
|
||
"title": video_data.get('title', '未知标题'),
|
||
"bvid": video_data.get('bvid', '未知BV号'),
|
||
"duration": video_data.get('duration', 0),
|
||
"cover_url": cover_url,
|
||
"play": stat.get('view', 0),
|
||
"like": stat.get('like', 0),
|
||
"coin": stat.get('coin', 0),
|
||
"favorite": stat.get('favorite', 0),
|
||
"share": stat.get('share', 0),
|
||
"owner_name": owner.get('name', '未知UP主'),
|
||
"owner_avatar": owner_avatar,
|
||
"followers": up_data.get('fans', 0),
|
||
}
|
||
|
||
except (httpx.RequestError, KeyError, AttributeError, json.JSONDecodeError) as e:
|
||
logger.error(f"解析视频信息失败: {e}")
|
||
|
||
return None
|
||
|
||
async def get_direct_video_url(video_url: str) -> Optional[str]:
|
||
"""
|
||
调用第三方API解析B站视频直链
|
||
:param video_url: B站视频的完整URL
|
||
:return: 视频直链URL,如果失败则返回None
|
||
"""
|
||
api_url = f"https://api.mir6.com/api/bzjiexi?url={video_url}&type=json"
|
||
try:
|
||
response = await async_client.get(api_url)
|
||
response.raise_for_status()
|
||
data = response.json()
|
||
if data.get("code") == 200 and data.get("data"):
|
||
return data["data"][0].get("video_url")
|
||
except (httpx.RequestError, json.JSONDecodeError, KeyError, IndexError) as e:
|
||
logger.error(f"[bili_parser] 调用第三方API解析视频失败: {e}")
|
||
return None
|
||
|
||
BILI_URL_PATTERN = re.compile(r"https?://(?:www\.)?(bilibili\.com/video/[a-zA-Z0-9_]+|b23\.tv/[a-zA-Z0-9]+)")
|
||
|
||
|
||
@matcher.on_message()
|
||
async def handle_bili_share(event: MessageEvent):
|
||
"""
|
||
处理消息,检测B站分享链接(JSON卡片或文本链接)并进行解析。
|
||
:param event: 消息事件对象
|
||
"""
|
||
# 消息去重
|
||
if event.message_id in processed_messages:
|
||
return
|
||
processed_messages[event.message_id] = True
|
||
|
||
# 忽略机器人自己发送的消息,防止无限循环
|
||
if event.user_id == event.self_id:
|
||
return
|
||
|
||
url_to_process = None
|
||
|
||
# 1. 优先解析JSON卡片中的短链接
|
||
for segment in event.message:
|
||
if segment.type == "json":
|
||
logger.info(f"[bili_parser] 检测到JSON CQ码: {segment.data}")
|
||
try:
|
||
json_data = json.loads(segment.data.get("data", "{}"))
|
||
short_url = json_data.get("meta", {}).get("detail_1", {}).get("qqdocurl")
|
||
|
||
if short_url and "b23.tv" in short_url:
|
||
url_to_process = short_url.split('?')[0]
|
||
logger.success(f"[bili_parser] 成功从JSON卡片中提取到B站短链接: {url_to_process}")
|
||
break # 找到后立即跳出循环
|
||
except (json.JSONDecodeError, KeyError) as e:
|
||
logger.error(f"[bili_parser] 解析JSON失败: {e}")
|
||
continue
|
||
|
||
# 2. 如果未在JSON卡片中找到链接,则在文本消息中查找
|
||
if not url_to_process:
|
||
for segment in event.message:
|
||
if segment.type == "text":
|
||
text_content = segment.data.get("text", "")
|
||
match = BILI_URL_PATTERN.search(text_content)
|
||
if match:
|
||
url_to_process = match.group(0)
|
||
logger.success(f"[bili_parser] 成功从文本中提取到B站链接: {url_to_process}")
|
||
break # 找到后立即跳出循环
|
||
|
||
# 3. 如果找到了任何类型的B站链接,则进行处理
|
||
if url_to_process:
|
||
await process_bili_link(event, url_to_process)
|
||
|
||
async def process_bili_link(event: MessageEvent, url: str):
|
||
"""
|
||
处理B站链接(长链接或短链接),获取信息并回复
|
||
:param event: 消息事件对象
|
||
:param url: 待处理的B站链接
|
||
"""
|
||
if "b23.tv" in url:
|
||
real_url = await get_real_url(url)
|
||
if not real_url:
|
||
logger.error(f"[bili_parser] 无法从 {url} 获取真实URL。")
|
||
await event.reply("无法解析B站短链接。")
|
||
return
|
||
else:
|
||
real_url = url.split('?')[0]
|
||
|
||
video_info = await parse_video_info(real_url)
|
||
if not video_info:
|
||
logger.error(f"[bili_parser] 无法从 {real_url} 解析视频信息。")
|
||
await event.reply("无法获取视频信息,可能是B站接口变动或视频不存在。")
|
||
return
|
||
|
||
title = video_info.get("title", "未知标题")
|
||
owner_name = video_info.get("owner_name", "未知UP主")
|
||
cover_url = video_info.get("cover_url")
|
||
bvid = video_info.get("bvid", "N/A")
|
||
play_count = format_count(video_info.get("play", 0))
|
||
like_count = format_count(video_info.get("like", 0))
|
||
|
||
text_part = (
|
||
f"标题: {title}\n"
|
||
f"UP主: {owner_name}\n"
|
||
f"BV: {bvid} | ▶️ {play_count} | 👍 {like_count}"
|
||
)
|
||
|
||
reply_message = [MessageSegment.from_text(text_part)]
|
||
if cover_url:
|
||
reply_message.append(MessageSegment.image(cover_url))
|
||
|
||
logger.success(f"[bili_parser] 成功解析视频信息并准备回复: {title}")
|
||
await event.reply(reply_message)
|