f26d4d8f7f
- Multi-source content aggregation (Bilibili/YouTube/Zhihu) - Smart inbox management with AI quality scoring - Long video chunking analysis (30min+ auto-segmentation) - Topic-based organization - Git auto-sync to Obsidian - Template system with Jinja2 and fallback rendering - CLI commands: subscribe, inbox, process, skip, status, search - Comprehensive test suite with unit and integration tests - Documentation: README.md and SKILL.md Checkpoint 7-8 completed by Agent E
181 lines
4.9 KiB
Python
181 lines
4.9 KiB
Python
"""
|
||
Bilibili Adapter 测试
|
||
"""
|
||
import sys
|
||
import os
|
||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src'))
|
||
|
||
from datetime import datetime, timedelta
|
||
from sources import SourceRegistry, BilibiliAdapter
|
||
from sources.bilibili import BilibiliAdapter as BiliAdapter
|
||
|
||
|
||
def test_registry():
|
||
"""测试注册表"""
|
||
print("=" * 50)
|
||
print("测试1: SourceRegistry 注册表")
|
||
print("=" * 50)
|
||
|
||
adapters = SourceRegistry.list()
|
||
print(f"已注册适配器: {adapters}")
|
||
|
||
adapter_class = SourceRegistry.get('bilibili')
|
||
assert adapter_class is not None, "bilibili适配器未注册"
|
||
print(f"✓ bilibili适配器类: {adapter_class}")
|
||
print()
|
||
|
||
|
||
def test_adapter_init():
|
||
"""测试适配器初始化"""
|
||
print("=" * 50)
|
||
print("测试2: 适配器初始化")
|
||
print("=" * 50)
|
||
|
||
adapter = BiliAdapter()
|
||
assert adapter is not None
|
||
print("✓ BilibiliAdapter实例化成功")
|
||
print()
|
||
|
||
|
||
def test_extract_bvid():
|
||
"""测试BV号提取"""
|
||
print("=" * 50)
|
||
print("测试3: BV号提取")
|
||
print("=" * 50)
|
||
|
||
adapter = BiliAdapter()
|
||
|
||
test_urls = [
|
||
("https://www.bilibili.com/video/BV1GJ411x7h7", "BV1GJ411x7h7"),
|
||
("https://b23.tv/BV1GJ411x7h7", "BV1GJ411x7h7"),
|
||
("BV1GJ411x7h7", "BV1GJ411x7h7"),
|
||
("https://www.bilibili.com/video/bv1gj411x7h7", "bv1gj411x7h7"),
|
||
]
|
||
|
||
for url, expected in test_urls:
|
||
result = adapter._extract_bvid_from_url(url)
|
||
if result and result.upper() == expected.upper():
|
||
print(f"✓ {url} -> {result}")
|
||
else:
|
||
print(f"✗ {url} -> {result} (期望: {expected})")
|
||
print()
|
||
|
||
|
||
def test_fetch_content():
|
||
"""测试获取视频内容(真实API调用)"""
|
||
print("=" * 50)
|
||
print("测试4: 从URL提取视频内容")
|
||
print("=" * 50)
|
||
|
||
adapter = BiliAdapter()
|
||
|
||
# 使用一个公开的视频链接测试
|
||
test_url = "https://www.bilibili.com/video/BV1GJ411x7h7"
|
||
|
||
try:
|
||
content = adapter.extract_content(test_url)
|
||
if content:
|
||
print(f"✓ 成功提取视频信息:")
|
||
print(f" - ID: {content.id}")
|
||
print(f" - 标题: {content.title[:50]}...")
|
||
print(f" - 作者: {content.author}")
|
||
print(f" - 时长: {content.duration}秒")
|
||
print(f" - URL: {content.url}")
|
||
else:
|
||
print(f"✗ 无法提取视频内容")
|
||
except Exception as e:
|
||
print(f"✗ 提取失败: {e}")
|
||
print()
|
||
|
||
|
||
def test_get_transcript():
|
||
"""测试获取字幕(真实API调用)"""
|
||
print("=" * 50)
|
||
print("测试5: 获取视频字幕")
|
||
print("=" * 50)
|
||
|
||
adapter = BiliAdapter()
|
||
|
||
# 找一个有字幕的视频
|
||
# BV1GJ411x7h7 是经典测试视频
|
||
test_url = "https://www.bilibili.com/video/BV1GJ411x7h7"
|
||
|
||
try:
|
||
content = adapter.extract_content(test_url)
|
||
if content:
|
||
transcript = adapter.get_transcript(content)
|
||
if transcript:
|
||
lines = transcript.split('\n')
|
||
print(f"✓ 成功获取字幕:")
|
||
print(f" - 总行数: {len(lines)}")
|
||
print(f" - 前200字符:\n{transcript[:200]}...")
|
||
else:
|
||
print(f"ℹ 该视频无字幕或字幕获取失败")
|
||
else:
|
||
print(f"✗ 无法获取视频内容")
|
||
except Exception as e:
|
||
print(f"✗ 获取字幕失败: {e}")
|
||
print()
|
||
|
||
|
||
def test_parse_duration():
|
||
"""测试时长解析"""
|
||
print("=" * 50)
|
||
print("测试6: 时长解析")
|
||
print("=" * 50)
|
||
|
||
adapter = BiliAdapter()
|
||
|
||
test_cases = [
|
||
("5:30", 330),
|
||
("1:23:45", 5025),
|
||
("0:45", 45),
|
||
("10:00", 600),
|
||
]
|
||
|
||
for duration_str, expected in test_cases:
|
||
result = adapter._parse_duration(duration_str)
|
||
if result == expected:
|
||
print(f"✓ {duration_str} -> {result}秒")
|
||
else:
|
||
print(f"✗ {duration_str} -> {result}秒 (期望: {expected})")
|
||
print()
|
||
|
||
|
||
def test_timestamp_conversion():
|
||
"""测试时间戳转换"""
|
||
print("=" * 50)
|
||
print("测试7: 时间戳转换")
|
||
print("=" * 50)
|
||
|
||
adapter = BiliAdapter()
|
||
|
||
# 测试秒级时间戳
|
||
ts = 1704067200 # 2024-01-01 00:00:00
|
||
dt = adapter._timestamp_to_datetime(ts)
|
||
print(f"✓ 秒级时间戳 {ts} -> {dt}")
|
||
|
||
# 测试毫秒级时间戳
|
||
ts_ms = 1704067200000
|
||
dt = adapter._timestamp_to_datetime(ts_ms)
|
||
print(f"✓ 毫秒级时间戳 {ts_ms} -> {dt}")
|
||
print()
|
||
|
||
|
||
if __name__ == "__main__":
|
||
print("\n" + "=" * 60)
|
||
print("Bilibili Adapter 测试套件")
|
||
print("=" * 60 + "\n")
|
||
|
||
test_registry()
|
||
test_adapter_init()
|
||
test_extract_bvid()
|
||
test_parse_duration()
|
||
test_timestamp_conversion()
|
||
test_fetch_content()
|
||
test_get_transcript()
|
||
|
||
print("\n" + "=" * 60)
|
||
print("测试完成")
|
||
print("=" * 60)
|