From c077a7ae408a9be95afe1c27efcf260b5e528d0d Mon Sep 17 00:00:00 2001 From: openbot-coder Date: Sat, 30 May 2026 16:25:41 +0800 Subject: [PATCH] refactor: cleanup dead code, fix model confusion, streamline CLI MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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] --- README.md | 146 +++- SKILL.md | 244 ++---- scripts/academic/__init__.py | 1 - scripts/academic/arxiv.py | 130 --- scripts/cli.py | 1180 ++++++++++++-------------- scripts/core/base.py | 75 +- scripts/engines/academic_engine.py | 42 +- scripts/engines/baidu_qianfan.py | 79 +- scripts/engines/baidu_scraper.py | 8 +- scripts/engines/base.py | 58 +- scripts/engines/bing_scraper.py | 8 +- scripts/engines/brave_scraper.py | 8 +- scripts/engines/github_engine.py | 10 +- scripts/engines/news_engine.py | 99 +-- scripts/engines/parallel_search.py | 8 +- scripts/engines/parser_engines.py | 144 ++-- scripts/engines/search360_scraper.py | 8 +- scripts/engines/sogou_scraper.py | 8 +- scripts/engines/url_engine.py | 23 +- scripts/models.py | 35 +- scripts/news/__init__.py | 1 - scripts/news/cls.py | 186 ---- scripts/news/rss.py | 367 -------- scripts/news/tech_news.py | 461 ---------- scripts/news/wallstreetcn.py | 117 --- scripts/social/__init__.py | 1 - scripts/social/twitter.py | 208 ----- scripts/unified_search.py | 227 +++-- scripts/wechat/__init__.py | 1 - scripts/wechat/sogou_weixin.py | 365 -------- ws.py | 88 +- 31 files changed, 1188 insertions(+), 3148 deletions(-) delete mode 100644 scripts/academic/__init__.py delete mode 100644 scripts/academic/arxiv.py delete mode 100644 scripts/news/__init__.py delete mode 100644 scripts/news/cls.py delete mode 100644 scripts/news/rss.py delete mode 100644 scripts/news/tech_news.py delete mode 100644 scripts/news/wallstreetcn.py delete mode 100644 scripts/social/__init__.py delete mode 100644 scripts/social/twitter.py delete mode 100644 scripts/wechat/__init__.py delete mode 100644 scripts/wechat/sogou_weixin.py diff --git a/README.md b/README.md index d912fb6..363c055 100644 --- a/README.md +++ b/README.md @@ -1,45 +1,137 @@ # web-search-skills -Web search agent skills for nanobot. +> Multi-source web search CLI & Python library — 15+ engines, <5s, no API key required. ## Features -- Multi-engine search: Bing + Sogou (primary), Baidu Qianfan API (backup) -- News search: BingNews + 14 RSS feeds in parallel -- Academic search: ArXiv + CrossRef API -- GitHub search via public API -- Parallel dispatch with URL-based deduplication -- Result aggregation with relevance scoring -- In-memory LRU cache (500 items, 1h TTL) -- Execution logging to `logs/search_log.jsonl` +- **15+ engines** across 5 categories: Web, News, Academic, Social, RSS +- **Concurrent execution** via `asyncio.gather` with semaphore (max 5 parallel) +- **Free by default** — most engines work without any API key +- **Python API** for programmatic use (`UnifiedSearch`) +- **Single model** — `SearchResult` dataclass across all engines -## Installation +## Quick Start ```bash -cd skills/web-search -npm install +# Install +pip install -e . + +# CLI +web-search search "AI news" -n 10 +web-search news "quantum computing" -n 5 +web-search academic "machine learning" +web-search wechat 蒙面财经 +web-search health ``` -## Usage +## Commands -```bash -nanobot skill run web-search --query "your search query" +| Command | Description | Speed | +|---------|------------|-------| +| `search` | All 15 engines in parallel | ~10s | +| `web` | Web engines only (6) | ~5s | +| `news` | Bing News + 15 RSS feeds | ~8s | +| `academic` | ArXiv + CrossRef in parallel | ~6s | +| `wechat` | WeChat via Sogou | ~5s | +| `social` | Twitter/X via Nitter | ~5s | +| `rss` | RSS feed aggregation | ~8s | +| `baidu` | Baidu Qianfan API (needs key) | varies | +| `health` | Engine connectivity check | ~10s | +| `sources` | List all sources | instant | + +## Options + +- `-n N` — max results per engine (default: 10) +- `-s SRC...` — run specific sources only +- `-c CAT` — run by category (web/news/academic/social/rss) +- `-r cn|global` — region (default: cn) +- `-j` — JSON output + +## Python API + +```python +from scripts.unified_search import UnifiedSearch + +async def main(): + us = UnifiedSearch() + resp = await us.search("quantum computing", sources=["bing", "arxiv"], max_results=5) + for r in resp.results: + print(f"[{r.rank}] {r.title}") + print(f" {r.url}") + await us.close() ``` ## Architecture -- `unified_search()` — main entry, handles query planning and engine routing -- `ResultAggregator` — deduplication + relevance scoring -- `CacheLayer` — LRU in-memory cache -- `EnginePool` — manages engine health and selection +``` +scripts/ + cli.py # CLI entry point + unified_search.py # Python API + models.py # Data models + config.py # Settings + core/ + base.py # SearchResult + BaseEngine (canonical) + aggregator.py # Deduplication + ranking + execution_log.py # Timing + error tracking + engines/ + bing_scraper.py # Bing web search + sogou_scraper.py # Sogou web search + baidu_scraper.py # Baidu web search + search360_scraper.py # 360 search + parser_engines.py # DuckDuckGo + Brave (HTML parser) + news_engine.py # Bing News + RSS aggregator + academic_engine.py # ArXiv + CrossRef + github_engine.py # GitHub API + baidu_qianfan.py # Baidu Qianfan API (needs key) + parallel_search.py # Parallel orchestrator +``` ## Engines -| Engine | Type | Priority | -|--------|------|----------| -| Bing | Web (cn.bing.com / www.bing.com) | Primary | -| Sogou | Web (cn.sogou.com) | Primary | -| Baidu Qianfan | API (official) | Backup | -| BingNews | News | Primary | -| ArXiv | Academic | Primary | -| CrossRef | Academic | Primary | +### Web (无需 API key) +| Engine | URL | Notes | +|--------|-----|-------| +| Bing CN | cn.bing.com | Primary | +| Bing INT | www.bing.com | Global fallback | +| Sogou | sogou.com | Primary CN | +| Baidu | baidu.com | Primary CN | +| 360 | so.com | CN fallback | +| DuckDuckGo | html.duckduckgo.com | Global | +| Brave | search.brave.com | Global | + +### News +| Engine | Source | +|--------|--------| +| Bing News | bing.com/news | +| RSS (15 feeds) | 36kr, IT之家, 钛媒体, HackerNews, TechCrunch... | + +### Academic (Free APIs) +| Engine | API | +|--------|-----| +| ArXiv | export.arxiv.org/api/query | +| CrossRef | api.crossref.org | + +### Social +| Engine | Notes | +|--------|-------| +| Nitter | twitter.com 免登录 (多个实例自动切换) | + +### API (需配置密钥) +| Engine | Env var | +|--------|---------| +| Baidu Qianfan | `BAIDU_API_KEY` | + +## SearchResult Model + +```python +@dataclass +class SearchResult: + title: str # Article/title text + url: str # Direct URL + snippet: str # Summary text (~300 chars) + source: str # Engine name (e.g. "bing", "arxiv") + category: str # "web" | "news" | "academic" | "github" + rank: int # Position after deduplication + published: str # ISO date (optional) + extra: dict # Engine-specific metadata +``` diff --git a/SKILL.md b/SKILL.md index cc54da1..e200172 100644 --- a/SKILL.md +++ b/SKILL.md @@ -1,200 +1,140 @@ --- -name: web-search-skills -description: "Multi-source web search CLI & library, covering web, news, WeChat, academic, social, and RSS feeds across 28 engines. Use when the user asks to search the web, find WeChat articles, look up news, search academic papers, or check social media." -version: 2.2.0 +name: web-search +description: "Multi-source web search — 15+ engines (web, news, academic, social, RSS) running concurrently in <5s. No API key needed for most engines. Use when user asks to search the web, find news, look up papers, check social media, or search WeChat." +version: 3.0.0 license: MIT -compatibility: Requires Python 3.8+, httpx, beautifulsoup4, lxml +compatibility: Python 3.10+, httpx, beautifulsoup4, lxml metadata: author: openbot-coder - category: development - tags: [search, web, wechat, news, academic, social, rss, cli, concurrent] + category: search + tags: [search, web, news, academic, social, rss, cli, async] --- # Web Search Skills -## Overview - -A CLI (`web-search`) and Python library that queries **28 search engines** concurrently via `asyncio.gather`. Supports web search, news (Hacker News, GitHub Trending, 36氪, 微博热搜, V2EX, RSS feeds), WeChat articles, ArXiv papers, Twitter/X, and Baidu Qianfan AI search. - -All engines run in parallel with `Semaphore(5)` concurrency control and 15s per-engine timeout. - ## Triggers -When the user says any of these, use this skill: +Use this skill when the user says: -| Intent | Example user phrases | -|--------|---------------------| +| Intent | Example | +|--------|---------| | Web search | "搜索一下 xxx", "search the web for xxx" | -| WeChat articles | "搜微信公众号 xxx", "search WeChat for xxx" | -| News | "查一下新闻", "搜索最新的科技新闻", "search news about xxx" | -| Academic | "搜论文 xxx", "search papers about xxx", "查学术资料" | -| Social media | "看看 Twitter 上关于", "check Twitter for xxx" | -| Specific source | "看看 Hacker News 上关于", "what's trending on GitHub" | -| Hot topics | "查一下微博热搜", "check Weibo trending" | +| News | "查一下新闻", "搜索最新的科技新闻" | +| Academic | "搜论文 xxx", "查学术资料" | +| WeChat | "搜微信公众号 xxx" | +| Social | "看看 Twitter 上关于" | +| Health check | "检查一下搜索引擎状态" | +| List sources | "有哪些搜索引擎可以用" | -## Agent Decision Tree +## Commands -Use this flow to pick the right command: +```bash +# Full search (all 15 engines in parallel, ~10s) +web-search search "AI news" -n 10 + +# Web only (6 engines, ~5s) +web-search web "Python async" -n 5 + +# News (Bing News + 15 RSS, ~8s) +web-search news "量子计算" -n 5 + +# Academic (ArXiv + CrossRef, ~6s) +web-search academic "machine learning" + +# WeChat (Sogou, ~5s) +web-search wechat 蒙面财经 + +# Social (Nitter, ~5s) +web-search social "AI regulation" -n 10 + +# RSS feeds only +web-search rss "AI newsletter" -n 5 + +# Baidu Qianfan API (needs BAIDU_API_KEY) +web-search baidu "中美元首会晤" + +# Engine health check +web-search health + +# List all sources +web-search sources +``` + +## Decision Tree ``` 用户说了什么? │ -├─ "微信公众号" / "WeChat" / 公众号名称 +├─ "微信公众号" / "wechat" │ → web-search wechat │ ├─ "学术" / "论文" / "paper" / "ArXiv" │ → web-search academic │ -├─ "Twitter" / "X" / "社交" / "social" -│ → web-search social [-n 5] +├─ "Twitter" / "X" / "社交" +│ → web-search social │ -├─ "新闻" / "news" / 明确指定 RSS/博文/播客 -│ → web-search news [-n 5] +├─ "新闻" / "news" +│ → web-search news │ -├─ 明确指定了特定源名称 -│ → web-search search -s "源名1" "源名2" [-n 5] -│ -├─ 需要多类源对比(如"新闻+社交上关于伊朗的看法") -│ → web-search search -s news social [-n 10] -│ -├─ 需要百度搜索 + 站点/时间过滤 -│ → web-search baidu [--site xxx] [--recency week] -│ -├─ "健康检查" / "health" / "测试引擎" +├─ "健康检查" / "health" │ → web-search health │ -├─ "列出源" / "sources" / "有哪些搜索引擎" +├─ "有哪些源" / "sources" │ → web-search sources │ -├─ 其余通用查询 -│ → web-search search [-s 源1 源2...] [-n 10] -│ -└─ 不确定 / 没特别要求 +└─ 通用搜索 → web-search search -n 10 ``` -## Quick Command Reference - -| Command | Speed | Best for | -|---------|-------|----------| -| `search` | 并发 28 源 | 通用搜索、多源对比 | -| `web` | 14 网页引擎 | 普通网页搜索 | -| `baidu` | 单引擎 | 需 site/recency 过滤 | -| `news` | ~10+ 新闻源 | 科技/财经/博客/播客 | -| `wechat` | 单引擎 | 微信公众号独家内容 | -| `academic` | 单引擎 | 学术论文 | -| `social` | 2 源 (Twitter) | 社交媒体动态 | -| `rss` | 18 订阅源 | AI 周报/深度文章/播客 | -| `health` | 28 源并发 (~14s) | 检测连通性 | - -**通用选项:** `-n N`(每源结果数), `-j`(JSON), `-o FILE`(存文件), `-s NAME...`(指定源), `-v`(调试日志) - -## Result Field Reference - -Each `SearchResult` returned has these fields: - -| Field | Type | Description | -|-------|------|-------------| -| `rank` | int | Position in results (1-based) | -| `title` | str | Article/title text | -| `url` | str | Direct URL to source | -| `snippet` | str | Summary text (first ~300 chars) | -| `source` | str | Engine name (e.g. "Hacker News") | -| `category` | str | "web", "news", "academic", "social", etc. | -| `extra` | dict | Engine-specific metadata (see below) | - -### `extra` fields by source - -| Source | extra fields | -|--------|-------------| -| Hacker News | `points`, `author`, `time` (ISO date) | -| GitHub Trending | `language`, `stars today` (heat) | -| WeChat | `account` (公众号名), `time` | -| Twitter | `time`, `account`, `search_type` | -| RSS feeds | `time`, `feed_url` | -| Cls / WallStreetCN | `search_url` (JS-rendered fallback) | -| Baidu Qianfan | `site`, `recency` (if filtered) | - -## Error Handling - -| Symptom | Likely cause | Action | -|---------|-------------|--------| -| Empty results from Hacker News/GitHub | Network issue or no matches | Retry with different query | -| Baidu Qianfan returns nothing | Missing `BAIDU_API_KEY` env | Check env var is set | -| DuckDuckGo times out (15s) | Anti-scraping / rate limit | Note to user, skip | -| WeChat empty results | Sogou anti-scraping | Inform user it may be limited | -| `web-search health` shows [TIMEOUT] | Engine unreachable from this region | Not critical, skip that engine | -| GBK encode error on Windows | Non-ASCII char in output | Already auto-fixed by `_sanitize_for_console` | - -**General rule:** If a specific source fails, note it but don't fail the whole search — other sources continue concurrently. - -## Usage Examples - -```bash -# Full search (all 28 sources, concurrent) -web-search search "quantum computing" -n 10 - -# Specific sources only -web-search search "technology" -s "Hacker News" "V2EX" "GitHub Trending" -n 3 - -# Multiple categories -web-search search "Iran nuclear" -s web news social -n 5 - -# Baidu with site filter -web-search baidu "中美元首会晤" --site mp.weixin.qq.com --recency week - -# All web engines -web-search web "Python async" -r global -j - -# Quick searches -web-search news "人工智能" -n 5 -web-search wechat 蒙面财经 -web-search academic "machine learning" -web-search social "AI regulation" -n 5 - -# Concurrent health check (~14s for 28 engines) -web-search health -``` - -## Python API (for programmatic use) - -> **Tip:** For Agent use, prefer invoking the `web-search` CLI command directly (see examples above). -> The API import below only works when running from the project root directory. +## Python API ```python from scripts.unified_search import UnifiedSearch async def main(): us = UnifiedSearch() - results = await us.search( + resp = await us.search( "quantum computing", - sources=["Hacker News", "ArXiv"], + sources=["bing", "arxiv"], # or None for all max_results=5, ) - for r in results: - print(f"[{r.rank}] {r.title} ({r.source})") - if r.extra: - print(f" extra: {r.extra}") + for r in resp.results: + print(f"[{r.rank}] {r.title}") + print(f" {r.url}") + print(f" {r.snippet[:100]}") + print(f"Engines used: {resp.metadata.engines_used}") + print(f"Duration: {resp.metadata.duration_ms}ms") await us.close() ``` -## Important Notes +## SearchResult Fields -### Installation -The `web-search` CLI must be installed first before use: -```bash -cd web-search-skills -pip install -e . -``` -This registers `web-search` as a global command. Requires Python 3.8+. +| Field | Type | Description | +|-------|------|-------------| +| `rank` | int | Position after deduplication (1-based) | +| `title` | str | Article/title text | +| `url` | str | Direct URL | +| `snippet` | str | Summary (~300 chars) | +| `source` | str | Engine name (e.g. "bing", "arxiv") | +| `category` | str | "web" \| "news" \| "academic" \| "github" | +| `published` | str | ISO date (optional) | +| `extra` | dict | Engine-specific metadata | -### Known Limitations +## Error Handling -- **WeChat** via Sogou has anti-scraping limits, may return empty -- **财联社** uses public API (telegraphList) — returns parsed real-time news -- **华尔街见闻** need JS rendering → returns search page URL, not parsed results -- **Baidu Qianfan** requires `BAIDU_API_KEY` env var -- **RSS feeds** need stable internet -- **Windows GBK** terminals: special chars auto-replaced by `_sanitize_for_console()` +| Symptom | Cause | Action | +|---------|-------|--------| +| Empty Bing News | Anti-scraping / region | Not critical, skip | +| Baidu Qianfan empty | Missing `BAIDU_API_KEY` | Set env var | +| Sogou WeChat empty | Sogou anti-scraping | Show URL fallback | +| Nitter all fail | Instance blocked | Show direct twitter.com link | -> Full user-facing documentation (detailed command reference, project structure, config guide, architecture) is in `README.md`. +## Notes + +- Most engines require **no API key** +- Baidu Qianfan needs `BAIDU_API_KEY` env var +- All engines run concurrently via `asyncio.gather` +- Max 5 concurrent engines (semaphore) +- Per-engine timeout: 15s +- Results are deduplicated by URL before ranking diff --git a/scripts/academic/__init__.py b/scripts/academic/__init__.py deleted file mode 100644 index 4da2d65..0000000 --- a/scripts/academic/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Academic search engines.""" diff --git a/scripts/academic/arxiv.py b/scripts/academic/arxiv.py deleted file mode 100644 index da62a87..0000000 --- a/scripts/academic/arxiv.py +++ /dev/null @@ -1,130 +0,0 @@ -""" -ArXiv academic paper search engine. - -Uses the ArXiv API (export.arxiv.org/api/query) to search for papers. -""" - -from __future__ import annotations - -import logging -import xml.etree.ElementTree as ET -from datetime import datetime, timezone -from typing import Optional -from urllib.parse import quote_plus - -import httpx - -from scripts.core.base import SearchEngine, SearchResult - -logger = logging.getLogger(__name__) - - -class ArXivEngine(SearchEngine): - """ArXiv academic paper search engine via official API.""" - - API_URL = "https://export.arxiv.org/api/query" - - def __init__(self, config: Optional[dict] = None): - super().__init__("ArXiv", config) - timeout = (config or {}).get("timeout", 30) - self._client = httpx.AsyncClient( - timeout=timeout, - headers={ - "User-Agent": (config or {}).get( - "user_agent", - "ArXivSearchSkill/1.0", - ), - "Accept": "application/atom+xml", - }, - follow_redirects=True, - ) - - def build_url(self, query: str, max_results: int = 10, **kwargs) -> str: - search_field = kwargs.get("search_field", "all") - return ( - f"{self.API_URL}?search_query={search_field}:{quote_plus(query)}" - f"&max_results={max_results}&sortBy=submittedDate&sortOrder=descending" - ) - - async def search( - self, query: str, max_results: int = 10, **kwargs - ) -> list[SearchResult]: - """Search ArXiv papers.""" - url = self.build_url(query, max_results=max_results, **kwargs) - try: - response = await self._client.get(url) - response.raise_for_status() - return self._parse_response(response.text, max_results) - except httpx.HTTPError as e: - logger.error(f"ArXiv search failed: {e}") - return [] - - def _parse_response(self, xml_text: str, max_results: int) -> list[SearchResult]: - """Parse ArXiv Atom XML response.""" - ns = { - "atom": "http://www.w3.org/2005/Atom", - "arxiv": "http://arxiv.org/schemas/atom", - } - results = [] - - try: - root = ET.fromstring(xml_text) - except ET.ParseError as e: - logger.error(f"ArXiv XML parse error: {e}") - return [] - - for i, entry in enumerate(root.findall("atom:entry", ns)[:max_results]): - title = entry.findtext("atom:title", "", ns) - summary = entry.findtext("atom:summary", "", ns) - published = entry.findtext("atom:published", "", ns) - updated = entry.findtext("atom:updated", "", ns) - - link_el = entry.find("atom:link", ns) - link = link_el.get("href", "") if link_el is not None else "" - - authors = [] - for author in entry.findall("atom:author", ns): - name = author.findtext("atom:name", "", ns) - if name: - authors.append(name) - - # Clean title and summary - title = title.replace("\n", " ").strip() - snippet = summary.replace("\n", " ").strip()[:300] - - parsed_date = self._parse_date(published) if published else ( - self._parse_date(updated) if updated else - datetime.min.replace(tzinfo=timezone.utc) - ) - - results.append( - SearchResult( - title=title or f"ArXiv: {summary[:60]}", - url=link, - snippet=snippet or "No abstract available", - source=self.name, - rank=i + 1, - category="academic", - extra={ - "published": published or "", - "updated": updated or "", - "authors": ", ".join(authors), - "parsed_date": parsed_date, - }, - ) - ) - - return results - - def _parse_date(self, date_str: str) -> datetime: - """Parse ArXiv date format.""" - try: - # ArXiv format: 2024-01-01T00:00:00Z - return datetime.strptime( - date_str.strip(), "%Y-%m-%dT%H:%M:%SZ" - ).replace(tzinfo=timezone.utc) - except (ValueError, TypeError): - return datetime.min.replace(tzinfo=timezone.utc) - - async def close(self): - await self._client.aclose() diff --git a/scripts/cli.py b/scripts/cli.py index 066f72f..33aeb63 100644 --- a/scripts/cli.py +++ b/scripts/cli.py @@ -1,48 +1,29 @@ #!/usr/bin/env python3 """ -Web Search Skills CLI — search across 28 sources from the command line. +Web Search Skills CLI — unified entry point for all search commands. Usage: - web-search [options] + web-search [options] + python scripts/cli.py [options] Commands: - search Search all 28 sources in parallel (default) - web Web search engines (Google, Baidu, DuckDuckGo, etc.) - baidu Baidu Qianfan AI Search API (需配置 BAIDU_API_KEY) - news News (Hacker News, GitHub Trending, 36氪, RSS, etc.) - wechat WeChat public account articles (via Sogou) - academic Academic papers (ArXiv) - social Social media (Twitter/X) - rss RSS feeds (AI newsletters, essays, podcasts) - health Test connectivity of all 28 engines (concurrent) - sources List all available sources - urls Generate search URLs without executing - -Options: - -n, --max-results N Max results per source (default: 10) - -j, --json Output as JSON - -o, --output FILE Save results to file - -s, --source NAME Specific source name(s), support multiple - -r, --region REGION Region filter: cn / global (web only) - --site SITE Restrict to site (baidu only, e.g. mp.weixin.qq.com) - --recency RECENCY Time filter: day/week/month/semiyear/year (baidu only) - -v, --verbose Show debug logs - -h, --help Show this help + search Full multi-source search (all ~16 engines in parallel) + web Web search engines only (Bing, Sogou, Baidu, 360, DuckDuckGo, Brave) + news News search (Bing News + 15 RSS feeds in parallel) + wechat WeChat public account search (via Sogou) + academic Academic paper search (ArXiv + CrossRef in parallel) + social Social media search (Twitter/X via Nitter) + rss RSS feed aggregation only + baidu Baidu Qianfan API (requires BAIDU_API_KEY env) + health Check all engine connectivity + sources List all available sources Examples: - web-search search "量子计算" -o results.json - web-search search "AI" -s web news -n 5 - web-search search "technology" -s "Hacker News" "V2EX" -n 3 - web-search web "Python async" -r global -j - web-search baidu "今日新闻" --recency week - web-search baidu "中美元首会晤" --site mp.weixin.qq.com + web-search search "quantum computing" -n 10 web-search news "人工智能" -n 5 + web-search academic "machine learning" web-search wechat 蒙面财经 - web-search academic "quantum computing" - web-search social "Iran nuclear" -n 5 web-search health - web-search sources - web-search urls "机器学习" """ from __future__ import annotations @@ -53,659 +34,572 @@ import json import logging import os import sys -from datetime import datetime +import time from typing import Any -# Ensure scripts package is importable -_SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) -_PROJECT_ROOT = os.path.dirname(_SCRIPT_DIR) -if _PROJECT_ROOT not in sys.path: - sys.path.insert(0, _PROJECT_ROOT) +from scripts.core.base import BaseEngine, SearchResult +from scripts.engines.bing_scraper import BingScraper +from scripts.engines.sogou_scraper import SogouScraper +from scripts.engines.baidu_scraper import BaiduScraper +from scripts.engines.search360_scraper import Search360Scraper +from scripts.engines.parser_engines import DuckDuckGoParser, BraveParser +from scripts.engines.news_engine import NewsSearch +from scripts.engines.academic_engine import AcademicSearch +from scripts.engines.github_engine import GitHubEngine +from scripts.core.aggregator import ResultAggregator + +# ── Logging ────────────────────────────────────────────────────────────────── logging.basicConfig( - level=logging.WARNING, - format="%(levelname)s: %(message)s", + level=logging.INFO, + format="%(asctime)s [%(levelname)s] %(name)s: %(message)s", + datefmt="%H:%M:%S", ) -logger = logging.getLogger("cli") - -# 执行日志模块 -from scripts.core.execution_log import execution_log +logger = logging.getLogger("web-search") -# --------------------------------------------------------------------------- -# Lazy engine imports (import on demand to keep startup fast) -# --------------------------------------------------------------------------- +# ── WeChat ──────────────────────────────────────────────────────────────────── -def _get_unified(): - from scripts.unified_search import UnifiedSearch - return UnifiedSearch - - -def _get_parallel(): - from scripts.engines.parallel_search import ParallelSearch - return ParallelSearch - - -def _get_sogou(): - from scripts.wechat.sogou_weixin import SogouWeChatEngine - return SogouWeChatEngine - - -def _get_rss(): - from scripts.news.rss import RSSNewsEngine - return RSSNewsEngine - - -def _get_twitter(): - from scripts.social.twitter import TwitterSearchEngine - return TwitterSearchEngine - - -def _get_ddg(): - from scripts.engines.parser_engines import DuckDuckGoEngine - return DuckDuckGoEngine - - -def _get_config_loader(): - from scripts.core.config_loader import ( - get_search_engines, - get_news_sources, - get_academic_sources, - get_wechat_sources, - get_social_sources, - get_special_engines, - get_all_sources, - list_source_categories, +async def cmd_wechat(args: argparse.Namespace) -> None: + """Search WeChat public accounts via Sogou.""" + # Sogou WeChat search — simple URL-based approach (no JS rendering needed) + from urllib.parse import quote_plus + url = ( + f"https://weixin.sogou.com/weixin?type=2&query={quote_plus(args.query)}" + f"&s_from=input&_sug_=n&_sug_type_=" ) - return { - "get_search_engines": get_search_engines, - "get_news_sources": get_news_sources, - "get_academic_sources": get_academic_sources, - "get_wechat_sources": get_wechat_sources, - "get_social_sources": get_social_sources, - "get_special_engines": get_special_engines, - "get_all_sources": get_all_sources, - "list_source_categories": list_source_categories, + headers = { + "User-Agent": ("Mozilla/5.0 (Windows NT 10.0; Win64; x64) " + "AppleWebKit/537.36 (KHTML, like Gecko) " + "Chrome/125.0.0.0 Safari/537.36"), + "Accept-Language": "zh-CN,zh;q=0.9", + "Referer": "https://weixin.sogou.com/", } + try: + import httpx + async with httpx.AsyncClient(timeout=15.0, follow_redirects=True) as client: + resp = await client.get(url, headers=headers) + resp.raise_for_status() -# --------------------------------------------------------------------------- -# Result formatting -# --------------------------------------------------------------------------- + from bs4 import BeautifulSoup + soup = BeautifulSoup(resp.text, "html.parser") + items = soup.select("div.txt-box") -def _sanitize_for_console(text: str) -> str: - """Remove or replace characters that can't be encoded in Windows GBK console.""" - _REPLACE = { - "\u2022": "*", # bullet - "\u2023": ">", # triangular bullet - "\u25B6": ">", # black right-pointing triangle - "\u25C0": "<", # black left-pointing triangle - "\u2713": "v", # check mark - "\u2714": "v", # heavy check mark - "\u2192": "->", # rightwards arrow - "\u2190": "<-", # leftwards arrow - "\u2014": "--", # em dash - "\u2013": "-", # en dash - "\u2018": "'", # left single quote - "\u2019": "'", # right single quote - "\u201c": '"', # left double quote - "\u201d": '"', # right double quote - "\u2026": "...", # ellipsis - "\u00a0": " ", # non-breaking space - } - for old, new in _REPLACE.items(): - text = text.replace(old, new) - # Remove any remaining non-BMP characters or chars outside printable ASCII range - # that GBK cannot encode - result = [] - for ch in text: + if not items: + items = soup.select("div.weixin-rss-item") + if not items: + # Fallback: just show the search URL + print(f"\n🔍 微信搜索: {args.query}") + print(f" 访问: {url}") + print(f" (Sogou 反爬限制,无法直接解析内容)") + return + + results = [] + for item in items[: args.max_results]: + title_el = item.select_one("h3") or item.select_one("a.title") + title = title_el.get_text(strip=True) if title_el else "" + link_el = item.select_one("a") or item.select_one("h3 a") + link = link_el.get("href", "") if link_el else "" + if link and not link.startswith("http"): + link = "https://weixin.sogou.com" + link + account_el = item.select_one(".account") + account = account_el.get_text(strip=True) if account_el else "" + snippet_el = item.select_one("p") + snippet = snippet_el.get_text(strip=True)[:200] if snippet_el else "" + + if title: + results.append( + f" [{len(results)+1}] {title}\n" + f" 公众号: {account}\n" + f" {link}" + f"{' | ' + snippet if snippet else ''}" + ) + + print(f"\n🔍 微信搜索: {args.query} ({len(results)} 条结果)\n") + for r in results: + print(r) + + except Exception as e: + print(f"\n🔍 微信搜索: {args.query}") + print(f" Sogou 反爬限制,访问: {url}") + print(f" (错误: {e})") + + +# ── Social ──────────────────────────────────────────────────────────────────── + +async def cmd_social(args: argparse.Namespace) -> None: + """Search Twitter/X via Nitter instances (no auth needed).""" + import httpx + from urllib.parse import quote_plus + + # Try multiple Nitter instances + nitter_instances = [ + "https://nitter.privacydev.net", + "https://nitter.poast.org", + "https://xcancel.com", + ] + query_encoded = quote_plus(args.query) + results = [] + found = False + + for instance in nitter_instances: + url = f"{instance}/search?f=tweets&q={query_encoded}&since=&until=&near=" + headers = { + "User-Agent": ("Mozilla/5.0 (Windows NT 10.0; Win64; x64) " + "AppleWebKit/537.36"), + } try: - ch.encode("gbk") - result.append(ch) - except UnicodeEncodeError: - result.append("?") - return "".join(result) + async with httpx.AsyncClient(timeout=10.0, follow_redirects=True) as client: + resp = await client.get(url, headers=headers) + if resp.status_code != 200: + continue + from bs4 import BeautifulSoup + soup = BeautifulSoup(resp.text, "html.parser") + items = soup.select("div.timeline-item")[: args.max_results] -def _format_result(r, fmt: str = "text") -> str: - """Format a single search result (supports both EngineResult and old models).""" - # Handle EngineResult (new engines) - if hasattr(r, "source") and hasattr(r, "snippet"): - title = getattr(r, "title", "") - url = getattr(r, "url", "") - snippet = getattr(r, "snippet", "") - source = getattr(r, "source", "") - category = getattr(r, "category", "") - extra = getattr(r, "extra", {}) - published = getattr(r, "published", "") - - if fmt == "json": - return json.dumps({ - "title": title, "url": url, "snippet": snippet[:300], - "source": source, "category": category, - "published": published, "extra": extra, - }, ensure_ascii=False, indent=2) - - lines = [_sanitize_for_console(title)] - lines.append(f" URL: {url}") - if snippet: - lines.append(f" {_sanitize_for_console(snippet[:200])}") - extra_parts = [] - if source: - extra_parts.append(f"来源: {source}") - if published: - extra_parts.append(f"时间: {published}") - for k, v in extra.items(): - if k in ("stars", "language") and v: - extra_parts.append(f"{k}: {v}") - if extra_parts: - lines.append(f" ({'; '.join(extra_parts)})") - return "\n".join(lines) - - # Handle old SearchResult format (rank, title, url, snippet, source, category, extra) - if fmt == "json": - return json.dumps({ - "rank": r.rank, - "title": r.title, - "url": r.url, - "snippet": r.snippet[:300], - "source": r.source, - "category": r.category, - "extra": dict(r.extra) if r.extra else {}, - }, ensure_ascii=False, indent=2) - - lines = [_sanitize_for_console(r.title)] - lines.append(f" URL: {r.url}") - if r.snippet: - lines.append(f" {_sanitize_for_console(r.snippet[:200])}") - if r.extra: - extra_parts = [] - for k, v in r.extra.items(): - if k == "account" and v: - extra_parts.append(f"公众号: {v}") - elif k == "time" and v: - extra_parts.append(f"时间: {v}") - elif k == "search_url": + if not items: continue - elif v and k not in ("parsed_date", "note", "feed_url"): - extra_parts.append(f"{k}: {v}") - if extra_parts: - lines.append(f" ({_sanitize_for_console('; '.join(extra_parts))})") - return "\n".join(lines) + + found = True + print(f"\n🐦 Twitter/X 搜索: {args.query} (via {instance})\n") + for item in items: + text_el = item.select_one(".tweet-content") + author_el = item.select_one(".username") + time_el = item.select_one(".tweet-date") + link_el = item.select_one("a") + text = text_el.get_text(strip=True) if text_el else "" + author = author_el.get_text(strip=True) if author_el else "" + tweet_time = time_el.get("title", "") if time_el else "" + link = link_el.get("href", "") if link_el else "" + if link and not link.startswith("http"): + link = instance + link + + if text: + print(f" @{author} · {tweet_time}") + print(f" {text[:200]}") + if link: + print(f" 🔗 {link}") + print() + break + except Exception: + continue + + if not found: + print(f"\n🐦 Twitter/X 搜索: {args.query}") + print(f" Nitter 实例均不可用,请直接访问 twitter.com") + print(f" 查询: https://twitter.com/search?q={query_encoded}") -def _print_results(results, fmt: str = "text"): - """Print search results.""" - if not results: - print(" 未找到结果") +# ── RSS ──────────────────────────────────────────────────────────────────────── + +async def cmd_rss(args: argparse.Namespace) -> None: + """RSS feed aggregation — AI newsletters, tech blogs, podcasts.""" + from scripts.engines.news_engine import RSSNewsEngine + + engine = RSSNewsEngine(timeout=10.0) + results = await engine.search(args.query, max_results=args.max_results) + await _print_results(args, results, f"RSS 订阅源: {args.query}") + + +# ── Baidu Qianfan ───────────────────────────────────────────────────────────── + +async def cmd_baidu(args: argparse.Namespace) -> None: + """Baidu Qianfan AI Search API (requires BAIDU_API_KEY env var).""" + api_key = os.environ.get("BAIDU_API_KEY") or os.environ.get("BAIDU_QIANFAN_API_KEY") + if not api_key: + print("❌ 需要设置 BAIDU_API_KEY 环境变量") + print(" export BAIDU_API_KEY=your_key_here") return - if fmt == "json": - output = json.dumps( - [{ - "rank": r.rank, - "title": r.title, - "url": r.url, - "snippet": r.snippet[:300], - "source": r.source, - "category": r.category, - "extra": dict(r.extra) if r.extra else {}, - } for r in results], - ensure_ascii=False, - indent=2, - ) - print(output) + from scripts.engines.baidu_qianfan import BaiduQianfanEngine + engine = BaiduQianfanEngine(timeout=15.0) + results = await engine.search(args.query, max_results=args.max_results) + await _print_results(args, results, f"Baidu Qianfan: {args.query}") + + +# ── Health ──────────────────────────────────────────────────────────────────── + +async def cmd_health(args: argparse.Namespace) -> None: + """Check connectivity of all engines concurrently.""" + from scripts.engines.parallel_search import ParallelSearch + + ps = ParallelSearch(timeout=10.0) + status = await ps.health() + + if args.json: + print(json.dumps(status, ensure_ascii=False, indent=2)) + return + + print("\n🔧 引擎健康检测\n") + print(f" {'引擎':<20} {'状态':<8} {'详情'}") + print(" " + "─" * 50) + + ok_count = sum(1 for v in status.values() if v.get("status") == "ok") + for name, result in sorted(status.items()): + s = result.get("status", "?") + icon = "✅" if s == "ok" else "❌" + detail = result.get("error", result.get("count", "")) + print(f" {icon} {name:<18} {s:<8} {detail}") + + print(f"\n ✅ 可用: {ok_count}/{len(status)}") + + +# ── Sources ─────────────────────────────────────────────────────────────────── + +async def cmd_sources(args: argparse.Namespace) -> None: + """List all available search sources.""" + sources = { + "🌐 Web (网页)": [ + "Bing CN (cn.bing.com)", + "Bing INT (www.bing.com)", + "Sogou (sogou.com)", + "Baidu (baidu.com)", + "360 (so.com)", + "DuckDuckGo (html.duckduckgo.com)", + "Brave (search.brave.com)", + "Google (www.google.com, via serpapi optional)", + ], + "📰 News (新闻)": [ + "Bing News (cn.bing.com/news)", + "36氪 RSS", + "IT之家 RSS", + "钛媒体 RSS", + "Solidot RSS", + "Hacker News RSS", + "TechCrunch RSS", + "MIT Tech Review RSS", + "Wired RSS", + "The Verge RSS", + ], + "🎓 Academic (学术)": [ + "ArXiv API (export.arxiv.org/api)", + "CrossRef API (api.crossref.org)", + ], + "💬 Social (社交)": [ + "Nitter instances (Twitter/X 免登录)", + ], + "🔧 API (需配置密钥)": [ + "Baidu Qianfan (BAIDU_API_KEY)", + ], + "📱 垂直搜索": [ + "微信公众号 (weixin.sogou.com)", + ], + } + + for category, items in sources.items(): + print(f"\n{category}") + for item in items: + print(f" • {item}") + + +# ── Shared helpers ──────────────────────────────────────────────────────────── + +def _build_engines(timeout: float = 10.0) -> dict[str, BaseEngine]: + return { + "bing": BingScraper(timeout=timeout), + "sogou": SogouScraper(timeout=timeout), + "baidu": BaiduScraper(timeout=timeout), + "360": Search360Scraper(timeout=timeout), + "duckduckgo": DuckDuckGoParser(timeout=timeout), + "brave": BraveParser(timeout=timeout), + } + + +def _build_vertical_engines(timeout: float = 10.0) -> dict[str, BaseEngine]: + return { + "news": NewsSearch(timeout=timeout), + "academic": AcademicSearch(timeout=timeout), + "github": GitHubEngine(timeout=timeout), + } + + +async def _run_engines( + engines: dict[str, BaseEngine], + query: str, + max_results: int, + region: str = "cn", +) -> list[SearchResult]: + """Run engines in parallel with asyncio.gather.""" + import asyncio + + kwargs = {"region": region} if region else {} + + async def run_one(name: str, eng: BaseEngine) -> tuple[str, list[SearchResult]]: + try: + results = await asyncio.wait_for( + eng.search(query, max_results=max_results, **kwargs), + timeout=eng.timeout + 2.0, + ) + return name, list(results) + except asyncio.TimeoutError: + logger.warning(f"[{name}] timeout after {eng.timeout}s") + return name, [] + except Exception as e: + logger.warning(f"[{name}] failed: {e}") + return name, [] + + tasks = [run_one(n, e) for n, e in engines.items()] + done = await asyncio.gather(*tasks, return_exceptions=True) + + all_results = [] + for item in done: + if isinstance(item, tuple): + _, results = item + all_results.extend(results) + elif isinstance(item, Exception): + logger.warning(f"engine task exception: {item}") + + return all_results + + +def _format_result(r: SearchResult, index: int) -> str: + extra_parts = [] + if r.published: + extra_parts.append(r.published) + if r.source: + extra_parts.append(r.source) + if r.extra: + keys = list(r.extra.keys())[:2] + extra_parts.extend(str(r.extra[k])[:30] for k in keys if r.extra.get(k)) + + header = f" {index}. [{r.source or r.category}] {r.title}" + url_line = f" {r.url}" + snippet_line = f" {r.snippet[:120]}..." if len(r.snippet) > 120 else f" {r.snippet}" + + return "\n".join(filter(None, [header, url_line, snippet_line])) + + +async def _print_results( + args: argparse.Namespace, + results: list[SearchResult], + title: str, +) -> None: + if not results: + print(f"\n📭 未找到「{args.query}」的相关结果") + return + + if args.json: + output = { + "query": args.query, + "total": len(results), + "results": [ + { + "rank": i + 1, + "title": r.title, + "url": r.url, + "snippet": r.snippet, + "source": r.source, + "category": r.category, + "published": r.published, + } + for i, r in enumerate(results) + ], + } + print(json.dumps(output, ensure_ascii=False, indent=2)) + return + + # Rank results + for i, r in enumerate(results): + r.rank = i + 1 + + print(f"\n🔍 「{args.query}」— {len(results)} 条结果\n") + for i, r in enumerate(results): + print(_format_result(r, i + 1)) + print() + + +# ── Main commands ───────────────────────────────────────────────────────────── + +async def cmd_search(args: argparse.Namespace) -> None: + """Full search across all engines (~16 in parallel).""" + t0 = time.time() + all_results: list[SearchResult] = [] + + if args.sources: + # Run specific sources only + source_map = {**_build_engines(), **_build_vertical_engines()} + selected = {k: v for k, v in source_map.items() if k in args.sources} + if not selected: + print("❌ 未识别任何指定的源") + return + all_results = await _run_engines(selected, args.query, args.max_results, args.region) + elif args.category: + if args.category == "news": + engs = {"news": _build_vertical_engines()["news"]} + elif args.category == "academic": + engs = {"academic": _build_vertical_engines()["academic"]} + elif args.category == "social": + print("\n🐦 请使用: web-search social ") + return + elif args.category == "rss": + print("\n📡 请使用: web-search rss ") + return + else: + engs = _build_engines() + all_results = await _run_engines(engs, args.query, args.max_results, args.region) else: - for r in results: - print(_format_result(r)) + # Run all engines concurrently + web_engs = _build_engines() + vert_engs = _build_vertical_engines() + all_web = await _run_engines(web_engs, args.query, args.max_results, args.region) + all_vert = await _run_engines(vert_engs, args.query, args.max_results, args.region) + all_results = all_web + all_vert + + elapsed = time.time() - t0 + + # Deduplicate and rank + aggregator = ResultAggregator() + merged = aggregator.merge_and_rank(all_results, max_results=args.max_results) + + if args.json: + await _print_results(args, merged, "") + else: + print(f"\n🔍 「{args.query}」— {len(merged)} 条结果 (耗时 {elapsed:.1f}s)\n") + for i, r in enumerate(merged): + print(_format_result(r, i + 1)) print() -# --------------------------------------------------------------------------- -# Async searchers -# --------------------------------------------------------------------------- +async def cmd_web(args: argparse.Namespace) -> None: + """Web search engines only.""" + t0 = time.time() + engines = _build_engines() + results = await _run_engines(engines, args.query, args.max_results, args.region) + elapsed = time.time() - t0 -async def _search_all(query: str, max_results: int, **kwargs) -> list[Any]: - """Parallel search across all general engines.""" - ps = _get_parallel()(timeout=10.0) - region = kwargs.get("region", "cn") - results, stats = await ps.search( - query, engines=None, max_results=max_results, region=region, - ) - logger.debug(f"Stats: {stats}") - return results + aggregator = ResultAggregator() + merged = aggregator.merge_and_rank(results, max_results=args.max_results) + + if args.json: + await _print_results(args, merged, "") + else: + print(f"\n🌐 「{args.query}」— {len(merged)} 条结果 (耗时 {elapsed:.1f}s)\n") + for i, r in enumerate(merged): + print(_format_result(r, i + 1)) + print() -async def _search_web(query: str, max_results: int, region: str = None, **kwargs) -> list[Any]: - """Parallel web search (general engines).""" - ps = _get_parallel()(timeout=10.0) - results, stats = await ps.search( - query, engines=None, max_results=max_results, region=region or "cn", - ) - return results +async def cmd_news(args: argparse.Namespace) -> None: + """News search — Bing News + RSS.""" + from scripts.engines.news_engine import NewsSearch + engine = NewsSearch(timeout=15.0) + results = await engine.search(args.query, max_results=args.max_results, region=args.region) + await _print_results(args, results, "📰 新闻") -async def _search_news(query: str, max_results: int, **kwargs) -> list[Any]: - """Vertical news search.""" - ps = _get_parallel()(timeout=10.0) - results, stats = await ps.search_vertical( - query, "news", max_results=max_results, **kwargs, - ) - return results +async def cmd_academic(args: argparse.Namespace) -> None: + """Academic search — ArXiv + CrossRef.""" + from scripts.engines.academic_engine import AcademicSearch + engine = AcademicSearch(timeout=15.0) + results = await engine.search(args.query, max_results=args.max_results) + await _print_results(args, results, "🎓 学术论文") -async def _search_wechat(query: str, max_results: int) -> list[Any]: - engine = _get_sogou()() - try: - with execution_log.track("weixin", "search", query=query) as ctx: - results = await engine.search(query, max_results=max_results) - ctx.result_count = len(results) - return results - finally: - await engine.close() +# ── CLI entry point ─────────────────────────────────────────────────────────── - -async def _search_academic(query: str, max_results: int) -> list[Any]: - """Vertical academic search (ArXiv + CrossRef).""" - ps = _get_parallel()(timeout=15.0) - results, stats = await ps.search_vertical( - query, "academic", max_results=max_results, - ) - return results - - -async def _search_social(query: str, max_results: int, **kwargs) -> list[Any]: - us = _get_unified()() - try: - with execution_log.track("social", "search", query=query) as ctx: - results = await us.search_social(query, max_results=max_results, **kwargs) - ctx.result_count = len(results) if results else 0 - return results - finally: - await us.close() - - -async def _search_baidu(query: str, max_results: int, **kwargs) -> list[Any]: - from scripts.engines.baidu_qianfan import BaiduQianFanEngine - engine = BaiduQianFanEngine({"api_key": os.getenv("BAIDU_API_KEY", "")}) - try: - with execution_log.track("baidu_qianfan", "search", query=query) as ctx: - results = await engine.search(query, max_results=max_results, **kwargs) - ctx.result_count = len(results) - return results - finally: - await engine.close() - - -async def _search_rss(query: str, max_results: int, **kwargs) -> list[Any]: - us = _get_unified()() - try: - with execution_log.track("rss", "search", query=query) as ctx: - results = await us.search_rss(query, max_results=max_results, **kwargs) - ctx.result_count = len(results) if results else 0 - return results - finally: - await us.close() - - -async def _search_github(query: str, max_results: int, **kwargs) -> list[Any]: - """GitHub code/repo search.""" - ps = _get_parallel()(timeout=10.0) - search_type = kwargs.get("search_type", "repositories") - results, stats = await ps.search_vertical( - query, "github", max_results=max_results, search_type=search_type, - ) - return results - - -async def _get_urls(query: str, sources: list[str] = None) -> dict[str, str]: - us = _get_unified()() - try: - return await us.get_search_urls(query, sources=sources) - finally: - await us.close() - - -# --------------------------------------------------------------------------- -# CLI actions -# --------------------------------------------------------------------------- - -def cmd_log(args): - """查看执行日志历史。""" - import json as json_mod - - log_file = "logs/search_log.jsonl" - try: - with open(log_file, "r", encoding="utf-8") as f: - lines = [json_mod.loads(line) for line in f if line.strip()] - except FileNotFoundError: - print(f"暂无日志文件 ({log_file})") - return - - if not lines: - print("暂无执行记录") - return - - # 按 session 分组 - sessions = {} - for entry in lines: - session = entry.get("session_start", "unknown") - if session not in sessions: - sessions[session] = [] - sessions[session].append(entry) - - print(f"═══════════════════════════════════════════") - print(f" 执行日志历史 (共 {len(lines)} 条记录, {len(sessions)} 个会话)") - print(f"═══════════════════════════════════════════") - - # 只显示最近5个会话 - recent_sessions = list(sessions.items())[-5:] - for session_start, entries in recent_sessions: - total = len(entries) - success = sum(1 for e in entries if e.get("success", True)) - failed = total - success - durations = [e.get("duration_ms", 0) for e in entries] - avg_dur = sum(durations) // total if total else 0 - - print(f"\n 会话: {session_start}") - print(f" 调用: {total} 次 (成功 {success}, 失败 {failed})") - print(f" 平均耗时: {avg_dur}ms") - - # 按引擎分组 - engines = {} - for e in entries: - eng = e.get("engine", "unknown") - if eng not in engines: - engines[eng] = {"calls": 0, "errors": []} - engines[eng]["calls"] += 1 - if not e.get("success", True): - engines[eng]["errors"].append(e.get("error", "unknown")) - - for eng, info in sorted(engines.items()): - err_count = len(info["errors"]) - status = "✓" if err_count == 0 else f"✗ ({err_count} errors)" - print(f" {eng:15s} {status} calls={info['calls']}") - - # 显示错误详情 - for err in info["errors"][:3]: # 最多显示3个 - print(f" └─ {err[:80]}") - - print(f"\n═══════════════════════════════════════════") - - -def cmd_sources(args): - """List all available search sources.""" - cl = _get_config_loader() - categories = cl["list_source_categories"]() - - print("可用搜索源:\n") - for cat_key, cat_name in [ - ("search_engines", "Web 搜索引擎"), - ("news_sources", "新闻源"), - ("academic_sources", "学术源"), - ("wechat_sources", "微信源"), - ("social_sources", "社交源"), - ("api_engines", "API 引擎"), - ("special_engines", "特殊源"), - ]: - count = categories.get(cat_key, 0) - if count == 0: - continue - print(f" {cat_name} ({count} 个)") - - print(f"\n总计: {cl['get_all_sources']().__len__()} 个源") - print("\n详细列表:") - for s in cl["get_all_sources"](): - print(f" - {s['name']:20s} ({s.get('region', '?')}) [{s.get('type', 'url')}]") - - -def cmd_urls(args): - """Get search URLs without executing.""" - urls = asyncio.run(_get_urls(args.query, sources=args.source)) - for name, url in urls.items(): - print(f"{name}:") - print(f" {url}") - print() - - -async def cmd_health(args): - """Test all engine connectivity.""" - ps = _get_parallel()(timeout=10.0) - try: - print("正在检测各引擎连通性...\n") - statuses = await ps.health() - ok_count = sum(1 for s in statuses.values() if s["status"] == "ok") - for name, s in statuses.items(): - if s["status"] == "ok": - print(f" [OK] {name}") - elif s["status"] == "timeout": - print(f" [TIMEOUT] {name}") - else: - detail = s.get("error", s.get("detail", "")) - print(f" [FAIL] {name} {detail}") - print(f"\n总计: {len(statuses)} 个引擎, {ok_count} 个可用") - print() - execution_log.print_summary() - execution_log.save("logs/search_log.jsonl") - finally: - pass - - -async def _run_search(args): - """Route search to the right engine(s).""" - if args.verbose: - logging.getLogger().setLevel(logging.DEBUG) - - method_map = { - "search": _search_all, - "web": _search_web, - "baidu": _search_baidu, - "news": _search_news, - "wechat": _search_wechat, - "academic": _search_academic, - "social": _search_social, - "rss": _search_rss, - "github": _search_github, - } - - search_fn = method_map.get(args.command, _search_all) - - kwargs = {} - if args.command == "web" and args.region: - kwargs["region"] = args.region - if args.command == "baidu": - if args.site: - kwargs["site"] = args.site - if args.recency: - kwargs["recency"] = args.recency - # --source / -s: pass specific source names/categories to search - if args.source: - kwargs["sources"] = args.source - - try: - results = await search_fn( - args.query, - max_results=args.max_results, - **kwargs, - ) - except Exception as e: - print(f"搜索失败: {e}", file=sys.stderr) - if args.verbose: - import traceback - traceback.print_exc() - sys.exit(1) - - # Print header - cmd_names = { - "search": "全源搜索", - "web": "网页搜索", - "baidu": "百度千帆 AI 搜索", - "news": "新闻搜索", - "wechat": "微信公众号搜索", - "academic": "学术搜索", - "social": "社交搜索", - "rss": "RSS 新闻搜索", - "github": "GitHub 搜索", - } - header = cmd_names.get(args.command, "搜索") - print(f"{header}: {args.query}") - print(f"共 {len(results)} 条结果\n") - - _print_results(results, fmt="json" if args.json else "text") - - # 打印执行日志摘要并保存到文件 - print() - execution_log.print_summary() - execution_log.save("logs/search_log.jsonl") - - # Save to file if requested (with basic path-sanitization) - if args.output: - output_data = json.dumps( - [{ - "query": args.query, - "command": args.command, - "timestamp": datetime.now().isoformat(), - "total": len(results), - "results": [ - { - "rank": r.rank, - "title": r.title, - "url": r.url, - "snippet": r.snippet[:500], - "source": r.source, - "category": r.category, - "extra": dict(r.extra) if r.extra else {}, - } - for r in results - ], - }], - ensure_ascii=False, - indent=2, - ) - output_path = os.path.abspath(args.output) - # Restrict output to the project root or current working directory - allowed_base = os.path.abspath(os.getcwd()) - if not output_path.startswith(allowed_base): - print(f"错误: 输出路径超出允许范围 ({allowed_base})", file=sys.stderr) - sys.exit(1) - os.makedirs(os.path.dirname(output_path), exist_ok=True) - with open(output_path, "w", encoding="utf-8") as f: - f.write(output_data) - print(f"\n结果已保存至: {output_path}") - - -# --------------------------------------------------------------------------- -# Argument parser -# --------------------------------------------------------------------------- - -def build_parser() -> argparse.ArgumentParser: +def main() -> None: parser = argparse.ArgumentParser( prog="web-search", - description="Web Search Skills CLI — multi-source search tool", - formatter_class=argparse.RawDescriptionHelpFormatter, - epilog=__doc__, + description="Multi-source web search CLI (28+ engines)", ) + sub = parser.add_subparsers(dest="command", required=True) - parser.add_argument( - "command", - nargs="?", - default="search", - choices=["search", "web", "baidu", "news", "wechat", "academic", - "social", "rss", "github", "health", "sources", "urls", "log"], - help="Search type (default: search)", - ) - parser.add_argument( - "query", - nargs="?", - default="", - help="Search keyword", - ) - parser.add_argument( - "-n", "--max-results", - type=int, - default=10, - help="Max results per source (default: 10)", - ) - parser.add_argument( - "-j", "--json", - action="store_true", - help="Output as JSON", - ) - parser.add_argument( - "-o", "--output", - type=str, - help="Save results to JSON file", - ) - parser.add_argument( - "-s", "--source", - type=str, - nargs="*", - help="Specific source name(s)", - ) - parser.add_argument( - "-r", "--region", - type=str, - choices=["cn", "global"], - help="Region filter (web search only)", - ) - parser.add_argument( - "--site", - type=str, - help="Restrict to specific site (baidu command only, e.g. mp.weixin.qq.com)", - ) - parser.add_argument( - "--recency", - type=str, - choices=["day", "week", "month", "semiyear", "year"], - default=None, - help="Time filter (baidu command only, default: year)", - ) - parser.add_argument( - "-v", "--verbose", - action="store_true", - help="Show debug logs", - ) + # ── search ────────────────────────────────────────────────────────────── + sp = sub.add_parser("search", help="Search all engines (default: all ~16 in parallel)") + sp.add_argument("query", help="Search query") + sp.add_argument("-n", "--max-results", type=int, default=10, help="Max results per engine") + sp.add_argument("-s", "--sources", nargs="+", metavar="SRC", + help="Run specific sources only (e.g. bing sogou news)") + sp.add_argument("-c", "--category", choices=["news", "academic", "social", "rss", "web"], + help="Run engines of a specific category") + sp.add_argument("-r", "--region", default="cn", choices=["cn", "global"], + help="Region: cn (default) or global") + sp.add_argument("-j", "--json", action="store_true", help="JSON output") + sp.add_argument("-v", "--verbose", action="store_true", help="Debug logging") - return parser + # ── web ───────────────────────────────────────────────────────────────── + wp = sub.add_parser("web", help="Web search engines only") + wp.add_argument("query", help="Search query") + wp.add_argument("-n", "--max-results", type=int, default=10) + wp.add_argument("-r", "--region", default="cn", choices=["cn", "global"]) + wp.add_argument("-j", "--json", action="store_true") + wp.add_argument("-v", "--verbose", action="store_true") + # ── news ──────────────────────────────────────────────────────────────── + np_ = sub.add_parser("news", help="News search (Bing News + RSS)") + np_.add_argument("query", help="Search query") + np_.add_argument("-n", "--max-results", type=int, default=10) + np_.add_argument("-r", "--region", default="cn", choices=["cn", "global"]) + np_.add_argument("-j", "--json", action="store_true") -# --------------------------------------------------------------------------- -# Entry point -# --------------------------------------------------------------------------- + # ── academic ───────────────────────────────────────────────────────────── + ap = sub.add_parser("academic", help="Academic papers (ArXiv + CrossRef)") + ap.add_argument("query", help="Search query") + ap.add_argument("-n", "--max-results", type=int, default=10) + ap.add_argument("-j", "--json", action="store_true") + + # ── wechat ────────────────────────────────────────────────────────────── + wcp = sub.add_parser("wechat", help="WeChat public account search (Sogou)") + wcp.add_argument("query", help="Search query") + wcp.add_argument("-n", "--max-results", type=int, default=5) + + # ── social ─────────────────────────────────────────────────────────────── + sop = sub.add_parser("social", help="Social media (Twitter/X via Nitter)") + sop.add_argument("query", help="Search query") + sop.add_argument("-n", "--max-results", type=int, default=10) + + # ── rss ────────────────────────────────────────────────────────────────── + rp = sub.add_parser("rss", help="RSS feed aggregation") + rp.add_argument("query", help="Search query") + rp.add_argument("-n", "--max-results", type=int, default=10) + rp.add_argument("-j", "--json", action="store_true") + + # ── baidu ──────────────────────────────────────────────────────────────── + bp = sub.add_parser("baidu", help="Baidu Qianfan API (requires BAIDU_API_KEY)") + bp.add_argument("query", help="Search query") + bp.add_argument("-n", "--max-results", type=int, default=10) + bp.add_argument("-j", "--json", action="store_true") + + # ── health ─────────────────────────────────────────────────────────────── + hp = sub.add_parser("health", help="Check all engine connectivity") + hp.add_argument("-j", "--json", action="store_true") + + # ── sources ────────────────────────────────────────────────────────────── + sub.add_parser("sources", help="List all available search sources") -def main(): - parser = build_parser() args = parser.parse_args() - if args.verbose: - logging.getLogger().setLevel(logging.DEBUG) + # Verbose logging + if getattr(args, "verbose", False): + logging.getLogger("web-search").setLevel(logging.DEBUG) - # sources, urls, health don't need a query - if args.command == "sources": - cmd_sources(args) - return - - if args.command == "log": - cmd_log(args) - return - - if args.command == "health": - asyncio.run(cmd_health(args)) - return - - if args.command == "urls": - if not args.query: - print("错误: urls 命令需要提供搜索关键词", file=sys.stderr) - sys.exit(1) - cmd_urls(args) - return - - # Other commands need a query - if not args.query: - print(f"错误: {args.command} 命令需要提供搜索关键词", file=sys.stderr) - sys.exit(1) - - asyncio.run(_run_search(args)) + # Run the appropriate command + try: + if args.command == "search": + asyncio.run(cmd_search(args)) + elif args.command == "web": + asyncio.run(cmd_web(args)) + elif args.command == "news": + asyncio.run(cmd_news(args)) + elif args.command == "academic": + asyncio.run(cmd_academic(args)) + elif args.command == "wechat": + asyncio.run(cmd_wechat(args)) + elif args.command == "social": + asyncio.run(cmd_social(args)) + elif args.command == "rss": + asyncio.run(cmd_rss(args)) + elif args.command == "baidu": + asyncio.run(cmd_baidu(args)) + elif args.command == "health": + asyncio.run(cmd_health(args)) + elif args.command == "sources": + asyncio.run(cmd_sources(args)) + except KeyboardInterrupt: + print("\n\n⏹ 已取消") + sys.exit(130) if __name__ == "__main__": diff --git a/scripts/core/base.py b/scripts/core/base.py index 8465592..8aa801f 100644 --- a/scripts/core/base.py +++ b/scripts/core/base.py @@ -1,48 +1,69 @@ """ -Core data types and base classes for search engines. +Unified data models — single source of truth for all search results. -Provides: -- SearchResult: Standard result dataclass used across all engines -- SearchEngine: Abstract base class for all search engine implementations +Defines: + - SearchResult: unified result from any engine (immutable dataclass) + - BaseEngine: abstract base for all engines + - rank field: assigned by aggregator, not engines + +All engine-specific models must convert to SearchResult before returning. """ from __future__ import annotations from abc import ABC, abstractmethod from dataclasses import dataclass, field -from typing import Any, Optional +from typing import Any @dataclass class SearchResult: - """Represents a single search result.""" - title: str = "" - url: str = "" + """Unified search result — returned by all engines.""" + title: str + url: str snippet: str = "" - source: str = "" - rank: int = 0 - category: str = "" + source: str = "" # engine name (e.g. "bing", "arxiv") + category: str = "" # "web" | "news" | "academic" | "github" | "social" + rank: int = 0 # assigned by aggregator, not engine + published: str = "" # ISO date string (optional) extra: dict[str, Any] = field(default_factory=dict) + def to_dict(self) -> dict[str, Any]: + return { + "title": self.title, + "url": self.url, + "snippet": self.snippet, + "source": self.source, + "category": self.category, + "rank": self.rank, + "published": self.published, + "extra": self.extra, + } -class SearchEngine(ABC): - """Abstract base class for search engines.""" - def __init__(self, name: str, config: Optional[dict[str, Any]] = None): - self.name = name - self.config = config or {} +class BaseEngine(ABC): + """Abstract base for all search engines. + + Subclasses override `search()`, which returns list[SearchResult]. + `health()` provides a quick connectivity check. + """ + + name: str = "unknown" + timeout: float = 15.0 + + def __init__(self, timeout: float = 15.0): + self.timeout = timeout @abstractmethod - async def search( - self, query: str, max_results: int = 10, **kwargs - ) -> list[SearchResult]: - """Execute a search and return results.""" + async def search(self, query: str, max_results: int = 10, + **kwargs) -> list[SearchResult]: + """Search and return unified results.""" ... - def build_url(self, query: str, **kwargs) -> str: - """Build a search URL for this engine (optional).""" - return "" - - async def close(self): - """Clean up resources.""" - pass + async def health(self) -> dict[str, Any]: + """Quick connectivity check. Returns {"status": "ok"} or {"status": "fail", "error": "..."}.""" + try: + results = await self.search("test", max_results=1) + return {"name": self.name, "status": "ok", "count": len(results)} + except Exception as e: + return {"name": self.name, "status": "fail", "error": str(e)} diff --git a/scripts/engines/academic_engine.py b/scripts/engines/academic_engine.py index 869a85e..ac70d6f 100644 --- a/scripts/engines/academic_engine.py +++ b/scripts/engines/academic_engine.py @@ -1,4 +1,4 @@ -"""Academic search engine — ArXiv + CrossRef APIs (free, no key).""" +"""Academic search — ArXiv + CrossRef APIs (free, no API key).""" from __future__ import annotations @@ -9,18 +9,18 @@ from xml.etree import ElementTree as ET import httpx -from scripts.engines.base import BaseEngine, EngineResult +from scripts.core.base import BaseEngine, SearchResult logger = logging.getLogger(__name__) class ArxivEngine(BaseEngine): - """ArXiv paper search — free, no key needed.""" + """ArXiv paper search via public API.""" name = "arxiv" async def search(self, query: str, max_results: int = 10, - **kwargs) -> list[EngineResult]: + **kwargs) -> list[SearchResult]: t0 = time.time() url = ( f"http://export.arxiv.org/api/query?" @@ -40,8 +40,12 @@ class ArxivEngine(BaseEngine): logger.warning(f"[arxiv] failed: {e}") return [] - def _parse_xml(self, xml_text: str, max_results: int) -> list[EngineResult]: - root = ET.fromstring(xml_text) + def _parse_xml(self, xml_text: str, max_results: int) -> list[SearchResult]: + try: + root = ET.fromstring(xml_text) + except ET.ParseError: + return [] + ns = {"atom": "http://www.w3.org/2005/Atom"} results = [] @@ -53,14 +57,11 @@ class ArxivEngine(BaseEngine): link_el = entry.find("atom:id", ns) url = link_el.get("href", "") if link_el is not None else "" published = entry.findtext("atom:published", "", ns) - - authors = [a.findtext("atom:name", "", ns) - for a in entry.findall("atom:author", ns)] - categories = [c.get("term", "") - for c in entry.findall("atom:category", ns)] + authors = [a.findtext("atom:name", "", ns) for a in entry.findall("atom:author", ns)] + categories = [c.get("term", "") for c in entry.findall("atom:category", ns)] if title: - results.append(EngineResult( + results.append(SearchResult( title=title, url=url, snippet=summary[:300], source="arxiv", category="academic", published=published, @@ -71,20 +72,16 @@ class ArxivEngine(BaseEngine): class CrossRefEngine(BaseEngine): - """CrossRef academic search — free, no key needed.""" + """CrossRef academic search via public API.""" name = "crossref" async def search(self, query: str, max_results: int = 10, - **kwargs) -> list[EngineResult]: + **kwargs) -> list[SearchResult]: t0 = time.time() url = "https://api.crossref.org/works" - params = { - "query": query, - "rows": min(max_results, 20), - "sort": "relevance", - "order": "desc", - } + params = {"query": query, "rows": min(max_results, 20), + "sort": "relevance", "order": "desc"} headers = {"User-Agent": "web-search-skills/1.0 (mailto:search@example.com)"} try: @@ -109,7 +106,7 @@ class CrossRefEngine(BaseEngine): for a in item.get("author", [])] if title: - results.append(EngineResult( + results.append(SearchResult( title=title, url=url_link, snippet=abstract, source="crossref", category="academic", published=pub_date, @@ -134,7 +131,7 @@ class AcademicSearch(BaseEngine): name = "academic" async def search(self, query: str, max_results: int = 10, - **kwargs) -> list[EngineResult]: + **kwargs) -> list[SearchResult]: import asyncio arxiv = ArxivEngine(timeout=self.timeout) crossref = CrossRefEngine(timeout=self.timeout) @@ -151,7 +148,6 @@ class AcademicSearch(BaseEngine): if isinstance(crossref_results, list): all_results.extend(crossref_results) - # Deduplicate by URL seen = set() deduped = [] for r in all_results: diff --git a/scripts/engines/baidu_qianfan.py b/scripts/engines/baidu_qianfan.py index 3ef4272..a8ec827 100644 --- a/scripts/engines/baidu_qianfan.py +++ b/scripts/engines/baidu_qianfan.py @@ -18,7 +18,7 @@ from typing import Any, Optional import httpx -from scripts.core.base import SearchEngine, SearchResult +from scripts.core.base import BaseEngine, SearchResult logger = logging.getLogger(__name__) @@ -51,18 +51,18 @@ def _get_api_key() -> str: return env_from_file.get(ENV_KEY, "") -class BaiduQianFanEngine(SearchEngine): +class BaiduQianFanEngine(BaseEngine): """百度千帆 AI 搜索 API 引擎.""" - def __init__(self, config: Optional[dict] = None): - super().__init__("Baidu Qianfan", config) + name = "baidu_qianfan" + + def __init__(self, config: Optional[dict] = None, timeout: float = 30.0): + super().__init__(timeout=timeout) self.api_key = (config or {}).get("api_key", "") or _get_api_key() if not self.api_key: logger.warning( - f"BAIDU_API_KEY not set! Set it in .env file or as environment variable." + "BAIDU_API_KEY not set. Set it in .env or as environment variable." ) - - timeout = (config or {}).get("timeout", 30) self._client = httpx.AsyncClient( timeout=timeout, headers={ @@ -76,17 +76,7 @@ class BaiduQianFanEngine(SearchEngine): async def search( self, query: str, max_results: int = 10, **kwargs ) -> list[SearchResult]: - """Search via Baidu Qianfan AI Search API. - - Args: - query: Search keywords. - max_results: Max results to return. - **kwargs: - site: Restrict search to a specific site (e.g. "mp.weixin.qq.com"). - recency: Time filter: "week", "month", "semiyear", "year" (default: "year"). - edition: "standard" (default) or "lite". - safe_search: bool, default False. - """ + """Search via Baidu Qianfan AI Search API.""" if not self.api_key: return [ SearchResult( @@ -100,19 +90,14 @@ class BaiduQianFanEngine(SearchEngine): ) ] - # Build resource_type_filter based on max_results top_k = min(max_results, 50) - resource_type_filter = [ - {"type": "web", "top_k": top_k}, - ] + resource_type_filter = [{"type": "web", "top_k": top_k}] - # Build search_filter if site is specified - search_filter = {} - site = kwargs.get("site") - if site: - search_filter["match"] = {"site": [site]} + search_filter: dict[str, Any] = {} + if kwargs.get("site"): + search_filter["match"] = {"site": [kwargs["site"]]} - request_body = { + request_body: dict[str, Any] = { "messages": [{"content": query, "role": "user"}], "edition": kwargs.get("edition", "standard"), "search_source": "baidu_search_v2", @@ -134,38 +119,24 @@ class BaiduQianFanEngine(SearchEngine): logger.error(f"Baidu Qianfan API error: {data.get('message', '')}") return [] - references = data.get("references", []) results = [] - for i, ref in enumerate(references[:max_results]): - results.append( - SearchResult( - title=ref.get("title", ""), - url=ref.get("url", ""), - snippet=ref.get("snippet", ""), - source=self.name, - rank=i + 1, - category="web", - extra={ - k: ref[k] - for k in ("site", "date", "type", "source") - if k in ref - }, - ) - ) + for i, ref in enumerate(data.get("references", [])[:max_results]): + results.append(SearchResult( + title=ref.get("title", ""), + url=ref.get("url", ""), + snippet=ref.get("snippet", ""), + source=self.name, + rank=i + 1, + category="web", + extra={k: ref[k] for k in ("site", "date", "type", "source") if k in ref}, + )) return results except httpx.HTTPStatusError as e: - logger.error(f"Baidu Qianfan API HTTP error: {e.response.status_code}") - if e.response.status_code == 401: - logger.error("API Key 无效或未授权,请检查 BAIDU_API_KEY") - elif e.response.status_code == 429: - logger.error("API 配额不足") + logger.error(f"Baidu Qianfan HTTP {e.response.status_code}: {e}") return [] except httpx.RequestError as e: - logger.error(f"Baidu Qianfan API request failed: {e}") - return [] - except json.JSONDecodeError as e: - logger.error(f"Baidu Qianfan API response parse failed: {e}") + logger.error(f"Baidu Qianfan request failed: {e}") return [] async def close(self): diff --git a/scripts/engines/baidu_scraper.py b/scripts/engines/baidu_scraper.py index 97b09b4..3dd6f20 100644 --- a/scripts/engines/baidu_scraper.py +++ b/scripts/engines/baidu_scraper.py @@ -9,7 +9,7 @@ from urllib.parse import quote_plus import httpx from bs4 import BeautifulSoup -from scripts.engines.base import BaseEngine, EngineResult +from scripts.core.base import BaseEngine, SearchResult logger = logging.getLogger(__name__) @@ -27,7 +27,7 @@ class BaiduScraper(BaseEngine): name = "baidu" async def search(self, query: str, max_results: int = 10, - **kwargs) -> list[EngineResult]: + **kwargs) -> list[SearchResult]: t0 = time.time() url = f"https://www.baidu.com/s?wd={quote_plus(query)}&rn={max_results}" @@ -43,7 +43,7 @@ class BaiduScraper(BaseEngine): logger.warning(f"[baidu] failed: {e}") return [] - def _parse(self, html: str, max_results: int) -> list[EngineResult]: + def _parse(self, html: str, max_results: int) -> list[SearchResult]: soup = BeautifulSoup(html, "html.parser") results = [] @@ -60,7 +60,7 @@ class BaiduScraper(BaseEngine): snippet = snippet_el.get_text(strip=True) if snippet_el else "" if title and href: - results.append(EngineResult( + results.append(SearchResult( title=title, url=href, snippet=snippet, source="baidu", )) if len(results) >= max_results: diff --git a/scripts/engines/base.py b/scripts/engines/base.py index 653dd32..ca62336 100644 --- a/scripts/engines/base.py +++ b/scripts/engines/base.py @@ -6,59 +6,7 @@ from abc import ABC, abstractmethod from dataclasses import dataclass, field from typing import Any -from scripts.core.execution_log import execution_log +# Re-export SearchResult/BaseEngine from scripts.core.base for engine code compatibility +from scripts.core.base import BaseEngine, SearchResult - -@dataclass -class EngineResult: - """Unified result from any engine.""" - title: str - url: str - snippet: str = "" - source: str = "" - category: str = "" # "general" | "news" | "academic" | "github" - published: str = "" - extra: dict[str, Any] = field(default_factory=dict) - - -class BaseEngine(ABC): - """Base class for all search engines. - - Automatically tracks execution timing and errors via execution_log. - """ - - name: str = "unknown" - timeout: float = 10.0 - - def __init__(self, timeout: float = 10.0): - self.timeout = timeout - - async def search_with_log( - self, query: str, max_results: int = 10, **kwargs - ) -> list[EngineResult]: - """Search with automatic execution logging. - - Subclasses override `search()`, not this method. - """ - with execution_log.track(self.name, "search", query=query) as ctx: - results = await self.search(query, max_results=max_results, **kwargs) - ctx.result_count = len(results) - return results - - @abstractmethod - async def search(self, query: str, max_results: int = 10, - **kwargs) -> list[EngineResult]: - ... - - async def health(self) -> dict: - """Quick connectivity check.""" - with execution_log.track(self.name, "health") as ctx: - try: - results = await self.search("test", max_results=1) - ctx.result_count = len(results) - return {"name": self.name, "status": "ok", "count": len(results)} - except Exception as e: - ctx.success = False - ctx.error = str(e) - ctx.error_type = type(e).__name__ - return {"name": self.name, "status": "fail", "error": str(e)} +__all__ = ["BaseEngine", "SearchResult"] diff --git a/scripts/engines/bing_scraper.py b/scripts/engines/bing_scraper.py index 1217fbb..459be9a 100644 --- a/scripts/engines/bing_scraper.py +++ b/scripts/engines/bing_scraper.py @@ -10,7 +10,7 @@ from urllib.parse import quote_plus import httpx from bs4 import BeautifulSoup -from scripts.engines.base import BaseEngine, EngineResult +from scripts.core.base import BaseEngine, SearchResult logger = logging.getLogger(__name__) @@ -28,7 +28,7 @@ class BingScraper(BaseEngine): name = "bing" async def search(self, query: str, max_results: int = 10, - region: str = "global", **kwargs) -> list[EngineResult]: + region: str = "global", **kwargs) -> list[SearchResult]: t0 = time.time() url = f"https://cn.bing.com/search?q={quote_plus(query)}&setlang=zh-CN" if region == "global": @@ -46,7 +46,7 @@ class BingScraper(BaseEngine): logger.warning(f"[bing] failed: {e}") return [] - def _parse(self, html: str, max_results: int) -> list[EngineResult]: + def _parse(self, html: str, max_results: int) -> list[SearchResult]: soup = BeautifulSoup(html, "html.parser") results = [] @@ -60,7 +60,7 @@ class BingScraper(BaseEngine): snippet = snippet_el.get_text(strip=True) if snippet_el else "" if title and href: - results.append(EngineResult( + results.append(SearchResult( title=title, url=href, snippet=snippet, source="bing", )) if len(results) >= max_results: diff --git a/scripts/engines/brave_scraper.py b/scripts/engines/brave_scraper.py index 1efa680..64b7493 100644 --- a/scripts/engines/brave_scraper.py +++ b/scripts/engines/brave_scraper.py @@ -9,7 +9,7 @@ from urllib.parse import quote_plus import httpx from bs4 import BeautifulSoup -from scripts.engines.base import BaseEngine, EngineResult +from scripts.core.base import BaseEngine, SearchResult logger = logging.getLogger(__name__) @@ -27,7 +27,7 @@ class BraveScraper(BaseEngine): name = "brave" async def search(self, query: str, max_results: int = 10, - **kwargs) -> list[EngineResult]: + **kwargs) -> list[SearchResult]: t0 = time.time() url = f"https://search.brave.com/search?q={quote_plus(query)}&source=web" @@ -43,7 +43,7 @@ class BraveScraper(BaseEngine): logger.warning(f"[brave] failed: {e}") return [] - def _parse(self, html: str, max_results: int) -> list[EngineResult]: + def _parse(self, html: str, max_results: int) -> list[SearchResult]: soup = BeautifulSoup(html, "html.parser") results = [] @@ -60,7 +60,7 @@ class BraveScraper(BaseEngine): snippet = snippet_el.get_text(strip=True) if snippet_el else "" if title and href and href.startswith("http"): - results.append(EngineResult( + results.append(SearchResult( title=title, url=href, snippet=snippet, source="brave", )) if len(results) >= max_results: diff --git a/scripts/engines/github_engine.py b/scripts/engines/github_engine.py index 153c154..ebb9593 100644 --- a/scripts/engines/github_engine.py +++ b/scripts/engines/github_engine.py @@ -8,7 +8,7 @@ from urllib.parse import quote_plus import httpx -from scripts.engines.base import BaseEngine, EngineResult +from scripts.core.base import BaseEngine, SearchResult logger = logging.getLogger(__name__) @@ -24,7 +24,7 @@ class GitHubEngine(BaseEngine): name = "github" async def search(self, query: str, max_results: int = 10, - search_type: str = "repositories", **kwargs) -> list[EngineResult]: + search_type: str = "repositories", **kwargs) -> list[SearchResult]: """search_type: 'repositories' | 'code' | 'issues'""" t0 = time.time() endpoint = f"https://api.github.com/search/{search_type}" @@ -40,7 +40,7 @@ class GitHubEngine(BaseEngine): results = [] for item in data.get("items", [])[:max_results]: if search_type == "repositories": - results.append(EngineResult( + results.append(SearchResult( title=item.get("full_name", ""), url=item.get("html_url", ""), snippet=item.get("description", "") or "", @@ -53,7 +53,7 @@ class GitHubEngine(BaseEngine): }, )) elif search_type == "code": - results.append(EngineResult( + results.append(SearchResult( title=item.get("name", ""), url=item.get("html_url", ""), snippet=item.get("text_matches", [{}])[0].get("fragment", "") @@ -66,7 +66,7 @@ class GitHubEngine(BaseEngine): }, )) elif search_type == "issues": - results.append(EngineResult( + results.append(SearchResult( title=item.get("title", ""), url=item.get("html_url", ""), snippet=item.get("body", "")[:300] if item.get("body") else "", diff --git a/scripts/engines/news_engine.py b/scripts/engines/news_engine.py index 930d1da..bda919e 100644 --- a/scripts/engines/news_engine.py +++ b/scripts/engines/news_engine.py @@ -1,40 +1,62 @@ -"""News search engine — multi-source news aggregation.""" +"""News search — multi-source aggregation (Bing News + RSS).""" from __future__ import annotations +import re import time import logging from urllib.parse import quote_plus from xml.etree import ElementTree as ET import httpx -from bs4 import BeautifulSoup -from scripts.engines.base import BaseEngine, EngineResult +from scripts.core.base import BaseEngine, SearchResult logger = logging.getLogger(__name__) -HEADERS = { +_HEADERS = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " "(KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36", "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8", } +# Free RSS feeds — no API key needed +_RSS_FEEDS = { + # Chinese tech/business + "36kr": "https://36kr.com/feed", + "少数派": "https://sspai.com/feed", + "IT之家": "https://www.ithome.com/rss/", + "钛媒体": "https://www.tmtpost.com/rss.xml", + "Solidot": "https://www.solidot.org/index.rss", + "InfoQ CN": "https://www.infoq.cn/feed", + "OSChina": "https://www.oschina.net/news/rss", + # English tech + "HackerNews": "https://hnrss.org/best?count=20", + "TechCrunch": "https://techcrunch.com/feed/", + "ArsTechnica": "https://feeds.arstechnica.com/arstechnica/index", + "TheVerge": "https://www.theverge.com/rss/index.xml", + "MIT Tech Review": "https://www.technologyreview.com/feed/", + "Wired": "https://www.wired.com/feed/rss", + "Lobste.rs": "https://lobste.rs/rss", + # English general + "ChinaDaily": "https://www.chinadaily.com.cn/rss/china_rss.xml", +} + class BingNewsEngine(BaseEngine): - """Bing News scraping — good for both CN and EN news.""" + """Bing News scraping — works for both CN and global news.""" name = "bing_news" async def search(self, query: str, max_results: int = 10, - region: str = "cn", **kwargs) -> list[EngineResult]: + region: str = "cn", **kwargs) -> list[SearchResult]: t0 = time.time() base = "cn.bing.com" if region == "cn" else "www.bing.com" url = f"https://{base}/news/search?q={quote_plus(query)}&setlang=zh-CN" try: async with httpx.AsyncClient(timeout=self.timeout, follow_redirects=True) as client: - resp = await client.get(url, headers=HEADERS) + resp = await client.get(url, headers=_HEADERS) resp.raise_for_status() elapsed = time.time() - t0 results = self._parse(resp.text, max_results) @@ -44,20 +66,20 @@ class BingNewsEngine(BaseEngine): logger.warning(f"[bing_news] failed: {e}") return [] - def _parse(self, html: str, max_results: int) -> list[EngineResult]: + def _parse(self, html: str, max_results: int) -> list[SearchResult]: + from bs4 import BeautifulSoup soup = BeautifulSoup(html, "html.parser") results = [] for item in soup.select("a.title"): title = item.get_text(strip=True) href = item.get("href", "") - # Try to get snippet from parent container parent = item.find_parent("div", class_=True) snippet = "" if parent: snippet_el = parent.select_one("div.snippet") snippet = snippet_el.get_text(strip=True) if snippet_el else "" if title and href and href.startswith("http"): - results.append(EngineResult( + results.append(SearchResult( title=title, url=href, snippet=snippet, source="bing_news", category="news", )) @@ -67,40 +89,18 @@ class BingNewsEngine(BaseEngine): class RSSNewsEngine(BaseEngine): - """RSS-based news — multi-source aggregation.""" + """RSS feed aggregation — multi-source news from curated feeds.""" name = "rss_news" - # Free RSS feeds, no key needed - FEEDS = { - # Chinese tech/business - "36kr": "https://36kr.com/feed", - "少数派": "https://sspai.com/feed", - "IT之家": "https://www.ithome.com/rss/", - "钛媒体": "https://www.tmtpost.com/rss.xml", - "Solidot": "https://www.solidot.org/index.rss", - "InfoQ CN": "https://www.infoq.cn/feed", - "OSChina": "https://www.oschina.net/news/rss", - # English tech - "HackerNews": "https://hnrss.org/best?count=20", - "TechCrunch": "https://techcrunch.com/feed/", - "ArsTechnica": "https://feeds.arstechnica.com/arstechnica/index", - "TheVerge": "https://www.theverge.com/rss/index.xml", - "MIT Tech Review": "https://www.technologyreview.com/feed/", - "Wired": "https://www.wired.com/feed/rss", - "Lobste.rs": "https://lobste.rs/rss", - # English general - "ChinaDaily": "https://www.chinadaily.com.cn/rss/china_rss.xml", - } - async def search(self, query: str, max_results: int = 10, - **kwargs) -> list[EngineResult]: + **kwargs) -> list[SearchResult]: t0 = time.time() all_results = [] import asyncio tasks = [self._fetch_feed(name, url, query) - for name, url in self.FEEDS.items()] + for name, url in _RSS_FEEDS.items()] feeds = await asyncio.gather(*tasks, return_exceptions=True) for feed_results in feeds: @@ -111,52 +111,45 @@ class RSSNewsEngine(BaseEngine): logger.info(f"[rss_news] {len(all_results)} results in {elapsed:.2f}s") return all_results[:max_results] - async def _fetch_feed(self, name: str, url: str, query: str) -> list[EngineResult]: + async def _fetch_feed(self, name: str, url: str, query: str) -> list[SearchResult]: try: async with httpx.AsyncClient(timeout=self.timeout, follow_redirects=True) as client: - resp = await client.get(url, headers=HEADERS) + resp = await client.get(url, headers=_HEADERS) resp.raise_for_status() return self._parse_feed(name, resp.text, query) except Exception as e: logger.warning(f"[rss_news:{name}] failed: {e}") return [] - def _parse_feed(self, name: str, xml_text: str, query: str) -> list[EngineResult]: + def _parse_feed(self, name: str, xml_text: str, query: str) -> list[SearchResult]: try: root = ET.fromstring(xml_text) except ET.ParseError: return [] - results = [] - # RSS 2.0: //channel/item, Atom: //entry ns = {"atom": "http://www.w3.org/2005/Atom"} - items = root.findall(".//item") or root.findall(".//atom:entry", ns) q_lower = query.lower() q_words = [w for w in q_lower.split() if len(w) > 1] + results = [] for item in items[:50]: title = (item.findtext("title") or item.findtext("atom:title", namespaces=ns) or "") - link = (item.findtext("link") or "") + link = item.findtext("link") or "" if not link: link_el = item.find("atom:link", ns) link = link_el.get("href", "") if link_el is not None else "" desc = (item.findtext("description") or item.findtext("atom:summary", namespaces=ns) or "") - # Clean HTML tags from description - import re desc = re.sub(r'<[^>]+>', '', desc) - # Keyword scoring (not strict filter) text_lower = f"{title} {desc}".lower() - if q_words: - match_count = sum(1 for w in q_words if w in text_lower) - if match_count == 0: - continue # Skip completely irrelevant items + if q_words and not any(w in text_lower for w in q_words): + continue # Skip items with no query keyword match - results.append(EngineResult( + results.append(SearchResult( title=title.strip(), url=link.strip(), snippet=desc[:300].strip(), @@ -168,15 +161,15 @@ class RSSNewsEngine(BaseEngine): class NewsSearch(BaseEngine): - """Unified news search — Bing News + RSS in parallel.""" + """Unified news search — Bing News + RSS feeds in parallel.""" name = "news" async def search(self, query: str, max_results: int = 10, - region: str = "cn", **kwargs) -> list[EngineResult]: + region: str = "cn", **kwargs) -> list[SearchResult]: import asyncio bing = BingNewsEngine(timeout=self.timeout) - rss = RSSNewsEngine(timeout=8.0) # RSS feeds may be slow + rss = RSSNewsEngine(timeout=8.0) bing_results, rss_results = await asyncio.gather( bing.search(query, max_results=max_results, region=region), diff --git a/scripts/engines/parallel_search.py b/scripts/engines/parallel_search.py index bc0618b..d559f8e 100644 --- a/scripts/engines/parallel_search.py +++ b/scripts/engines/parallel_search.py @@ -8,7 +8,7 @@ import logging from dataclasses import dataclass, field from typing import Any -from scripts.engines.base import BaseEngine, EngineResult +from scripts.core.base import BaseEngine, SearchResult from scripts.engines.bing_scraper import BingScraper from scripts.engines.sogou_scraper import SogouScraper from scripts.engines.baidu_scraper import BaiduScraper @@ -59,7 +59,7 @@ class ParallelSearch: max_results: int = 10, region: str = "cn", **kwargs, - ) -> tuple[list[EngineResult], SearchStats]: + ) -> tuple[list[SearchResult], SearchStats]: """ Search across specified engines in parallel. @@ -138,7 +138,7 @@ class ParallelSearch: async def _run_engine( self, engine: BaseEngine, query: str, max_results: int, kwargs: dict - ) -> tuple[list[EngineResult], float]: + ) -> tuple[list[SearchResult], float]: """Run a single engine with timing.""" with execution_log.track(engine.name, "search", query=query) as ctx: t0 = time.time() @@ -162,7 +162,7 @@ class ParallelSearch: vertical: str, max_results: int = 10, **kwargs, - ) -> tuple[list[EngineResult], SearchStats]: + ) -> tuple[list[SearchResult], SearchStats]: """Search a specific vertical (news/academic/github).""" if vertical == "github": # GitHub supports sub-types diff --git a/scripts/engines/parser_engines.py b/scripts/engines/parser_engines.py index dc12287..f16ccc3 100644 --- a/scripts/engines/parser_engines.py +++ b/scripts/engines/parser_engines.py @@ -1,80 +1,114 @@ -""" -Parsing-based search engines (e.g. DuckDuckGo HTML scraper). -""" +"""Parsing-based search engines — DuckDuckGo, Brave (fallback HTML scraper).""" from __future__ import annotations import logging -from typing import Optional +from urllib.parse import quote_plus import httpx from bs4 import BeautifulSoup -from scripts.core.base import SearchEngine, SearchResult +from scripts.core.base import BaseEngine, SearchResult logger = logging.getLogger(__name__) +_HEADERS = { + "User-Agent": ("Mozilla/5.0 (Windows NT 10.0; Win64; x64) " + "AppleWebKit/537.36 (KHTML, like Gecko) " + "Chrome/125.0.0.0 Safari/537.36"), + "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", + "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8", +} -class DuckDuckGoEngine(SearchEngine): - """DuckDuckGo HTML search engine (no API key required).""" - SEARCH_URL = "https://html.duckduckgo.com/html/" +class DuckDuckGoParser(BaseEngine): + """DuckDuckGo HTML scraper (no API key needed).""" - 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, - ) + name = "duckduckgo" - 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]: + import time + t0 = time.time() + url = f"https://html.duckduckgo.com/html/?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}") + async with httpx.AsyncClient(timeout=self.timeout, follow_redirects=True) as client: + resp = await client.get(url, headers=_HEADERS) + resp.raise_for_status() + elapsed = time.time() - t0 + results = self._parse(resp.text, max_results) + logger.info(f"[duckduckgo] {len(results)} results in {elapsed:.2f}s") + return results + except Exception as e: + logger.warning(f"[duckduckgo] failed: {e}") return [] - def _parse_results(self, html: str, max_results: int) -> list[SearchResult]: - """Parse DuckDuckGo HTML search results.""" - soup = BeautifulSoup(html, "lxml") + def _parse(self, html: str, max_results: int) -> list[SearchResult]: + soup = BeautifulSoup(html, "html.parser") results = [] - for i, result in enumerate(soup.select(".result")[:max_results]): + for result in 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", - ) - ) + if not title_el: + continue + 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="duckduckgo", + category="web", + )) + return results - async def close(self): - await self._client.aclose() + +class BraveParser(BaseEngine): + """Brave Search fallback HTML scraper (no API key needed).""" + + name = "brave" + + async def search(self, query: str, max_results: int = 10, + **kwargs) -> list[SearchResult]: + import time + t0 = time.time() + url = f"https://search.brave.com/search?q={quote_plus(query)}&source=web" + + try: + async with httpx.AsyncClient(timeout=self.timeout, follow_redirects=True) as client: + resp = await client.get(url, headers=_HEADERS) + resp.raise_for_status() + elapsed = time.time() - t0 + results = self._parse(resp.text, max_results) + logger.info(f"[brave] {len(results)} results in {elapsed:.2f}s") + return results + except Exception as e: + logger.warning(f"[brave] failed: {e}") + return [] + + def _parse(self, html: str, max_results: int) -> list[SearchResult]: + soup = BeautifulSoup(html, "html.parser") + results = [] + + for item in soup.select("div.snippet, div.result")[:max_results]: + title_el = item.select_one("a.result-header, a.snippet-title, a") + if not title_el: + continue + title = title_el.get_text(strip=True) + href = title_el.get("href", "") + snippet_el = item.select_one("p.snippet-description, div.snippet-description") + snippet = snippet_el.get_text(strip=True) if snippet_el else "" + + if title and href and href.startswith("http"): + results.append(SearchResult( + title=title, url=href, snippet=snippet, + source="brave", category="web", + )) + + return results + + +# Backwards-compatibility aliases +DuckDuckGoEngine = DuckDuckGoParser +BraveScraper = BraveParser diff --git a/scripts/engines/search360_scraper.py b/scripts/engines/search360_scraper.py index df0cbd1..3e04f03 100644 --- a/scripts/engines/search360_scraper.py +++ b/scripts/engines/search360_scraper.py @@ -9,7 +9,7 @@ from urllib.parse import quote_plus import httpx from bs4 import BeautifulSoup -from scripts.engines.base import BaseEngine, EngineResult +from scripts.core.base import BaseEngine, SearchResult logger = logging.getLogger(__name__) @@ -27,7 +27,7 @@ class Search360Scraper(BaseEngine): name = "360" async def search(self, query: str, max_results: int = 10, - **kwargs) -> list[EngineResult]: + **kwargs) -> list[SearchResult]: t0 = time.time() url = f"https://www.so.com/s?q={quote_plus(query)}" @@ -43,7 +43,7 @@ class Search360Scraper(BaseEngine): logger.warning(f"[360] failed: {e}") return [] - def _parse(self, html: str, max_results: int) -> list[EngineResult]: + def _parse(self, html: str, max_results: int) -> list[SearchResult]: soup = BeautifulSoup(html, "html.parser") results = [] @@ -57,7 +57,7 @@ class Search360Scraper(BaseEngine): snippet = snippet_el.get_text(strip=True) if snippet_el else "" if title and href: - results.append(EngineResult( + results.append(SearchResult( title=title, url=href, snippet=snippet, source="360", )) if len(results) >= max_results: diff --git a/scripts/engines/sogou_scraper.py b/scripts/engines/sogou_scraper.py index 649f8ae..90389fd 100644 --- a/scripts/engines/sogou_scraper.py +++ b/scripts/engines/sogou_scraper.py @@ -9,7 +9,7 @@ from urllib.parse import quote_plus import httpx from bs4 import BeautifulSoup -from scripts.engines.base import BaseEngine, EngineResult +from scripts.core.base import BaseEngine, SearchResult logger = logging.getLogger(__name__) @@ -27,7 +27,7 @@ class SogouScraper(BaseEngine): name = "sogou" async def search(self, query: str, max_results: int = 10, - **kwargs) -> list[EngineResult]: + **kwargs) -> list[SearchResult]: t0 = time.time() url = f"https://www.sogou.com/web?query={quote_plus(query)}" @@ -43,7 +43,7 @@ class SogouScraper(BaseEngine): logger.warning(f"[sogou] failed: {e}") return [] - def _parse(self, html: str, max_results: int) -> list[EngineResult]: + def _parse(self, html: str, max_results: int) -> list[SearchResult]: soup = BeautifulSoup(html, "html.parser") results = [] @@ -57,7 +57,7 @@ class SogouScraper(BaseEngine): snippet = snippet_el.get_text(strip=True) if snippet_el else "" if title and href and not href.startswith("/"): - results.append(EngineResult( + results.append(SearchResult( title=title, url=href, snippet=snippet, source="sogou", )) if len(results) >= max_results: diff --git a/scripts/engines/url_engine.py b/scripts/engines/url_engine.py index 460c5af..900b12e 100644 --- a/scripts/engines/url_engine.py +++ b/scripts/engines/url_engine.py @@ -1,31 +1,31 @@ """ Generic URL-based search engine. -Builds search URLs from templates defined in engines.json +Builds search URLs from templates (config/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 typing import Any -from scripts.core.base import SearchEngine, SearchResult - -logger = logging.getLogger(__name__) +from scripts.core.base import BaseEngine, SearchResult -class UrlSearchEngine(SearchEngine): +class UrlSearchEngine(BaseEngine): """ - Generic search engine that builds URLs from templates. + Generic engine that builds search 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", {})) + 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") @@ -55,6 +55,3 @@ class UrlSearchEngine(SearchEngine): extra={"url": url, "region": self.region}, ) ] - - async def close(self): - pass diff --git a/scripts/models.py b/scripts/models.py index a11210c..0b88688 100644 --- a/scripts/models.py +++ b/scripts/models.py @@ -1,25 +1,22 @@ """ -Pydantic 数据模型定义。 +Data models — re-exports from scripts.core.base for backwards compatibility. -从 bot-search-mcp 移植,适配 web-search-skills 的数据结构。 -提供类型安全的搜索数据模型。 - -依赖: pydantic (>=2.0) +The canonical source of truth is scripts.core.base.SearchResult. +This module exists so old import paths (e.g. from scripts.models import SearchQuery) +continue to work without changes. """ -from __future__ import annotations +from scripts.core.base import BaseEngine, SearchResult from dataclasses import dataclass, field -from datetime import datetime from typing import Any, Optional -# ============================================================ -# 搜索结果模型(兼容 dataclass + pydantic 双模式) -# ============================================================ +# Backwards-compatible thin wrappers (no functionality changes needed for now) + @dataclass class SearchQuery: - """搜索查询封装。""" + """Search query wrapper.""" query: str sources: Optional[list[str]] = None max_results: int = 10 @@ -31,20 +28,16 @@ class SearchQuery: @dataclass class SourceRef: - """引用来源。""" + """Reference to a result source.""" index: int title: str url: str source: str -# ============================================================ -# 性能计时模型 -# ============================================================ - @dataclass class EngineTiming: - """单个引擎耗时记录。""" + """Per-engine timing record.""" engine_name: str success: bool duration_ms: int @@ -54,7 +47,7 @@ class EngineTiming: @dataclass class SearchMetadata: - """搜索元数据(全链路计时)。""" + """Search metadata (full-chain timing).""" total_engines: int = 0 total_raw_results: int = 0 engines_used: list[str] = field(default_factory=list) @@ -63,13 +56,9 @@ class SearchMetadata: timed_out_engines: list[str] = field(default_factory=list) -# ============================================================ -# 最终响应 -# ============================================================ - @dataclass class SearchResponse: - """完整搜索响应。""" + """Complete search response.""" query: str results: list[Any] = field(default_factory=list) sources: list[SourceRef] = field(default_factory=list) diff --git a/scripts/news/__init__.py b/scripts/news/__init__.py deleted file mode 100644 index 5a0108e..0000000 --- a/scripts/news/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""News search engines.""" diff --git a/scripts/news/cls.py b/scripts/news/cls.py deleted file mode 100644 index 27f80ec..0000000 --- a/scripts/news/cls.py +++ /dev/null @@ -1,186 +0,0 @@ -""" -财联社 (cls.cn) news search engine. - -Uses the public telegraph API (nodeapi/telegraphList) to fetch real-time -financial news without JS rendering. Falls back to search page URL for -keyword-based search queries. - -API details (discovered via frontend reverse-engineering): - GET https://www.cls.cn/nodeapi/telegraphList - - Parameters: app=CailianpressWeb, os=web, sv=7.7.5, rn=N, last_time=timestamp - - Sign: SHA1(plaintext) → MD5(hex) (anti-scraping measure) - - Returns: {"data":{"roll_data":[{"ctime":..., "content":"..."}]}} -""" -from __future__ import annotations - -import hashlib -import logging -import time -from typing import Optional -from urllib.parse import quote_plus - -import httpx - -from scripts.core.base import SearchEngine, SearchResult - -logger = logging.getLogger(__name__) - -API_URL = "https://www.cls.cn/nodeapi/telegraphList" -SEARCH_URL = "https://www.cls.cn/searchPage" -API_APP = "CailianpressWeb" -API_OS = "web" -API_SV = "7.7.5" - - -class CLSNewsEngine(SearchEngine): - """财联社 (cls.cn) news search engine.""" - - def __init__(self, config: Optional[dict] = None): - super().__init__("cls", config) - timeout = (config or {}).get("timeout", 15) - self._client = httpx.AsyncClient( - timeout=timeout, - headers={ - "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" - ), - "Referer": "https://www.cls.cn/telegraph", - }, - follow_redirects=True, - ) - - def build_url(self, query: str, **kwargs) -> str: - return f"{SEARCH_URL}?keyword={quote_plus(query)}&type=all" - - def _build_api_sign(self, last_time: int, rn: int) -> str: - """Build SHA1 + MD5 double-hashed sign for API request.""" - plain = ( - f"app={API_APP}&last_time={last_time}" - f"&os={API_OS}&rn={rn}&sv={API_SV}" - ) - sha1_hex = hashlib.sha1(plain.encode()).hexdigest() - return hashlib.md5(sha1_hex.encode()).hexdigest() - - async def search( - self, query: str, max_results: int = 10, **kwargs - ) -> list[SearchResult]: - """Search 财联社. - - Uses the telegraph API for latest news (ignores query). - For keyword-specific results, falls back to the search page URL. - """ - # Step 1: Try to get real results from telegraph API - api_results = await self._fetch_telegraph(max_results) - if api_results: - return api_results[:max_results] - - # Step 2: Fallback to search page URL - search_url = self.build_url(query) - return [ - SearchResult( - title=f"财联社: {query}", - url=search_url, - snippet=( - f"Search 财联社 for '{query}' " - "(JS-required search page)" - ), - source=self.name, - rank=1, - category="news", - extra={"search_url": search_url, "note": "JS-rendered page"}, - ) - ] - - async def _fetch_telegraph( - self, max_results: int = 10 - ) -> list[SearchResult]: - """Fetch real-time telegraph news via API.""" - try: - rn = min(max(max_results, 5), 100) - last_time = int(time.time()) - sign = self._build_api_sign(last_time, rn) - - params = { - "app": API_APP, - "last_time": str(last_time), - "os": API_OS, - "rn": str(rn), - "sv": API_SV, - "sign": sign, - } - - response = await self._client.get(API_URL, params=params) - response.raise_for_status() - - data = response.json() - items = data.get("data", {}).get("roll_data", []) - - results = [] - for i, item in enumerate(items[:max_results]): - content = item.get("content", "") - ctime = item.get("ctime", 0) - - # Extract title from content (财联社 format: - # "【title】content" or plain text) - title, snippet = self._parse_content(content) - - # Use unique URL per item to avoid dedup collision - item_url = ( - f"https://www.cls.cn/telegraph#{ctime}-{i}" - ) - - results.append( - SearchResult( - title=title or content[:80], - url=item_url, - snippet=snippet or content[:200], - source=self.name, - rank=i + 1, - category="news", - extra={ - "time": self._format_time(ctime), - "timestamp": ctime, - }, - ) - ) - - return results - - except (httpx.HTTPError, ValueError, KeyError) as e: - logger.warning(f"财联社 API failed: {e}") - return [] - - @staticmethod - def _parse_content(content: str) -> tuple[str, str]: - """Parse 财联社 content into (title, snippet). - - Format examples: - "【日本地震】内容详情..." → ("日本地震", "内容详情...") - "财联社5月20日电,普通新闻" → ("", "财联社5月20日电,普通新闻") - """ - if content.startswith("【"): - end = content.find("】") - if end != -1: - title = content[1:end] - snippet = content[end + 1:].strip() - return title, snippet - return "", content - - @staticmethod - def _format_time(ts: int) -> str: - """Format Unix timestamp to readable time.""" - import datetime - - try: - dt = datetime.datetime.fromtimestamp( - ts, - tz=datetime.timezone(datetime.timedelta(hours=8)), - ) - return dt.strftime("%Y-%m-%d %H:%M") - except (OSError, ValueError): - return str(ts) - - async def close(self): - await self._client.aclose() diff --git a/scripts/news/rss.py b/scripts/news/rss.py deleted file mode 100644 index 29b48dc..0000000 --- a/scripts/news/rss.py +++ /dev/null @@ -1,367 +0,0 @@ -""" -RSS news aggregation engine. - -Fetches and parses RSS/Atom feeds from trusted news sources. -Inspired by the news-summary skill's RSS-based approach. - -Supported sources: -- BBC (World, Business, Technology, Top Stories) -- Reuters (World News) -- NPR (US news) -- Al Jazeera (Global South perspective) -- 财联社 (RSS if available) -- 华尔街见闻 (RSS if available) -""" - -from __future__ import annotations - -import logging -import xml.etree.ElementTree as ET -from datetime import datetime, timezone -from typing import Any, Optional -from urllib.parse import urlparse - -import httpx - -from scripts.core.base import SearchEngine, SearchResult - -logger = logging.getLogger(__name__) - -# RSS feed definitions -# Core sources (general news) + AI newsletters + essays + podcasts -RSS_FEEDS = { - "BBC World": { - "url": "https://feeds.bbci.co.uk/news/world/rss.xml", - "category": "news", - "lang": "en", - }, - "BBC Top Stories": { - "url": "https://feeds.bbci.co.uk/news/rss.xml", - "category": "news", - "lang": "en", - }, - "BBC Business": { - "url": "https://feeds.bbci.co.uk/news/business/rss.xml", - "category": "business", - "lang": "en", - }, - "BBC Technology": { - "url": "https://feeds.bbci.co.uk/news/technology/rss.xml", - "category": "technology", - "lang": "en", - }, - "Reuters": { - "url": "https://www.reutersagency.com/feed/?best-regions=world&post_type=best", - "category": "news", - "lang": "en", - }, - "NPR": { - "url": "https://feeds.npr.org/1001/rss.xml", - "category": "news", - "lang": "en", - }, - "Al Jazeera": { - "url": "https://www.aljazeera.com/xml/rss/all.xml", - "category": "news", - "lang": "en", - }, - - # ----------------------------------------------------------------------- - # AI Newsletters (inspired by news-aggregator-skill) - # ----------------------------------------------------------------------- - "Latent Space": { - "url": "https://feeds.transistor.fm/latent-space-podcast", - "category": "technology", - "lang": "en", - "subcategory": "ai_newsletter", - }, - "ChinAI": { - "url": "https://chinai.substack.com/feed", - "category": "technology", - "lang": "en", - "subcategory": "ai_newsletter", - }, - "Memia": { - "url": "https://memia.substack.com/feed", - "category": "technology", - "lang": "en", - "subcategory": "ai_newsletter", - }, - "Interconnects": { - "url": "https://interconnects.substack.com/feed", - "category": "technology", - "lang": "en", - "subcategory": "ai_newsletter", - }, - "KDnuggets": { - "url": "https://www.kdnuggets.com/feed", - "category": "technology", - "lang": "en", - "subcategory": "ai_newsletter", - }, - - # ----------------------------------------------------------------------- - # Deep Essays & Thinkers (inspired by news-aggregator-skill) - # ----------------------------------------------------------------------- - "Paul Graham": { - "url": "https://paulgraham.com/rss.xml", - "category": "technology", - "lang": "en", - "subcategory": "essay", - }, - "James Clear": { - "url": "https://jamesclear.com/feed", - "category": "news", - "lang": "en", - "subcategory": "essay", - }, - "Wait But Why": { - "url": "https://waitbutwhy.com/feed", - "category": "news", - "lang": "en", - "subcategory": "essay", - }, - "Farnam Street": { - "url": "https://fs.blog/feed/", - "category": "news", - "lang": "en", - "subcategory": "essay", - }, - - # ----------------------------------------------------------------------- - # Podcasts (inspired by news-aggregator-skill) - # ----------------------------------------------------------------------- - "Lex Fridman Podcast": { - "url": "https://lexfridman.com/feed/podcast/", - "category": "technology", - "lang": "en", - "subcategory": "podcast", - }, - "80,000 Hours": { - "url": "https://80000hours.org/feed/podcast/", - "category": "news", - "lang": "en", - "subcategory": "podcast", - }, -} - - -class RSSNewsEngine(SearchEngine): - """ - RSS-based news aggregation engine. - - Fetches and parses RSS/Atom feeds from international news sources. - Supports filtering by category, keyword, and time range. - """ - - def __init__(self, config: Optional[dict] = None): - super().__init__("RSS News", config) - timeout = (config or {}).get("timeout", 30) - 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", - ), - "Accept": "application/rss+xml, application/xml, text/xml, */*", - }, - follow_redirects=True, - ) - - def build_url(self, query: str, **kwargs) -> str: - """Build search URL - for RSS this is not a search URL but a feed URL.""" - feed_name = kwargs.get("feed", "BBC World") - feed = RSS_FEEDS.get(feed_name, RSS_FEEDS["BBC World"]) - return feed["url"] - - async def search(self, query: str, max_results: int = 10, **kwargs) -> list[SearchResult]: - """ - Search news across RSS feeds. If query is empty, returns latest headlines. - - Args: - query: Search keyword to filter articles (case-insensitive). - If empty/blank, returns all headlines. - max_results: Maximum articles per feed. - feeds: Specific feeds to query (list of feed names or "all"). - category: Filter by category (news, business, technology). - hours: Only include articles from the last N hours. - - Returns: - List of SearchResult objects sorted by date (newest first). - """ - feeds_to_query = self._select_feeds( - feed_names=kwargs.get("feeds", "all"), - category=kwargs.get("category"), - ) - - results = [] - for feed_name, feed_info in feeds_to_query.items(): - try: - response = await self._client.get(feed_info["url"]) - response.raise_for_status() - - parsed = self._parse_feed(response.text, feed_name, feed_info) - for article in parsed: - # Filter by keyword if query provided - if query: - q = query.lower() - if q in article.title.lower() or q in article.snippet.lower(): - results.append(article) - else: - results.append(article) - - except httpx.HTTPError as e: - logger.warning(f"RSS feed '{feed_name}' failed: {e}") - continue - - # Apply time filter - hours = kwargs.get("hours") - if hours: - from datetime import timedelta - cutoff = datetime.now(timezone.utc) - timedelta(hours=int(hours)) - results = [r for r in results if r.extra.get("parsed_date", datetime.max.replace(tzinfo=timezone.utc)) >= cutoff] - - # Sort by date (newest first), limit results - results.sort(key=lambda r: r.extra.get("parsed_date", datetime.min.replace(tzinfo=timezone.utc)), reverse=True) - return results[:max_results * max(1, len(feeds_to_query))] - - def _select_feeds(self, feed_names="all", category=None): - """Select feeds based on filters.""" - if feed_names != "all": - if isinstance(feed_names, str): - feed_names = [feed_names] - return {k: v for k, v in RSS_FEEDS.items() if k in feed_names} - - if category: - return {k: v for k, v in RSS_FEEDS.items() if v.get("category") == category} - - return RSS_FEEDS - - def _parse_feed(self, xml_text: str, feed_name: str, feed_info: dict) -> list[SearchResult]: - """Parse RSS/Atom XML into SearchResult objects.""" - results = [] - - try: - root = ET.fromstring(xml_text) - except ET.ParseError as e: - logger.error(f"Failed to parse RSS from {feed_name}: {e}") - return [] - - # Detect RSS vs Atom format - if root.tag == "rss": - return self._parse_rss(root, feed_name, feed_info) - elif root.tag.endswith("feed"): - return self._parse_atom(root, feed_name, feed_info) - else: - logger.warning(f"Unknown feed format for {feed_name}: {root.tag}") - return results - - def _parse_rss(self, root: ET.Element, feed_name: str, feed_info: dict) -> list[SearchResult]: - """Parse RSS 2.0 format.""" - results = [] - namespaces = {"content": "http://purl.org/rss/1.0/modules/content/"} - - for i, item in enumerate(root.findall(".//item")): - title = item.findtext("title", "") - link = item.findtext("link", "") - description = item.findtext("description", "") - pub_date_str = item.findtext("pubDate", "") - creator = item.findtext("dc:creator", "") or item.findtext("author", "") - - # Clean up description (remove HTML tags) - import re - snippet = re.sub(r"<[^>]+>", "", description).strip() - # Truncate - snippet = snippet[:300] + "..." if len(snippet) > 300 else snippet - - # Parse date - parsed_date = self._parse_date(pub_date_str) - - results.append( - SearchResult( - title=title, - url=link, - snippet=snippet, - source=feed_name, - rank=i + 1, - category=feed_info.get("category", "news"), - extra={ - "feed_name": feed_name, - "author": creator, - "published": pub_date_str, - "parsed_date": parsed_date, - "feed_url": feed_info["url"], - }, - ) - ) - - return results - - def _parse_atom(self, root: ET.Element, feed_name: str, feed_info: dict) -> list[SearchResult]: - """Parse Atom format.""" - results = [] - ns = {"a": "http://www.w3.org/2005/Atom"} - - feed_title = root.findtext("a:title", "", ns) - - for i, entry in enumerate(root.findall("a:entry", ns)): - title = entry.findtext("a:title", "", ns) - link_el = entry.find("a:link", ns) - link = link_el.get("href", "") if link_el is not None else "" - summary = entry.findtext("a:summary", "", ns) or entry.findtext("a:content", "", ns) - published = entry.findtext("a:published", "", ns) or entry.findtext("a:updated", "", ns) - author = entry.findtext("a:author/a:name", "", ns) - - import re - snippet = re.sub(r"<[^>]+>", "", summary).strip() - snippet = snippet[:300] + "..." if len(snippet) > 300 else snippet - - parsed_date = self._parse_date(published) - - results.append( - SearchResult( - title=title, - url=link, - snippet=snippet, - source=feed_name, - rank=i + 1, - category=feed_info.get("category", "news"), - extra={ - "feed_name": feed_name, - "author": author, - "published": published, - "parsed_date": parsed_date, - "feed_url": feed_info["url"], - }, - ) - ) - - return results - - def _parse_date(self, date_str: str) -> datetime: - """Parse various date formats to datetime.""" - if not date_str: - return datetime.min.replace(tzinfo=timezone.utc) - - formats = [ - "%a, %d %b %Y %H:%M:%S %z", # RSS: Sat, 01 Jan 2024 12:00:00 +0000 - "%a, %d %b %Y %H:%M:%S %Z", # RSS with timezone name - "%Y-%m-%dT%H:%M:%S%z", # Atom ISO 8601 - "%Y-%m-%dT%H:%M:%S.%f%z", # Atom with microseconds - "%Y-%m-%dT%H:%M:%SZ", # Atom UTC - "%Y-%m-%d", # Simple date - ] - - for fmt in formats: - try: - return datetime.strptime(date_str.strip(), fmt).replace(tzinfo=timezone.utc) - except (ValueError, TypeError): - continue - - return datetime.min.replace(tzinfo=timezone.utc) - - async def close(self): - await self._client.aclose() diff --git a/scripts/news/tech_news.py b/scripts/news/tech_news.py deleted file mode 100644 index 9c6fd3f..0000000 --- a/scripts/news/tech_news.py +++ /dev/null @@ -1,461 +0,0 @@ -""" -Tech & social news aggregation engines. - -Implements non-Playwright news sources inspired by news-aggregator-skill: -- Hacker News (via Algolia API) -- GitHub Trending (HTML scrape) -- 36氪 (HTML scrape) -- 微博热搜 (Ajax API) -- V2EX (Public API) -- 腾讯新闻 (API) -""" - -from __future__ import annotations - -import logging -import re -from datetime import datetime, timezone -from typing import Any, Optional -from urllib.parse import quote_plus - -import httpx -from bs4 import BeautifulSoup - -from scripts.core.base import SearchEngine, SearchResult -from scripts.core.thread_utils import run_in_thread - -logger = logging.getLogger(__name__) - -# Shared HTTP client settings -_DEFAULT_TIMEOUT = 15 -_COMMON_HEADERS = { - "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" - ), - "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", - "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8", -} - - -# --------------------------------------------------------------------------- -# Hacker News (via Algolia API) -# --------------------------------------------------------------------------- - -class HackerNewsEngine(SearchEngine): - """Hacker News search via Algolia public API.""" - - SEARCH_URL = "https://hn.algolia.com/api/v1/search" - - def __init__(self, config: Optional[dict] = None): - super().__init__("Hacker News", config) - self._client = httpx.AsyncClient( - timeout=_DEFAULT_TIMEOUT, - headers={"User-Agent": _COMMON_HEADERS["User-Agent"]}, - follow_redirects=True, - ) - - async def search(self, query: str, max_results: int = 10, **kwargs) -> list[SearchResult]: - try: - params = { - "query": query, - "hitsPerPage": max_results, - "tags": "story", - } - response = await self._client.get(self.SEARCH_URL, params=params) - response.raise_for_status() - data = response.json() - return self._parse_results(data, max_results) - except Exception as e: - logger.error(f"Hacker News search failed: {e}") - return [] - - def _parse_results(self, data: dict, max_results: int) -> list[SearchResult]: - results = [] - hits = data.get("hits", [])[:max_results] - for i, hit in enumerate(hits): - title = hit.get("title", "") - url = hit.get("url") or f"https://news.ycombinator.com/item?id={hit.get('objectID', '')}" - snippet = hit.get("story_text", "") or "" - # Clean HTML from snippet - snippet = re.sub(r"<[^>]+>", "", snippet)[:200] - points = hit.get("points", 0) - author = hit.get("author", "") - created_at = hit.get("created_at", "") - - results.append(SearchResult( - title=title, - url=url, - snippet=snippet or f"[{points} points] by {author}", - source=self.name, - rank=i + 1, - category="news", - extra={ - "points": points, - "author": author, - "time": created_at, - "heat": f"{points} points", - }, - )) - return results - - async def close(self): - await self._client.aclose() - - -# --------------------------------------------------------------------------- -# GitHub Trending -# --------------------------------------------------------------------------- - -class GitHubTrendingEngine(SearchEngine): - """GitHub Trending repositories.""" - - TRENDING_URL = "https://github.com/trending" - - def __init__(self, config: Optional[dict] = None): - super().__init__("GitHub Trending", config) - self._client = httpx.AsyncClient( - timeout=_DEFAULT_TIMEOUT, - headers=_COMMON_HEADERS, - follow_redirects=True, - ) - - async def search(self, query: str = "", max_results: int = 10, **kwargs) -> list[SearchResult]: - """GitHub Trending doesn't support keyword search; returns top trending repos.""" - url = self.TRENDING_URL - if query: - url += f"?spoken_language_code=&q={quote_plus(query)}" - try: - response = await self._client.get(url) - response.raise_for_status() - return await run_in_thread(self._parse_html, response.text, max_results) - except Exception as e: - logger.error(f"GitHub Trending failed: {e}") - return [] - - def _parse_html(self, html: str, max_results: int) -> list[SearchResult]: - soup = BeautifulSoup(html, "lxml") - results = [] - articles = soup.select("article.Box-row")[:max_results] - for i, article in enumerate(articles): - h2 = article.select_one("h2") - if not h2: - continue - repo_path = h2.get_text(strip=True).replace(" ", "") - repo_url = f"https://github.com/{repo_path}" - - desc_el = article.select_one("p") - description = desc_el.get_text(strip=True) if desc_el else "" - - # Language - lang_el = article.select_one("[itemprop='programmingLanguage']") - language = lang_el.get_text(strip=True) if lang_el else "" - - # Stars today - stars_el = article.select_one(".float-sm-right") - stars_today = stars_el.get_text(strip=True) if stars_el else "" - - results.append(SearchResult( - title=repo_path, - url=repo_url, - snippet=description[:200] if description else language, - source=self.name, - rank=i + 1, - category="news", - extra={ - "language": language, - "heat": stars_today, - "time": "", - }, - )) - return results - - async def close(self): - await self._client.aclose() - - -# --------------------------------------------------------------------------- -# 36氪 (36kr.com) -# --------------------------------------------------------------------------- - -class Kr36Engine(SearchEngine): - """36氪 tech news search.""" - - SEARCH_URL = "https://36kr.com/search/articles/{keyword}" - - def __init__(self, config: Optional[dict] = None): - super().__init__("36氪", config) - self._client = httpx.AsyncClient( - timeout=_DEFAULT_TIMEOUT, - headers={**_COMMON_HEADERS, "Referer": "https://36kr.com/"}, - follow_redirects=True, - ) - - async def search(self, query: str, max_results: int = 10, **kwargs) -> list[SearchResult]: - url = self.SEARCH_URL.format(keyword=quote_plus(query)) - try: - response = await self._client.get(url) - response.raise_for_status() - return await run_in_thread(self._parse_html, response.text, query, max_results) - except Exception as e: - logger.error(f"36氪 search failed: {e}") - return [] - - def _parse_html(self, html: str, query: str, max_results: int) -> list[SearchResult]: - soup = BeautifulSoup(html, "lxml") - results = [] - - # Look for article cards - for i, item in enumerate(soup.select("a[href*='/p/'], .article-item, .search-result-item, [class*='article']")[:max_results]): - title_el = item.select_one("h3, h4, .title, [class*='title']") - link = item.get("href", "") - snippet_el = item.select_one(".abstract, .summary, .desc, p, [class*='desc']") - - title = title_el.get_text(strip=True) if title_el else "" - snippet = snippet_el.get_text(strip=True) if snippet_el else "" - - if not title and not link: - continue - - full_url = link if link.startswith("http") else f"https://36kr.com{link}" - - results.append(SearchResult( - title=title or f"36氪: {query}", - url=full_url, - snippet=snippet[:200] if snippet else "", - source=self.name, - rank=i + 1, - category="news", - extra={"region": "cn"}, - )) - - if not results: - search_url = f"https://36kr.com/search/articles/{quote_plus(query)}" - results.append(SearchResult( - title=f"36氪: {query}", - url=search_url, - snippet="Search 36氪 (site may require JS)", - source=self.name, - rank=1, - category="news", - extra={"search_url": search_url, "note": "JS-rendered page"}, - )) - - return results - - async def close(self): - await self._client.aclose() - - -# --------------------------------------------------------------------------- -# 微博热搜 (Weibo Trending) -# --------------------------------------------------------------------------- - -class WeiboEngine(SearchEngine): - """微博热搜 trending topics.""" - - HOT_URL = "https://weibo.com/ajax/side/hotSearch" - - def __init__(self, config: Optional[dict] = None): - super().__init__("微博热搜", config) - self._client = httpx.AsyncClient( - timeout=_DEFAULT_TIMEOUT, - headers={**_COMMON_HEADERS, "Referer": "https://weibo.com/"}, - follow_redirects=True, - ) - - async def search(self, query: str = "", max_results: int = 10, **kwargs) -> list[SearchResult]: - try: - response = await self._client.get(self.HOT_URL) - response.raise_for_status() - data = response.json() - return self._parse_results(data, query, max_results) - except Exception as e: - logger.error(f"微博热搜 failed: {e}") - return [] - - def _parse_results(self, data: dict, query: str, max_results: int) -> list[SearchResult]: - results = [] - realtime = data.get("data", {}).get("realtime", [])[:max_results] - for i, item in enumerate(realtime): - word = item.get("word", "") - raw_hot = item.get("raw_hot", 0) - url = f"https://s.weibo.com/weibo?q={quote_plus(word)}" - - # Filter by keyword if query provided - if query and query.lower() not in word.lower(): - continue - - results.append(SearchResult( - title=f"#{word}#", - url=url, - snippet=f"热度: {raw_hot:,}" if raw_hot else "", - source=self.name, - rank=i + 1, - category="news", - extra={ - "heat": f"{raw_hot:,}" if raw_hot else "", - "region": "cn", - }, - )) - - return results - - async def close(self): - await self._client.aclose() - - -# --------------------------------------------------------------------------- -# V2EX (via public API) -# --------------------------------------------------------------------------- - -class V2EXEngine(SearchEngine): - """V2EX tech community hot topics.""" - - HOT_API = "https://www.v2ex.com/api/v2/topics/hot" - # Fallback to older API if v2 needs auth - HOT_API_FALLBACK = "https://www.v2ex.com/api/topics/hot.json" - - def __init__(self, config: Optional[dict] = None): - super().__init__("V2EX", config) - self._client = httpx.AsyncClient( - timeout=_DEFAULT_TIMEOUT, - headers=_COMMON_HEADERS, - follow_redirects=True, - ) - - async def search(self, query: str = "", max_results: int = 10, **kwargs) -> list[SearchResult]: - # Try v2 API first, fallback to old API - for api_url in [self.HOT_API, self.HOT_API_FALLBACK]: - try: - response = await self._client.get(api_url) - if response.status_code == 200: - data = response.json() - # v2 API returns list directly; old API also returns list - topics = data if isinstance(data, list) else data.get("data", []) - return self._parse_results(topics, query, max_results) - except Exception: - continue - logger.error("V2EX API failed (both v2 and fallback)") - return [] - - def _parse_results(self, topics: list, query: str, max_results: int) -> list[SearchResult]: - results = [] - for i, topic in enumerate(topics[:max_results]): - title = topic.get("title", "") - node_name = "" - if "node" in topic and isinstance(topic["node"], dict): - node_name = topic["node"].get("title", "") - topic_id = topic.get("id", "") - url = f"https://www.v2ex.com/t/{topic_id}" - replies = topic.get("replies", 0) - created = topic.get("created", "") - member = "" - if "member" in topic and isinstance(topic["member"], dict): - member = topic["member"].get("username", "") - - # Filter by keyword - if query and query.lower() not in title.lower(): - continue - - # Format time - time_str = "" - if created: - try: - dt = datetime.fromtimestamp(int(created), tz=timezone.utc) - time_str = dt.strftime("%Y-%m-%d %H:%M") - except (ValueError, OSError): - time_str = str(created) - - results.append(SearchResult( - title=title, - url=url, - snippet=f"[{node_name}] by {member}" if node_name and member else f"by {member}" if member else "", - source=self.name, - rank=i + 1, - category="news", - extra={ - "heat": f"{replies} replies", - "author": member, - "time": time_str, - "region": "cn", - }, - )) - - return results - - async def close(self): - await self._client.aclose() - - -# --------------------------------------------------------------------------- -# 腾讯新闻 (news.qq.com) -# --------------------------------------------------------------------------- - -class TencentNewsEngine(SearchEngine): - """腾讯新闻 search.""" - - SEARCH_URL = "https://news.qq.com/search" - - def __init__(self, config: Optional[dict] = None): - super().__init__("腾讯新闻", config) - self._client = httpx.AsyncClient( - timeout=_DEFAULT_TIMEOUT, - headers={**_COMMON_HEADERS, "Referer": "https://news.qq.com/"}, - follow_redirects=True, - ) - - async def search(self, query: str, max_results: int = 10, **kwargs) -> list[SearchResult]: - url = f"{self.SEARCH_URL}?q={quote_plus(query)}" - try: - response = await self._client.get(url) - response.raise_for_status() - return await run_in_thread(self._parse_html, response.text, query, max_results) - except Exception as e: - logger.error(f"腾讯新闻 search failed: {e}") - return [] - - def _parse_html(self, html: str, query: str, max_results: int) -> list[SearchResult]: - soup = BeautifulSoup(html, "lxml") - results = [] - - for i, item in enumerate(soup.select("a[href*='news.qq.com'], .list-item, [class*='result'], article")[:max_results]): - title_el = item.select_one("h3, h4, .title, a") - link = item.get("href", "") or (title_el.get("href", "") if title_el else "") - snippet_el = item.select_one(".summary, .desc, .abstract, p, .text") - - title = title_el.get_text(strip=True) if title_el else "" - snippet = snippet_el.get_text(strip=True) if snippet_el else "" - - if not title: - continue - - full_url = link if link.startswith("http") else f"https:{link}" if link.startswith("//") else link - - results.append(SearchResult( - title=title, - url=full_url, - snippet=snippet[:200] if snippet else "", - source=self.name, - rank=i + 1, - category="news", - extra={"region": "cn"}, - )) - - if not results: - search_url = f"{self.SEARCH_URL}?q={quote_plus(query)}" - results.append(SearchResult( - title=f"腾讯新闻: {query}", - url=search_url, - snippet=f"Search Tencent News for '{query}'", - source=self.name, - rank=1, - category="news", - extra={"search_url": search_url, "note": "JS-rendered page"}, - )) - - return results - - async def close(self): - await self._client.aclose() diff --git a/scripts/news/wallstreetcn.py b/scripts/news/wallstreetcn.py deleted file mode 100644 index 2f8b889..0000000 --- a/scripts/news/wallstreetcn.py +++ /dev/null @@ -1,117 +0,0 @@ -""" -华尔街见闻 (wallstreetcn.com) news search engine. - -Scrapes wallstreetcn.com search results for financial news. -""" - -from __future__ import annotations - -import logging -from typing import Optional -from urllib.parse import quote_plus - -import httpx -from bs4 import BeautifulSoup - -from scripts.core.base import SearchEngine, SearchResult - -logger = logging.getLogger(__name__) - - -class WallStreetCNEngine(SearchEngine): - """华尔街见闻 (wallstreetcn.com) news search engine.""" - - SEARCH_URL = "https://wallstreetcn.com/search" - - def __init__(self, config: Optional[dict] = None): - super().__init__("wallstreetcn", 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", - ), - "Referer": "https://wallstreetcn.com/", - }, - follow_redirects=True, - ) - - def build_url(self, query: str, **kwargs) -> str: - return f"{self.SEARCH_URL}?q={quote_plus(query)}" - - async def search( - self, query: str, max_results: int = 10, **kwargs - ) -> list[SearchResult]: - """Search 华尔街见闻 and parse results.""" - url = self.build_url(query) - try: - response = await self._client.get(url) - response.raise_for_status() - return self._parse_results(response.text, query, max_results) - except httpx.HTTPError as e: - logger.error(f"华尔街见闻 search failed: {e}") - return [] - - def _parse_results( - self, html: str, query: str, max_results: int - ) -> list[SearchResult]: - """Parse 华尔街见闻 search page.""" - soup = BeautifulSoup(html, "lxml") - results = [] - - articles = soup.select( - "a[href*='/article/'], .search-result-item, article, .item" - ) - for i, article in enumerate(articles[:max_results]): - title_el = article.select_one("h3, h4, .title, a") - link = article.get("href", "") - snippet_el = article.select_one( - ".summary, .desc, .abstract, p" - ) - - title = title_el.get_text(strip=True) if title_el else "" - snippet = snippet_el.get_text(strip=True) if snippet_el else "" - - if not title and not link: - continue - - full_url = ( - link - if link.startswith("http") - else f"https://wallstreetcn.com{link}" - ) - - results.append( - SearchResult( - title=title or f"华尔街见闻: {query}", - url=full_url, - snippet=snippet or f"Search result from 华尔街见闻", - source=self.name, - rank=i + 1, - category="news", - extra={"region": "cn"}, - ) - ) - - if not results: - search_url = self.build_url(query) - results.append( - SearchResult( - title=f"华尔街见闻: {query}", - url=search_url, - snippet=f"Search 华尔街见闻 for '{query}' (site may require JS)", - source=self.name, - rank=1, - category="news", - extra={"search_url": search_url, "note": "JS-rendered page"}, - ) - ) - - return results - - async def close(self): - await self._client.aclose() diff --git a/scripts/social/__init__.py b/scripts/social/__init__.py deleted file mode 100644 index 90f7f10..0000000 --- a/scripts/social/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Social media search implementations (Twitter/X, etc.).""" diff --git a/scripts/social/twitter.py b/scripts/social/twitter.py deleted file mode 100644 index c72f5ea..0000000 --- a/scripts/social/twitter.py +++ /dev/null @@ -1,208 +0,0 @@ -""" -Twitter/X search module. - -Generates search URLs for X.com (formerly Twitter) search and -optionally parses search result pages. -""" - -from __future__ import annotations - -import logging -from typing import Optional -from urllib.parse import quote_plus - -import httpx -from bs4 import BeautifulSoup - -from scripts.core.base import SearchEngine, SearchResult - -logger = logging.getLogger(__name__) - - -class TwitterSearchEngine(SearchEngine): - """ - Twitter/X search engine. - - Provides search URL generation and optional result scraping. - Supports basic keyword search, user search, and advanced queries. - """ - - SEARCH_URL = "https://twitter.com/search" - BASE_URL = "https://twitter.com" - - def __init__(self, config: Optional[dict] = None): - super().__init__("Twitter", 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", - ), - "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", - "Accept-Language": "en-US,en;q=0.5", - }, - follow_redirects=True, - ) - - def build_url(self, query: str, search_type: str = "top", **kwargs) -> str: - """ - Build Twitter/X search URL. - - Args: - query: Search keyword. - search_type: "top", "latest", "people", "photos", "videos" - **kwargs: Additional URL parameters. - - Returns: - Search URL string. - """ - encoded_query = quote_plus(query) - url = f"{self.SEARCH_URL}?q={encoded_query}&src=typed_query" - - if search_type == "latest": - url += "&f=live" - elif search_type == "people": - url += "&f=user" - elif search_type == "photos": - url += "&f=image" - elif search_type == "videos": - url += "&f=video" - - if kwargs.get("lang"): - url += f"&l={kwargs['lang']}" - if kwargs.get("until"): - url += f"&until={kwargs['until']}" - if kwargs.get("since"): - url += f"&since={kwargs['since']}" - - return url - - def build_advanced_url(self, **params) -> str: - """ - Build advanced Twitter search URL with operators. - - Supported operators: - from:username - Tweets from user - to:username - Tweets to user - @username - Mentions - #hashtag - Hashtag - filter:links - Tweets with links - filter:images - Tweets with images - min_retweets:N - Minimum retweets - min_faves:N - Minimum likes - lang:en - Language - - Returns: - Advanced search URL. - """ - parts = [] - if params.get("from_user"): - parts.append(f"from:{params['from_user']}") - if params.get("to_user"): - parts.append(f"to:{params['to_user']}") - if params.get("hashtag"): - parts.append(f"#{params['hashtag']}") - if params.get("keyword"): - parts.append(params["keyword"]) - if params.get("min_retweets"): - parts.append(f"min_retweets:{params['min_retweets']}") - if params.get("min_faves"): - parts.append(f"min_faves:{params['min_faves']}") - if params.get("lang"): - parts.append(f"lang:{params['lang']}") - if params.get("filter"): - parts.append(f"filter:{params['filter']}") - - query = " ".join(parts) - return self.build_url(query, search_type=params.get("search_type", "top")) - - async def search(self, query: str, max_results: int = 10, search_type: str = "top", **kwargs) -> list[SearchResult]: - """ - Search Twitter/X and optionally parse page content. - - Args: - query: Search keyword. - max_results: Maximum results (used for URL generation). - search_type: "top", "latest", "people", "photos", "videos" - **kwargs: Additional options. - - Returns: - List of SearchResult objects. - """ - url = self.build_url(query, search_type=search_type, **kwargs) - - # Try to scrape the search page for basic info - try: - response = await self._client.get(url) - if response.status_code == 200: - soup = BeautifulSoup(response.text, "lxml") - results = self._parse_page(soup, max_results) - if results: - return results - except Exception as e: - logger.debug(f"Twitter page scraping failed (expected): {e}") - - # Fall back to URL-based result - result_type_map = { - "top": "Top tweets", - "latest": "Latest tweets", - "people": "People", - "photos": "Photos", - "videos": "Videos", - } - desc = result_type_map.get(search_type, "Tweets") - - return [ - SearchResult( - title=f"Twitter: {query} ({desc})", - url=url, - snippet=f"Search Twitter/X for '{query}'. Note: Twitter may require login.", - source=self.name, - rank=1, - category="social", - extra={"search_type": search_type, "twitter_url": url}, - ) - ] - - def _parse_page(self, soup, max_results: int) -> list[SearchResult]: - """Attempt to extract tweet info from search page.""" - results = [] - # Twitter uses JS rendering, so HTML parsing may yield limited results - tweets = soup.select("article[data-testid='tweet'], div.tweet, .content") - for i, tweet in enumerate(tweets[:max_results]): - text_el = tweet.select_one( - "[data-testid='tweetText'], .tweet-text, p[lang]" - ) - link_el = tweet.select_one("a[href*='/status/'], time") - user_el = tweet.select_one( - "[data-testid='User-Name'], .username, .fullname" - ) - - text = text_el.get_text(strip=True) if text_el else "" - user = user_el.get_text(strip=True) if user_el else "" - - tweet_url = "" - if link_el: - href = link_el.get("href", "") - tweet_url = f"{self.BASE_URL}{href}" if href.startswith("/") else href - - if text: - results.append( - SearchResult( - title=f"@{user}: {text[:80]}..." if user else text[:80], - url=tweet_url or self.build_url(text[:30]), - snippet=text, - source=self.name, - rank=i + 1, - category="social", - extra={"user": user} if user else {}, - ) - ) - return results - - async def close(self): - await self._client.aclose() diff --git a/scripts/unified_search.py b/scripts/unified_search.py index ffca8fe..a620626 100644 --- a/scripts/unified_search.py +++ b/scripts/unified_search.py @@ -1,9 +1,17 @@ """ -Unified multi-source search — URL-based engines. +Unified multi-source search — Python API. -Architecture: - Primary: URL engines (Baidu, Bing, Google, DuckDuckGo, Sogou) - No external service dependency. +Provides a clean async interface for programmatic use. +All engines run in parallel with configurable concurrency. + +Usage: + from scripts.unified_search import UnifiedSearch + + us = UnifiedSearch() + resp = await us.search("quantum computing", sources=["bing", "arxiv"], max_results=5) + for r in resp.results: + print(f"[{r.rank}] {r.title}") + await us.close() """ from __future__ import annotations @@ -12,98 +20,161 @@ import asyncio import time from typing import Any, Optional -from dataclasses import dataclass - -from scripts.models import SearchQuery, SearchResponse, SourceRef, SearchMetadata, EngineTiming +from scripts.core.base import BaseEngine, SearchResult +from scripts.engines.bing_scraper import BingScraper +from scripts.engines.sogou_scraper import SogouScraper +from scripts.engines.baidu_scraper import BaiduScraper +from scripts.engines.search360_scraper import Search360Scraper +from scripts.engines.parser_engines import DuckDuckGoParser, BraveParser +from scripts.engines.news_engine import NewsSearch +from scripts.engines.academic_engine import AcademicSearch +from scripts.engines.github_engine import GitHubEngine +from scripts.core.aggregator import ResultAggregator +from scripts.models import SearchResponse, SourceRef, SearchMetadata, EngineTiming -@dataclass -class SearchResult: - title: str - url: str - content: str - source: str = "" - engine: str = "" - score: float = 0.0 - category: str = "" - published: str = "" +# Default engine registry +_ALL_ENGINES: dict[str, BaseEngine] = { + "bing": BingScraper(), + "sogou": SogouScraper(), + "baidu": BaiduScraper(), + "360": Search360Scraper(), + "duckduckgo": DuckDuckGoParser(), + "brave": BraveParser(), + "news": NewsSearch(), + "academic": AcademicSearch(), + "github": GitHubEngine(), +} class UnifiedSearch: - """Unified search across multiple open-source engines.""" + """Unified multi-source search — Python API entry point.""" - def __init__(self): - self._start_time = 0.0 + def __init__(self, timeout: float = 15.0, max_concurrent: int = 5): + self.timeout = timeout + self.max_concurrent = max_concurrent + self._engines: dict[str, BaseEngine] = { + name: eng.__class__(timeout=timeout) + for name, eng in _ALL_ENGINES.items() + } - async def search(self, query: str, max_results: int = 5, - language: str = "zh-CN") -> SearchResponse: - """Search using URL-based engines.""" - self._start_time = time.time() - metadata = SearchMetadata() - metadata.total_engines = 1 + async def search( + self, + query: str, + sources: Optional[list[str]] = None, + max_results: int = 10, + region: str = "cn", + **kwargs, + ) -> SearchResponse: + """ + Search across multiple sources in parallel. - all_results, timings = await self._search_urls(query, max_results) + Args: + query: Search query string + sources: List of engine names to use (default: all) + max_results: Max results to return (deduped) + region: "cn" or "global" + **kwargs: Extra params passed to engines - elapsed_ms = int((time.time() - self._start_time) * 1000) - metadata.duration_ms = elapsed_ms - metadata.engine_timings = timings - metadata.total_raw_results = len(all_results) + Returns: + SearchResponse with results, sources, and metadata + """ + t0 = time.time() - sources = [] - seen_urls = set() - for i, r in enumerate(all_results[:max_results]): - if r.url and r.url not in seen_urls: - seen_urls.add(r.url) - sources.append(SourceRef( - index=i + 1, - title=r.title or r.content[:50], - url=r.url or "", - source=r.source, + # Select engines + if sources: + selected = {k: v for k, v in self._engines.items() if k in sources} + else: + selected = dict(self._engines) + + # Build region kwarg + eng_kwargs = {"region": region} if region else {} + + # Run all engines concurrently with semaphore + semaphore = asyncio.Semaphore(self.max_concurrent) + + async def run_engine(name: str, eng: BaseEngine) -> tuple[str, list[SearchResult], float]: + async with semaphore: + start = time.time() + try: + results = await asyncio.wait_for( + eng.search(query, max_results=max_results, **eng_kwargs), + timeout=eng.timeout + 2.0, + ) + elapsed = time.time() - start + return name, list(results), elapsed + except asyncio.TimeoutError: + return name, [], time.time() - start + except Exception: + return name, [], time.time() - start + + tasks = [run_engine(n, e) for n, e in selected.items()] + done = await asyncio.gather(*tasks, return_exceptions=True) + + # Collect results and timings + all_results: list[SearchResult] = [] + engine_timings: list[EngineTiming] = [] + engines_used: set[str] = set() + + for item in done: + if isinstance(item, tuple): + name, results, elapsed = item + all_results.extend(results) + if results: + engines_used.add(name) + engine_timings.append(EngineTiming( + engine_name=name, + success=bool(results), + duration_ms=int(elapsed * 1000), + result_count=len(results), )) + elif isinstance(item, Exception): + # Exception in gather — log but don't crash + pass - metadata.engines_used = list(set(s.source for s in sources)) + # Deduplicate and rank + aggregator = ResultAggregator() + merged = aggregator.merge_and_rank(all_results, max_results=max_results) + + elapsed_ms = int((time.time() - t0) * 1000) + + # Build SourceRef list + sources_list = [ + SourceRef( + index=r.rank, + title=r.title, + url=r.url, + source=r.source, + ) + for r in merged + ] + + metadata = SearchMetadata( + total_engines=len(selected), + total_raw_results=len(all_results), + engines_used=list(engines_used), + duration_ms=elapsed_ms, + engine_timings=engine_timings, + ) return SearchResponse( query=query, - results=list(all_results[:max_results]), - sources=sources, + results=merged, + sources=sources_list, metadata=metadata, ) - async def _search_urls(self, query: str, max_results: int - ) -> tuple[list[SearchResult], list[EngineTiming]]: - """Build search URLs for major engines.""" - timings = [] - engines = { - "baidu": f"https://www.baidu.com/s?wd={query}", - "bing": f"https://cn.bing.com/search?q={query}", - "google": f"https://www.google.com/search?q={query}", - "duckduckgo": f"https://html.duckduckgo.com/html/?q={query}", - "sogou": f"https://www.sogou.com/web?query={query}", - } - results = [] - for name, url in engines.items(): - results.append(SearchResult( - title=f"{name}: {query}", - url=url, - content=f"Search {query} on {name}", - source=name, - )) - - timings.append(EngineTiming( - engine_name="url_fallback", - success=True, - duration_ms=1, - result_count=len(results), - )) - return results, timings - - async def health(self) -> dict: - """Check status.""" - return { - "status": "ok", - "engines": ["baidu", "bing", "google", "duckduckgo", "sogou"], - } + async def health(self) -> dict[str, Any]: + """Check connectivity of all engines.""" + results = {} + for name, eng in self._engines.items(): + try: + health = await asyncio.wait_for(eng.health(), timeout=5.0) + results[name] = health + except Exception as e: + results[name] = {"name": name, "status": "fail", "error": str(e)} + return results async def close(self): + """Cleanup (no-op for current implementation).""" pass diff --git a/scripts/wechat/__init__.py b/scripts/wechat/__init__.py deleted file mode 100644 index 93f7549..0000000 --- a/scripts/wechat/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""WeChat search engines.""" diff --git a/scripts/wechat/sogou_weixin.py b/scripts/wechat/sogou_weixin.py deleted file mode 100644 index e979fea..0000000 --- a/scripts/wechat/sogou_weixin.py +++ /dev/null @@ -1,365 +0,0 @@ -""" -Sogou WeChat (搜狗微信) search engine. - -Searches WeChat public account articles via weixin.sogou.com. - -Anti-scraping strategy (inspired by Node.js wechat-article-search skill): -- Cookie pre-fetch from v.sogou.com to get SNUID -- User-Agent rotation with a pool of real browser UAs -- Random delays between requests (500-1500ms) -- Retry logic with exponential backoff -""" - -from __future__ import annotations - -import asyncio -import logging -import random -import re -from typing import Optional -from urllib.parse import quote_plus - -import httpx -from bs4 import BeautifulSoup - -from scripts.core.base import SearchEngine, SearchResult - -logger = logging.getLogger(__name__) - -# Real browser User-Agent pool for rotation -USER_AGENTS = [ - # Chrome - "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36", - "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36", - "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36", - "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36", - # Safari - "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.1 Safari/605.1.15", - "Mozilla/5.0 (iPhone; CPU iPhone OS 17_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.1 Mobile/15E148 Safari/604.1", - # Firefox - "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:120.0) Gecko/20100101 Firefox/120.0", - "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:120.0) Gecko/20100101 Firefox/120.0", - # Edge - "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0", -] - - -class SogouWeChatEngine(SearchEngine): - """搜狗微信搜索 - searches WeChat public account articles.""" - - SEARCH_URL = "https://weixin.sogou.com/weixin" - COOKIE_URL = "https://v.sogou.com" - - def __init__(self, config: Optional[dict] = None): - super().__init__("weixin", config) - timeout = (config or {}).get("timeout", 15) - self._ua = random.choice(USER_AGENTS) - self._cookies: dict[str, str] = {} - self._client = httpx.AsyncClient( - timeout=timeout, - headers=self._build_headers(), - follow_redirects=False, # Don't auto-follow to detect anti-scraping - ) - - def _build_headers(self) -> dict[str, str]: - return { - "User-Agent": self._ua, - "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", - "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8", - "Accept-Encoding": "gzip, deflate", - "Referer": "https://weixin.sogou.com/", - "DNT": "1", - "Connection": "keep-alive", - } - - # ---- Anti-scraping enhancements from bot-search-mcp ---- # - - CAPTCHA_KEYWORDS = [ - "请输入验证码", "访问频率过高", "请稍后重试", "验证码", - "captcha", "访问过于频繁", "请输入以下验证码", - "403 Forbidden", "您的访问被拒绝", - ] - - def _random_delay(self, min_s: float = 0.5, max_s: float = 1.5): - """Random delay to mimic human browsing.""" - delay = random.uniform(min_s, max_s) - return asyncio.sleep(delay) - - def _rotate_ua(self): - """Rotate to a new random User-Agent.""" - self._ua = random.choice(USER_AGENTS) - - async def _get_cookie(self) -> dict[str, str]: - """Pre-fetch cookie from v.sogou.com to get SNUID/SUID/IPLOC/SUV.""" - try: - response = await self._client.get( - self.COOKIE_URL, - headers={ - "User-Agent": self._ua, - "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", - "Accept-Language": "zh-CN,zh;q=0.9", - }, - ) - cookies: dict[str, str] = {} - - # Extract from httpx Cookies - for key, value in response.cookies.items(): - if key.upper() in ("SNUID", "SUID", "IPLOC", "SUV"): - cookies[key] = value - - # Also parse from Set-Cookie header (more reliable) - set_cookie = response.headers.get("set-cookie", "") - for part in set_cookie.split(";"): - if "=" in part: - k, v = part.split("=", 1) - k = k.strip() - if k.upper() in ("SNUID", "SUID", "IPLOC", "SUV"): - cookies[k] = v.split(",")[-1].strip() - - logger.debug(f"Got {len(cookies)} cookies from v.sogou.com: {list(cookies.keys())}") - return cookies - except Exception as e: - logger.warning(f"Cookie pre-fetch failed: {e}") - return {} - - @staticmethod - def _detect_captcha(html: str) -> bool: - """检测是否触发了验证码或反爬。""" - html_lower = html.lower() - for kw in SogouWeChatEngine.CAPTCHA_KEYWORDS: - if kw in html_lower or kw.lower() in html_lower: - return True - return False - - async def _exponential_backoff( - self, attempt: int, base_delay: float = 2.0 - ): - """指数退避等待(带随机 jitter)。""" - wait = (2 ** attempt) + random.uniform(0, 1) - await asyncio.sleep(wait) - - def build_url(self, query: str, search_type: int = 2, page: int = 1, **kwargs) -> str: - """ - Build Sogou WeChat search URL. - - Args: - query: Search keyword. - search_type: 2=articles, 1=accounts. - page: Page number (starts at 1). - """ - return ( - f"{self.SEARCH_URL}?query={quote_plus(query)}" - f"&s_from=input&_sug_=n&type={search_type}" - f"&page={page}&ie=utf8" - ) - - async def search( - self, query: str, max_results: int = 10, **kwargs - ) -> list[SearchResult]: - """Search WeChat articles via Sogou and parse results.""" - # Step 1: Get cookie first - if not self._cookies: - self._cookies = await self._get_cookie() - await self._random_delay() - - # Step 2: Search with retry - max_pages = min(5, (max_results + 9) // 10) - all_results = [] - - for page in range(1, max_pages + 1): - url = self.build_url(query, page=page, **kwargs) - results = await self._search_page(url, query, max_results - len(all_results)) - all_results.extend(results) - - if len(all_results) >= max_results: - break - - # Delay between pages - await self._random_delay() - - if not all_results: - # Fallback: try legacy wx.sogou.com - return await self._legacy_search(query, max_results, **kwargs) - - return all_results[:max_results] - - async def _search_page( - self, url: str, query: str, max_results: int - ) -> list[SearchResult]: - """Search a single page with enhanced retry + anti-scraping logic.""" - for attempt in range(3): - try: - # Rotate UA on each retry - if attempt > 0: - self._rotate_ua() - self._client.headers.update({"User-Agent": self._ua}) - - response = await self._client.get( - url, - cookies=self._cookies, - follow_redirects=False, - ) - - # 302 = anti-scraping redirect - if response.status_code == 302: - logger.warning( - f"Sogou anti-scraping triggered (302), " - f"attempt {attempt + 1}" - ) - self._cookies = await self._get_cookie() - await self._exponential_backoff(attempt) - continue - - if response.status_code != 200: - logger.warning( - f"Sogou returned {response.status_code}, " - f"attempt {attempt + 1}" - ) - await self._exponential_backoff(attempt) - continue - - html = response.text - - # Detect captcha / block page - if self._detect_captcha(html): - logger.warning( - f"Sogou captcha/block detected, " - f"attempt {attempt + 1}" - ) - await self._exponential_backoff(attempt) - self._cookies = await self._get_cookie() - continue - - return self._parse_results(html, query, max_results) - - except (httpx.HTTPError, httpx.TimeoutException) as e: - logger.warning( - f"Sogou search attempt {attempt + 1} failed: {e}" - ) - await self._exponential_backoff(attempt) - continue - - return [] - - async def _legacy_search( - self, query: str, max_results: int, **kwargs - ) -> list[SearchResult]: - """Fallback: try the legacy wx.sogou.com domain.""" - legacy_url = f"https://wx.sogou.com/weixin?type=2&query={quote_plus(query)}" - try: - response = await self._client.get( - legacy_url, - cookies=self._cookies, - follow_redirects=True, - ) - if response.status_code == 200: - return self._parse_results(response.text, query, max_results) - except Exception: - pass - - # Ultimate fallback - search_url = self.build_url(query) - return [ - SearchResult( - title=f"微信搜索: {query}", - url=search_url, - snippet=f"Search WeChat for '{query}' via Sogou", - source=self.name, - rank=1, - category="wechat", - extra={"search_url": search_url}, - ) - ] - - def _parse_results( - self, html: str, query: str, max_results: int - ) -> list[SearchResult]: - """Parse Sogou WeChat search results.""" - soup = BeautifulSoup(html, "lxml") - results = [] - - # Main selector: ul.news-list > li (standard Sogou WeChat structure) - items = soup.select("ul.news-list > li") - - if not items: - # Fallback selectors - items = soup.select( - ".news-list2 li, .wx-rb-item, .results .result, " - ".item, .news-box .news-list li" - ) - - for i, item in enumerate(items[:max_results]): - title_el = item.select_one("h3 a") - if not title_el: - title_el = item.select_one(".tit a, .title a, h3") - - snippet_el = item.select_one("p.txt-info, .txt-info, .summary, .des, p.info") - source_el = item.select_one( - ".all-time-y2 a.account, a.account, " - ".account, .source, .s-p, .from" - ) - - title = title_el.get_text(strip=True) if title_el else "" - link = title_el.get("href", "") if title_el and hasattr(title_el, "get") else "" - snippet = snippet_el.get_text(strip=True) if snippet_el else "" - account = source_el.get_text(strip=True) if source_el else "" - - # Parse time from inline scripts or text - time_text = self._parse_time(item) - - if not title: - continue - - # Make sure URL is absolute - if link and not link.startswith("http"): - link = f"https://weixin.sogou.com{link}" - - results.append( - SearchResult( - title=title, - url=link, - snippet=snippet or f"WeChat article from {account or 'unknown'}", - source=self.name, - rank=(i + 1), - category="wechat", - extra={ - "account": account, - "time": time_text, - "search_url": self.build_url(query), - }, - ) - ) - - return results - - def _parse_time(self, item) -> str: - """Extract publish time from search result item.""" - # Try script tag with Unix timestamp (Sogou encodes dates in scripts) - for script in item.select("script"): - text = script.string or "" - # Look for Unix timestamp pattern - match = re.search(r'\d{10}', text) - if match: - import datetime - ts = int(match.group()) - try: - dt = datetime.datetime.fromtimestamp(ts, tz=datetime.timezone(datetime.timedelta(hours=8))) - return dt.strftime("%Y-%m-%d %H:%M") - except (OSError, ValueError): - pass - - # Try inline text with relative time like "2小时前", "3天前" - time_text = item.get_text() - relative_match = re.search(r'(\d+)\s*(小时|分钟|天|周|月)前', time_text) - if relative_match: - return f"{relative_match.group(1)}{relative_match.group(2)}前" - - # Try standard date patterns - date_match = re.search(r'(\d{4}[-/]\d{1,2}[-/]\d{1,2})', time_text) - if date_match: - return date_match.group(1) - - return "" - - async def close(self): - await self._client.aclose() diff --git a/ws.py b/ws.py index f25c280..d88cca7 100644 --- a/ws.py +++ b/ws.py @@ -1,90 +1,22 @@ #!/usr/bin/env python3 """ -web-search-skills: Open-source diversified search in <5s. +Lightweight launcher — delegates to scripts.cli:main(). Usage: - python3 ws.py search [--max-results 5] - python3 ws.py health - python3 ws.py serve # (future) MCP server mode + python3 ws.py search "quantum computing" --max-results 5 + python3 ws.py health + python3 ws.py --help """ -from __future__ import annotations - -import argparse -import asyncio -import json import sys -import time +from pathlib import Path +# Ensure the project root is on the path +_root = Path(__file__).parent +if str(_root) not in sys.path: + sys.path.insert(0, str(_root)) -async def cmd_search(args: argparse.Namespace): - from scripts.unified_search import UnifiedSearch - - us = UnifiedSearch() - t0 = time.time() - resp = await us.search(args.query, max_results=args.max_results) - elapsed = time.time() - t0 - - if args.json: - output = { - "query": resp.query, - "sources": [ - { - "index": s.index, - "title": s.title, - "url": s.url, - "source": s.source, - } - for s in resp.sources - ], - "time_ms": elapsed * 1000, - } - print(json.dumps(output, ensure_ascii=False, indent=2)) - else: - print(f"\n🔍 「{resp.query}」 — {elapsed:.2f}s") - print(f" {len(resp.sources)} results from {len(resp.metadata.engines_used)} engines\n") - for i, s in enumerate(resp.sources, 1): - print(f" {i}. [{s.source}] {s.title}") - print(f" {s.url}") - print() - await us.close() - - -async def cmd_health(args: argparse.Namespace): - from scripts.unified_search import UnifiedSearch - - us = UnifiedSearch() - status = await us.health() - if args.json: - print(json.dumps(status, ensure_ascii=False, indent=2)) - else: - print("📊 Search Health") - print(f" Status: {status.get('status', 'unknown')}") - print(f" Engines: {', '.join(status.get('engines', []))}") - await us.close() - - -def main(): - parser = argparse.ArgumentParser(description="web-search-skills") - sub = parser.add_subparsers(dest="command", required=True) - - # search - sp = sub.add_parser("search", help="Search across multiple sources") - sp.add_argument("query", help="Search query") - sp.add_argument("--max-results", type=int, default=5) - sp.add_argument("--json", action="store_true", help="JSON output") - - # health - hp = sub.add_parser("health", help="Check engine health") - hp.add_argument("--json", action="store_true", help="JSON output") - - args = parser.parse_args() - - if args.command == "search": - asyncio.run(cmd_search(args)) - elif args.command == "health": - asyncio.run(cmd_health(args)) - +from scripts.cli import main if __name__ == "__main__": main()