Spaces:
Sleeping
Sleeping
Download app/scrapers/github_trending.py from HFShishir/tech-updates: direct link, hf CLI and curl.
- Browser
- Download file 3.29 kB
-
https://huggingface.co/spaces/HFShishir/tech-updates/resolve/main/app/scrapers/github_trending.py
- Command line
-
hf download hf://spaces/HFShishir/tech-updates/app/scrapers/github_trending.py
-
curl -L -o github_trending.py https://huggingface.co/spaces/HFShishir/tech-updates/resolve/main/app/scrapers/github_trending.py
3.29 kB
| import httpx | |
| from bs4 import BeautifulSoup | |
| import logging | |
| from typing import List, Dict, Any | |
| logger = logging.getLogger(__name__) | |
| GITHUB_TRENDING_URL = "https://github.com/trending" | |
| GITHUB_SEARCH_API = "https://api.github.com/search/repositories?q=created:>2026-01-01&sort=stars&order=desc" | |
| async def fetch_github_trending(limit: int = 10) -> List[Dict[str, Any]]: | |
| """ | |
| Fetches trending GitHub repositories. | |
| First tries HTML scraping of github.com/trending, falls back to REST search API if blocked. | |
| """ | |
| repos = [] | |
| headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"} | |
| try: | |
| async with httpx.AsyncClient(timeout=12.0, follow_redirects=True) as client: | |
| res = await client.get(GITHUB_TRENDING_URL, headers=headers) | |
| if res.status_code == 200: | |
| soup = BeautifulSoup(res.text, "html.parser") | |
| rows = soup.select("article.Box-row") | |
| for row in rows[:limit]: | |
| title_elem = row.select_one("h2 a") | |
| desc_elem = row.select_one("p") | |
| lang_elem = row.select_one("span[itemprop='programmingLanguage']") | |
| repo_name = title_elem.get_text(strip=True).replace(" ", "") if title_elem else "Unknown Repo" | |
| repo_link = f"https://github.com{title_elem['href']}" if title_elem and 'href' in title_elem.attrs else "" | |
| desc = desc_elem.get_text(strip=True) if desc_elem else "No description provided." | |
| lang = lang_elem.get_text(strip=True) if lang_elem else "Multi-language" | |
| repos.append({ | |
| "title": f"GitHub Trending: {repo_name}", | |
| "url": repo_link, | |
| "source": "GitHub Trends", | |
| "score": 90, | |
| "language": lang, | |
| "raw_content": f"Repository: {repo_name}\nLanguage: {lang}\nDescription: {desc}" | |
| }) | |
| except Exception as e: | |
| logger.warning(f"Error scraping GitHub trending HTML ({e}), trying GitHub API fallback...") | |
| # Fallback to GitHub Search API if scraping returns no items | |
| if not repos: | |
| try: | |
| async with httpx.AsyncClient(timeout=10.0) as client: | |
| api_res = await client.get("https://api.github.com/search/repositories?q=stars:>500&sort=updated&order=desc", headers=headers) | |
| if api_res.status_code == 200: | |
| items = api_res.json().get("items", [])[:limit] | |
| for item in items: | |
| repos.append({ | |
| "title": f"GitHub Repo: {item.get('full_name')}", | |
| "url": item.get("html_url", ""), | |
| "source": "GitHub Trends", | |
| "score": item.get("stargazers_count", 0), | |
| "language": item.get("language", "Unknown"), | |
| "raw_content": f"Repository: {item.get('full_name')}\nStars: {item.get('stargazers_count')}\nDescription: {item.get('description', '')}" | |
| }) | |
| except Exception as api_err: | |
| logger.error(f"GitHub API fallback failed: {api_err}") | |
| return repos | |