feat: implement obsidian-kb skill with long video chunking
- 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
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
"""Obsidian KB - 知识库内容管理技能
|
||||
Checkpoint 4-5: 内容处理管道与长视频分段分析
|
||||
"""
|
||||
|
||||
from .models import Content, ContentType, ContentStatus, TimelineEntry, AnalysisResult
|
||||
from .light_scanner import LightScanner
|
||||
from .deep_analyzer import DeepAnalyzer
|
||||
from .pipeline import ProcessingPipeline
|
||||
from .content_chunker import ContentChunker, Chunk, get_chunking_strategy, chunk_content
|
||||
from .ai_client import AIClient
|
||||
from .config import Config, get_config
|
||||
|
||||
__all__ = [
|
||||
# 模型
|
||||
'Content',
|
||||
'ContentType',
|
||||
'ContentStatus',
|
||||
'TimelineEntry',
|
||||
'AnalysisResult',
|
||||
# 处理器
|
||||
'LightScanner',
|
||||
'DeepAnalyzer',
|
||||
'ProcessingPipeline',
|
||||
# 分片
|
||||
'ContentChunker',
|
||||
'Chunk',
|
||||
'get_chunking_strategy',
|
||||
'chunk_content',
|
||||
# AI客户端
|
||||
'AIClient',
|
||||
# 配置
|
||||
'Config',
|
||||
'get_config',
|
||||
]
|
||||
|
||||
__version__ = '0.2.0'
|
||||
@@ -0,0 +1,317 @@
|
||||
"""Obsidian KB - AI客户端
|
||||
C5.3: 实现长视频分段分析逻辑
|
||||
"""
|
||||
import os
|
||||
import json
|
||||
from typing import Dict, Any, List, Optional
|
||||
from src.models import ContentType
|
||||
from src.config import get_config
|
||||
from src.content_chunker import ContentChunker, get_chunking_strategy, chunk_content
|
||||
|
||||
|
||||
class AIClient:
|
||||
"""
|
||||
AI客户端 - 支持分段分析长视频内容
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self.config = get_config()
|
||||
self.chunker = ContentChunker()
|
||||
|
||||
def quick_generate(self, prompt: str, max_tokens: int = 100) -> str:
|
||||
"""快速生成(用于轻量扫描)"""
|
||||
# 模拟AI响应(实际应调用API)
|
||||
return self._mock_generate(prompt, max_tokens)
|
||||
|
||||
def generate_json(self, prompt: str) -> Dict[str, Any]:
|
||||
"""生成JSON格式响应"""
|
||||
response = self.quick_generate(prompt, max_tokens=1000)
|
||||
try:
|
||||
return json.loads(response)
|
||||
except:
|
||||
return self._parse_json_from_text(response)
|
||||
|
||||
def _deep_analyze(self, content_text: str, content_type: str, duration: Optional[int] = None) -> Dict[str, Any]:
|
||||
"""
|
||||
C5.3: 深度分析入口,根据内容时长决定是否分段分析
|
||||
|
||||
Args:
|
||||
content_text: 内容文本
|
||||
content_type: 内容类型
|
||||
duration: 视频时长(秒)
|
||||
|
||||
Returns:
|
||||
分析结果字典
|
||||
"""
|
||||
duration = duration or 0
|
||||
|
||||
print(f"[AIClient] 深度分析: type={content_type}, duration={duration}s")
|
||||
|
||||
# 长视频(>=30分钟)且是视频类型,使用分段分析
|
||||
if duration >= 1800 and content_type == "video":
|
||||
print(f"[AIClient] 使用分段分析策略")
|
||||
return self._analyze_in_chunks(content_text, duration)
|
||||
|
||||
# 短视频直接分析
|
||||
print(f"[AIClient] 使用直接分析策略")
|
||||
return self._analyze_direct(content_text)
|
||||
|
||||
def _analyze_in_chunks(self, content_text: str, duration: int) -> Dict[str, Any]:
|
||||
"""
|
||||
C5.3: 分段分析长视频
|
||||
|
||||
Args:
|
||||
content_text: SRT字幕文本
|
||||
duration: 视频时长(秒)
|
||||
|
||||
Returns:
|
||||
合并后的分析结果
|
||||
"""
|
||||
# 1. 获取分段策略
|
||||
strategy = get_chunking_strategy(duration)
|
||||
print(f"[AIClient] 分段策略: {strategy['description']}")
|
||||
|
||||
# 2. 执行分片
|
||||
chunks = chunk_content(content_text, duration)
|
||||
print(f"[AIClient] 内容已分为 {len(chunks)} 段")
|
||||
|
||||
# 3. 逐段分析
|
||||
chunk_summaries = []
|
||||
for i, chunk in enumerate(chunks):
|
||||
print(f"[AIClient] 分析第 {i+1}/{len(chunks)} 段...")
|
||||
summary = self._analyze_chunk(chunk.text, chunk.index)
|
||||
|
||||
# 处理时间格式(可能是float秒数或字符串)
|
||||
start_time = chunk.start_time
|
||||
end_time = chunk.end_time
|
||||
if isinstance(start_time, (int, float)):
|
||||
start_time = self._format_time(start_time)
|
||||
if isinstance(end_time, (int, float)):
|
||||
end_time = self._format_time(end_time)
|
||||
|
||||
chunk_summaries.append({
|
||||
'index': chunk.index,
|
||||
'start_time': start_time,
|
||||
'end_time': end_time,
|
||||
'summary': summary
|
||||
})
|
||||
|
||||
# 4. 合并所有段摘要
|
||||
print(f"[AIClient] 合并 {len(chunk_summaries)} 段分析结果")
|
||||
final_result = self._merge_summaries(chunk_summaries, "video")
|
||||
|
||||
# 5. 构建完整时间轴
|
||||
final_result['timeline'] = self._build_timeline_from_chunks(chunk_summaries)
|
||||
|
||||
return final_result
|
||||
|
||||
def _analyze_chunk(self, chunk_text: str, chunk_index: int) -> str:
|
||||
"""
|
||||
C5.3: 分析单个内容分片
|
||||
|
||||
Args:
|
||||
chunk_text: 分片文本
|
||||
chunk_index: 分片索引
|
||||
|
||||
Returns:
|
||||
分片摘要(限制500字以内)
|
||||
"""
|
||||
# 截断文本避免过长
|
||||
text_sample = chunk_text[:3000] if len(chunk_text) > 3000 else chunk_text
|
||||
|
||||
prompt = f"""请对以下视频片段进行简要分析(限制在300字以内):
|
||||
|
||||
片段内容:
|
||||
{text_sample}
|
||||
|
||||
请提供:
|
||||
1. 这段的核心主题(50字以内)
|
||||
2. 关键要点(3-5条)
|
||||
3. 重要细节或引用
|
||||
|
||||
请用中文回答,控制在300字以内。"""
|
||||
|
||||
try:
|
||||
summary = self.quick_generate(prompt, max_tokens=500)
|
||||
# 确保不超过500字
|
||||
if len(summary) > 500:
|
||||
summary = summary[:497] + "..."
|
||||
return summary
|
||||
except Exception as e:
|
||||
print(f"[AIClient] 分片 {chunk_index} 分析失败: {e}")
|
||||
return f"第{chunk_index+1}段分析失败"
|
||||
|
||||
def _merge_summaries(self, chunk_summaries: List[Dict], content_type: str) -> Dict[str, Any]:
|
||||
"""
|
||||
C5.3: 合并所有段摘要,生成完整分析报告
|
||||
|
||||
Args:
|
||||
chunk_summaries: 各段摘要列表
|
||||
content_type: 内容类型
|
||||
|
||||
Returns:
|
||||
完整分析结果
|
||||
"""
|
||||
# 构建合并提示
|
||||
summaries_text = "\n\n".join([
|
||||
f"=== 第{s['index']+1}段 ({s['start_time']}-{s['end_time']}) ===\n{s['summary']}"
|
||||
for s in chunk_summaries
|
||||
])
|
||||
|
||||
prompt = f"""基于以下各段分析,生成完整的视频分析报告:
|
||||
|
||||
{summaries_text}
|
||||
|
||||
请提供以下格式的JSON:
|
||||
{{
|
||||
"summary": "整体摘要(200字以内)",
|
||||
"topics": ["主题1", "主题2", "主题3", "主题4"],
|
||||
"insights": ["深入见解1", "深入见解2", "深入见解3"],
|
||||
"actions": ["建议行动1", "建议行动2"],
|
||||
"full_analysis": "详细分析(500字以内)"
|
||||
}}"""
|
||||
|
||||
try:
|
||||
result = self.generate_json(prompt)
|
||||
|
||||
# 确保必要字段存在
|
||||
if 'topics' not in result:
|
||||
result['topics'] = ["主题A", "主题B", "主题C"]
|
||||
if 'insights' not in result:
|
||||
result['insights'] = ["重要见解1", "重要见解2"]
|
||||
if 'actions' not in result:
|
||||
result['actions'] = []
|
||||
if 'summary' not in result:
|
||||
result['summary'] = "长视频分析完成"
|
||||
if 'full_analysis' not in result:
|
||||
result['full_analysis'] = summaries_text[:500]
|
||||
|
||||
return result
|
||||
|
||||
except Exception as e:
|
||||
print(f"[AIClient] 合并摘要失败: {e}")
|
||||
# 返回基础合并结果
|
||||
return {
|
||||
'summary': f'视频共分为{len(chunk_summaries)}个部分进行分析',
|
||||
'topics': [f"主题{i+1}" for i in range(min(4, len(chunk_summaries)))],
|
||||
'insights': ['从分段分析中综合得出'],
|
||||
'actions': [],
|
||||
'full_analysis': summaries_text[:800]
|
||||
}
|
||||
|
||||
def _analyze_direct(self, content_text: str) -> Dict[str, Any]:
|
||||
"""直接分析(不分段)"""
|
||||
# 截断文本
|
||||
text_sample = content_text[:5000] if len(content_text) > 5000 else content_text
|
||||
|
||||
prompt = f"""请分析以下内容,提供JSON格式结果:
|
||||
|
||||
{text_sample}
|
||||
|
||||
返回格式:
|
||||
{{
|
||||
"summary": "整体摘要(150字以内)",
|
||||
"topics": ["主题1", "主题2", "主题3"],
|
||||
"insights": ["见解1", "见解2"],
|
||||
"actions": ["行动建议1"],
|
||||
"timeline": [
|
||||
{{"start_time": "00:00", "end_time": "05:00", "title": "第一部分", "summary": "..."}}
|
||||
]
|
||||
}}"""
|
||||
|
||||
try:
|
||||
return self.generate_json(prompt)
|
||||
except Exception as e:
|
||||
print(f"[AIClient] 直接分析失败: {e}")
|
||||
return {
|
||||
'summary': '分析失败',
|
||||
'topics': ['无法解析'],
|
||||
'insights': ['请检查内容格式'],
|
||||
'actions': [],
|
||||
'timeline': []
|
||||
}
|
||||
|
||||
def _format_time(self, seconds: float) -> str:
|
||||
"""格式化秒数为时间字符串"""
|
||||
hours = int(seconds // 3600)
|
||||
minutes = int((seconds % 3600) // 60)
|
||||
secs = int(seconds % 60)
|
||||
if hours > 0:
|
||||
return f"{hours:02d}:{minutes:02d}:{secs:02d}"
|
||||
return f"{minutes:02d}:{secs:02d}"
|
||||
|
||||
def _build_timeline_from_chunks(self, chunk_summaries: List[Dict]) -> List[Dict[str, Any]]:
|
||||
"""从分片摘要构建时间轴"""
|
||||
timeline = []
|
||||
for summary in chunk_summaries:
|
||||
timeline.append({
|
||||
'index': summary['index'],
|
||||
'start_time': summary['start_time'],
|
||||
'end_time': summary['end_time'],
|
||||
'title': f"第{summary['index']+1}部分",
|
||||
'summary': summary['summary'][:200] + "..." if len(summary['summary']) > 200 else summary['summary']
|
||||
})
|
||||
return timeline
|
||||
|
||||
def _mock_generate(self, prompt: str, max_tokens: int) -> str:
|
||||
"""模拟AI生成(实际实现应调用真实API)"""
|
||||
# 根据prompt内容返回模拟响应
|
||||
prompt_lower = prompt.lower()
|
||||
|
||||
if 'json' in prompt_lower or 'json格式' in prompt:
|
||||
if '主题' in prompt or 'topic' in prompt_lower:
|
||||
return json.dumps({
|
||||
'summary': '这是一个关于人工智能和技术发展的内容,涵盖了多个重要主题。',
|
||||
'topics': ['人工智能', '机器学习', '未来发展', '技术趋势'],
|
||||
'insights': [
|
||||
'AI正在改变我们的工作方式',
|
||||
'机器学习应用越来越广泛',
|
||||
'未来技术发展趋势值得关注'
|
||||
],
|
||||
'actions': ['深入研究相关技术', '关注行业动态'],
|
||||
'full_analysis': '详细分析内容...'
|
||||
}, ensure_ascii=False)
|
||||
return json.dumps({'result': '模拟JSON响应'})
|
||||
|
||||
if '摘要' in prompt or 'summary' in prompt_lower:
|
||||
return '这是一段关于人工智能发展的重要讨论,涵盖了技术趋势和未来应用。'
|
||||
|
||||
if '关键词' in prompt or 'keyword' in prompt_lower:
|
||||
return '人工智能, 机器学习, 深度学习, 神经网络, 自然语言处理'
|
||||
|
||||
return '模拟AI响应内容'
|
||||
|
||||
def _parse_json_from_text(self, text: str) -> Dict[str, Any]:
|
||||
"""从文本中解析JSON"""
|
||||
# 尝试提取JSON块
|
||||
import re
|
||||
json_match = re.search(r'\{[\s\S]*\}', text)
|
||||
if json_match:
|
||||
try:
|
||||
return json.loads(json_match.group())
|
||||
except:
|
||||
pass
|
||||
return {'raw_response': text}
|
||||
|
||||
|
||||
class MultiAIClient:
|
||||
"""
|
||||
多AI客户端 - 支持多种AI提供商的客户端
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self.config = get_config()
|
||||
self.primary_client = AIClient()
|
||||
self.clients = {
|
||||
'openai': self.primary_client,
|
||||
'local': AIClient(), # 可以配置为本地模型
|
||||
}
|
||||
|
||||
def _deep_analyze(self, content_text: str, content_type: str, duration: Optional[int] = None) -> Dict[str, Any]:
|
||||
"""使用主客户端进行深度分析"""
|
||||
return self.primary_client._deep_analyze(content_text, content_type, duration)
|
||||
|
||||
def quick_generate(self, prompt: str, max_tokens: int = 100, provider: str = None) -> str:
|
||||
"""快速生成"""
|
||||
client = self.clients.get(provider, self.primary_client)
|
||||
return client.quick_generate(prompt, max_tokens)
|
||||
@@ -0,0 +1,569 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Obsidian KB Knowledge Base CLI Tool
|
||||
|
||||
Command line interface for managing Obsidian knowledge base content.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
import os
|
||||
from typing import Optional
|
||||
from datetime import datetime
|
||||
|
||||
# Add parent directory to path for imports
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
try:
|
||||
from src.config import Config
|
||||
from src.storage import Storage
|
||||
from src.pipeline import ProcessingPipeline
|
||||
from src.models import ContentStatus
|
||||
CONFIG_AVAILABLE = True
|
||||
except ImportError as e:
|
||||
print(f"Import warning: {e}", file=sys.stderr)
|
||||
CONFIG_AVAILABLE = False
|
||||
|
||||
|
||||
def print_header(text: str):
|
||||
"""Print formatted header."""
|
||||
print(f"\n{'=' * 50}")
|
||||
print(f" {text}")
|
||||
print(f"{'=' * 50}\n")
|
||||
|
||||
|
||||
def print_table(headers: list, rows: list, max_width: int = 80):
|
||||
"""Print a simple formatted table."""
|
||||
if not rows:
|
||||
print("(No data)")
|
||||
return
|
||||
|
||||
# Calculate column widths
|
||||
col_widths = [len(h) for h in headers]
|
||||
for row in rows:
|
||||
for i, cell in enumerate(row):
|
||||
if i < len(col_widths):
|
||||
col_widths[i] = max(col_widths[i], len(str(cell)[:max_width]))
|
||||
|
||||
# Print headers
|
||||
header_line = " | ".join(h.ljust(w) for h, w in zip(headers, col_widths))
|
||||
print(header_line)
|
||||
print("-" * len(header_line))
|
||||
|
||||
# Print rows
|
||||
for row in rows:
|
||||
row_str = " | ".join(str(cell)[:max_width].ljust(w) for cell, w in zip(row, col_widths))
|
||||
print(row_str)
|
||||
print()
|
||||
|
||||
|
||||
def cmd_subscribe(args: argparse.Namespace):
|
||||
"""Handle subscribe command - save subscription configuration."""
|
||||
print_header("Subscribe Configuration")
|
||||
|
||||
if not CONFIG_AVAILABLE:
|
||||
print("Error: Config module not available. Please check installation.")
|
||||
return 1
|
||||
|
||||
config = Config()
|
||||
|
||||
# Build subscription entry
|
||||
subscription = {
|
||||
'platform': args.platform,
|
||||
'type': args.type,
|
||||
'id': args.id,
|
||||
'name': args.name or f"{args.platform}_{args.type}_{args.id}",
|
||||
'created_at': datetime.now().isoformat()
|
||||
}
|
||||
|
||||
# Load existing subscriptions
|
||||
subscriptions = config.get('subscriptions', [])
|
||||
|
||||
# Check for duplicates
|
||||
for sub in subscriptions:
|
||||
if sub['platform'] == args.platform and sub['id'] == args.id:
|
||||
print(f"⚠️ Subscription already exists: {sub['name']}")
|
||||
print(" Updating configuration...")
|
||||
sub.update(subscription)
|
||||
break
|
||||
else:
|
||||
subscriptions.append(subscription)
|
||||
print(f"✅ Added new subscription: {subscription['name']}")
|
||||
|
||||
# Save configuration
|
||||
config.set('subscriptions', subscriptions)
|
||||
config.save()
|
||||
|
||||
print(f"\n Platform: {args.platform}")
|
||||
print(f" Type: {args.type}")
|
||||
print(f" ID: {args.id}")
|
||||
print(f" Name: {subscription['name']}")
|
||||
print(f"\n💾 Configuration saved to config.yaml")
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
def cmd_inbox(args: argparse.Namespace):
|
||||
"""Handle inbox command - display pending items."""
|
||||
print_header("Inbox Contents")
|
||||
|
||||
if not CONFIG_AVAILABLE:
|
||||
print("Error: Storage module not available.")
|
||||
return 1
|
||||
|
||||
storage = Storage()
|
||||
# Filter for CONFIRMED status (ready to process)
|
||||
contents = storage.list_inbox(status=ContentStatus.CONFIRMED)
|
||||
|
||||
if not contents:
|
||||
print("📭 Inbox is empty!")
|
||||
return 0
|
||||
|
||||
# Sort by the requested criteria
|
||||
if args.sort == 'score':
|
||||
contents.sort(key=lambda x: x.quality_metrics.overall_score if hasattr(x, 'quality_metrics') else 0, reverse=True)
|
||||
else: # date
|
||||
contents.sort(key=lambda x: x.created_at, reverse=True)
|
||||
|
||||
# Limit results
|
||||
contents = contents[:args.limit]
|
||||
|
||||
print(f"Showing {len(contents)} item(s) (sorted by {args.sort}):\n")
|
||||
|
||||
# Format table rows
|
||||
rows = []
|
||||
for idx, content in enumerate(contents, 1):
|
||||
title = content.title[:40] if content.title else 'Untitled'
|
||||
score = content.quality_metrics.overall_score if hasattr(content, 'quality_metrics') else 0
|
||||
summary = content.summary[:50] if content.summary else 'No summary'
|
||||
content_id = content.id
|
||||
|
||||
rows.append([
|
||||
f"{idx:2d}",
|
||||
content_id[:12],
|
||||
f"{score:.1f}",
|
||||
title,
|
||||
summary + "..." if len(content.summary or '') > 50 else summary
|
||||
])
|
||||
|
||||
print_table(
|
||||
["#", "ID", "Score", "Title", "Summary"],
|
||||
rows
|
||||
)
|
||||
|
||||
print(f"\n💡 Use 'process <id>' to process an item, or 'process all' to process all")
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
def cmd_process(args: argparse.Namespace):
|
||||
"""Handle process command - process content items."""
|
||||
print_header("Process Content")
|
||||
|
||||
if not CONFIG_AVAILABLE:
|
||||
print("Error: Pipeline module not available.")
|
||||
return 1
|
||||
|
||||
storage = Storage()
|
||||
|
||||
# Import pipeline components
|
||||
try:
|
||||
from src.pipeline import ProcessingPipeline
|
||||
from src.light_scanner import LightScanner
|
||||
from src.deep_analyzer import DeepAnalyzer
|
||||
from src.config import get_config
|
||||
|
||||
config = get_config()
|
||||
pipeline = ProcessingPipeline()
|
||||
except Exception as e:
|
||||
print(f"❌ Error initializing pipeline: {e}")
|
||||
return 1
|
||||
|
||||
# Determine what to process
|
||||
if args.content_id == 'all':
|
||||
items = storage.list_inbox(status=ContentStatus.CONFIRMED)
|
||||
if not items:
|
||||
print("📭 Inbox is empty! Nothing to process.")
|
||||
return 0
|
||||
|
||||
print(f"Found {len(items)} item(s) to process.\n")
|
||||
|
||||
if not args.yes:
|
||||
confirm = input("Process all items? [Y/n]: ").strip().lower()
|
||||
if confirm and confirm not in ('y', 'yes'):
|
||||
print("❌ Cancelled.")
|
||||
return 0
|
||||
else:
|
||||
item = storage.load_content(args.content_id)
|
||||
if not item:
|
||||
print(f"❌ Content not found: {args.content_id}")
|
||||
return 1
|
||||
items = [item]
|
||||
print(f"Processing single item: {item.title}\n")
|
||||
|
||||
# Process items with progress
|
||||
success_count = 0
|
||||
fail_count = 0
|
||||
|
||||
for i, item in enumerate(items, 1):
|
||||
content_id = item.id
|
||||
title = item.title or 'Untitled'
|
||||
|
||||
print(f"[{i}/{len(items)}] Processing: {title[:50]}...")
|
||||
print(f" ID: {content_id}")
|
||||
|
||||
try:
|
||||
# Use the pipeline to process the content
|
||||
# The pipeline expects content_text - we'll use transcript or summary
|
||||
content_text = item.transcript or item.summary or ""
|
||||
|
||||
result = pipeline.process_full(item, content_text)
|
||||
|
||||
# Save the updated content
|
||||
storage.save_content(result)
|
||||
|
||||
print(f" ✅ Success")
|
||||
print(f" 📄 Note: {result.output_path or 'N/A'}")
|
||||
print(f" 🏷️ Tags: {', '.join(result.tags)}")
|
||||
success_count += 1
|
||||
|
||||
except Exception as e:
|
||||
print(f" ❌ Error: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
fail_count += 1
|
||||
|
||||
print()
|
||||
|
||||
# Summary
|
||||
print(f"\n📊 Processing Complete")
|
||||
print(f" Successful: {success_count}")
|
||||
print(f" Failed: {fail_count}")
|
||||
print(f" Total: {len(items)}")
|
||||
|
||||
return 0 if fail_count == 0 else 1
|
||||
|
||||
|
||||
def cmd_skip(args: argparse.Namespace):
|
||||
"""Handle skip command - skip/remove an item from inbox."""
|
||||
print_header("Skip Content")
|
||||
|
||||
if not CONFIG_AVAILABLE:
|
||||
print("Error: Storage module not available.")
|
||||
return 1
|
||||
|
||||
storage = Storage()
|
||||
|
||||
# Find the item
|
||||
content = storage.load_content(args.content_id)
|
||||
if not content:
|
||||
print(f"❌ Content not found: {args.content_id}")
|
||||
return 1
|
||||
|
||||
print(f"Skipping: {content.title or 'Untitled'}")
|
||||
print(f"ID: {args.content_id}")
|
||||
|
||||
if args.reason:
|
||||
print(f"Reason: {args.reason}")
|
||||
|
||||
# Mark as skipped
|
||||
content.status = ContentStatus.SKIPPED
|
||||
if args.reason:
|
||||
content.summary = f"[Skipped: {args.reason}] {content.summary or ''}"
|
||||
|
||||
storage.save_content(content)
|
||||
|
||||
print("✅ Item skipped and marked as skipped.")
|
||||
if args.reason:
|
||||
print(f" Reason recorded: {args.reason}")
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
def cmd_status(args: argparse.Namespace):
|
||||
"""Handle status command - show system status."""
|
||||
print_header("System Status")
|
||||
|
||||
# Git status
|
||||
print("📁 Git Repository Status")
|
||||
print("-" * 30)
|
||||
|
||||
import subprocess
|
||||
try:
|
||||
# Check if we're in a git repo
|
||||
result = subprocess.run(
|
||||
['git', 'status', '--short'],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
cwd=os.getcwd()
|
||||
)
|
||||
|
||||
if result.returncode == 0:
|
||||
if result.stdout.strip():
|
||||
print("Uncommitted changes:")
|
||||
print(result.stdout)
|
||||
else:
|
||||
print("✅ Working directory clean")
|
||||
|
||||
# Get branch info
|
||||
branch_result = subprocess.run(
|
||||
['git', 'branch', '--show-current'],
|
||||
capture_output=True,
|
||||
text=True
|
||||
)
|
||||
if branch_result.returncode == 0:
|
||||
print(f"Branch: {branch_result.stdout.strip()}")
|
||||
else:
|
||||
print("⚠️ Not a git repository or git not available")
|
||||
except FileNotFoundError:
|
||||
print("⚠️ Git not available")
|
||||
except Exception as e:
|
||||
print(f"⚠️ Git error: {e}")
|
||||
|
||||
print()
|
||||
|
||||
# Inbox statistics
|
||||
print("📥 Inbox Statistics")
|
||||
print("-" * 30)
|
||||
|
||||
if CONFIG_AVAILABLE:
|
||||
storage = Storage()
|
||||
stats = storage.get_stats()
|
||||
|
||||
print(f"Pending (confirmed): {stats.get(ContentStatus.CONFIRMED.value, 0)}")
|
||||
print(f"New: {stats.get(ContentStatus.NEW.value, 0)}")
|
||||
print(f"Scanned: {stats.get(ContentStatus.SCANNED.value, 0)}")
|
||||
print(f"Analyzed: {stats.get(ContentStatus.ANALYZED.value, 0)}")
|
||||
print(f"Generated: {stats.get(ContentStatus.GENERATED.value, 0)}")
|
||||
print(f"Skipped: {stats.get(ContentStatus.SKIPPED.value, 0)}")
|
||||
else:
|
||||
print("⚠️ Storage module not available")
|
||||
|
||||
print()
|
||||
|
||||
# Configuration summary
|
||||
print("⚙️ Configuration")
|
||||
print("-" * 30)
|
||||
|
||||
if CONFIG_AVAILABLE:
|
||||
config = Config()
|
||||
subscriptions = config.get('subscriptions', [])
|
||||
print(f"Active subscriptions: {len(subscriptions)}")
|
||||
|
||||
if subscriptions:
|
||||
for sub in subscriptions:
|
||||
print(f" • {sub.get('name', 'Unnamed')} ({sub.get('platform')})")
|
||||
else:
|
||||
print("⚠️ Config module not available")
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
def cmd_search(args: argparse.Namespace):
|
||||
"""Handle search command - search processed notes."""
|
||||
print_header(f"Search: '{args.query}'")
|
||||
|
||||
if not CONFIG_AVAILABLE:
|
||||
print("Error: Storage module not available.")
|
||||
return 1
|
||||
|
||||
storage = Storage()
|
||||
|
||||
# Perform search across all content
|
||||
results = storage.search(args.query)
|
||||
|
||||
if not results:
|
||||
print("🔍 No results found.")
|
||||
return 0
|
||||
|
||||
# Filter by tags if requested
|
||||
if args.in_tags:
|
||||
query_lower = args.query.lower()
|
||||
results = [r for r in results if any(query_lower in tag.lower() for tag in r.tags)]
|
||||
|
||||
if not results:
|
||||
print("🔍 No results found in tags.")
|
||||
return 0
|
||||
|
||||
print(f"Found {len(results)} result(s):\n")
|
||||
|
||||
# Format results
|
||||
for i, content in enumerate(results, 1):
|
||||
title = content.title or 'Untitled'
|
||||
path = content.output_path or f"inbox/{content.id}.json"
|
||||
tags = content.tags
|
||||
|
||||
print(f"{i}. {title}")
|
||||
print(f" ID: {content.id}")
|
||||
print(f" Status: {content.status.value}")
|
||||
print(f" Path: {path}")
|
||||
if tags:
|
||||
print(f" Tags: {', '.join(tags)}")
|
||||
|
||||
# Show matching content preview
|
||||
if not args.in_tags and content.summary:
|
||||
content_text = content.summary
|
||||
query_lower = args.query.lower()
|
||||
idx = content_text.lower().find(query_lower)
|
||||
if idx >= 0:
|
||||
start = max(0, idx - 30)
|
||||
end = min(len(content_text), idx + len(args.query) + 30)
|
||||
preview = content_text[start:end]
|
||||
if start > 0:
|
||||
preview = "..." + preview
|
||||
if end < len(content_text):
|
||||
preview = preview + "..."
|
||||
print(f" Match: {preview}")
|
||||
|
||||
print()
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
def main():
|
||||
"""Main entry point for CLI."""
|
||||
parser = argparse.ArgumentParser(
|
||||
description='Obsidian KB 知识库维护工具',
|
||||
prog='obsidian-kb',
|
||||
formatter_class=argparse.RawDescriptionHelpFormatter,
|
||||
epilog="""
|
||||
Examples:
|
||||
%(prog)s subscribe bilibili fav 12345 --name="Tech Favorites"
|
||||
%(prog)s inbox --limit 10 --sort date
|
||||
%(prog)s process all --yes
|
||||
%(prog)s process abc123
|
||||
%(prog)s skip abc123 --reason="Low quality"
|
||||
%(prog)s status
|
||||
%(prog)s search "python tutorial" --in-tags
|
||||
"""
|
||||
)
|
||||
|
||||
subparsers = parser.add_subparsers(dest='command', help='Available commands')
|
||||
|
||||
# subscribe 子命令
|
||||
subscribe_parser = subparsers.add_parser(
|
||||
'subscribe',
|
||||
aliases=['sub'],
|
||||
help='Subscribe to a content source'
|
||||
)
|
||||
subscribe_parser.add_argument(
|
||||
'platform',
|
||||
choices=['bilibili', 'youtube', 'zhihu'],
|
||||
help='Platform to subscribe to'
|
||||
)
|
||||
subscribe_parser.add_argument(
|
||||
'type',
|
||||
choices=['fav', 'up', 'channel'],
|
||||
help='Subscription type'
|
||||
)
|
||||
subscribe_parser.add_argument(
|
||||
'id',
|
||||
help='Platform-specific ID (user ID, channel ID, etc.)'
|
||||
)
|
||||
subscribe_parser.add_argument(
|
||||
'--name',
|
||||
help='Friendly name for this subscription'
|
||||
)
|
||||
subscribe_parser.set_defaults(func=cmd_subscribe)
|
||||
|
||||
# inbox 子命令
|
||||
inbox_parser = subparsers.add_parser(
|
||||
'inbox',
|
||||
aliases=['i'],
|
||||
help='Show inbox contents'
|
||||
)
|
||||
inbox_parser.add_argument(
|
||||
'--limit',
|
||||
type=int,
|
||||
default=20,
|
||||
help='Maximum number of items to show (default: 20)'
|
||||
)
|
||||
inbox_parser.add_argument(
|
||||
'--sort',
|
||||
choices=['score', 'date'],
|
||||
default='score',
|
||||
help='Sort order (default: score)'
|
||||
)
|
||||
inbox_parser.set_defaults(func=cmd_inbox)
|
||||
|
||||
# process 子命令
|
||||
process_parser = subparsers.add_parser(
|
||||
'process',
|
||||
aliases=['p'],
|
||||
help='Process content items'
|
||||
)
|
||||
process_parser.add_argument(
|
||||
'content_id',
|
||||
nargs='?',
|
||||
default='all',
|
||||
help='Content ID to process, or "all" for all items (default: all)'
|
||||
)
|
||||
process_parser.add_argument(
|
||||
'--yes',
|
||||
action='store_true',
|
||||
help='Auto-confirm without prompting'
|
||||
)
|
||||
process_parser.set_defaults(func=cmd_process)
|
||||
|
||||
# skip 子命令
|
||||
skip_parser = subparsers.add_parser(
|
||||
'skip',
|
||||
aliases=['s'],
|
||||
help='Skip an item (remove from inbox)'
|
||||
)
|
||||
skip_parser.add_argument(
|
||||
'content_id',
|
||||
help='Content ID to skip'
|
||||
)
|
||||
skip_parser.add_argument(
|
||||
'--reason',
|
||||
help='Reason for skipping'
|
||||
)
|
||||
skip_parser.set_defaults(func=cmd_skip)
|
||||
|
||||
# status 子命令
|
||||
status_parser = subparsers.add_parser(
|
||||
'status',
|
||||
aliases=['st'],
|
||||
help='Show system status'
|
||||
)
|
||||
status_parser.set_defaults(func=cmd_status)
|
||||
|
||||
# search 子命令
|
||||
search_parser = subparsers.add_parser(
|
||||
'search',
|
||||
aliases=['q'],
|
||||
help='Search processed notes'
|
||||
)
|
||||
search_parser.add_argument(
|
||||
'query',
|
||||
help='Search query'
|
||||
)
|
||||
search_parser.add_argument(
|
||||
'--in-tags',
|
||||
action='store_true',
|
||||
help='Search only in tags'
|
||||
)
|
||||
search_parser.set_defaults(func=cmd_search)
|
||||
|
||||
# Parse arguments
|
||||
args = parser.parse_args()
|
||||
|
||||
# Execute command
|
||||
if args.command is None:
|
||||
parser.print_help()
|
||||
return 0
|
||||
|
||||
try:
|
||||
return args.func(args) or 0
|
||||
except KeyboardInterrupt:
|
||||
print("\n\n⚠️ Interrupted by user")
|
||||
return 130
|
||||
except Exception as e:
|
||||
print(f"\n❌ Error: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return 1
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
@@ -0,0 +1,280 @@
|
||||
"""
|
||||
Configuration management for Obsidian KB Skill.
|
||||
"""
|
||||
import os
|
||||
import yaml
|
||||
from pathlib import Path
|
||||
from typing import Optional, Dict, Any
|
||||
|
||||
|
||||
# Global config instance
|
||||
_config_instance: Optional['Config'] = None
|
||||
|
||||
|
||||
def get_config(config_path: Optional[str] = None) -> 'Config':
|
||||
"""Get global config instance."""
|
||||
global _config_instance
|
||||
if _config_instance is None or config_path:
|
||||
_config_instance = Config(config_path)
|
||||
return _config_instance
|
||||
|
||||
|
||||
class Config:
|
||||
"""Configuration manager."""
|
||||
|
||||
DEFAULT_CONFIG = {
|
||||
'ai': {
|
||||
'provider': 'openai',
|
||||
'model': 'gpt-4',
|
||||
'api_key': None,
|
||||
},
|
||||
'storage': {
|
||||
'inbox_dir': 'inbox',
|
||||
'topics_dir': 'topics',
|
||||
'data_dir': 'data',
|
||||
},
|
||||
'git': {
|
||||
'enabled': False,
|
||||
'vault_path': '~/Documents/Obsidian/KB',
|
||||
'remote': 'origin',
|
||||
'branch': 'main',
|
||||
'auto_push': False,
|
||||
'commit_message_template': 'Add note: {title}',
|
||||
},
|
||||
'obsidian': {
|
||||
'vault_path': '~/Documents/Obsidian/KB',
|
||||
'notes_folder': 'Notes',
|
||||
},
|
||||
'chunking': {
|
||||
'enabled': True,
|
||||
'chunk_duration_seconds': 900, # 15 minutes
|
||||
'max_chunk_duration_seconds': 1800, # 30 minutes
|
||||
'long_video_threshold': 1800, # 30 minutes
|
||||
},
|
||||
'sources': {
|
||||
'bilibili': {
|
||||
'enabled': True,
|
||||
'cookie': None,
|
||||
},
|
||||
'youtube': {
|
||||
'enabled': False,
|
||||
},
|
||||
'zhihu': {
|
||||
'enabled': False,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
def __init__(self, config_path: Optional[str] = None, load_env: bool = True):
|
||||
"""
|
||||
Initialize configuration.
|
||||
|
||||
Args:
|
||||
config_path: Path to config file. If None, uses default locations.
|
||||
load_env: Whether to load environment variable overrides.
|
||||
"""
|
||||
self.config_path = config_path or self._find_config_file()
|
||||
self._config = self._load_config(load_env)
|
||||
|
||||
def _find_config_file(self) -> str:
|
||||
"""Find configuration file in standard locations."""
|
||||
search_paths = [
|
||||
Path.cwd() / 'config.yaml',
|
||||
Path.cwd() / 'config.yml',
|
||||
Path.home() / '.obsidian-kb' / 'config.yaml',
|
||||
Path.home() / '.config' / 'obsidian-kb' / 'config.yaml',
|
||||
]
|
||||
|
||||
for path in search_paths:
|
||||
if path.exists():
|
||||
return str(path)
|
||||
|
||||
# Return default path
|
||||
return str(Path.cwd() / 'config.yaml')
|
||||
|
||||
def _load_config(self, load_env: bool = True) -> Dict[str, Any]:
|
||||
"""Load configuration from file or use defaults."""
|
||||
config = self.DEFAULT_CONFIG.copy()
|
||||
|
||||
if Path(self.config_path).exists():
|
||||
with open(self.config_path, 'r', encoding='utf-8') as f:
|
||||
user_config = yaml.safe_load(f)
|
||||
if user_config:
|
||||
self._deep_update(config, user_config)
|
||||
|
||||
# Override with environment variables
|
||||
if load_env:
|
||||
self._apply_env_overrides(config)
|
||||
|
||||
return config
|
||||
|
||||
def _deep_update(self, base: Dict, update: Dict) -> None:
|
||||
"""Deep update dictionary."""
|
||||
for key, value in update.items():
|
||||
if key in base and isinstance(base[key], dict) and isinstance(value, dict):
|
||||
self._deep_update(base[key], value)
|
||||
else:
|
||||
base[key] = value
|
||||
|
||||
def _apply_env_overrides(self, config: Dict[str, Any]) -> None:
|
||||
"""Apply environment variable overrides."""
|
||||
# AI settings
|
||||
if os.getenv('OPENAI_API_KEY'):
|
||||
config['ai']['api_key'] = os.getenv('OPENAI_API_KEY')
|
||||
if os.getenv('AI_MODEL'):
|
||||
config['ai']['model'] = os.getenv('AI_MODEL')
|
||||
|
||||
# Git settings
|
||||
if os.getenv('OBSIDIAN_VAULT_PATH'):
|
||||
config['git']['vault_path'] = os.getenv('OBSIDIAN_VAULT_PATH')
|
||||
|
||||
# Bilibili cookie
|
||||
if os.getenv('BILIBILI_COOKIE'):
|
||||
config['sources']['bilibili']['cookie'] = os.getenv('BILIBILI_COOKIE')
|
||||
|
||||
def get(self, key: str, default: Any = None) -> Any:
|
||||
"""
|
||||
Get configuration value using dot notation.
|
||||
|
||||
Args:
|
||||
key: Key in dot notation (e.g., 'ai.model')
|
||||
default: Default value if key not found
|
||||
|
||||
Returns:
|
||||
Configuration value
|
||||
"""
|
||||
keys = key.split('.')
|
||||
value = self._config
|
||||
|
||||
for k in keys:
|
||||
if isinstance(value, dict) and k in value:
|
||||
value = value[k]
|
||||
else:
|
||||
return default
|
||||
|
||||
return value
|
||||
|
||||
def set(self, key: str, value: Any) -> None:
|
||||
"""
|
||||
Set configuration value using dot notation.
|
||||
|
||||
Args:
|
||||
key: Key in dot notation
|
||||
value: Value to set
|
||||
"""
|
||||
keys = key.split('.')
|
||||
config = self._config
|
||||
|
||||
for k in keys[:-1]:
|
||||
if k not in config:
|
||||
config[k] = {}
|
||||
config = config[k]
|
||||
|
||||
config[keys[-1]] = value
|
||||
|
||||
def save(self) -> None:
|
||||
"""Save configuration to file."""
|
||||
Path(self.config_path).parent.mkdir(parents=True, exist_ok=True)
|
||||
with open(self.config_path, 'w', encoding='utf-8') as f:
|
||||
yaml.dump(self._config, f, default_flow_style=False, allow_unicode=True)
|
||||
|
||||
@property
|
||||
def obsidian_path(self) -> str:
|
||||
"""Get obsidian path (alias for vault_path)."""
|
||||
return str(self.vault_path)
|
||||
|
||||
@property
|
||||
def vault_path(self) -> Path:
|
||||
"""Get Obsidian vault path."""
|
||||
path = self.get('obsidian.vault_path') or self.get('git.vault_path', '~/Documents/Obsidian/KB')
|
||||
return Path(path).expanduser()
|
||||
|
||||
@property
|
||||
def notes_folder(self) -> str:
|
||||
"""Get notes folder."""
|
||||
return self.get('obsidian.notes_folder', 'Notes')
|
||||
|
||||
@property
|
||||
def ai_api_key(self) -> Optional[str]:
|
||||
"""Get AI API key."""
|
||||
return self.get('ai.api_key')
|
||||
|
||||
@property
|
||||
def ai_model(self) -> str:
|
||||
"""Get AI model."""
|
||||
return self.get('ai.model', 'gpt-4')
|
||||
|
||||
@property
|
||||
def chunking_enabled(self) -> bool:
|
||||
"""Check if chunking is enabled."""
|
||||
return self.get('chunking.enabled', True)
|
||||
|
||||
@property
|
||||
def chunk_duration(self) -> int:
|
||||
"""Get chunk duration in seconds."""
|
||||
return self.get('chunking.chunk_duration_seconds', 900)
|
||||
|
||||
@property
|
||||
def long_video_threshold(self) -> int:
|
||||
"""Get long video threshold in seconds."""
|
||||
return self.get('chunking.long_video_threshold', 1800)
|
||||
|
||||
@property
|
||||
def quality_threshold(self) -> float:
|
||||
"""Get quality threshold."""
|
||||
return self.get('quality_threshold', 0.7)
|
||||
|
||||
@property
|
||||
def obsidian(self) -> 'Config':
|
||||
"""Get obsidian config section (returns self for compatibility)."""
|
||||
return self
|
||||
|
||||
@property
|
||||
def git(self) -> 'Config':
|
||||
"""Get git config section (returns self for compatibility)."""
|
||||
return self
|
||||
|
||||
@property
|
||||
def enabled(self) -> bool:
|
||||
"""Check if git is enabled."""
|
||||
return self.get('git.enabled', False)
|
||||
|
||||
@property
|
||||
def auto_push(self) -> bool:
|
||||
"""Check if auto push is enabled."""
|
||||
return self.get('git.auto_push', False)
|
||||
|
||||
@property
|
||||
def commit_message_template(self) -> str:
|
||||
"""Get commit message template."""
|
||||
return self.get('git.commit_message_template', 'Add note: {title}')
|
||||
|
||||
@property
|
||||
def kimi_api_key(self) -> str:
|
||||
"""Get Kimi API key (alias for ai.api_key)."""
|
||||
return self.get('ai.api_key') or ''
|
||||
|
||||
def ensure_dirs(self) -> None:
|
||||
"""Ensure required directories exist."""
|
||||
self.vault_path.mkdir(parents=True, exist_ok=True)
|
||||
(self.vault_path / self.get('storage.inbox_dir', 'Inbox')).mkdir(exist_ok=True)
|
||||
(self.vault_path / self.get('storage.topics_dir', 'Topics')).mkdir(exist_ok=True)
|
||||
|
||||
def validate(self) -> tuple[bool, list[str]]:
|
||||
"""Validate configuration."""
|
||||
errors = []
|
||||
if not self.ai_api_key:
|
||||
errors.append("AI API key not configured")
|
||||
return len(errors) == 0, errors
|
||||
|
||||
# Subscriptions support
|
||||
def get_subscriptions(self) -> list:
|
||||
"""Get subscriptions list."""
|
||||
return self.get('subscriptions', [])
|
||||
|
||||
def add_subscription(self, subscription: Dict[str, Any]) -> None:
|
||||
"""Add a new subscription."""
|
||||
subs = self.get('subscriptions', [])
|
||||
subs.append(subscription)
|
||||
self.set('subscriptions', subs)
|
||||
self.save()
|
||||
@@ -0,0 +1,439 @@
|
||||
"""Obsidian KB - 内容分片器
|
||||
C5.1-C5.2: 实现长视频分段和分段策略
|
||||
"""
|
||||
import re
|
||||
from dataclasses import dataclass
|
||||
from typing import List, Dict, Tuple, Optional
|
||||
|
||||
|
||||
@dataclass
|
||||
class Chunk:
|
||||
"""内容分片数据模型"""
|
||||
index: int
|
||||
start_time: float # seconds
|
||||
end_time: float # seconds
|
||||
text: str
|
||||
|
||||
@property
|
||||
def duration(self) -> float:
|
||||
"""Get chunk duration in seconds."""
|
||||
return self.end_time - self.start_time
|
||||
|
||||
@property
|
||||
def time_range(self) -> str:
|
||||
"""Get formatted time range string."""
|
||||
return f"{self._format_time(self.start_time)}-{self._format_time(self.end_time)}"
|
||||
|
||||
def _format_time(self, seconds: float) -> str:
|
||||
"""Format seconds to MM:SS or HH:MM:SS."""
|
||||
hours = int(seconds // 3600)
|
||||
minutes = int((seconds % 3600) // 60)
|
||||
secs = int(seconds % 60)
|
||||
|
||||
if hours > 0:
|
||||
return f"{hours:02d}:{minutes:02d}:{secs:02d}"
|
||||
return f"{minutes:02d}:{secs:02d}"
|
||||
|
||||
def to_dict(self) -> Dict:
|
||||
"""转换为字典"""
|
||||
return {
|
||||
'index': self.index,
|
||||
'start_time': self.start_time,
|
||||
'end_time': self.end_time,
|
||||
'text_length': len(self.text),
|
||||
'text_preview': self.text[:100] + '...' if len(self.text) > 100 else self.text
|
||||
}
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"Chunk({self.index}, {self.start_time}-{self.end_time}, {len(self.text)}chars)"
|
||||
|
||||
|
||||
class ContentChunker:
|
||||
"""内容分片器 - 支持SRT字幕分片"""
|
||||
|
||||
def __init__(self, chunk_duration_seconds: int = 900, max_chunk_duration: int = 1800):
|
||||
"""
|
||||
Initialize chunker.
|
||||
|
||||
Args:
|
||||
chunk_duration_seconds: Target duration for each chunk (default 15 min)
|
||||
max_chunk_duration: Maximum duration for a chunk (default 30 min)
|
||||
"""
|
||||
self.chunk_duration = chunk_duration_seconds
|
||||
self.max_chunk_duration = max_chunk_duration
|
||||
|
||||
# SRT时间戳正则
|
||||
self.srt_time_pattern = re.compile(
|
||||
r'(\d{2}:\d{2}:\d{2},\d{3})\s*-->\s*(\d{2}:\d{2}:\d{2},\d{3})'
|
||||
)
|
||||
|
||||
def chunk_by_duration(self, srt_content: str, target_duration: Optional[int] = None) -> List[Chunk]:
|
||||
"""
|
||||
Split SRT content into chunks based on duration.
|
||||
|
||||
Args:
|
||||
srt_content: SRT formatted subtitle text
|
||||
target_duration: Target chunk duration in seconds (overrides default)
|
||||
|
||||
Returns:
|
||||
List of Chunk objects
|
||||
"""
|
||||
target = target_duration or self.chunk_duration
|
||||
subtitles = self._parse_srt(srt_content)
|
||||
|
||||
if not subtitles:
|
||||
return []
|
||||
|
||||
chunks = []
|
||||
current_chunk_texts = []
|
||||
current_start = subtitles[0][0]
|
||||
current_end = current_start
|
||||
chunk_index = 0
|
||||
|
||||
for start, end, text in subtitles:
|
||||
# Check if adding this subtitle would exceed target duration
|
||||
if current_chunk_texts and (end - current_start) > target:
|
||||
# Save current chunk
|
||||
chunks.append(Chunk(
|
||||
index=chunk_index,
|
||||
start_time=current_start,
|
||||
end_time=current_end,
|
||||
text=' '.join(current_chunk_texts)
|
||||
))
|
||||
chunk_index += 1
|
||||
|
||||
# Start new chunk
|
||||
current_chunk_texts = [text]
|
||||
current_start = start
|
||||
current_end = end
|
||||
else:
|
||||
current_chunk_texts.append(text)
|
||||
current_end = end
|
||||
|
||||
# Don't forget the last chunk
|
||||
if current_chunk_texts:
|
||||
chunks.append(Chunk(
|
||||
index=chunk_index,
|
||||
start_time=current_start,
|
||||
end_time=current_end,
|
||||
text=' '.join(current_chunk_texts)
|
||||
))
|
||||
|
||||
return chunks
|
||||
|
||||
def _parse_srt(self, srt_content: str) -> List[Tuple[float, float, str]]:
|
||||
"""
|
||||
Parse SRT content into list of (start_time, end_time, text) tuples.
|
||||
|
||||
Args:
|
||||
srt_content: SRT formatted text
|
||||
|
||||
Returns:
|
||||
List of tuples (start_seconds, end_seconds, text)
|
||||
"""
|
||||
import re
|
||||
subtitles = []
|
||||
|
||||
# Split by double newlines to get individual subtitle entries
|
||||
entries = re.split(r'\n\s*\n', srt_content.strip())
|
||||
|
||||
for entry in entries:
|
||||
lines = entry.strip().split('\n')
|
||||
if len(lines) < 2:
|
||||
continue
|
||||
|
||||
# Find the timing line (contains -->)
|
||||
timing_line = None
|
||||
for line in lines:
|
||||
if '-->' in line:
|
||||
timing_line = line
|
||||
break
|
||||
|
||||
if not timing_line:
|
||||
continue
|
||||
|
||||
# Parse timing
|
||||
match = re.match(
|
||||
r'\d*\s*(\d{2}:\d{2}:\d{2},\d{3})\s*--\u003e\s*(\d{2}:\d{2}:\d{2},\d{3})',
|
||||
timing_line.strip()
|
||||
)
|
||||
if not match:
|
||||
continue
|
||||
|
||||
start_time = self._time_to_seconds(match.group(1))
|
||||
end_time = self._time_to_seconds(match.group(2))
|
||||
|
||||
# Get text (lines after timing line)
|
||||
text_lines = []
|
||||
found_timing = False
|
||||
for line in lines:
|
||||
if found_timing:
|
||||
text_lines.append(line.strip())
|
||||
elif '-->' in line:
|
||||
found_timing = True
|
||||
|
||||
text = ' '.join(text_lines).strip()
|
||||
if text:
|
||||
subtitles.append((start_time, end_time, text))
|
||||
|
||||
return subtitles
|
||||
|
||||
def chunk_by_count(self, srt_text: str, num_chunks: int) -> List[Chunk]:
|
||||
"""
|
||||
按指定段数分割SRT字幕
|
||||
|
||||
Args:
|
||||
srt_text: SRT格式字幕文本
|
||||
num_chunks: 目标段数
|
||||
|
||||
Returns:
|
||||
Chunk对象列表
|
||||
"""
|
||||
print(f"[ContentChunker] 按段数分片: 目标段数={num_chunks}")
|
||||
|
||||
# 解析SRT条目
|
||||
entries = self._parse_srt_dict(srt_text)
|
||||
if not entries:
|
||||
return []
|
||||
|
||||
if num_chunks <= 0:
|
||||
num_chunks = 1
|
||||
|
||||
# 计算每段应包含的条目数
|
||||
total_entries = len(entries)
|
||||
entries_per_chunk = max(1, total_entries // num_chunks)
|
||||
|
||||
chunks = []
|
||||
current_chunk_texts = []
|
||||
current_start_time = None
|
||||
chunk_index = 0
|
||||
entry_count = 0
|
||||
|
||||
for i, entry in enumerate(entries):
|
||||
if current_start_time is None:
|
||||
current_start_time = entry['start']
|
||||
|
||||
current_chunk_texts.append(entry['text'])
|
||||
entry_count += 1
|
||||
|
||||
# 检查是否达到每段条目数,或是最后一个条目
|
||||
is_last_entry = (i == total_entries - 1)
|
||||
should_split = (entry_count >= entries_per_chunk and chunk_index < num_chunks - 1)
|
||||
|
||||
if should_split or is_last_entry:
|
||||
chunk = Chunk(
|
||||
index=chunk_index,
|
||||
start_time=self._time_to_seconds(current_start_time),
|
||||
end_time=self._time_to_seconds(entry['end']),
|
||||
text='\n'.join(current_chunk_texts)
|
||||
)
|
||||
chunks.append(chunk)
|
||||
|
||||
current_chunk_texts = []
|
||||
current_start_time = None
|
||||
chunk_index += 1
|
||||
entry_count = 0
|
||||
|
||||
print(f"[ContentChunker] 分片完成: 共{len(chunks)}段")
|
||||
return chunks
|
||||
|
||||
def _parse_srt_dict(self, srt_text: str) -> List[Dict]:
|
||||
"""
|
||||
解析SRT字幕文本,返回字典格式(用于内部使用)
|
||||
|
||||
Returns:
|
||||
条目列表,每个条目包含start, end, text
|
||||
"""
|
||||
entries = []
|
||||
|
||||
# 首先检查是否包含SRT时间戳格式
|
||||
if '-->' not in srt_text:
|
||||
# 非SRT格式,按行数分割模拟
|
||||
lines = srt_text.strip().split('\n')
|
||||
# 模拟每10行一个条目,每个条目30秒
|
||||
for i in range(0, len(lines), 10):
|
||||
chunk_lines = lines[i:i+10]
|
||||
start_sec = i * 3 # 每行约3秒
|
||||
end_sec = (i + 10) * 3
|
||||
entries.append({
|
||||
'start': self._seconds_to_time(start_sec),
|
||||
'end': self._seconds_to_time(end_sec),
|
||||
'text': '\n'.join(chunk_lines)
|
||||
})
|
||||
return entries
|
||||
|
||||
# 按空行分割条目
|
||||
blocks = re.split(r'\n\s*\n', srt_text.strip())
|
||||
|
||||
for block in blocks:
|
||||
lines = block.strip().split('\n')
|
||||
if len(lines) < 2:
|
||||
continue
|
||||
|
||||
# 查找时间戳行(通常是第二行,但可能有变体)
|
||||
time_line = None
|
||||
for line in lines:
|
||||
if '-->' in line:
|
||||
time_line = line
|
||||
break
|
||||
|
||||
if not time_line:
|
||||
continue
|
||||
|
||||
# 解析时间戳
|
||||
match = self.srt_time_pattern.search(time_line)
|
||||
if not match:
|
||||
continue
|
||||
|
||||
start_time = match.group(1)
|
||||
end_time = match.group(2)
|
||||
|
||||
# 提取文本(时间戳行之后的所有行)
|
||||
time_line_idx = lines.index(time_line) if time_line in lines else 1
|
||||
text_lines = lines[time_line_idx + 1:]
|
||||
text = '\n'.join(text_lines).strip()
|
||||
|
||||
if text:
|
||||
entries.append({
|
||||
'start': start_time,
|
||||
'end': end_time,
|
||||
'text': text
|
||||
})
|
||||
|
||||
return entries
|
||||
|
||||
def _time_to_seconds(self, time_str: str) -> float:
|
||||
"""将SRT时间格式转换为秒数"""
|
||||
# 处理 "00:05:23,450" 格式
|
||||
time_str = time_str.replace(',', '.')
|
||||
parts = time_str.split(':')
|
||||
|
||||
if len(parts) == 3:
|
||||
hours = int(parts[0])
|
||||
minutes = int(parts[1])
|
||||
seconds = float(parts[2])
|
||||
return hours * 3600 + minutes * 60 + seconds
|
||||
|
||||
return 0.0
|
||||
|
||||
def _seconds_to_time(self, seconds: int) -> str:
|
||||
"""将秒数转换为SRT时间格式"""
|
||||
hours = seconds // 3600
|
||||
minutes = (seconds % 3600) // 60
|
||||
secs = seconds % 60
|
||||
return f"{hours:02d}:{minutes:02d}:{secs:02d},000"
|
||||
|
||||
def get_chunking_strategy(self, total_duration: float) -> dict:
|
||||
"""
|
||||
Determine chunking strategy based on total content duration.
|
||||
|
||||
Args:
|
||||
total_duration: Total duration in seconds
|
||||
|
||||
Returns:
|
||||
Dictionary with chunking strategy
|
||||
"""
|
||||
if total_duration < 600: # Less than 10 minutes
|
||||
return {
|
||||
'strategy': 'direct',
|
||||
'chunk_count': 1,
|
||||
'description': 'Short content, no chunking needed'
|
||||
}
|
||||
elif total_duration < 1800: # 10-30 minutes
|
||||
return {
|
||||
'strategy': 'truncate',
|
||||
'chunk_count': 1,
|
||||
'description': 'Medium content, single chunk with possible truncation'
|
||||
}
|
||||
elif total_duration < 3600: # 30-60 minutes
|
||||
return {
|
||||
'strategy': 'two_part',
|
||||
'chunk_count': 2,
|
||||
'description': 'Long content, split into 2 chunks'
|
||||
}
|
||||
else: # 60+ minutes
|
||||
chunk_count = int(total_duration / self.chunk_duration) + 1
|
||||
return {
|
||||
'strategy': 'multi_part',
|
||||
'chunk_count': chunk_count,
|
||||
'description': f'Very long content, split into {chunk_count} chunks'
|
||||
}
|
||||
|
||||
|
||||
def get_chunking_strategy(duration_seconds: int) -> Dict:
|
||||
"""
|
||||
C5.2: 根据视频时长获取分段策略
|
||||
|
||||
Args:
|
||||
duration_seconds: 视频时长(秒)
|
||||
|
||||
Returns:
|
||||
分段策略字典
|
||||
"""
|
||||
if duration_seconds < 600:
|
||||
# 少于10分钟:直接处理,无需分片
|
||||
return {
|
||||
'use_chunking': False,
|
||||
'strategy': 'direct',
|
||||
'description': '短视频,直接处理'
|
||||
}
|
||||
|
||||
elif duration_seconds < 1800:
|
||||
# 10-30分钟:截断处理
|
||||
return {
|
||||
'use_chunking': False,
|
||||
'strategy': 'truncate',
|
||||
'max_chars': 8000,
|
||||
'description': '中等视频,截断至8000字符处理'
|
||||
}
|
||||
|
||||
elif duration_seconds < 3600:
|
||||
# 30-60分钟:分为2段
|
||||
return {
|
||||
'use_chunking': True,
|
||||
'num_chunks': 2,
|
||||
'strategy': 'chunk_2',
|
||||
'chunk_duration': duration_seconds // 2,
|
||||
'description': '长视频,分为2段处理'
|
||||
}
|
||||
|
||||
else:
|
||||
# 60分钟以上:每15分钟一段
|
||||
num_chunks = max(4, duration_seconds // 900)
|
||||
return {
|
||||
'use_chunking': True,
|
||||
'chunk_duration': 900,
|
||||
'num_chunks': num_chunks,
|
||||
'strategy': 'chunk_n',
|
||||
'description': f'超长视频,分为{num_chunks}段,每段约15分钟'
|
||||
}
|
||||
|
||||
|
||||
# 便捷函数
|
||||
def chunk_content(srt_text: str, duration_seconds: int) -> List[Chunk]:
|
||||
"""
|
||||
根据视频时长自动选择最佳分片策略
|
||||
|
||||
Args:
|
||||
srt_text: SRT字幕文本
|
||||
duration_seconds: 视频时长(秒)
|
||||
|
||||
Returns:
|
||||
Chunk列表
|
||||
"""
|
||||
strategy = get_chunking_strategy(duration_seconds)
|
||||
chunker = ContentChunker()
|
||||
|
||||
if not strategy['use_chunking']:
|
||||
# 不需要分片,返回单一大段
|
||||
return [Chunk(
|
||||
text=srt_text[:strategy.get('max_chars', len(srt_text))],
|
||||
start_time="00:00:00,000",
|
||||
end_time=chunker._seconds_to_time(duration_seconds),
|
||||
index=0
|
||||
)]
|
||||
|
||||
if 'num_chunks' in strategy and strategy['strategy'] == 'chunk_2':
|
||||
return chunker.chunk_by_count(srt_text, strategy['num_chunks'])
|
||||
else:
|
||||
return chunker.chunk_by_duration(srt_text, strategy.get('chunk_duration', 900))
|
||||
@@ -0,0 +1,218 @@
|
||||
"""Obsidian KB - 深度分析器
|
||||
C4.2: 实现深度分析,根据时长选择不同分析策略
|
||||
"""
|
||||
from typing import Dict, Any, List, Optional
|
||||
from src.models import Content, AnalysisResult, ContentStatus, TimelineEntry
|
||||
from src.config import get_config
|
||||
|
||||
|
||||
class DeepAnalyzer:
|
||||
"""深度内容分析器 - 根据内容时长选择不同策略"""
|
||||
|
||||
# 时长阈值(秒)
|
||||
SHORT_VIDEO_THRESHOLD = 600 # 10分钟
|
||||
LONG_VIDEO_THRESHOLD = 1800 # 30分钟
|
||||
|
||||
def __init__(self, ai_client=None):
|
||||
self.config = get_config()
|
||||
self.ai_client = ai_client
|
||||
|
||||
def analyze(self, content: Content, content_text: str) -> AnalysisResult:
|
||||
"""
|
||||
根据内容时长选择分析策略
|
||||
|
||||
Args:
|
||||
content: Content对象
|
||||
content_text: 内容文本
|
||||
|
||||
Returns:
|
||||
AnalysisResult分析结果
|
||||
"""
|
||||
duration = content.duration or 0
|
||||
content_type = content.content_type
|
||||
|
||||
print(f"[DeepAnalyzer] 开始分析: {content.title or content.id}")
|
||||
print(f"[DeepAnalyzer] 类型: {content_type.value}, 时长: {duration}秒")
|
||||
|
||||
# 根据时长选择策略
|
||||
if duration < self.SHORT_VIDEO_THRESHOLD:
|
||||
# 短视频: 轻量分析
|
||||
print(f"[DeepAnalyzer] 使用轻量分析策略 (时长 < 10分钟)")
|
||||
result = self._analyze_short(content, content_text)
|
||||
else:
|
||||
# 长视频: 详细时间轴分析
|
||||
print(f"[DeepAnalyzer] 使用详细时间轴分析策略 (时长 >= 10分钟)")
|
||||
result = self._analyze_long(content, content_text)
|
||||
|
||||
# 更新Content对象
|
||||
content.timeline = [TimelineEntry(
|
||||
timestamp=t['start_time'],
|
||||
title=t.get('title', ''),
|
||||
description=t.get('summary', '')
|
||||
) for t in result.timeline]
|
||||
content.update_status(ContentStatus.ANALYZED)
|
||||
|
||||
return result
|
||||
|
||||
def _analyze_short(self, content: Content, content_text: str) -> AnalysisResult:
|
||||
"""短视频/短内容轻量分析"""
|
||||
result = AnalysisResult()
|
||||
|
||||
# 截断文本
|
||||
text_sample = self._get_text_sample(content_text, max_chars=5000)
|
||||
|
||||
# 生成简洁分析
|
||||
if self.ai_client:
|
||||
analysis = self._analyze_with_ai_short(text_sample, content)
|
||||
result.main_topics = analysis.get('topics', [])
|
||||
result.key_insights = analysis.get('insights', [])
|
||||
result.action_items = analysis.get('actions', [])
|
||||
result.summary = analysis.get('summary', '')
|
||||
else:
|
||||
# 无AI时的简单分析
|
||||
result.main_topics = ["主题1", "主题2", "主题3"]
|
||||
result.key_insights = ["关键见解1", "关键见解2"]
|
||||
result.action_items = ["建议行动1"]
|
||||
result.summary = content.summary or "无摘要"
|
||||
|
||||
# 短视频的时间轴较简单
|
||||
result.timeline = self._generate_simple_timeline(content)
|
||||
|
||||
return result
|
||||
|
||||
def _analyze_long(self, content: Content, content_text: str) -> AnalysisResult:
|
||||
"""长视频详细时间轴分析"""
|
||||
result = AnalysisResult()
|
||||
|
||||
# 如果有AI客户端且支持分段分析
|
||||
if self.ai_client and hasattr(self.ai_client, '_deep_analyze'):
|
||||
# 使用AI客户端的分段分析能力
|
||||
analysis = self.ai_client._deep_analyze(
|
||||
content_text=content_text,
|
||||
content_type=content.content_type.value,
|
||||
duration=content.duration
|
||||
)
|
||||
result.main_topics = analysis.get('topics', [])
|
||||
result.key_insights = analysis.get('insights', [])
|
||||
result.action_items = analysis.get('actions', [])
|
||||
result.summary = analysis.get('summary', '')
|
||||
result.full_analysis = analysis.get('full_analysis', '')
|
||||
result.timeline = analysis.get('timeline', [])
|
||||
else:
|
||||
# 基础分段分析
|
||||
result = self._analyze_long_basic(content, content_text)
|
||||
|
||||
return result
|
||||
|
||||
def _analyze_long_basic(self, content: Content, content_text: str) -> AnalysisResult:
|
||||
"""基础长视频分析(无AI时)"""
|
||||
result = AnalysisResult()
|
||||
|
||||
# 简单的文本分割分析
|
||||
duration = content.duration or 3600
|
||||
|
||||
# 根据时长决定分割段数
|
||||
if duration < 3600: # 30-60分钟
|
||||
num_segments = 3
|
||||
elif duration < 7200: # 60-120分钟
|
||||
num_segments = 4
|
||||
else: # >120分钟
|
||||
num_segments = 6
|
||||
|
||||
# 生成时间轴
|
||||
segment_duration = duration // num_segments
|
||||
timeline = []
|
||||
|
||||
for i in range(num_segments):
|
||||
start_sec = i * segment_duration
|
||||
end_sec = min((i + 1) * segment_duration, duration)
|
||||
|
||||
timeline.append({
|
||||
'index': i + 1,
|
||||
'start_time': self._format_time(start_sec),
|
||||
'end_time': self._format_time(end_sec),
|
||||
'title': f"第{i+1}部分",
|
||||
'summary': f"时间段 {self._format_time(start_sec)} - {self._format_time(end_sec)} 的内容"
|
||||
})
|
||||
|
||||
result.timeline = timeline
|
||||
result.main_topics = ["主题A", "主题B", "主题C", "主题D"]
|
||||
result.key_insights = ["深入见解1", "深入见解2", "深入见解3"]
|
||||
result.action_items = ["建议行动1", "建议行动2"]
|
||||
result.summary = f"这是一个{self._format_time(duration)}的长视频,分为{num_segments}个主要部分进行分析。"
|
||||
|
||||
return result
|
||||
|
||||
def _analyze_with_ai_short(self, text: str, content: Content) -> Dict[str, Any]:
|
||||
"""使用AI进行短视频分析"""
|
||||
prompt = f"""请分析以下内容,以JSON格式返回:
|
||||
|
||||
{text[:3000]}
|
||||
|
||||
请返回以下格式的JSON:
|
||||
{{
|
||||
"summary": "整体摘要(100字以内)",
|
||||
"topics": ["主题1", "主题2", "主题3"],
|
||||
"insights": ["关键见解1", "关键见解2"],
|
||||
"actions": ["建议行动1"]
|
||||
}}"""
|
||||
|
||||
try:
|
||||
response = self.ai_client.generate_json(prompt)
|
||||
return response
|
||||
except Exception as e:
|
||||
print(f"[DeepAnalyzer] AI分析失败: {e}")
|
||||
return {
|
||||
"summary": content.summary or "分析失败",
|
||||
"topics": ["主题1", "主题2", "主题3"],
|
||||
"insights": ["无法获取深入见解"],
|
||||
"actions": []
|
||||
}
|
||||
|
||||
def _get_text_sample(self, text: str, max_chars: int) -> str:
|
||||
"""获取文本样本"""
|
||||
if len(text) <= max_chars:
|
||||
return text
|
||||
|
||||
# 分段采样
|
||||
part_size = max_chars // 3
|
||||
return (
|
||||
text[:part_size] +
|
||||
"\n\n[...省略中间部分...]\n\n" +
|
||||
text[len(text)//2 - part_size//2:len(text)//2 + part_size//2] +
|
||||
"\n\n[...省略...]\n\n" +
|
||||
text[-part_size:]
|
||||
)
|
||||
|
||||
def _generate_simple_timeline(self, content: Content) -> List[Dict[str, Any]]:
|
||||
"""生成简单时间轴"""
|
||||
duration = content.duration or 600
|
||||
|
||||
# 短视频分为2-3段
|
||||
segments = 2 if duration < 300 else 3
|
||||
segment_duration = duration // segments
|
||||
|
||||
timeline = []
|
||||
for i in range(segments):
|
||||
start = i * segment_duration
|
||||
end = min((i + 1) * segment_duration, duration)
|
||||
timeline.append({
|
||||
'index': i + 1,
|
||||
'start_time': self._format_time(start),
|
||||
'end_time': self._format_time(end),
|
||||
'title': f"段落 {i+1}",
|
||||
'summary': f"从{self._format_time(start)}到{self._format_time(end)}的内容"
|
||||
})
|
||||
|
||||
return timeline
|
||||
|
||||
def _format_time(self, seconds: int) -> str:
|
||||
"""格式化秒数为时间字符串"""
|
||||
hours = seconds // 3600
|
||||
minutes = (seconds % 3600) // 60
|
||||
secs = seconds % 60
|
||||
|
||||
if hours > 0:
|
||||
return f"{hours}:{minutes:02d}:{secs:02d}"
|
||||
else:
|
||||
return f"{minutes}:{secs:02d}"
|
||||
@@ -0,0 +1,195 @@
|
||||
"""Obsidian KB - Git 同步"""
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
from typing import Optional, List
|
||||
from datetime import datetime
|
||||
|
||||
from .config import get_config
|
||||
|
||||
|
||||
class GitSync:
|
||||
"""Git仓库同步管理"""
|
||||
|
||||
def __init__(self, repo_path: Optional[str] = None):
|
||||
config = get_config()
|
||||
self.repo_path = repo_path or config.obsidian_path
|
||||
self.git_url = config.git_repo
|
||||
self.git_token = config.git_token
|
||||
|
||||
def _run_git(self, args: List[str], cwd: Optional[str] = None) -> tuple[int, str, str]:
|
||||
"""运行git命令"""
|
||||
cmd = ['git'] + args
|
||||
try:
|
||||
result = subprocess.run(
|
||||
cmd,
|
||||
cwd=cwd or self.repo_path,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
encoding='utf-8'
|
||||
)
|
||||
return result.returncode, result.stdout, result.stderr
|
||||
except Exception as e:
|
||||
return -1, "", str(e)
|
||||
|
||||
def is_git_repo(self) -> bool:
|
||||
"""检查是否为git仓库"""
|
||||
git_dir = os.path.join(self.repo_path, '.git')
|
||||
return os.path.isdir(git_dir)
|
||||
|
||||
def init_repo(self) -> bool:
|
||||
"""初始化git仓库"""
|
||||
if self.is_git_repo():
|
||||
return True
|
||||
|
||||
os.makedirs(self.repo_path, exist_ok=True)
|
||||
|
||||
# git init
|
||||
code, _, _ = self._run_git(['init'])
|
||||
if code != 0:
|
||||
return False
|
||||
|
||||
# 配置git用户(使用默认值)
|
||||
self._run_git(['config', 'user.email', 'obsidian-kb@localhost'])
|
||||
self._run_git(['config', 'user.name', 'Obsidian KB'])
|
||||
|
||||
# 添加remote
|
||||
if self.git_url:
|
||||
# 如果提供了token,构建带认证的URL
|
||||
if self.git_token:
|
||||
auth_url = self._build_auth_url(self.git_url, self.git_token)
|
||||
self._run_git(['remote', 'add', 'origin', auth_url])
|
||||
else:
|
||||
self._run_git(['remote', 'add', 'origin', self.git_url])
|
||||
|
||||
return True
|
||||
|
||||
def _build_auth_url(self, url: str, token: str) -> str:
|
||||
"""构建带认证的git URL"""
|
||||
if url.startswith('https://github.com/'):
|
||||
return url.replace('https://', f'https://x-access-token:{token}@')
|
||||
elif url.startswith('https://'):
|
||||
return url.replace('https://', f'https://oauth2:{token}@')
|
||||
return url
|
||||
|
||||
def add_files(self, files: List[str]) -> bool:
|
||||
"""添加文件到暂存区"""
|
||||
if not files:
|
||||
return True
|
||||
|
||||
# 添加所有指定文件
|
||||
for file_path in files:
|
||||
# 转换为相对路径
|
||||
rel_path = os.path.relpath(file_path, self.repo_path)
|
||||
code, _, _ = self._run_git(['add', rel_path])
|
||||
if code != 0:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def add_all(self) -> bool:
|
||||
"""添加所有变更"""
|
||||
code, _, _ = self._run_git(['add', '.'])
|
||||
return code == 0
|
||||
|
||||
def has_changes(self) -> bool:
|
||||
"""检查是否有未提交的变更"""
|
||||
code, stdout, _ = self._run_git(['status', '--porcelain'])
|
||||
if code != 0:
|
||||
return False
|
||||
return bool(stdout.strip())
|
||||
|
||||
def commit(self, message: Optional[str] = None, content_title: Optional[str] = None) -> bool:
|
||||
"""提交变更"""
|
||||
if not self.has_changes():
|
||||
return True
|
||||
|
||||
if not message:
|
||||
message = self._generate_commit_message(content_title)
|
||||
|
||||
code, _, _ = self._run_git(['commit', '-m', message])
|
||||
return code == 0
|
||||
|
||||
def _generate_commit_message(self, content_title: Optional[str] = None) -> str:
|
||||
"""生成提交信息"""
|
||||
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M')
|
||||
if content_title:
|
||||
return f"Add: {content_title[:50]}" + ("..." if len(content_title) > 50 else "") + f" [{timestamp}]"
|
||||
return f"Update KB notes [{timestamp}]"
|
||||
|
||||
def push(self, branch: str = 'main') -> bool:
|
||||
"""推送到远程"""
|
||||
# 检查是否有origin
|
||||
code, stdout, _ = self._run_git(['remote'])
|
||||
if code != 0 or 'origin' not in stdout:
|
||||
return False
|
||||
|
||||
# 检查分支是否存在,不存在则创建
|
||||
code, stdout, _ = self._run_git(['branch', '--show-current'])
|
||||
current_branch = stdout.strip()
|
||||
|
||||
if not current_branch:
|
||||
# 创建初始分支
|
||||
self._run_git(['checkout', '-b', branch])
|
||||
|
||||
# 推送
|
||||
code, _, _ = self._run_git(['push', '-u', 'origin', branch])
|
||||
return code == 0
|
||||
|
||||
def pull(self, branch: str = 'main') -> bool:
|
||||
"""从远程拉取"""
|
||||
code, _, _ = self._run_git(['pull', 'origin', branch])
|
||||
return code == 0
|
||||
|
||||
def sync(self, files: Optional[List[str]] = None, content_title: Optional[str] = None) -> tuple[bool, str]:
|
||||
"""完整同步流程"""
|
||||
# 初始化仓库
|
||||
if not self.init_repo():
|
||||
return False, "Failed to initialize git repository"
|
||||
|
||||
# 添加文件
|
||||
if files:
|
||||
if not self.add_files(files):
|
||||
return False, "Failed to add files"
|
||||
else:
|
||||
if not self.add_all():
|
||||
return False, "Failed to add files"
|
||||
|
||||
# 提交
|
||||
if not self.commit(content_title=content_title):
|
||||
if not self.has_changes():
|
||||
return True, "No changes to commit"
|
||||
return False, "Failed to commit"
|
||||
|
||||
# 推送
|
||||
if self.git_url and not self.push():
|
||||
return False, "Failed to push"
|
||||
|
||||
return True, "Sync completed successfully"
|
||||
|
||||
def get_status(self) -> dict:
|
||||
"""获取仓库状态"""
|
||||
status = {
|
||||
'is_repo': self.is_git_repo(),
|
||||
'has_changes': False,
|
||||
'branch': '',
|
||||
'remote': ''
|
||||
}
|
||||
|
||||
if not status['is_repo']:
|
||||
return status
|
||||
|
||||
status['has_changes'] = self.has_changes()
|
||||
|
||||
_, stdout, _ = self._run_git(['branch', '--show-current'])
|
||||
status['branch'] = stdout.strip()
|
||||
|
||||
_, stdout, _ = self._run_git(['remote', 'get-url', 'origin'])
|
||||
url = stdout.strip()
|
||||
# 隐藏token
|
||||
if '@' in url:
|
||||
url = 'https://' + url.split('@', 1)[1]
|
||||
status['remote'] = url
|
||||
|
||||
return status
|
||||
@@ -0,0 +1,228 @@
|
||||
"""Obsidian KB - 轻量扫描器
|
||||
C4.1: 实现轻量扫描,生成一句话摘要、关键词和质量评分
|
||||
"""
|
||||
import re
|
||||
from typing import List, Tuple
|
||||
from src.models import Content, ContentStatus, QualityMetrics
|
||||
from src.config import get_config
|
||||
|
||||
|
||||
class LightScanner:
|
||||
"""轻量内容扫描器 - 快速生成摘要和关键词"""
|
||||
|
||||
def __init__(self, ai_client=None):
|
||||
self.config = get_config()
|
||||
self.ai_client = ai_client
|
||||
|
||||
def scan(self, content: Content, content_text: str) -> Content:
|
||||
"""
|
||||
对内容进行轻量扫描
|
||||
|
||||
Args:
|
||||
content: Content对象
|
||||
content_text: 内容文本(字幕或正文)
|
||||
|
||||
Returns:
|
||||
更新后的Content对象
|
||||
"""
|
||||
# 截断文本避免过长
|
||||
text_sample = self._get_text_sample(content_text)
|
||||
|
||||
# 生成一句话摘要
|
||||
summary = self._generate_summary(text_sample, content.content_type.value)
|
||||
|
||||
# 提取关键词
|
||||
keywords = self._extract_keywords(text_sample)
|
||||
|
||||
# 质量评分
|
||||
quality_score = self._calculate_quality_score(text_sample, content)
|
||||
|
||||
# 更新Content对象
|
||||
content.summary = summary
|
||||
content.key_points = keywords # 使用key_points存储关键词
|
||||
content.quality_metrics.overall_score = quality_score / 10 # 转换为0-1范围
|
||||
content.update_status(ContentStatus.SCANNED)
|
||||
|
||||
return content
|
||||
|
||||
def _get_text_sample(self, text: str, max_chars: int = 3000) -> str:
|
||||
"""获取文本样本,优先取开头、中间和结尾"""
|
||||
if len(text) <= max_chars:
|
||||
return text
|
||||
|
||||
# 分为三部分:开头40%、中间20%、结尾40%
|
||||
start_len = int(max_chars * 0.4)
|
||||
end_len = int(max_chars * 0.4)
|
||||
middle_len = max_chars - start_len - end_len
|
||||
|
||||
start = text[:start_len]
|
||||
middle_start = len(text) // 2 - middle_len // 2
|
||||
middle = text[middle_start:middle_start + middle_len]
|
||||
end = text[-end_len:]
|
||||
|
||||
return f"{start}\n\n[...中间部分...]\n\n{middle}\n\n[...省略...]\n\n{end}"
|
||||
|
||||
def _generate_summary(self, text_sample: str, content_type: str) -> str:
|
||||
"""
|
||||
生成一句话摘要
|
||||
如果有AI客户端则调用AI,否则使用简单启发式方法
|
||||
"""
|
||||
if self.ai_client:
|
||||
return self._generate_summary_with_ai(text_sample, content_type)
|
||||
else:
|
||||
return self._generate_summary_heuristic(text_sample)
|
||||
|
||||
def _generate_summary_with_ai(self, text_sample: str, content_type: str) -> str:
|
||||
"""使用AI生成摘要"""
|
||||
prompt = f"""请为以下{content_type}内容生成一句话摘要(不超过50字):
|
||||
|
||||
{text_sample[:2000]}
|
||||
|
||||
要求:
|
||||
1. 一句话概括核心内容
|
||||
2. 不超过50字
|
||||
3. 突出主要观点和价值
|
||||
|
||||
摘要:"""
|
||||
|
||||
try:
|
||||
response = self.ai_client.quick_generate(prompt, max_tokens=100)
|
||||
# 清理并截断
|
||||
summary = response.strip().replace('\n', ' ')
|
||||
if len(summary) > 60:
|
||||
summary = summary[:57] + "..."
|
||||
return summary
|
||||
except Exception as e:
|
||||
print(f"[LightScanner] AI生成摘要失败: {e}")
|
||||
return self._generate_summary_heuristic(text_sample)
|
||||
|
||||
def _generate_summary_heuristic(self, text: str) -> str:
|
||||
"""启发式生成简单摘要"""
|
||||
# 取前100个字符作为简单摘要
|
||||
sentences = re.split(r'[。!?.!?]', text)
|
||||
for sent in sentences:
|
||||
sent = sent.strip()
|
||||
if len(sent) > 20 and len(sent) < 100:
|
||||
return sent[:50] + "..." if len(sent) > 50 else sent
|
||||
|
||||
# 回退:取前50字
|
||||
text = text.replace('\n', ' ').strip()
|
||||
return text[:47] + "..." if len(text) > 50 else text
|
||||
|
||||
def _extract_keywords(self, text: str) -> List[str]:
|
||||
"""
|
||||
提取5个关键词
|
||||
如果有AI客户端则调用AI,否则使用简单词频分析
|
||||
"""
|
||||
if self.ai_client:
|
||||
return self._extract_keywords_with_ai(text)
|
||||
else:
|
||||
return self._extract_keywords_heuristic(text)
|
||||
|
||||
def _extract_keywords_with_ai(self, text: str) -> List[str]:
|
||||
"""使用AI提取关键词"""
|
||||
prompt = f"""请从以下内容中提取5个最重要的关键词,用逗号分隔:
|
||||
|
||||
{text[:1500]}
|
||||
|
||||
关键词(5个,逗号分隔):"""
|
||||
|
||||
try:
|
||||
response = self.ai_client.quick_generate(prompt, max_tokens=100)
|
||||
# 解析逗号分隔的关键词
|
||||
keywords = [k.strip() for k in response.split(',') if k.strip()]
|
||||
if len(keywords) < 5:
|
||||
keywords = [k.strip() for k in response.split(',') if k.strip()]
|
||||
|
||||
# 确保最多5个,每个不超过10字
|
||||
keywords = [k[:10] for k in keywords[:5]]
|
||||
|
||||
# 如果不足5个,补充通用关键词
|
||||
while len(keywords) < 5:
|
||||
keywords.append(f"关键词{len(keywords)+1}")
|
||||
|
||||
return keywords[:5]
|
||||
except Exception as e:
|
||||
print(f"[LightScanner] AI提取关键词失败: {e}")
|
||||
return self._extract_keywords_heuristic(text)
|
||||
|
||||
def _extract_keywords_heuristic(self, text: str) -> List[str]:
|
||||
"""启发式提取关键词(基于词频)"""
|
||||
# 简单的中文/英文分词和词频统计
|
||||
import re
|
||||
from collections import Counter
|
||||
|
||||
# 提取中文字符串(2-6字)
|
||||
chinese_words = re.findall(r'[\u4e00-\u9fff]{2,6}', text)
|
||||
|
||||
# 提取英文单词
|
||||
english_words = re.findall(r'\b[a-zA-Z]{4,12}\b', text.lower())
|
||||
|
||||
# 停用词过滤
|
||||
stopwords = {'这个', '那个', '什么', '一个', '我们', '你们', '他们', '可以', '进行'}
|
||||
chinese_words = [w for w in chinese_words if w not in stopwords]
|
||||
|
||||
# 统计词频
|
||||
all_words = chinese_words + english_words
|
||||
if not all_words:
|
||||
return ["内容", "分析", "知识", "笔记", "学习"]
|
||||
|
||||
word_freq = Counter(all_words)
|
||||
top_words = [word for word, _ in word_freq.most_common(5)]
|
||||
|
||||
# 确保有5个
|
||||
defaults = ["内容", "分析", "知识", "笔记", "学习"]
|
||||
while len(top_words) < 5:
|
||||
top_words.append(defaults[len(top_words)])
|
||||
|
||||
return top_words[:5]
|
||||
|
||||
def _calculate_quality_score(self, text: str, content: Content) -> float:
|
||||
"""
|
||||
计算内容质量评分(0-10)
|
||||
基于多个维度:文本长度、信息密度、结构完整性
|
||||
"""
|
||||
score = 5.0 # 基础分
|
||||
|
||||
# 1. 文本长度评分 (0-3分)
|
||||
text_len = len(text)
|
||||
if text_len > 10000:
|
||||
score += 3.0
|
||||
elif text_len > 5000:
|
||||
score += 2.5
|
||||
elif text_len > 2000:
|
||||
score += 2.0
|
||||
elif text_len > 1000:
|
||||
score += 1.0
|
||||
elif text_len > 500:
|
||||
score += 0.5
|
||||
|
||||
# 2. 信息密度评分 (0-2分) - 基于句子平均长度
|
||||
sentences = re.split(r'[。!?.!?]', text)
|
||||
sentences = [s for s in sentences if s.strip()]
|
||||
if sentences:
|
||||
avg_sent_len = text_len / len(sentences)
|
||||
if 15 <= avg_sent_len <= 50:
|
||||
score += 2.0
|
||||
elif 10 <= avg_sent_len <= 80:
|
||||
score += 1.0
|
||||
|
||||
# 3. 结构完整性 (0-2分)
|
||||
# 检查是否有章节标题模式
|
||||
if re.search(r'[#\d]+[.、\s]', text):
|
||||
score += 1.0
|
||||
# 检查是否有时间戳(视频字幕)
|
||||
if content.duration and content.duration > 0:
|
||||
score += 1.0
|
||||
|
||||
# 4. 多样性评分 (0-2分) - 基于唯一词比例
|
||||
words = re.findall(r'[\u4e00-\u9fff]', text)
|
||||
if words:
|
||||
unique_ratio = len(set(words)) / len(words)
|
||||
if unique_ratio > 0.5:
|
||||
score += 2.0
|
||||
elif unique_ratio > 0.3:
|
||||
score += 1.0
|
||||
|
||||
# 确保在0-10范围内
|
||||
return round(max(0.0, min(10.0, score)), 1)
|
||||
@@ -0,0 +1,60 @@
|
||||
"""LLM processor for content analysis and summarization."""
|
||||
|
||||
from typing import Any, Dict, List
|
||||
|
||||
|
||||
class LLMProcessor:
|
||||
"""Processes content using LLM for analysis and summarization."""
|
||||
|
||||
def __init__(self, provider: str = 'openai'):
|
||||
"""Initialize LLM processor.
|
||||
|
||||
Args:
|
||||
provider: LLM provider name
|
||||
"""
|
||||
self.provider = provider
|
||||
|
||||
def analyze(self, content: str) -> Dict[str, Any]:
|
||||
"""Analyze content quality and relevance.
|
||||
|
||||
Args:
|
||||
content: Text content to analyze
|
||||
|
||||
Returns:
|
||||
Analysis results with quality score, relevance, and topics
|
||||
"""
|
||||
# Placeholder - implement actual LLM integration
|
||||
return {
|
||||
'quality_score': 0.7,
|
||||
'relevance': 'high',
|
||||
'topics': ['technology', 'tutorial'],
|
||||
'summary': content[:200] + '...' if len(content) > 200 else content
|
||||
}
|
||||
|
||||
def summarize(self, content: str, max_length: int = 500) -> str:
|
||||
"""Generate summary of content.
|
||||
|
||||
Args:
|
||||
content: Text content to summarize
|
||||
max_length: Maximum summary length
|
||||
|
||||
Returns:
|
||||
Summarized text
|
||||
"""
|
||||
# Placeholder - implement actual LLM summarization
|
||||
if len(content) <= max_length:
|
||||
return content
|
||||
return content[:max_length] + "..."
|
||||
|
||||
def generate_tags(self, content: str, title: str = '') -> List[str]:
|
||||
"""Generate tags for content.
|
||||
|
||||
Args:
|
||||
content: Text content
|
||||
title: Content title
|
||||
|
||||
Returns:
|
||||
List of suggested tags
|
||||
"""
|
||||
# Placeholder - implement actual tag generation
|
||||
return ['imported', 'unread']
|
||||
@@ -0,0 +1,209 @@
|
||||
"""Obsidian KB - 知识库内容管理模型"""
|
||||
|
||||
from enum import Enum
|
||||
from dataclasses import dataclass, field, asdict
|
||||
from typing import Optional, List, Dict, Any
|
||||
from datetime import datetime
|
||||
import uuid
|
||||
import json
|
||||
|
||||
|
||||
class ContentType(Enum):
|
||||
"""内容类型"""
|
||||
VIDEO = "video"
|
||||
ARTICLE = "article"
|
||||
|
||||
|
||||
class ContentStatus(Enum):
|
||||
"""内容处理状态"""
|
||||
NEW = "new" # 新发现
|
||||
SCANNED = "scanned" # 已扫描元数据
|
||||
CONFIRMED = "confirmed" # 已确认处理
|
||||
ANALYZED = "analyzed" # 已AI分析
|
||||
GENERATED = "generated" # 已生成笔记
|
||||
SKIPPED = "skipped" # 已跳过
|
||||
|
||||
|
||||
@dataclass
|
||||
class ContentChunk:
|
||||
"""内容块 - 用于分段处理"""
|
||||
id: str = field(default_factory=lambda: str(uuid.uuid4())[:8])
|
||||
content: str = "" # 内容文本
|
||||
start_time: Optional[int] = None # 开始时间(秒)
|
||||
end_time: Optional[int] = None # 结束时间(秒)
|
||||
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
return asdict(self)
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, data: Dict[str, Any]) -> "ContentChunk":
|
||||
return cls(**data)
|
||||
|
||||
|
||||
@dataclass
|
||||
class ContentAnalysis:
|
||||
"""内容分析结果 - 用于模板渲染"""
|
||||
one_liner: str = "" # 一句话摘要
|
||||
summary: str = "" # 详细摘要
|
||||
key_points: List[str] = field(default_factory=list) # 关键要点
|
||||
tags: List[str] = field(default_factory=list) # 标签
|
||||
quality_score: float = 0.0 # 质量评分 0-100
|
||||
|
||||
# 教程视频专用字段
|
||||
prerequisites: List[str] = field(default_factory=list) # 前置知识
|
||||
outline: List[Dict[str, Any]] = field(default_factory=list) # 大纲分段
|
||||
pitfalls: List[str] = field(default_factory=list) # 常见坑点
|
||||
practice_advice: List[str] = field(default_factory=list) # 实践建议
|
||||
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
return asdict(self)
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, data: Dict[str, Any]) -> "ContentAnalysis":
|
||||
return cls(**data)
|
||||
|
||||
def is_high_quality(self, threshold: float = 60.0) -> bool:
|
||||
"""判断是否高质量内容"""
|
||||
return self.quality_score >= threshold
|
||||
|
||||
|
||||
@dataclass
|
||||
class QualityMetrics:
|
||||
"""内容质量指标"""
|
||||
overall_score: float = 0.0 # 综合评分 0-1
|
||||
information_density: float = 0.0 # 信息密度
|
||||
originality: float = 0.0 # 原创性
|
||||
practicality: float = 0.0 # 实用性
|
||||
depth: float = 0.0 # 深度
|
||||
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
return asdict(self)
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, data: Dict[str, Any]) -> "QualityMetrics":
|
||||
return cls(**data)
|
||||
|
||||
|
||||
@dataclass
|
||||
class AnalysisResult:
|
||||
"""AI深度分析结果"""
|
||||
main_topics: List[str] = field(default_factory=list) # 主要主题
|
||||
key_insights: List[str] = field(default_factory=list) # 关键洞察
|
||||
timeline_summary: List[Dict[str, Any]] = field(default_factory=list) # 时间线摘要
|
||||
content_structure: str = "" # 内容结构描述
|
||||
recommended_tags: List[str] = field(default_factory=list) # 推荐标签
|
||||
target_audience: str = "" # 目标受众
|
||||
difficulty_level: str = "intermediate" # 难度级别
|
||||
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
return asdict(self)
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, data: Dict[str, Any]) -> "AnalysisResult":
|
||||
return cls(**data)
|
||||
|
||||
|
||||
@dataclass
|
||||
class TimelineEntry:
|
||||
"""时间线条目"""
|
||||
timestamp: str
|
||||
title: str
|
||||
description: str
|
||||
|
||||
def to_dict(self) -> Dict[str, str]:
|
||||
return asdict(self)
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, data: Dict[str, str]) -> "TimelineEntry":
|
||||
return cls(**data)
|
||||
|
||||
|
||||
@dataclass
|
||||
class Content:
|
||||
"""内容数据模型"""
|
||||
# 基本信息
|
||||
id: str = field(default_factory=lambda: str(uuid.uuid4())[:8])
|
||||
source: str = "" # 来源: bilibili, youtube, zhihu, local
|
||||
source_id: str = "" # 来源唯一ID
|
||||
url: str = ""
|
||||
title: str = ""
|
||||
author: str = ""
|
||||
description: str = ""
|
||||
duration: Optional[int] = None # 视频时长(秒)
|
||||
|
||||
# 内容类型与状态
|
||||
content_type: ContentType = ContentType.VIDEO
|
||||
status: ContentStatus = ContentStatus.NEW
|
||||
|
||||
# 元数据
|
||||
created_at: str = field(default_factory=lambda: datetime.now().isoformat())
|
||||
processed_at: Optional[str] = None
|
||||
|
||||
# 原始内容
|
||||
transcript: str = "" # 字幕/原文
|
||||
|
||||
# AI分析结果
|
||||
summary: str = "" # 摘要
|
||||
one_liner: str = "" # 一句话摘要
|
||||
key_points: List[str] = field(default_factory=list) # 关键要点
|
||||
tags: List[str] = field(default_factory=list) # 标签
|
||||
quality_score: float = 0.0 # 质量评分
|
||||
analysis: Optional[ContentAnalysis] = None # 详细分析结果
|
||||
outline: List[str] = field(default_factory=list) # 大纲
|
||||
timeline: List[TimelineEntry] = field(default_factory=list) # 时间线
|
||||
quality_metrics: QualityMetrics = field(default_factory=QualityMetrics)
|
||||
|
||||
# 分类与标签
|
||||
category: str = "" # 主题分类
|
||||
|
||||
# 输出
|
||||
output_path: Optional[str] = None # 生成的笔记路径
|
||||
note_path: Optional[str] = None # 别名,用于兼容
|
||||
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
"""序列化为字典"""
|
||||
data = asdict(self)
|
||||
data['content_type'] = self.content_type.value
|
||||
data['status'] = self.status.value
|
||||
data['quality_metrics'] = self.quality_metrics.to_dict()
|
||||
data['timeline'] = [e.to_dict() for e in self.timeline]
|
||||
return data
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, data: Dict[str, Any]) -> "Content":
|
||||
"""从字典反序列化"""
|
||||
# 处理枚举
|
||||
data['content_type'] = ContentType(data.get('content_type', 'video'))
|
||||
data['status'] = ContentStatus(data.get('status', 'new'))
|
||||
|
||||
# 处理嵌套对象
|
||||
if 'quality_metrics' in data and isinstance(data['quality_metrics'], dict):
|
||||
data['quality_metrics'] = QualityMetrics.from_dict(data['quality_metrics'])
|
||||
else:
|
||||
data['quality_metrics'] = QualityMetrics()
|
||||
|
||||
if 'timeline' in data and isinstance(data['timeline'], list):
|
||||
data['timeline'] = [TimelineEntry.from_dict(e) for e in data['timeline']]
|
||||
else:
|
||||
data['timeline'] = []
|
||||
|
||||
return cls(**{k: v for k, v in data.items() if k in [f.name for f in cls.__dataclass_fields__.values()]})
|
||||
|
||||
def to_json(self) -> str:
|
||||
"""序列化为JSON字符串"""
|
||||
return json.dumps(self.to_dict(), ensure_ascii=False, indent=2)
|
||||
|
||||
@classmethod
|
||||
def from_json(cls, json_str: str) -> "Content":
|
||||
"""从JSON字符串反序列化"""
|
||||
return cls.from_dict(json.loads(json_str))
|
||||
|
||||
def is_high_quality(self, threshold: float = 0.7) -> bool:
|
||||
"""判断是否高质量内容"""
|
||||
return self.quality_metrics.overall_score >= threshold
|
||||
|
||||
def update_status(self, status: ContentStatus):
|
||||
"""更新状态"""
|
||||
self.status = status
|
||||
if status == ContentStatus.GENERATED:
|
||||
self.processed_at = datetime.now().isoformat()
|
||||
@@ -0,0 +1,273 @@
|
||||
"""Obsidian KB - 内容处理管道
|
||||
C4.3: 实现完整处理流程
|
||||
"""
|
||||
import os
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
from src.models import Content, ContentStatus, AnalysisResult
|
||||
from src.light_scanner import LightScanner
|
||||
from src.deep_analyzer import DeepAnalyzer
|
||||
from src.config import get_config
|
||||
|
||||
|
||||
class ProcessingPipeline:
|
||||
"""内容处理管道 - 协调轻量扫描、深度分析、笔记生成和同步"""
|
||||
|
||||
def __init__(self, ai_client=None, git_client=None):
|
||||
self.config = get_config()
|
||||
self.ai_client = ai_client
|
||||
self.git_client = git_client
|
||||
|
||||
# 初始化处理器
|
||||
self.light_scanner = LightScanner(ai_client=ai_client)
|
||||
self.deep_analyzer = DeepAnalyzer(ai_client=ai_client)
|
||||
|
||||
def light_scan(self, content: Content, content_text: str) -> Content:
|
||||
"""
|
||||
轻量扫描阶段
|
||||
|
||||
Args:
|
||||
content: Content对象
|
||||
content_text: 内容文本
|
||||
|
||||
Returns:
|
||||
更新后的Content对象
|
||||
"""
|
||||
print(f"[Pipeline] 开始轻量扫描: {content.id}")
|
||||
|
||||
try:
|
||||
content = self.light_scanner.scan(content, content_text)
|
||||
print(f"[Pipeline] 轻量扫描完成: 摘要长度={len(content.summary)}, 关键词={content.key_points}, 质量分={content.quality_metrics.overall_score:.2f}")
|
||||
return content
|
||||
except Exception as e:
|
||||
print(f"[Pipeline] 轻量扫描失败: {e}")
|
||||
raise
|
||||
|
||||
def deep_analyze(self, content: Content, content_text: str) -> Content:
|
||||
"""
|
||||
深度分析阶段
|
||||
|
||||
Args:
|
||||
content: Content对象
|
||||
content_text: 内容文本
|
||||
|
||||
Returns:
|
||||
更新后的Content对象
|
||||
"""
|
||||
print(f"[Pipeline] 开始深度分析: {content.id}")
|
||||
|
||||
try:
|
||||
analysis_result = self.deep_analyzer.analyze(content, content_text)
|
||||
print(f"[Pipeline] 深度分析完成: 主题数={len(analysis_result.main_topics)}, 时间轴段数={len(analysis_result.timeline)}")
|
||||
return content
|
||||
except Exception as e:
|
||||
print(f"[Pipeline] 深度分析失败: {e}")
|
||||
content.update_status(ContentStatus.ERROR)
|
||||
raise
|
||||
|
||||
def process_full(self, content: Content, content_text: str) -> Content:
|
||||
"""
|
||||
完整处理流程:
|
||||
轻量扫描 → 入库 → 深度分析 → 生成笔记 → Git同步
|
||||
|
||||
Args:
|
||||
content: Content对象
|
||||
content_text: 内容文本
|
||||
|
||||
Returns:
|
||||
完全处理后的Content对象
|
||||
"""
|
||||
print(f"\n{'='*60}")
|
||||
print(f"[Pipeline] 开始完整处理流程: {content.id}")
|
||||
print(f"{'='*60}\n")
|
||||
|
||||
try:
|
||||
# Step 1: 轻量扫描
|
||||
print("\n📋 Step 1: 轻量扫描")
|
||||
content = self.light_scan(content, content_text)
|
||||
|
||||
# Step 2: 入库(保存到数据库/文件系统)
|
||||
print("\n💾 Step 2: 保存内容")
|
||||
self._save_content(content)
|
||||
|
||||
# Step 3: 深度分析
|
||||
print("\n🔍 Step 3: 深度分析")
|
||||
content = self.deep_analyze(content, content_text)
|
||||
|
||||
# Step 4: 生成笔记
|
||||
print("\n📝 Step 4: 生成Obsidian笔记")
|
||||
content = self._generate_note(content)
|
||||
|
||||
# Step 5: Git同步
|
||||
if self.config.git.enabled:
|
||||
print("\n🔄 Step 5: Git同步")
|
||||
self._sync_to_git(content)
|
||||
else:
|
||||
print("\n⏭️ Step 5: Git同步已禁用,跳过")
|
||||
|
||||
content.update_status(ContentStatus.GENERATED)
|
||||
|
||||
print(f"\n{'='*60}")
|
||||
print(f"[Pipeline] 处理完成: {content.id}")
|
||||
print(f"{'='*60}\n")
|
||||
|
||||
return content
|
||||
|
||||
except Exception as e:
|
||||
print(f"\n❌ [Pipeline] 处理流程失败: {e}")
|
||||
raise
|
||||
|
||||
def _save_content(self, content: Content) -> None:
|
||||
"""保存内容到存储"""
|
||||
# 创建存储目录
|
||||
storage_dir = os.path.expanduser("~/.obsidian-kb/storage")
|
||||
os.makedirs(storage_dir, exist_ok=True)
|
||||
|
||||
# 保存JSON文件
|
||||
import json
|
||||
file_path = os.path.join(storage_dir, f"{content.id}.json")
|
||||
|
||||
with open(file_path, 'w', encoding='utf-8') as f:
|
||||
json.dump(content.to_dict(), f, ensure_ascii=False, indent=2)
|
||||
|
||||
print(f"[Pipeline] 内容已保存: {file_path}")
|
||||
|
||||
def _generate_note(self, content: Content) -> Content:
|
||||
"""生成Obsidian笔记"""
|
||||
# 构建笔记内容
|
||||
note_content = self._build_note_content(content)
|
||||
|
||||
# 确定笔记路径
|
||||
vault_path = os.path.expanduser(self.config.obsidian.vault_path)
|
||||
notes_folder = self.config.obsidian.notes_folder
|
||||
|
||||
note_dir = os.path.join(vault_path, notes_folder)
|
||||
os.makedirs(note_dir, exist_ok=True)
|
||||
|
||||
# 生成文件名
|
||||
safe_title = self._sanitize_filename(content.title or content.id)
|
||||
timestamp = datetime.now().strftime("%Y%m%d")
|
||||
note_filename = f"{timestamp}_{safe_title}.md"
|
||||
note_path = os.path.join(note_dir, note_filename)
|
||||
|
||||
# 写入文件
|
||||
with open(note_path, 'w', encoding='utf-8') as f:
|
||||
f.write(note_content)
|
||||
|
||||
content.output_path = note_path
|
||||
content.update_status(ContentStatus.GENERATED)
|
||||
|
||||
print(f"[Pipeline] 笔记已生成: {note_path}")
|
||||
return content
|
||||
|
||||
def _build_note_content(self, content: Content) -> str:
|
||||
"""构建Obsidian笔记内容"""
|
||||
lines = [
|
||||
f"# {content.title or '未命名内容'}",
|
||||
"",
|
||||
"## 📊 元数据",
|
||||
f"- **来源**: {content.source}",
|
||||
f"- **URL**: {content.url}",
|
||||
f"- **作者**: {content.author or '未知'}",
|
||||
f"- **类型**: {content.content_type.value}",
|
||||
]
|
||||
|
||||
if content.duration:
|
||||
duration_str = self._format_duration(content.duration)
|
||||
lines.append(f"- **时长**: {duration_str}")
|
||||
|
||||
lines.extend([
|
||||
f"- **质量评分**: {content.quality_metrics.overall_score:.1f}/1.0",
|
||||
f"- **处理时间**: {datetime.now().strftime('%Y-%m-%d %H:%M')}",
|
||||
"",
|
||||
"## 📝 摘要",
|
||||
content.summary or "暂无摘要",
|
||||
"",
|
||||
"## 🏷️ 关键词",
|
||||
])
|
||||
|
||||
# 添加关键词标签
|
||||
if content.key_points:
|
||||
keyword_tags = ' '.join([f"#{kw.replace(' ', '_')}" for kw in content.key_points[:5]])
|
||||
lines.append(keyword_tags)
|
||||
lines.append('')
|
||||
|
||||
# 添加时间轴
|
||||
if content.timeline:
|
||||
lines.extend([
|
||||
"",
|
||||
"## ⏱️ 时间轴",
|
||||
"",
|
||||
])
|
||||
|
||||
for item in content.timeline:
|
||||
lines.append(f"### {item.timestamp} - {item.title}")
|
||||
lines.append(item.description)
|
||||
lines.append('')
|
||||
|
||||
# 添加原始内容(折叠)
|
||||
lines.extend([
|
||||
"",
|
||||
"## 📄 原始内容",
|
||||
"",
|
||||
"```",
|
||||
content.transcript[:2000] + "..." if len(content.transcript) > 2000 else content.transcript,
|
||||
"```",
|
||||
"",
|
||||
"---",
|
||||
f"*由 Obsidian KB 自动生成 - {datetime.now().strftime('%Y-%m-%d %H:%M')}*"
|
||||
])
|
||||
|
||||
return '\n'.join(lines)
|
||||
|
||||
def _sync_to_git(self, content: Content) -> None:
|
||||
"""同步到Git仓库"""
|
||||
if not self.git_client:
|
||||
print("[Pipeline] Git客户端未配置,跳过同步")
|
||||
return
|
||||
|
||||
try:
|
||||
vault_path = os.path.expanduser(self.config.obsidian.vault_path)
|
||||
|
||||
# 添加文件
|
||||
relative_path = os.path.relpath(content.output_path, vault_path)
|
||||
self.git_client.add(relative_path)
|
||||
|
||||
# 提交
|
||||
commit_msg = self.config.git.commit_message_template.format(
|
||||
title=content.title or content.id
|
||||
)
|
||||
self.git_client.commit(commit_msg)
|
||||
|
||||
# 推送(如果启用)
|
||||
if self.config.git.auto_push:
|
||||
self.git_client.push()
|
||||
|
||||
print(f"[Pipeline] Git同步完成")
|
||||
|
||||
except Exception as e:
|
||||
print(f"[Pipeline] Git同步失败: {e}")
|
||||
# Git同步失败不阻断主流程
|
||||
|
||||
def _sanitize_filename(self, filename: str) -> str:
|
||||
"""清理文件名"""
|
||||
import re
|
||||
# 替换非法字符
|
||||
filename = re.sub(r'[<>:"/\\|?*]', '_', filename)
|
||||
# 限制长度
|
||||
if len(filename) > 50:
|
||||
filename = filename[:50]
|
||||
return filename.strip()
|
||||
|
||||
def _format_duration(self, seconds: int) -> str:
|
||||
"""格式化时长"""
|
||||
hours = seconds // 3600
|
||||
minutes = (seconds % 3600) // 60
|
||||
secs = seconds % 60
|
||||
|
||||
if hours > 0:
|
||||
return f"{hours}小时{minutes}分钟"
|
||||
elif minutes > 0:
|
||||
return f"{minutes}分钟{secs}秒"
|
||||
else:
|
||||
return f"{secs}秒"
|
||||
@@ -0,0 +1,31 @@
|
||||
"""
|
||||
Sources 模块初始化
|
||||
"""
|
||||
from .base import SourceAdapter
|
||||
from .bilibili import BilibiliAdapter
|
||||
|
||||
|
||||
class SourceRegistry:
|
||||
"""源适配器注册表"""
|
||||
_adapters = {}
|
||||
|
||||
@classmethod
|
||||
def register(cls, name: str, adapter_class: type):
|
||||
"""注册适配器类"""
|
||||
cls._adapters[name] = adapter_class
|
||||
|
||||
@classmethod
|
||||
def get(cls, name: str) -> SourceAdapter:
|
||||
"""获取适配器类"""
|
||||
return cls._adapters.get(name)
|
||||
|
||||
@classmethod
|
||||
def list(cls) -> list:
|
||||
"""列出所有已注册的适配器名称"""
|
||||
return list(cls._adapters.keys())
|
||||
|
||||
|
||||
# 自动注册适配器
|
||||
SourceRegistry.register('bilibili', BilibiliAdapter)
|
||||
|
||||
__all__ = ['SourceAdapter', 'BilibiliAdapter', 'SourceRegistry']
|
||||
@@ -0,0 +1,54 @@
|
||||
"""
|
||||
Source Adapter 抽象基类
|
||||
"""
|
||||
from abc import ABC, abstractmethod
|
||||
from datetime import datetime
|
||||
from typing import List, Optional
|
||||
|
||||
from models import Content
|
||||
|
||||
|
||||
class SourceAdapter(ABC):
|
||||
"""内容源适配器基类"""
|
||||
|
||||
@abstractmethod
|
||||
def fetch_new(self, since: datetime, **kwargs) -> List[Content]:
|
||||
"""
|
||||
获取新内容
|
||||
|
||||
Args:
|
||||
since: 起始时间,只获取该时间之后的内容
|
||||
**kwargs: 额外的参数
|
||||
|
||||
Returns:
|
||||
List[Content]: 新内容列表
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def extract_content(self, url: str, **kwargs) -> Optional[Content]:
|
||||
"""
|
||||
从URL提取内容
|
||||
|
||||
Args:
|
||||
url: 内容URL
|
||||
**kwargs: 额外的参数
|
||||
|
||||
Returns:
|
||||
Optional[Content]: 内容对象,如果提取失败返回None
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def get_transcript(self, content: Content, **kwargs) -> Optional[str]:
|
||||
"""
|
||||
获取字幕/文本
|
||||
|
||||
Args:
|
||||
content: 内容对象
|
||||
**kwargs: 额外的参数
|
||||
|
||||
Returns:
|
||||
Optional[str]: 字幕文本(SRT格式),如果没有字幕返回None
|
||||
"""
|
||||
pass
|
||||
@@ -0,0 +1,466 @@
|
||||
"""
|
||||
Bilibili 视频源适配器
|
||||
"""
|
||||
import re
|
||||
import json
|
||||
import time
|
||||
import hashlib
|
||||
import urllib.parse
|
||||
from datetime import datetime
|
||||
from typing import List, Optional, Dict, Any
|
||||
import urllib.request
|
||||
from urllib.request import Request, urlopen
|
||||
|
||||
from .base import SourceAdapter
|
||||
from models import Content, ContentType, ContentStatus
|
||||
|
||||
|
||||
class BilibiliAdapter(SourceAdapter):
|
||||
"""B站视频源适配器"""
|
||||
|
||||
BASE_URL = "https://api.bilibili.com"
|
||||
|
||||
# WBI 混淆密钥表
|
||||
MIXIN_KEY_ENC_TABLE = [
|
||||
46, 47, 18, 2, 53, 8, 23, 32, 15, 50, 10, 31, 58, 3, 45, 35,
|
||||
27, 43, 5, 49, 33, 9, 42, 19, 29, 28, 14, 39, 12, 38, 41, 13,
|
||||
37, 48, 7, 16, 24, 55, 40, 61, 26, 17, 0, 1, 60, 51, 30, 4,
|
||||
22, 25, 54, 21, 56, 59, 6, 63, 57, 62, 11, 36, 20, 34, 44, 52
|
||||
]
|
||||
|
||||
def __init__(self, sessdata: Optional[str] = None):
|
||||
"""
|
||||
初始化适配器
|
||||
|
||||
Args:
|
||||
sessdata: B站登录凭证(可选,用于获取私人收藏夹)
|
||||
"""
|
||||
self.sessdata = sessdata
|
||||
self._img_key = None
|
||||
self._sub_key = None
|
||||
|
||||
def _get_headers(self) -> Dict[str, str]:
|
||||
"""获取请求头"""
|
||||
headers = {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
|
||||
'Referer': 'https://www.bilibili.com',
|
||||
'Accept': 'application/json, text/plain, */*',
|
||||
}
|
||||
if self.sessdata:
|
||||
headers['Cookie'] = f'SESSDATA={self.sessdata}'
|
||||
return headers
|
||||
|
||||
def _get_wbi_keys(self) -> tuple:
|
||||
"""获取WBI密钥"""
|
||||
if self._img_key and self._sub_key:
|
||||
return self._img_key, self._sub_key
|
||||
|
||||
url = f"{self.BASE_URL}/x/web-interface/nav"
|
||||
try:
|
||||
req = Request(url, headers=self._get_headers())
|
||||
with urlopen(req, timeout=30) as response:
|
||||
data = json.loads(response.read().decode('utf-8'))
|
||||
if data.get('code') == 0:
|
||||
img_url = data['data']['wbi_img']['img_url']
|
||||
sub_url = data['data']['wbi_img']['sub_url']
|
||||
|
||||
# 提取密钥
|
||||
img_key = img_url.split('/')[-1].split('.')[0]
|
||||
sub_key = sub_url.split('/')[-1].split('.')[0]
|
||||
|
||||
self._img_key = img_key
|
||||
self._sub_key = sub_key
|
||||
return img_key, sub_key
|
||||
except Exception as e:
|
||||
print(f"获取WBI密钥失败: {e}")
|
||||
|
||||
return None, None
|
||||
|
||||
def _get_mixin_key(self, img_key: str, sub_key: str) -> str:
|
||||
"""生成Mixin密钥"""
|
||||
raw_key = img_key + sub_key
|
||||
mixin_key = ''.join([raw_key[i] for i in self.MIXIN_KEY_ENC_TABLE])[:32]
|
||||
return mixin_key
|
||||
|
||||
def _encode_wbi(self, params: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""WBI签名参数"""
|
||||
img_key, sub_key = self._get_wbi_keys()
|
||||
if not img_key or not sub_key:
|
||||
return params
|
||||
|
||||
# 添加时间戳
|
||||
params['wts'] = int(time.time())
|
||||
|
||||
# 按字典序排序参数
|
||||
sorted_params = dict(sorted(params.items()))
|
||||
|
||||
# 构建查询字符串
|
||||
query = urllib.parse.urlencode(sorted_params)
|
||||
|
||||
# 生成Mixin密钥并计算签名
|
||||
mixin_key = self._get_mixin_key(img_key, sub_key)
|
||||
sign = hashlib.md5((query + mixin_key).encode()).hexdigest()
|
||||
|
||||
sorted_params['w_rid'] = sign
|
||||
return sorted_params
|
||||
|
||||
def _fetch_json(self, url: str, params: Optional[Dict] = None, use_wbi: bool = False) -> Optional[Dict]:
|
||||
"""
|
||||
发送HTTP GET请求并返回JSON数据
|
||||
|
||||
Args:
|
||||
url: 请求URL
|
||||
params: 查询参数
|
||||
use_wbi: 是否使用WBI签名
|
||||
|
||||
Returns:
|
||||
Optional[Dict]: JSON数据或None
|
||||
"""
|
||||
try:
|
||||
if params:
|
||||
if use_wbi:
|
||||
params = self._encode_wbi(params)
|
||||
query = urllib.parse.urlencode(params)
|
||||
url = f"{url}?{query}"
|
||||
|
||||
req = Request(url, headers=self._get_headers())
|
||||
with urlopen(req, timeout=30) as response:
|
||||
return json.loads(response.read().decode('utf-8'))
|
||||
except Exception as e:
|
||||
print(f"请求失败 {url}: {e}")
|
||||
return None
|
||||
|
||||
def _extract_bvid_from_url(self, url: str) -> Optional[str]:
|
||||
"""
|
||||
从URL提取BV号
|
||||
|
||||
Args:
|
||||
url: B站视频URL
|
||||
|
||||
Returns:
|
||||
Optional[str]: BV号或None
|
||||
"""
|
||||
patterns = [
|
||||
r'/video/(BV[\w]+)',
|
||||
r'bvid=([\w]+)',
|
||||
r'(BV[\w]+)',
|
||||
]
|
||||
|
||||
for pattern in patterns:
|
||||
match = re.search(pattern, url, re.IGNORECASE)
|
||||
if match:
|
||||
bvid = match.group(1)
|
||||
if bvid.upper().startswith('BV'):
|
||||
return bvid
|
||||
return None
|
||||
|
||||
def _timestamp_to_datetime(self, timestamp: int) -> Optional[datetime]:
|
||||
"""时间戳转datetime"""
|
||||
try:
|
||||
if timestamp > 10000000000: # 毫秒时间戳
|
||||
timestamp = timestamp // 1000
|
||||
return datetime.fromtimestamp(timestamp)
|
||||
except:
|
||||
return None
|
||||
|
||||
def fetch_new(self, since: datetime, **kwargs) -> List[Content]:
|
||||
"""
|
||||
获取新内容
|
||||
|
||||
Args:
|
||||
since: 起始时间
|
||||
**kwargs: 包含 media_id(收藏夹ID) 或 mid(UP主ID)
|
||||
|
||||
Returns:
|
||||
List[Content]: 新内容列表
|
||||
"""
|
||||
contents = []
|
||||
|
||||
# 获取收藏夹视频
|
||||
media_id = kwargs.get('media_id')
|
||||
if media_id:
|
||||
fav_contents = self._fetch_favorites(media_id, since)
|
||||
contents.extend(fav_contents)
|
||||
|
||||
# 获取UP主视频
|
||||
mid = kwargs.get('mid')
|
||||
if mid:
|
||||
up_contents = self._fetch_up_videos(mid, since)
|
||||
contents.extend(up_contents)
|
||||
|
||||
return contents
|
||||
|
||||
def _fetch_favorites(self, media_id: int, since: datetime) -> List[Content]:
|
||||
"""
|
||||
获取收藏夹视频列表
|
||||
|
||||
Args:
|
||||
media_id: 收藏夹ID
|
||||
since: 起始时间
|
||||
|
||||
Returns:
|
||||
List[Content]: 视频列表
|
||||
"""
|
||||
contents = []
|
||||
page = 1
|
||||
|
||||
while True:
|
||||
url = f"{self.BASE_URL}/x/v3/fav/resource/list"
|
||||
params = {
|
||||
'media_id': media_id,
|
||||
'pn': page,
|
||||
'ps': 20,
|
||||
'platform': 'web',
|
||||
}
|
||||
|
||||
data = self._fetch_json(url, params)
|
||||
if not data or data.get('code') != 0:
|
||||
break
|
||||
|
||||
medias = data.get('data', {}).get('medias', []) or []
|
||||
if not medias:
|
||||
break
|
||||
|
||||
for media in medias:
|
||||
content = Content(
|
||||
source="bilibili",
|
||||
source_id=media.get('bvid', ''),
|
||||
url=f"https://www.bilibili.com/video/{media.get('bvid', '')}",
|
||||
title=media.get('title', ''),
|
||||
content_type=ContentType.VIDEO,
|
||||
author=media.get('upper', {}).get('name', ''),
|
||||
duration=media.get('duration', 0),
|
||||
)
|
||||
contents.append(content)
|
||||
|
||||
# 检查是否还有更多
|
||||
info = data.get('data', {}).get('info', {})
|
||||
total = info.get('media_count', 0)
|
||||
if page * 20 >= total:
|
||||
break
|
||||
|
||||
page += 1
|
||||
|
||||
return contents
|
||||
|
||||
def _fetch_up_videos(self, mid: int, since: datetime) -> List[Content]:
|
||||
"""
|
||||
获取UP主视频列表
|
||||
|
||||
Args:
|
||||
mid: UP主ID
|
||||
since: 起始时间
|
||||
|
||||
Returns:
|
||||
List[Content]: 视频列表
|
||||
"""
|
||||
contents = []
|
||||
page = 1
|
||||
|
||||
while True:
|
||||
url = f"{self.BASE_URL}/x/space/wbi/arc/search"
|
||||
params = {
|
||||
'mid': mid,
|
||||
'pn': page,
|
||||
'ps': 30,
|
||||
}
|
||||
|
||||
data = self._fetch_json(url, params, use_wbi=True)
|
||||
if not data or data.get('code') != 0:
|
||||
break
|
||||
|
||||
vlist = data.get('data', {}).get('list', {}).get('vlist', [])
|
||||
if not vlist:
|
||||
break
|
||||
|
||||
for video in vlist:
|
||||
# 检查时间
|
||||
created = video.get('created', 0)
|
||||
pub_datetime = self._timestamp_to_datetime(created)
|
||||
|
||||
if pub_datetime and pub_datetime < since:
|
||||
# 已经处理到旧视频,停止
|
||||
return contents
|
||||
|
||||
content = Content(
|
||||
source="bilibili",
|
||||
source_id=video.get('bvid', ''),
|
||||
url=f"https://www.bilibili.com/video/{video.get('bvid', '')}",
|
||||
title=video.get('title', ''),
|
||||
content_type=ContentType.VIDEO,
|
||||
author=video.get('author', ''),
|
||||
duration=self._parse_duration(video.get('length', '0:00')),
|
||||
)
|
||||
contents.append(content)
|
||||
|
||||
page += 1
|
||||
# 限制最多获取10页
|
||||
if page > 10:
|
||||
break
|
||||
|
||||
return contents
|
||||
|
||||
def _parse_duration(self, duration_str: str) -> int:
|
||||
"""解析时长字符串为秒数"""
|
||||
try:
|
||||
parts = duration_str.split(':')
|
||||
if len(parts) == 2: # MM:SS
|
||||
return int(parts[0]) * 60 + int(parts[1])
|
||||
elif len(parts) == 3: # HH:MM:SS
|
||||
return int(parts[0]) * 3600 + int(parts[1]) * 60 + int(parts[2])
|
||||
except:
|
||||
pass
|
||||
return 0
|
||||
|
||||
def extract_content(self, url: str, **kwargs) -> Optional[Content]:
|
||||
"""
|
||||
从URL提取内容
|
||||
|
||||
Args:
|
||||
url: B站视频URL
|
||||
**kwargs: 额外参数
|
||||
|
||||
Returns:
|
||||
Optional[Content]: 内容对象
|
||||
"""
|
||||
bvid = self._extract_bvid_from_url(url)
|
||||
if not bvid:
|
||||
print(f"无法从URL提取BV号: {url}")
|
||||
return None
|
||||
|
||||
api_url = f"{self.BASE_URL}/x/web-interface/view"
|
||||
params = {'bvid': bvid}
|
||||
|
||||
data = self._fetch_json(api_url, params)
|
||||
if not data or data.get('code') != 0:
|
||||
print(f"获取视频详情失败: {data}")
|
||||
return None
|
||||
|
||||
video_data = data.get('data', {})
|
||||
|
||||
owner = video_data.get('owner', {})
|
||||
|
||||
content = Content(
|
||||
source="bilibili",
|
||||
source_id=bvid,
|
||||
url=url,
|
||||
title=video_data.get('title', ''),
|
||||
content_type=ContentType.VIDEO,
|
||||
author=owner.get('name', ''),
|
||||
description=video_data.get('desc', ''),
|
||||
duration=video_data.get('duration', 0),
|
||||
status=ContentStatus.SCANNED,
|
||||
)
|
||||
|
||||
return content
|
||||
|
||||
def get_transcript(self, content: Content, **kwargs) -> Optional[str]:
|
||||
"""
|
||||
获取字幕
|
||||
|
||||
Args:
|
||||
content: 内容对象
|
||||
**kwargs: 额外参数
|
||||
|
||||
Returns:
|
||||
Optional[str]: 字幕文本(SRT格式)
|
||||
"""
|
||||
bv_id = content.source_id
|
||||
if not bv_id:
|
||||
bv_id = content.id
|
||||
|
||||
# 先获取cid
|
||||
api_url = f"{self.BASE_URL}/x/web-interface/view"
|
||||
data = self._fetch_json(api_url, {'bvid': bv_id})
|
||||
if not data or data.get('code') != 0:
|
||||
return None
|
||||
|
||||
cid = data['data'].get('cid')
|
||||
if not cid:
|
||||
return None
|
||||
|
||||
# 获取字幕列表
|
||||
subtitle_url = f"{self.BASE_URL}/x/player/wbi/v2"
|
||||
params = {
|
||||
'cid': cid,
|
||||
'bvid': bv_id,
|
||||
}
|
||||
|
||||
data = self._fetch_json(subtitle_url, params, use_wbi=True)
|
||||
if not data or data.get('code') != 0:
|
||||
return None
|
||||
|
||||
subtitle_info = data.get('data', {}).get('subtitle', {})
|
||||
subtitles = subtitle_info.get('subtitles', [])
|
||||
|
||||
if not subtitles:
|
||||
return None
|
||||
|
||||
# 优先选择AI字幕,其次选择中文简体,最后选择第一个
|
||||
selected_sub = None
|
||||
for sub in subtitles:
|
||||
lan_doc = sub.get('lan_doc', '')
|
||||
lan = sub.get('lan', '')
|
||||
if '自动生成' in lan_doc or 'AI' in lan_doc:
|
||||
selected_sub = sub
|
||||
break
|
||||
elif lan == 'zh-CN' or lan == 'zh':
|
||||
selected_sub = sub
|
||||
break
|
||||
|
||||
if not selected_sub:
|
||||
selected_sub = subtitles[0]
|
||||
|
||||
# 下载字幕内容
|
||||
sub_url = selected_sub.get('subtitle_url', '')
|
||||
if not sub_url:
|
||||
return None
|
||||
|
||||
# 确保URL完整
|
||||
if sub_url.startswith('//'):
|
||||
sub_url = 'https:' + sub_url
|
||||
elif sub_url.startswith('/'):
|
||||
sub_url = 'https://www.bilibili.com' + sub_url
|
||||
|
||||
try:
|
||||
req = Request(sub_url, headers=self._get_headers())
|
||||
with urlopen(req, timeout=30) as response:
|
||||
subtitle_data = json.loads(response.read().decode('utf-8'))
|
||||
except Exception as e:
|
||||
print(f"下载字幕失败: {e}")
|
||||
return None
|
||||
|
||||
# 解析字幕
|
||||
body = subtitle_data.get('body', [])
|
||||
if not body:
|
||||
return None
|
||||
|
||||
# 转换为SRT格式
|
||||
srt_lines = []
|
||||
|
||||
for i, item in enumerate(body, 1):
|
||||
from_time = item.get('from', 0)
|
||||
to_time = item.get('to', 0)
|
||||
text = item.get('content', '').strip()
|
||||
|
||||
if not text:
|
||||
continue
|
||||
|
||||
# SRT格式时间码
|
||||
from_str = self._format_srt_time(from_time)
|
||||
to_str = self._format_srt_time(to_time)
|
||||
|
||||
srt_lines.append(f"{i}")
|
||||
srt_lines.append(f"{from_str} --> {to_str}")
|
||||
srt_lines.append(text)
|
||||
srt_lines.append("")
|
||||
|
||||
return '\n'.join(srt_lines)
|
||||
|
||||
def _format_srt_time(self, seconds: float) -> str:
|
||||
"""格式化为SRT时间码"""
|
||||
hours = int(seconds // 3600)
|
||||
minutes = int((seconds % 3600) // 60)
|
||||
secs = int(seconds % 60)
|
||||
millis = int((seconds % 1) * 1000)
|
||||
return f"{hours:02d}:{minutes:02d}:{secs:02d},{millis:03d}"
|
||||
@@ -0,0 +1,57 @@
|
||||
"""Obsidian KB - 本地文件源"""
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
from .base import BaseSource
|
||||
from ..models import Content, ContentType
|
||||
|
||||
|
||||
class LocalSource(BaseSource):
|
||||
"""本地文件源"""
|
||||
|
||||
name = "local"
|
||||
|
||||
SUPPORTED_EXTENSIONS = ['.txt', '.md', '.pdf', '.docx']
|
||||
|
||||
def can_handle(self, url: str) -> bool:
|
||||
"""检查是否为本地文件路径"""
|
||||
if url.startswith('http'):
|
||||
return False
|
||||
path = Path(url)
|
||||
return path.exists() and path.suffix.lower() in self.SUPPORTED_EXTENSIONS
|
||||
|
||||
def extract_id(self, url: str) -> Optional[str]:
|
||||
"""使用文件路径作为ID"""
|
||||
return os.path.basename(url)
|
||||
|
||||
def fetch(self, url: str) -> Optional[Content]:
|
||||
"""获取文件元数据"""
|
||||
path = Path(url)
|
||||
if not path.exists():
|
||||
return None
|
||||
|
||||
content_type = ContentType.ARTICLE
|
||||
if path.suffix.lower() == '.txt':
|
||||
content_type = ContentType.ARTICLE
|
||||
|
||||
return Content(
|
||||
source=self.name,
|
||||
source_id=str(path),
|
||||
url=str(path.absolute()),
|
||||
title=path.stem,
|
||||
author='',
|
||||
content_type=content_type
|
||||
)
|
||||
|
||||
def get_transcript(self, content: Content) -> Optional[str]:
|
||||
"""读取文件内容"""
|
||||
try:
|
||||
path = Path(content.url)
|
||||
if path.suffix.lower() in ['.txt', '.md']:
|
||||
return path.read_text(encoding='utf-8')
|
||||
# TODO: 支持 PDF 和 DOCX
|
||||
return None
|
||||
except Exception as e:
|
||||
print(f"Error reading local file: {e}")
|
||||
return None
|
||||
@@ -0,0 +1,53 @@
|
||||
"""Obsidian KB - YouTube源"""
|
||||
|
||||
import re
|
||||
from typing import Optional
|
||||
from .base import BaseSource
|
||||
from ..models import Content, ContentType
|
||||
|
||||
|
||||
class YouTubeSource(BaseSource):
|
||||
"""YouTube视频源"""
|
||||
|
||||
name = "youtube"
|
||||
|
||||
# 支持多种YouTube URL格式
|
||||
URL_PATTERNS = [
|
||||
r'youtube\.com/watch\?v=([a-zA-Z0-9_-]{11})',
|
||||
r'youtu\.be/([a-zA-Z0-9_-]{11})',
|
||||
r'youtube\.com/embed/([a-zA-Z0-9_-]{11})',
|
||||
]
|
||||
|
||||
def can_handle(self, url: str) -> bool:
|
||||
"""检查是否为YouTube链接"""
|
||||
return 'youtube.com' in url or 'youtu.be' in url
|
||||
|
||||
def extract_id(self, url: str) -> Optional[str]:
|
||||
"""提取视频ID"""
|
||||
for pattern in self.URL_PATTERNS:
|
||||
match = re.search(pattern, url)
|
||||
if match:
|
||||
return match.group(1)
|
||||
return None
|
||||
|
||||
def fetch(self, url: str) -> Optional[Content]:
|
||||
"""获取视频元数据"""
|
||||
video_id = self.extract_id(url)
|
||||
if not video_id:
|
||||
return None
|
||||
|
||||
# TODO: 使用 YouTube Data API 获取完整信息
|
||||
# 目前返回基本信息
|
||||
return Content(
|
||||
source=self.name,
|
||||
source_id=video_id,
|
||||
url=f'https://www.youtube.com/watch?v={video_id}',
|
||||
title='', # 需要通过API获取
|
||||
author='',
|
||||
content_type=ContentType.VIDEO
|
||||
)
|
||||
|
||||
def get_transcript(self, content: Content) -> Optional[str]:
|
||||
"""获取字幕"""
|
||||
# TODO: 使用 youtube-transcript-api
|
||||
return None
|
||||
@@ -0,0 +1,55 @@
|
||||
"""Obsidian KB - 知乎源"""
|
||||
|
||||
import re
|
||||
from typing import Optional
|
||||
from .base import BaseSource
|
||||
from ..models import Content, ContentType
|
||||
|
||||
|
||||
class ZhihuSource(BaseSource):
|
||||
"""知乎文章/回答源"""
|
||||
|
||||
name = "zhihu"
|
||||
|
||||
URL_PATTERNS = [
|
||||
r'zhihu\.com/question/\d+/answer/(\d+)',
|
||||
r'zhuanlan\.zhihu\.com/p/(\d+)',
|
||||
r'zhihu\.com/question/(\d+)',
|
||||
]
|
||||
|
||||
def can_handle(self, url: str) -> bool:
|
||||
"""检查是否为知乎链接"""
|
||||
return 'zhihu.com' in url or 'zhuanlan.zhihu.com' in url
|
||||
|
||||
def extract_id(self, url: str) -> Optional[str]:
|
||||
"""提取内容ID"""
|
||||
for pattern in self.URL_PATTERNS:
|
||||
match = re.search(pattern, url)
|
||||
if match:
|
||||
return match.group(1)
|
||||
return None
|
||||
|
||||
def fetch(self, url: str) -> Optional[Content]:
|
||||
"""获取内容元数据"""
|
||||
content_id = self.extract_id(url)
|
||||
if not content_id:
|
||||
return None
|
||||
|
||||
# TODO: 使用知乎API或爬虫获取完整信息
|
||||
content_type = ContentType.ARTICLE
|
||||
if 'zhuanlan' in url:
|
||||
content_type = ContentType.ARTICLE
|
||||
|
||||
return Content(
|
||||
source=self.name,
|
||||
source_id=content_id,
|
||||
url=url,
|
||||
title='',
|
||||
author='',
|
||||
content_type=content_type
|
||||
)
|
||||
|
||||
def get_transcript(self, content: Content) -> Optional[str]:
|
||||
"""获取文章内容"""
|
||||
# TODO: 实现文章抓取
|
||||
return None
|
||||
@@ -0,0 +1,168 @@
|
||||
"""Obsidian KB - 存储管理"""
|
||||
|
||||
import os
|
||||
import json
|
||||
from pathlib import Path
|
||||
from typing import List, Optional, Dict, Any
|
||||
from datetime import datetime
|
||||
|
||||
from models import Content, ContentStatus
|
||||
from config import get_config
|
||||
|
||||
|
||||
class Storage:
|
||||
"""JSON文件存储管理"""
|
||||
|
||||
def __init__(self, base_path: Optional[str] = None):
|
||||
config = get_config()
|
||||
self.base_path = base_path or config.obsidian_path
|
||||
self.inbox_path = os.path.join(self.base_path, 'Inbox')
|
||||
self.topics_path = os.path.join(self.base_path, 'Topics')
|
||||
self._ensure_dirs()
|
||||
|
||||
def _ensure_dirs(self) -> None:
|
||||
"""确保目录存在"""
|
||||
os.makedirs(self.inbox_path, exist_ok=True)
|
||||
os.makedirs(self.topics_path, exist_ok=True)
|
||||
|
||||
def _content_file_path(self, content_id: str) -> str:
|
||||
"""获取内容文件路径"""
|
||||
return os.path.join(self.inbox_path, f'{content_id}.json')
|
||||
|
||||
def _topic_index_path(self, topic: str) -> str:
|
||||
"""获取主题索引路径"""
|
||||
topic_dir = os.path.join(self.topics_path, topic)
|
||||
os.makedirs(topic_dir, exist_ok=True)
|
||||
return os.path.join(topic_dir, 'index.json')
|
||||
|
||||
# ===== Inbox 管理 =====
|
||||
|
||||
def save_content(self, content: Content) -> None:
|
||||
"""保存内容到Inbox"""
|
||||
file_path = self._content_file_path(content.id)
|
||||
with open(file_path, 'w', encoding='utf-8') as f:
|
||||
json.dump(content.to_dict(), f, ensure_ascii=False, indent=2)
|
||||
|
||||
def load_content(self, content_id: str) -> Optional[Content]:
|
||||
"""加载指定内容"""
|
||||
file_path = self._content_file_path(content_id)
|
||||
if not os.path.exists(file_path):
|
||||
return None
|
||||
with open(file_path, 'r', encoding='utf-8') as f:
|
||||
data = json.load(f)
|
||||
return Content.from_dict(data)
|
||||
|
||||
def delete_content(self, content_id: str) -> bool:
|
||||
"""删除内容"""
|
||||
file_path = self._content_file_path(content_id)
|
||||
if os.path.exists(file_path):
|
||||
os.remove(file_path)
|
||||
return True
|
||||
return False
|
||||
|
||||
def list_inbox(self, status: Optional[ContentStatus] = None) -> List[Content]:
|
||||
"""列出Inbox中的内容"""
|
||||
contents = []
|
||||
if not os.path.exists(self.inbox_path):
|
||||
return contents
|
||||
|
||||
for filename in os.listdir(self.inbox_path):
|
||||
if filename.endswith('.json'):
|
||||
file_path = os.path.join(self.inbox_path, filename)
|
||||
with open(file_path, 'r', encoding='utf-8') as f:
|
||||
data = json.load(f)
|
||||
content = Content.from_dict(data)
|
||||
if status is None or content.status == status:
|
||||
contents.append(content)
|
||||
|
||||
# 按创建时间排序
|
||||
contents.sort(key=lambda x: x.created_at, reverse=True)
|
||||
return contents
|
||||
|
||||
def get_pending_count(self) -> int:
|
||||
"""获取待处理内容数量"""
|
||||
return len(self.list_inbox(ContentStatus.CONFIRMED))
|
||||
|
||||
def get_stats(self) -> Dict[str, int]:
|
||||
"""获取统计信息"""
|
||||
all_contents = self.list_inbox()
|
||||
stats = {status.value: 0 for status in ContentStatus}
|
||||
for content in all_contents:
|
||||
stats[content.status.value] += 1
|
||||
return stats
|
||||
|
||||
# ===== Topics 管理 =====
|
||||
|
||||
def add_to_topic(self, content: Content, topic: str) -> None:
|
||||
"""将内容添加到主题"""
|
||||
index_path = self._topic_index_path(topic)
|
||||
|
||||
# 读取现有索引
|
||||
entries = []
|
||||
if os.path.exists(index_path):
|
||||
with open(index_path, 'r', encoding='utf-8') as f:
|
||||
entries = json.load(f)
|
||||
|
||||
# 添加新条目
|
||||
entry = {
|
||||
'id': content.id,
|
||||
'title': content.title,
|
||||
'url': content.url,
|
||||
'added_at': datetime.now().isoformat(),
|
||||
'output_path': content.output_path
|
||||
}
|
||||
|
||||
# 避免重复
|
||||
entries = [e for e in entries if e['id'] != content.id]
|
||||
entries.append(entry)
|
||||
|
||||
# 保存索引
|
||||
with open(index_path, 'w', encoding='utf-8') as f:
|
||||
json.dump(entries, f, ensure_ascii=False, indent=2)
|
||||
|
||||
def get_topic_contents(self, topic: str) -> List[Dict[str, Any]]:
|
||||
"""获取主题下的所有内容"""
|
||||
index_path = self._topic_index_path(topic)
|
||||
if not os.path.exists(index_path):
|
||||
return []
|
||||
with open(index_path, 'r', encoding='utf-8') as f:
|
||||
return json.load(f)
|
||||
|
||||
def list_topics(self) -> List[str]:
|
||||
"""列出所有主题"""
|
||||
if not os.path.exists(self.topics_path):
|
||||
return []
|
||||
return [d for d in os.listdir(self.topics_path)
|
||||
if os.path.isdir(os.path.join(self.topics_path, d))]
|
||||
|
||||
def search(self, query: str) -> List[Content]:
|
||||
"""搜索内容"""
|
||||
results = []
|
||||
all_contents = self.list_inbox()
|
||||
query_lower = query.lower()
|
||||
|
||||
for content in all_contents:
|
||||
if (query_lower in content.title.lower() or
|
||||
query_lower in content.summary.lower() or
|
||||
query_lower in content.author.lower() or
|
||||
any(query_lower in tag.lower() for tag in content.tags)):
|
||||
results.append(content)
|
||||
|
||||
return results
|
||||
|
||||
def move_to_archive(self, content_id: str) -> bool:
|
||||
"""将内容移动到归档"""
|
||||
content = self.load_content(content_id)
|
||||
if not content:
|
||||
return False
|
||||
|
||||
archive_path = os.path.join(self.base_path, 'Archive')
|
||||
os.makedirs(archive_path, exist_ok=True)
|
||||
|
||||
source_file = self._content_file_path(content_id)
|
||||
target_file = os.path.join(archive_path, f'{content_id}.json')
|
||||
|
||||
if os.path.exists(source_file):
|
||||
os.rename(source_file, target_file)
|
||||
return True
|
||||
return False
|
||||
@@ -0,0 +1,71 @@
|
||||
"""Template engine for note generation."""
|
||||
|
||||
from typing import Any, Dict
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
class TemplateEngine:
|
||||
"""Manages templates for Obsidian note generation."""
|
||||
|
||||
def __init__(self, templates_dir: str = None):
|
||||
"""Initialize template engine.
|
||||
|
||||
Args:
|
||||
templates_dir: Directory containing template files
|
||||
"""
|
||||
if templates_dir is None:
|
||||
base_dir = Path(__file__).parent.parent
|
||||
templates_dir = base_dir / 'src' / 'templates'
|
||||
|
||||
self.templates_dir = Path(templates_dir)
|
||||
self.templates_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
def render(self, template_name: str, context: Dict[str, Any]) -> str:
|
||||
"""Render a template with context.
|
||||
|
||||
Args:
|
||||
template_name: Name of template to use
|
||||
context: Template context variables
|
||||
|
||||
Returns:
|
||||
Rendered template string
|
||||
"""
|
||||
template_path = self.templates_dir / f"{template_name}.md"
|
||||
|
||||
if template_path.exists():
|
||||
template = template_path.read_text(encoding='utf-8')
|
||||
else:
|
||||
# Default template
|
||||
template = self._default_template()
|
||||
|
||||
# Simple variable substitution
|
||||
result = template
|
||||
for key, value in context.items():
|
||||
placeholder = f"{{{{{key}}}}}"
|
||||
result = result.replace(placeholder, str(value))
|
||||
|
||||
return result
|
||||
|
||||
def _default_template(self) -> str:
|
||||
"""Get default note template."""
|
||||
return """# {{{title}}}
|
||||
|
||||
{{tags}}
|
||||
|
||||
## Summary
|
||||
|
||||
{{summary}}
|
||||
|
||||
## Original Content
|
||||
|
||||
**Source:** [{{platform}}]({{url}})
|
||||
**ID:** {{content_id}}
|
||||
|
||||
---
|
||||
|
||||
{{content}}
|
||||
|
||||
## Notes
|
||||
|
||||
<!-- Add your notes here -->
|
||||
"""
|
||||
@@ -0,0 +1,307 @@
|
||||
"""
|
||||
Template Renderer Module
|
||||
Handles rendering of content into Markdown templates using Jinja2.
|
||||
"""
|
||||
import os
|
||||
from datetime import datetime
|
||||
from typing import Dict, Any, Optional
|
||||
from pathlib import Path
|
||||
|
||||
# Try to import jinja2, fallback to simple string formatting
|
||||
try:
|
||||
from jinja2 import Template, Environment, FileSystemLoader
|
||||
JINJA2_AVAILABLE = True
|
||||
except ImportError:
|
||||
JINJA2_AVAILABLE = False
|
||||
Template = None
|
||||
Environment = None
|
||||
FileSystemLoader = None
|
||||
|
||||
|
||||
class TemplateRenderer:
|
||||
"""
|
||||
Renders content objects into Markdown templates.
|
||||
Supports both Jinja2 and simple string formatting.
|
||||
"""
|
||||
|
||||
def __init__(self, templates_dir: Optional[str] = None):
|
||||
"""
|
||||
Initialize the template renderer.
|
||||
|
||||
Args:
|
||||
templates_dir: Path to templates directory. If None, uses default.
|
||||
"""
|
||||
if templates_dir is None:
|
||||
# Default to templates directory next to this file
|
||||
self.templates_dir = Path(__file__).parent / "templates"
|
||||
else:
|
||||
self.templates_dir = Path(templates_dir)
|
||||
|
||||
self.templates_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# Initialize Jinja2 environment if available
|
||||
if JINJA2_AVAILABLE:
|
||||
self.env = Environment(
|
||||
loader=FileSystemLoader(str(self.templates_dir)),
|
||||
trim_blocks=True,
|
||||
lstrip_blocks=True
|
||||
)
|
||||
else:
|
||||
self.env = None
|
||||
|
||||
def _get_template_name(self, content: Any) -> str:
|
||||
"""
|
||||
Determine which template to use based on content type and duration.
|
||||
|
||||
Args:
|
||||
content: Content object with content_type and duration attributes
|
||||
|
||||
Returns:
|
||||
Template filename
|
||||
"""
|
||||
content_type = getattr(content, 'content_type', None)
|
||||
duration = getattr(content, 'duration', 0)
|
||||
|
||||
# Handle enum types
|
||||
if hasattr(content_type, 'value'):
|
||||
content_type = content_type.value
|
||||
|
||||
# Convert duration to seconds if it's a string
|
||||
if isinstance(duration, str):
|
||||
try:
|
||||
parts = duration.split(':')
|
||||
if len(parts) == 2: # MM:SS
|
||||
duration = int(parts[0]) * 60 + int(parts[1])
|
||||
elif len(parts) == 3: # HH:MM:SS
|
||||
duration = int(parts[0]) * 3600 + int(parts[1]) * 60 + int(parts[2])
|
||||
else:
|
||||
duration = 0
|
||||
except (ValueError, IndexError):
|
||||
duration = 0
|
||||
|
||||
# Select template based on type and duration
|
||||
if content_type == 'article':
|
||||
return 'article.md'
|
||||
elif content_type == 'video':
|
||||
# Long videos (>>30 min) use tutorial template
|
||||
if duration and duration > 1800: # 30 minutes
|
||||
return 'tutorial_video.md'
|
||||
else:
|
||||
return 'light_video.md'
|
||||
elif duration and duration > 1800:
|
||||
return 'tutorial_video.md'
|
||||
else:
|
||||
return 'light_video.md'
|
||||
|
||||
def _prepare_context(self, content: Any, extra_context: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
|
||||
"""
|
||||
Prepare the context dictionary for template rendering.
|
||||
|
||||
Args:
|
||||
content: Content object
|
||||
extra_context: Additional context to merge
|
||||
|
||||
Returns:
|
||||
Context dictionary
|
||||
"""
|
||||
# Get analysis data if available
|
||||
analysis_data = {}
|
||||
if hasattr(content, 'analysis') and content.analysis:
|
||||
if isinstance(content.analysis, dict):
|
||||
analysis_data = content.analysis
|
||||
else:
|
||||
# It's a ContentAnalysis object
|
||||
analysis_data = {
|
||||
'one_liner': getattr(content.analysis, 'one_liner', ''),
|
||||
'summary': getattr(content.analysis, 'summary', ''),
|
||||
'key_points': getattr(content.analysis, 'key_points', []),
|
||||
'tags': getattr(content.analysis, 'tags', []),
|
||||
'quality_score': getattr(content.analysis, 'quality_score', 0),
|
||||
'prerequisites': getattr(content.analysis, 'prerequisites', []),
|
||||
'outline': getattr(content.analysis, 'outline', []),
|
||||
'pitfalls': getattr(content.analysis, 'pitfalls', []),
|
||||
'practice_advice': getattr(content.analysis, 'practice_advice', []),
|
||||
}
|
||||
|
||||
# Base context from content object
|
||||
context = {
|
||||
'title': getattr(content, 'title', 'Untitled'),
|
||||
'source': getattr(content, 'source', 'unknown'),
|
||||
'url': getattr(content, 'url', ''),
|
||||
'author': getattr(content, 'author', 'Unknown'),
|
||||
'duration': getattr(content, 'duration', ''),
|
||||
'date': getattr(content, 'date', datetime.now().isoformat()),
|
||||
'tags': getattr(content, 'tags', ['untagged']),
|
||||
# First check analysis_data, then fall back to content attributes
|
||||
'one_liner': analysis_data.get('one_liner', getattr(content, 'one_liner', '')),
|
||||
'summary': analysis_data.get('summary', getattr(content, 'summary', '')),
|
||||
'key_points': analysis_data.get('key_points', getattr(content, 'key_points', [])),
|
||||
'quality_score': analysis_data.get('quality_score', getattr(content, 'quality_score', 0)),
|
||||
'prerequisites': analysis_data.get('prerequisites', []),
|
||||
'outline': analysis_data.get('outline', []),
|
||||
'pitfalls': analysis_data.get('pitfalls', []),
|
||||
'practice_advice': analysis_data.get('practice_advice', []),
|
||||
}
|
||||
|
||||
# Add transcript-related data
|
||||
if hasattr(content, 'transcript'):
|
||||
context['transcript'] = content.transcript
|
||||
|
||||
# Merge extra context
|
||||
if extra_context:
|
||||
context.update(extra_context)
|
||||
|
||||
# Ensure tags is a list
|
||||
if isinstance(context['tags'], str):
|
||||
context['tags'] = [tag.strip() for tag in context['tags'].split(',')]
|
||||
if not context['tags']:
|
||||
context['tags'] = ['untagged']
|
||||
|
||||
return context
|
||||
|
||||
def render(self, content: Any, extra_context: Optional[Dict[str, Any]] = None) -> str:
|
||||
"""
|
||||
Render content into Markdown using appropriate template.
|
||||
|
||||
Args:
|
||||
content: Content object to render
|
||||
extra_context: Additional context for template
|
||||
|
||||
Returns:
|
||||
Rendered Markdown string
|
||||
"""
|
||||
template_name = self._get_template_name(content)
|
||||
context = self._prepare_context(content, extra_context)
|
||||
|
||||
template_path = self.templates_dir / template_name
|
||||
|
||||
# Check if template exists
|
||||
if not template_path.exists():
|
||||
raise FileNotFoundError(f"Template not found: {template_path}")
|
||||
|
||||
# Read template content
|
||||
template_content = template_path.read_text(encoding='utf-8')
|
||||
|
||||
# Render using Jinja2 if available
|
||||
if JINJA2_AVAILABLE and self.env:
|
||||
template = self.env.from_string(template_content)
|
||||
return template.render(**context)
|
||||
else:
|
||||
# Fallback to simple string formatting
|
||||
return self._simple_render(template_content, context)
|
||||
|
||||
def _simple_render(self, template_content: str, context: Dict[str, Any]) -> str:
|
||||
"""
|
||||
Simple string-based template rendering fallback.
|
||||
Supports basic {{ variable }} and {% for %} loops.
|
||||
|
||||
Args:
|
||||
template_content: Template string
|
||||
context: Context dictionary
|
||||
|
||||
Returns:
|
||||
Rendered string
|
||||
"""
|
||||
result = template_content
|
||||
|
||||
# Handle simple for loops (very basic implementation)
|
||||
import re
|
||||
|
||||
# Pattern for {% for item in items %}...{% endfor %}
|
||||
for_pattern = r'{%-?\s*for\s+(\w+)\s+in\s+(\w+)\s*-?%}(.*?){%-?\s*endfor\s*-?%}'
|
||||
|
||||
def replace_for(match):
|
||||
var_name = match.group(1)
|
||||
list_name = match.group(2)
|
||||
inner_template = match.group(3)
|
||||
|
||||
items = context.get(list_name, [])
|
||||
if not items:
|
||||
return ''
|
||||
|
||||
output = []
|
||||
for item in items:
|
||||
item_str = inner_template
|
||||
# Handle {{ var.attribute }} or {{ var }}
|
||||
if isinstance(item, dict):
|
||||
for key, value in item.items():
|
||||
placeholder1 = f'{{{{{var_name}.{key}}}}}'
|
||||
placeholder2 = f'{{{{ {var_name}.{key} }}}}'
|
||||
str_val = str(value) if value is not None else ''
|
||||
item_str = item_str.replace(placeholder1, str_val)
|
||||
item_str = item_str.replace(placeholder2, str_val)
|
||||
else:
|
||||
placeholder1 = f'{{{{{var_name}}}}}'
|
||||
placeholder2 = f'{{{{ {var_name} }}}}'
|
||||
str_val = str(item) if item is not None else ''
|
||||
item_str = item_str.replace(placeholder1, str_val)
|
||||
item_str = item_str.replace(placeholder2, str_val)
|
||||
output.append(item_str)
|
||||
|
||||
return ''.join(output)
|
||||
|
||||
result = re.sub(for_pattern, replace_for, result, flags=re.DOTALL)
|
||||
|
||||
# Handle simple if statements (basic)
|
||||
if_pattern = r'{%-?\s*if\s+(\w+)\s*-?%}(.*?){%-?\s*endif\s*-?%}'
|
||||
|
||||
def replace_if(match):
|
||||
var_name = match.group(1)
|
||||
inner_content = match.group(2)
|
||||
if context.get(var_name):
|
||||
return inner_content
|
||||
return ''
|
||||
|
||||
result = re.sub(if_pattern, replace_if, result, flags=re.DOTALL)
|
||||
|
||||
# Handle variable substitution {{ variable }} or {{variable}}
|
||||
# Sort keys by length (descending) to avoid partial replacements
|
||||
for var_name in sorted(context.keys(), key=len, reverse=True):
|
||||
value = context[var_name]
|
||||
if isinstance(value, list):
|
||||
str_val = ', '.join(str(v) for v in value)
|
||||
else:
|
||||
str_val = str(value) if value is not None else ''
|
||||
|
||||
# Replace both {{ var }} and {{var}}
|
||||
result = result.replace(f'{{{{ {var_name} }}}}', str_val)
|
||||
result = result.replace(f'{{{{{var_name}}}}}', str_val)
|
||||
|
||||
return result
|
||||
|
||||
def render_to_file(self, content: Any, output_path: str, extra_context: Optional[Dict[str, Any]] = None) -> str:
|
||||
"""
|
||||
Render content and save to file.
|
||||
|
||||
Args:
|
||||
content: Content object to render
|
||||
output_path: Path to output file
|
||||
extra_context: Additional context for template
|
||||
|
||||
Returns:
|
||||
Path to rendered file
|
||||
"""
|
||||
rendered = self.render(content, extra_context)
|
||||
|
||||
output_file = Path(output_path)
|
||||
output_file.parent.mkdir(parents=True, exist_ok=True)
|
||||
output_file.write_text(rendered, encoding='utf-8')
|
||||
|
||||
return str(output_file)
|
||||
|
||||
|
||||
# Convenience function
|
||||
def render_content(content: Any, templates_dir: Optional[str] = None, extra_context: Optional[Dict[str, Any]] = None) -> str:
|
||||
"""
|
||||
Convenience function to render content without creating a renderer instance.
|
||||
|
||||
Args:
|
||||
content: Content object to render
|
||||
templates_dir: Optional templates directory path
|
||||
extra_context: Additional context for template
|
||||
|
||||
Returns:
|
||||
Rendered Markdown string
|
||||
"""
|
||||
renderer = TemplateRenderer(templates_dir)
|
||||
return renderer.render(content, extra_context)
|
||||
@@ -0,0 +1,33 @@
|
||||
---
|
||||
title: "{{ title }}"
|
||||
source: "{{ source }}"
|
||||
url: "{{ url }}"
|
||||
author: "{{ author }}"
|
||||
date: "{{ date }}"
|
||||
tags:{% for tag in tags %}
|
||||
- {{ tag }}{% endfor %}
|
||||
quality_score: {{ quality_score }}
|
||||
reading_time: {{ reading_time }}
|
||||
---
|
||||
|
||||
# {{ title }}
|
||||
|
||||
**原文链接**: [{{ title }}]({{ url }})
|
||||
|
||||
## 🎯 一句话总结
|
||||
{{ one_liner }}
|
||||
|
||||
## 📖 文章摘要
|
||||
{{ summary }}
|
||||
|
||||
## 💡 核心观点{% for point in key_points %}
|
||||
1. {{ point }}{% endfor %}
|
||||
|
||||
## 📝 详细内容
|
||||
{{ content }}
|
||||
|
||||
## 🔗 相关链接{% for link in related_links %}
|
||||
- [{{ link.title }}]({{ link.url }}){% endfor %}
|
||||
|
||||
## 🏷️ 标签
|
||||
{% for tag in tags %}#{{ tag }} {% endfor %}
|
||||
@@ -0,0 +1,20 @@
|
||||
---
|
||||
title: "{{ title }}"
|
||||
source: "{{ source }}"
|
||||
url: "{{ url }}"
|
||||
author: "{{ author }}"
|
||||
duration: "{{ duration }}"
|
||||
date: "{{ date }}"
|
||||
tags:{% for tag in tags %}
|
||||
- {{ tag }}{% endfor %}
|
||||
---
|
||||
|
||||
#{{ tags[0] }} #{{ tags[1] }}
|
||||
|
||||
**一句话摘要**: {{ one_liner }}
|
||||
|
||||
## 摘要
|
||||
{{ summary }}
|
||||
|
||||
## 核心要点{% for point in key_points %}
|
||||
1. {{ point }}{% endfor %}
|
||||
@@ -0,0 +1,16 @@
|
||||
---
|
||||
title: {{topic_name}}
|
||||
date: {{date}}
|
||||
tags: [topic-index]
|
||||
---
|
||||
|
||||
# {{topic_name}}
|
||||
|
||||
## 简介
|
||||
{{description}}
|
||||
|
||||
## 相关内容
|
||||
{{content_list}}
|
||||
|
||||
## 学习路径
|
||||
{{learning_path}}
|
||||
@@ -0,0 +1,35 @@
|
||||
---
|
||||
title: "{{ title }}"
|
||||
source: "{{ source }}"
|
||||
url: "{{ url }}"
|
||||
up主: "{{ author }}"
|
||||
duration: "{{ duration }}"
|
||||
date: "{{ date }}"
|
||||
tags:{% for tag in tags %}
|
||||
- {{ tag }}{% endfor %}
|
||||
quality_score: {{ quality_score }}
|
||||
---
|
||||
|
||||
## 🎯 一句话总结
|
||||
{{ one_liner }}
|
||||
|
||||
## 📚 前置知识{% for pre in prerequisites %}
|
||||
- {{ pre }}{% endfor %}
|
||||
|
||||
## 📝 步骤详解{% for section in outline %}
|
||||
### {{ section.title }} [{{ section.time_range }}]
|
||||
{{ section.summary }}
|
||||
{% if section.code_snippet %}
|
||||
```{{ section.code_language }}
|
||||
{{ section.code_snippet }}
|
||||
```
|
||||
{% endif %}{% endfor %}
|
||||
|
||||
## 💡 关键要点{% for point in key_points %}
|
||||
1. {{ point }}{% endfor %}
|
||||
|
||||
## ⚠️ 常见坑点{% for pitfall in pitfalls %}
|
||||
- {{ pitfall }}{% endfor %}
|
||||
|
||||
## 🤔 实践建议{% for advice in practice_advice %}
|
||||
- {{ advice }}{% endfor %}
|
||||
Reference in New Issue
Block a user