- Replace setup.py with pyproject.toml (uv-compatible) - Add CLI entry point (web-search) with 9 subcommands - Add multi-engine architecture (14 web + 7 special sources) - Add WeChat/CLS/WallStreetCN/ArXiv/Twitter search engines - Remove old src/ and examples/ directories - Remove redundant requirements.txt and setup.py - Update README.md and SKILL.md with full documentation - SKILL.md now compliant with skill-builder standards (frontmatter, triggers, structure)
61 lines
1.7 KiB
Python
61 lines
1.7 KiB
Python
"""
|
|
Generic URL-based search engine.
|
|
|
|
Builds search URLs from templates defined in engines.json
|
|
and returns them as URL results.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from typing import Any, Optional
|
|
from urllib.parse import quote_plus
|
|
|
|
from scripts.core.base import SearchEngine, SearchResult
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class UrlSearchEngine(SearchEngine):
|
|
"""
|
|
Generic search engine that builds URLs from templates.
|
|
|
|
Reads URL templates from config (engines.json) and returns
|
|
the constructed search URL as a SearchResult.
|
|
"""
|
|
|
|
def __init__(self, name: str, engine_def: dict[str, Any]):
|
|
super().__init__(name, engine_def.get("config", {}))
|
|
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},
|
|
)
|
|
]
|
|
|
|
async def close(self):
|
|
pass
|