""" Content Feed Discovery Engine - pulls trending topics from real sources. HN, Reddit, GitHub, Google News, RSS. No auth required. """ import json, time, requests, re from datetime import datetime from urllib.parse import quote # Vertical → keywords for feed matching VERTICALS = { "ai": ["ai", "machine learning", "llm", "gpt", "neural", "transformer", "openai", "deepmind", "anthropic"], "tech": ["tech", "startup", "saas", "cloud", "api", "software", "apple", "google", "microsoft", "aws"], "science": ["science", "research", "nasa", "space", "physics", "biology", "chemistry", "quantum", "climate"], "crypto": ["crypto", "bitcoin", "ethereum", "defi", "nft", "web3", "blockchain", "solana", "stablecoin"], "linux": ["linux", "kernel", "ubuntu", "debian", "fedora", "arch", "bash", "systemd", "gnome", "kde"], "gaming": ["game", "gaming", "steam", "playstation", "xbox", "nintendo", "esports", "unreal", "unity"], "diy": ["diy", "maker", "3d print", "raspberry", "arduino", "woodwork", "electronics", "repair", "build"], "guides": ["how to", "guide", "tutorial", "tips", "productivity", "learn", "setup", "configure"], } def fetch_hn_top(): """Hacker News top stories - returns list of {title, url, score}.""" try: r = requests.get("https://hacker-news.firebaseio.com/v0/topstories.json", timeout=10) ids = r.json()[:20] stories = [] for sid in ids[:20]: item = requests.get(f"https://hacker-news.firebaseio.com/v0/item/{sid}.json", timeout=5).json() if item and item.get("title"): stories.append({"title": item["title"], "url": item.get("url",""), "score": item.get("score",0), "source": "hackernews"}) return stories except Exception as e: print(f" HN fetch failed: {e}") return [] def fetch_reddit_hot(subreddit="all", limit=15): """Reddit hot posts via RSS.""" try: headers = {"User-Agent": "Hermes/1.0"} url = f"https://www.reddit.com/r/{subreddit}/hot.json?limit={limit}" r = requests.get(url, headers=headers, timeout=10) posts = r.json().get("data", {}).get("children", []) return [{"title": p["data"]["title"], "url": p["data"]["url"], "score": p["data"]["score"], "source": f"reddit/r/{subreddit}"} for p in posts] except Exception as e: print(f" Reddit fetch failed: {e}") return [] def fetch_github_trending(): """GitHub trending repos.""" try: r = requests.get("https://api.github.com/search/repositories?q=stars:>100+pushed:>2026-07-01&sort=stars&per_page=15", timeout=10) repos = r.json().get("items", []) return [{"title": f"{repo['full_name']}: {repo.get('description','')}", "url": repo["html_url"], "score": repo["stargazers_count"], "source": "github"} for repo in repos] except Exception as e: print(f" GitHub fetch failed: {e}") return [] def fetch_google_news(topic="technology"): """Google News RSS.""" try: url = f"https://news.google.com/rss/search?q={quote(topic)}&hl=en-US&gl=US&ceid=US:en" r = requests.get(url, timeout=10) titles = re.findall(r"