Files
web-search-skills/scripts/engines/github_engine.py
T
openbot-coder c077a7ae40 refactor: cleanup dead code, fix model confusion, streamline CLI
- 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]
2026-05-30 16:25:41 +08:00

87 lines
3.4 KiB
Python

"""GitHub code search engine — uses GitHub API (no key needed for basic search)."""
from __future__ import annotations
import time
import logging
from urllib.parse import quote_plus
import httpx
from scripts.core.base import BaseEngine, SearchResult
logger = logging.getLogger(__name__)
HEADERS = {
"Accept": "application/vnd.github.v3+json",
"User-Agent": "web-search-skills/1.0",
}
class GitHubEngine(BaseEngine):
"""GitHub code & repo search via public API."""
name = "github"
async def search(self, query: str, max_results: int = 10,
search_type: str = "repositories", **kwargs) -> list[SearchResult]:
"""search_type: 'repositories' | 'code' | 'issues'"""
t0 = time.time()
endpoint = f"https://api.github.com/search/{search_type}"
params = {"q": query, "per_page": min(max_results, 30), "sort": "stars"}
try:
async with httpx.AsyncClient(timeout=self.timeout, follow_redirects=True) as client:
resp = await client.get(endpoint, headers=HEADERS, params=params)
resp.raise_for_status()
data = resp.json()
elapsed = time.time() - t0
results = []
for item in data.get("items", [])[:max_results]:
if search_type == "repositories":
results.append(SearchResult(
title=item.get("full_name", ""),
url=item.get("html_url", ""),
snippet=item.get("description", "") or "",
source="github",
category="github",
extra={
"stars": item.get("stargazers_count", 0),
"language": item.get("language", ""),
"updated": item.get("updated_at", ""),
},
))
elif search_type == "code":
results.append(SearchResult(
title=item.get("name", ""),
url=item.get("html_url", ""),
snippet=item.get("text_matches", [{}])[0].get("fragment", "")
if item.get("text_matches") else "",
source="github",
category="github",
extra={
"repo": item.get("repository", {}).get("full_name", ""),
"language": item.get("language", ""),
},
))
elif search_type == "issues":
results.append(SearchResult(
title=item.get("title", ""),
url=item.get("html_url", ""),
snippet=item.get("body", "")[:300] if item.get("body") else "",
source="github",
category="github",
extra={
"state": item.get("state", ""),
"repo": item.get("repository_url", "").split("/")[-2:]
if item.get("repository_url") else [],
},
))
logger.info(f"[github] {len(results)} results in {elapsed:.2f}s")
return results
except Exception as e:
logger.warning(f"[github] failed: {e}")
return []