08f7c9f26c
- Add extractors layer with BilibiliExtractor (Playwright stealth mode) - Add outputs layer with ObsidianWriter and GitSync - Implement subtitle/no-subtitle graceful degradation - Update config with output and bilibili extraction settings - Complete integration pipeline: extract → process → output → sync - Add comprehensive tests (100% pass rate) - Update documentation (SKILL.md, README.md, requirements.txt)
84 lines
2.5 KiB
Python
84 lines
2.5 KiB
Python
"""
|
|
Extractor 抽象基类
|
|
"""
|
|
from abc import ABC, abstractmethod
|
|
from typing import Dict, Any, Optional
|
|
|
|
|
|
class ExtractionError(Exception):
|
|
"""提取器异常基类"""
|
|
def __init__(self, message: str, source_id: str = "", details: Optional[Dict[str, Any]] = None):
|
|
self.message = message
|
|
self.source_id = source_id
|
|
self.details = details or {}
|
|
super().__init__(self.message)
|
|
|
|
|
|
class Extractor(ABC):
|
|
"""
|
|
内容提取器抽象基类
|
|
|
|
负责从各种来源(视频、文章等)提取结构化数据
|
|
"""
|
|
|
|
@abstractmethod
|
|
def extract(self, source_id: str) -> Dict[str, Any]:
|
|
"""
|
|
提取源内容
|
|
|
|
Args:
|
|
source_id: 来源唯一标识符(如视频ID、文章URL等)
|
|
|
|
Returns:
|
|
Dict[str, Any]: 包含以下键的字典:
|
|
- metadata: Dict[str, Any] - 元数据(标题、作者、时长等)
|
|
- stats: Dict[str, Any] - 统计信息(观看次数、点赞数等)
|
|
- subtitle: str - 字幕/文本内容
|
|
- description: str - 内容描述
|
|
|
|
Raises:
|
|
ExtractionError: 提取失败时抛出
|
|
"""
|
|
pass
|
|
|
|
def validate_source_id(self, source_id: str) -> bool:
|
|
"""
|
|
验证源ID格式是否有效(可选实现)
|
|
|
|
Args:
|
|
source_id: 来源唯一标识符
|
|
|
|
Returns:
|
|
bool: 如果ID格式有效返回True,否则返回False
|
|
"""
|
|
# 默认实现:非空即有效
|
|
return bool(source_id and source_id.strip())
|
|
|
|
def _handle_error(self, error: Exception, source_id: str, context: Optional[Dict[str, Any]] = None) -> None:
|
|
"""
|
|
统一错误处理基类方法
|
|
|
|
Args:
|
|
error: 原始异常
|
|
source_id: 来源ID
|
|
context: 额外的上下文信息
|
|
|
|
Raises:
|
|
ExtractionError: 封装后的提取器异常
|
|
"""
|
|
error_details = {
|
|
'error_type': type(error).__name__,
|
|
'error_message': str(error),
|
|
'source_id': source_id,
|
|
'extractor_class': self.__class__.__name__
|
|
}
|
|
|
|
if context:
|
|
error_details.update(context)
|
|
|
|
raise ExtractionError(
|
|
message=f"提取失败: {str(error)}",
|
|
source_id=source_id,
|
|
details=error_details
|
|
) from error
|