refactor(06-01): update platform adapters to use standard imports
- Remove sys.path manipulation from bilibili.py and zhihu.py - Use standard imports from kb_workflow.metadata - Remove try/except wrappers around imports Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -18,7 +18,6 @@ import asyncio
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
from typing import Dict, Any, Optional
|
||||
@@ -27,21 +26,10 @@ from .base import PlatformAdapter
|
||||
from ..errors import NetworkError, AuthError
|
||||
from ..retry import retry_on_network_error
|
||||
from ..state import WorkflowStateManager
|
||||
from kb_workflow.metadata.bilibili_extractor import BilibiliExtractor
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Add kb-bilibili-metadata to path if not installed as package
|
||||
_BILIBILI_METADATA_PATH = Path(__file__).parent.parent.parent / "kb-bilibili-metadata"
|
||||
if _BILIBILI_METADATA_PATH.exists():
|
||||
sys.path.insert(0, str(_BILIBILI_METADATA_PATH))
|
||||
|
||||
try:
|
||||
from bilibili_extractor import BilibiliExtractor
|
||||
BILIBILI_EXTRACTOR_AVAILABLE = True
|
||||
except ImportError:
|
||||
BILIBILI_EXTRACTOR_AVAILABLE = False
|
||||
BilibiliExtractor = None
|
||||
|
||||
|
||||
class BilibiliAdapter(PlatformAdapter):
|
||||
"""Bilibili platform adapter for video and article content
|
||||
@@ -86,12 +74,6 @@ class BilibiliAdapter(PlatformAdapter):
|
||||
timeout: Request timeout in seconds (default 30)
|
||||
state_manager: Optional WorkflowStateManager for recording step status
|
||||
"""
|
||||
if not BILIBILI_EXTRACTOR_AVAILABLE:
|
||||
raise ImportError(
|
||||
"BilibiliExtractor not available. "
|
||||
"Ensure kb-bilibili-metadata/bilibili_extractor.py is accessible."
|
||||
)
|
||||
|
||||
self._extractor = BilibiliExtractor(headless=headless, timeout=timeout)
|
||||
self._state_manager = state_manager
|
||||
|
||||
|
||||
@@ -11,17 +11,11 @@ from typing import Dict, Any, Optional
|
||||
from datetime import datetime
|
||||
import re
|
||||
import asyncio
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
# Add kb-zhihu-metadata to path for import
|
||||
_kb_zhihu_path = Path(__file__).parent.parent.parent / "kb-zhihu-metadata"
|
||||
if str(_kb_zhihu_path) not in sys.path:
|
||||
sys.path.insert(0, str(_kb_zhihu_path))
|
||||
|
||||
from kb_pipeline.platforms.base import PlatformAdapter
|
||||
from kb_pipeline.errors import AuthError
|
||||
from kb_pipeline.config import get_settings
|
||||
from kb_workflow.metadata.zhihu_extractor import ZhihuPlaywrightExtractor
|
||||
|
||||
|
||||
class ZhihuAdapter(PlatformAdapter):
|
||||
@@ -89,24 +83,15 @@ class ZhihuAdapter(PlatformAdapter):
|
||||
def _get_extractor(self):
|
||||
"""Get or create ZhihuPlaywrightExtractor instance.
|
||||
|
||||
Lazy import to avoid dependency issues when kb-zhihu-metadata is not installed.
|
||||
|
||||
Returns:
|
||||
ZhihuPlaywrightExtractor instance
|
||||
"""
|
||||
if self._extractor is None:
|
||||
try:
|
||||
from zhihu_extractor import ZhihuPlaywrightExtractor
|
||||
self._extractor = ZhihuPlaywrightExtractor(
|
||||
headless=self.headless,
|
||||
timeout=self.timeout,
|
||||
cookie=self.cookie
|
||||
)
|
||||
except ImportError as e:
|
||||
raise ImportError(
|
||||
"kb-zhihu-metadata module not found. "
|
||||
"Please ensure kb-zhihu-metadata directory exists."
|
||||
) from e
|
||||
self._extractor = ZhihuPlaywrightExtractor(
|
||||
headless=self.headless,
|
||||
timeout=self.timeout,
|
||||
cookie=self.cookie
|
||||
)
|
||||
return self._extractor
|
||||
|
||||
def _extract_with_playwright(self, url: str) -> Optional[Dict[str, Any]]:
|
||||
|
||||
Reference in New Issue
Block a user