re-style: 格式化代码

This commit is contained in:
John Richard
2025-10-02 20:26:01 +08:00
parent ecb02cae31
commit 7923eafef3
263 changed files with 3103 additions and 3123 deletions

View File

@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Bilibili 插件包
提供B站视频观看体验功能像真实用户一样浏览和评价视频

View File

@@ -1,16 +1,17 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Bilibili 工具基础模块
提供 B 站视频信息获取和视频分析功能
"""
import re
import aiohttp
import asyncio
from typing import Optional, Dict, Any
from src.common.logger import get_logger
import re
from typing import Any
import aiohttp
from src.chat.utils.utils_video import get_video_analyzer
from src.common.logger import get_logger
logger = get_logger("bilibili_tool")
@@ -25,7 +26,7 @@ class BilibiliVideoAnalyzer:
"Referer": "https://www.bilibili.com/",
}
def extract_bilibili_url(self, text: str) -> Optional[str]:
def extract_bilibili_url(self, text: str) -> str | None:
"""从文本中提取哔哩哔哩视频链接"""
# 哔哩哔哩短链接模式
short_pattern = re.compile(r"https?://b23\.tv/[\w]+", re.IGNORECASE)
@@ -44,7 +45,7 @@ class BilibiliVideoAnalyzer:
return None
async def get_video_info(self, url: str) -> Optional[Dict[str, Any]]:
async def get_video_info(self, url: str) -> dict[str, Any] | None:
"""获取哔哩哔哩视频基本信息"""
try:
logger.info(f"🔍 解析视频URL: {url}")
@@ -127,7 +128,7 @@ class BilibiliVideoAnalyzer:
logger.exception("详细错误信息:")
return None
async def get_video_stream_url(self, aid: int, cid: int) -> Optional[str]:
async def get_video_stream_url(self, aid: int, cid: int) -> str | None:
"""获取视频流URL"""
try:
logger.info(f"🎥 获取视频流URL: aid={aid}, cid={cid}")
@@ -164,7 +165,7 @@ class BilibiliVideoAnalyzer:
return stream_url
# 降级到FLV格式
if "durl" in play_data and play_data["durl"]:
if play_data.get("durl"):
logger.info("📹 使用FLV格式视频流")
stream_url = play_data["durl"][0].get("url")
if stream_url:
@@ -185,7 +186,7 @@ class BilibiliVideoAnalyzer:
logger.exception("详细错误信息:")
return None
async def download_video_bytes(self, stream_url: str, max_size_mb: int = 100) -> Optional[bytes]:
async def download_video_bytes(self, stream_url: str, max_size_mb: int = 100) -> bytes | None:
"""下载视频字节数据
Args:
@@ -244,7 +245,7 @@ class BilibiliVideoAnalyzer:
logger.exception("详细错误信息:")
return None
async def analyze_bilibili_video(self, url: str, prompt: str = None) -> Dict[str, Any]:
async def analyze_bilibili_video(self, url: str, prompt: str = None) -> dict[str, Any]:
"""分析哔哩哔哩视频并返回详细信息和AI分析结果"""
try:
logger.info(f"🎬 开始分析哔哩哔哩视频: {url}")
@@ -322,10 +323,10 @@ class BilibiliVideoAnalyzer:
return result
except Exception as e:
error_msg = f"分析哔哩哔哩视频时发生异常: {str(e)}"
error_msg = f"分析哔哩哔哩视频时发生异常: {e!s}"
logger.error(f"{error_msg}")
logger.exception("详细错误信息:") # 记录完整的异常堆栈
return {"error": f"分析失败: {str(e)}"}
return {"error": f"分析失败: {e!s}"}
# 全局实例

View File

@@ -1,14 +1,15 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Bilibili 视频观看体验工具
支持哔哩哔哩视频链接解析和AI视频内容分析
"""
from typing import Dict, Any, List, Tuple, Type
from src.plugin_system import BaseTool, ToolParamType, BasePlugin, register_plugin, ComponentInfo, ConfigField
from .bilibli_base import get_bilibili_analyzer
from typing import Any
from src.common.logger import get_logger
from src.plugin_system import BasePlugin, BaseTool, ComponentInfo, ConfigField, ToolParamType, register_plugin
from .bilibli_base import get_bilibili_analyzer
logger = get_logger("bilibili_tool")
@@ -41,7 +42,7 @@ class BilibiliTool(BaseTool):
super().__init__(plugin_config)
self.analyzer = get_bilibili_analyzer()
async def execute(self, function_args: Dict[str, Any]) -> Dict[str, Any]:
async def execute(self, function_args: dict[str, Any]) -> dict[str, Any]:
"""执行哔哩哔哩视频观看体验"""
try:
url = function_args.get("url", "").strip()
@@ -83,7 +84,7 @@ class BilibiliTool(BaseTool):
return {"name": self.name, "content": content.strip()}
except Exception as e:
error_msg = f"😅 看视频的时候出了点问题: {str(e)}"
error_msg = f"😅 看视频的时候出了点问题: {e!s}"
logger.error(error_msg)
return {"name": self.name, "content": error_msg}
@@ -104,7 +105,7 @@ class BilibiliTool(BaseTool):
return base_prompt
def _format_watch_experience(self, video_info: Dict, ai_analysis: str, interest_focus: str = None) -> str:
def _format_watch_experience(self, video_info: dict, ai_analysis: str, interest_focus: str = None) -> str:
"""格式化观看体验报告"""
# 根据播放量生成热度评价
@@ -191,8 +192,8 @@ class BilibiliPlugin(BasePlugin):
# 插件基本信息
plugin_name: str = "bilibili_video_watcher"
enable_plugin: bool = True
dependencies: List[str] = []
python_dependencies: List[str] = []
dependencies: list[str] = []
python_dependencies: list[str] = []
config_file_name: str = "config.toml"
# 配置节描述
@@ -220,6 +221,6 @@ class BilibiliPlugin(BasePlugin):
},
}
def get_plugin_components(self) -> List[Tuple[ComponentInfo, Type]]:
def get_plugin_components(self) -> list[tuple[ComponentInfo, type]]:
"""返回插件包含的工具组件"""
return [(BilibiliTool.get_tool_info(), BilibiliTool)]