refactor: restructure project with CLI tool, multi-engine support, and skill-builder compliant SKILL.md
- 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)
This commit is contained in:
1 parent
80ac14491e
commit
87935b59ce
29 files changed
+2364
-354
No files matched your search
@@ -0,0 +1 @@
|
||||
"""Search engine implementations."""
|
||||
@@ -0,0 +1,80 @@
|
||||
"""
|
||||
Parsing-based search engines (e.g. DuckDuckGo HTML scraper).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import Optional
|
||||
|
||||
import httpx
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
from scripts.core.base import SearchEngine, SearchResult
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class DuckDuckGoEngine(SearchEngine):
|
||||
"""DuckDuckGo HTML search engine (no API key required)."""
|
||||
|
||||
SEARCH_URL = "https://html.duckduckgo.com/html/"
|
||||
|
||||
def __init__(self, config: Optional[dict] = None):
|
||||
super().__init__("DuckDuckGo", config)
|
||||
timeout = (config or {}).get("timeout", 15)
|
||||
self._client = httpx.AsyncClient(
|
||||
timeout=timeout,
|
||||
headers={
|
||||
"User-Agent": (config or {}).get(
|
||||
"user_agent",
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
|
||||
"AppleWebKit/537.36 (KHTML, like Gecko) "
|
||||
"Chrome/120.0.0.0 Safari/537.36",
|
||||
),
|
||||
},
|
||||
follow_redirects=True,
|
||||
)
|
||||
|
||||
def build_url(self, query: str, **kwargs) -> str:
|
||||
"""Build DuckDuckGo search URL."""
|
||||
from urllib.parse import quote_plus
|
||||
return f"https://duckduckgo.com/?q={quote_plus(query)}"
|
||||
|
||||
async def search(
|
||||
self, query: str, max_results: int = 10, **kwargs
|
||||
) -> list[SearchResult]:
|
||||
"""Search DuckDuckGo and parse HTML results."""
|
||||
params = {"q": query}
|
||||
try:
|
||||
response = await self._client.post(self.SEARCH_URL, data=params)
|
||||
response.raise_for_status()
|
||||
return self._parse_results(response.text, max_results)
|
||||
except httpx.HTTPError as e:
|
||||
logger.error(f"DuckDuckGo search failed: {e}")
|
||||
return []
|
||||
|
||||
def _parse_results(self, html: str, max_results: int) -> list[SearchResult]:
|
||||
"""Parse DuckDuckGo HTML search results."""
|
||||
soup = BeautifulSoup(html, "lxml")
|
||||
results = []
|
||||
|
||||
for i, result in enumerate(soup.select(".result")[:max_results]):
|
||||
title_el = result.select_one(".result__title a")
|
||||
snippet_el = result.select_one(".result__snippet")
|
||||
if title_el:
|
||||
results.append(
|
||||
SearchResult(
|
||||
title=title_el.get_text(strip=True),
|
||||
url=title_el.get("href", ""),
|
||||
snippet=snippet_el.get_text(strip=True)
|
||||
if snippet_el else "",
|
||||
source=self.name,
|
||||
rank=i + 1,
|
||||
category="web",
|
||||
)
|
||||
)
|
||||
return results
|
||||
|
||||
async def close(self):
|
||||
await self._client.aclose()
|
||||
@@ -0,0 +1,60 @@
|
||||
"""
|
||||
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
|
||||
Reference in new issue
Block a user