- Remove 3 dead modules: academic/, wechat/, social/ (merged into engines/) - Remove 4 stale news scripts (tech_news.py, wallstreetcn.py, cls.py, rss.py) - scripts/core/base.py: canonical SearchResult + BaseEngine (was in engines/base.py) - scripts/engines/base.py: re-exports from core.base for compatibility - scripts/engines/news_engine.py: Bing News + RSS unified (was in scripts/news/rss.py) - scripts/engines/academic_engine.py: ArXiv + CrossRef unified (was in scripts/academic/arxiv.py) - scripts/cli.py: complete rewrite — 10 commands (search/web/news/academic/wechat/social/rss/baidu/health/sources) - scripts/unified_search.py: clean async Python API (was a stub) - ws.py: lightweight launcher - README.md + SKILL.md: updated to reflect actual 15+ engines - All EngineResult → SearchResult - All engines return list[SearchResult]
58 lines
1.6 KiB
Python
58 lines
1.6 KiB
Python
"""
|
|
Generic URL-based search engine.
|
|
|
|
Builds search URLs from templates (config/engines.json)
|
|
and returns them as URL results.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from urllib.parse import quote_plus
|
|
from typing import Any
|
|
|
|
from scripts.core.base import BaseEngine, SearchResult
|
|
|
|
|
|
class UrlSearchEngine(BaseEngine):
|
|
"""
|
|
Generic engine that builds search URLs from templates.
|
|
|
|
Reads URL templates from config (engines.json) and returns
|
|
the constructed search URL as a SearchResult.
|
|
"""
|
|
|
|
name: str = "url"
|
|
|
|
def __init__(self, name: str, engine_def: dict[str, Any], timeout: float = 10.0):
|
|
super().__init__(timeout=timeout)
|
|
self.name = name
|
|
self.url_template = engine_def.get("url", "")
|
|
self.region = engine_def.get("region", "global")
|
|
|
|
def build_url(self, query: str, **kwargs) -> str:
|
|
"""Build search URL by replacing {keyword} with encoded query."""
|
|
if not self.url_template:
|
|
return ""
|
|
encoded = quote_plus(query)
|
|
return self.url_template.replace("{keyword}", encoded)
|
|
|
|
async def search(
|
|
self, query: str, max_results: int = 10, **kwargs
|
|
) -> list[SearchResult]:
|
|
"""Return the search URL as a single result."""
|
|
url = self.build_url(query, **kwargs)
|
|
if not url:
|
|
return []
|
|
|
|
return [
|
|
SearchResult(
|
|
title=f"{self.name}: {query}",
|
|
url=url,
|
|
snippet=f"Search {self.name} for '{query}'",
|
|
source=self.name,
|
|
rank=1,
|
|
category="web",
|
|
extra={"url": url, "region": self.region},
|
|
)
|
|
]
|