64 lines
1.7 KiB
Python
64 lines
1.7 KiB
Python
"""搜索源抽象基类 — 所有搜索源统一接口"""
|
|
|
|
from abc import ABC, abstractmethod
|
|
|
|
|
|
class SearchResult:
|
|
"""统一搜索结果格式"""
|
|
def __init__(self, title='', url='', content='', score=0.0, source='',
|
|
published_date=''):
|
|
self.title = title
|
|
self.url = url
|
|
self.content = content
|
|
self.score = score
|
|
self.source = source
|
|
self.published_date = published_date
|
|
|
|
def to_dict(self):
|
|
return {
|
|
'title': self.title,
|
|
'url': self.url,
|
|
'content': self.content,
|
|
'score': self.score,
|
|
'source': self.source,
|
|
'published_date': self.published_date or '',
|
|
}
|
|
|
|
|
|
class BaseProvider(ABC):
|
|
"""搜索源基类"""
|
|
|
|
# 源名称(唯一标识)
|
|
name = ''
|
|
# 展示名称
|
|
display_name = ''
|
|
# 是否需要 API key
|
|
needs_api_key = False
|
|
# 是否默认启用
|
|
enabled = False
|
|
# 优先级(数字越小越优先)
|
|
priority = 100
|
|
|
|
def __init__(self, config: dict):
|
|
self.config = config
|
|
|
|
@abstractmethod
|
|
def search(self, query: str, max_results: int = 10) -> list:
|
|
"""执行搜索,返回 SearchResult 列表"""
|
|
...
|
|
|
|
def is_available(self) -> bool:
|
|
"""检查当前源是否可用"""
|
|
return True
|
|
|
|
def get_status(self) -> dict:
|
|
"""返回源状态信息"""
|
|
return {
|
|
'name': self.name,
|
|
'display_name': self.display_name,
|
|
'available': self.is_available(),
|
|
'enabled': self.enabled,
|
|
'needs_api_key': self.needs_api_key,
|
|
'priority': self.priority,
|
|
}
|