feat(obsidian-kb): 实现短链接解析功能

- 新增 shortlink_resolver.py 模块
  * 检测 B站短链接(b23.tv)和其他短链接服务
  * 提供友好的用户引导信息
  * 支持 Playwright 浏览器自动化解析(可选)

- 集成到 handler.py
  * 短链接自动检测和提示
  * 智能区分短链接和完整 URL

- 更新 add_video.py
  * 支持短链接解析
  * 添加 --playwright 参数启用浏览器解析

- 新增 test_shortlink.py 测试脚本
  * 测试单个短链接检测
  * 测试多个短链接检测
  * 测试混合内容场景
  * 所有测试通过

- 更新文档
  * SKILL.md: 添加短链接说明
  * AUTO_TRIGGER.md: 添加短链接章节
  * obsidian-kb-improvements.md: 标记任务2为已完成

功能特点:
- 方案A(用户引导):已实现并作为默认行为
- 方案B(浏览器自动化):提供 PlaywrightResolver 类,可选使用
- 方案C(代理池):未实现(复杂度高,收益低)

验收标准达成:
✅ 短链接能够友好处理(显示引导信息)
✅ 用户不会感到困惑
✅ 支持 b23.tv 和其他短链接服务(检测)
✅ 集成到 handler.py
✅ 测试各种短链接场景
This commit is contained in:
OpenClaw Agent
2026-02-06 21:22:45 +08:00
parent 7a1ad6088a
commit fbcc3df2cf
7 changed files with 746 additions and 77 deletions
+20 -16
View File
@@ -16,6 +16,13 @@ from urllib.parse import urlparse
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'src'))
# Import short link resolver
from shortlink_resolver import (
ShortLinkResolver,
detect_and_format,
PlaywrightResolver
)
# Pattern definitions for auto-detection
PATTERNS = {
'bilibili_full': re.compile(r'https?://(?:www\.)?bilibili\.com/video/(BV\w+)'),
@@ -147,27 +154,17 @@ def format_preview(detected: List[Dict[str, str]]) -> str:
return "\n".join(lines)
def format_short_link_help() -> str:
def format_short_link_help(short_link: Optional[str] = None) -> str:
"""
Format help message for short links.
Args:
short_link: Optional short link URL to mention
Returns:
Help message
"""
return """🔗 检测到短链接
⚠️ 由于反爬虫保护,短链接无法自动解析
📱 手机用户:
1. 打开链接
2. 在浏览器地址栏复制完整 URL
3. 发送给助手
💻 桌面用户:
1. 发送 BV 号(如:BV1jcFqz7EFW)
2. 或使用完整 URL
💡 提示:完整 URL 格式为:https://www.bilibili.com/video/BVxxxxx"""
return ShortLinkResolver.format_short_link_help(short_link)
def get_action_prompt(detected: List[Dict[str, str]]) -> str:
@@ -289,12 +286,19 @@ def handle_message(text: str) -> Optional[str]:
Returns:
Response message, or None if no action needed
"""
# Detect links
# First, check for short links and provide help
has_short, short_msg = detect_and_format(text)
# Detect all links (including short ones)
detected = detect_links(text)
if not detected:
return None
# If we have short links, show help message first
if has_short:
return short_msg
# Check if this is a confirmation (y/yes)
text_lower = text.lower().strip()
if text_lower in ('y', 'yes', '是', '好的', 'ok'):