#!/usr/bin/env python3
"""Quick news fetcher - single file, no deps needed."""
import json
import urllib.request
import ssl
import sys

ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

H = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36"}

def get(url):
    try:
        req = urllib.request.Request(url, headers=H)
        with urllib.request.urlopen(req, timeout=15, context=ctx) as r:
            return r.read().decode("utf-8", errors="replace")
    except Exception as e:
        return None

def getj(url):
    d = get(url)
    return json.loads(d) if d else None

def show_results():
    out = []
    out.append("=" * 65)
    out.append("  🔥  今 日 热 点 新 闻  —  2026年6月20日 (周六)")
    out.append("=" * 65)
    
    # 1. Hacker News
    hn = getj("https://hacker-news.firebaseio.com/v0/topstories.json")
    if hn:
        out.append("\n  🌍 Hacker News (科技)")
        out.append("  " + "─" * 60)
        for i, sid in enumerate(hn[:10], 1):
            item = getj(f"https://hacker-news.firebaseio.com/v0/item/{sid}.json")
            if item:
                t = item.get("title", "")
                s = item.get("score", 0)
                if t:
                    out.append(f"  {i:2d}. {t[:75]}  (👍{s})")
    
    # 2. Reddit worldnews
    rd = get("https://www.reddit.com/r/worldnews/hot/.json?limit=8")
    if rd:
        try:
            data = json.loads(rd)
            out.append("\n  📰 Reddit /r/worldnews (国际新闻)")
            out.append("  " + "─" * 60)
            for i, c in enumerate(data["data"]["children"][:8], 1):
                p = c["data"]
                t = p.get("title", "")
                s = p.get("score", 0)
                if t:
                    out.append(f"  {i:2d}. {t[:75]}  (👍{s})")
        except:
            pass
    
    # 3. GitHub
    gh = getj("https://api.github.com/search/repositories?q=stars:>100000&sort=stars&order=desc&per_page=10")
    if gh:
        out.append("\n  ⭐ GitHub 超级热门开源项目")
        out.append("  " + "─" * 60)
        for i, r in enumerate(gh.get("items", []), 1):
            n = r.get("full_name", "")
            s = r.get("stargazers_count", 0)
            d = (r.get("description") or "")[:60]
            out.append(f"  {i:2d}. {n}  (⭐{s})")
            if d:
                out.append(f"     {d}")
    
    out.append("\n" + "=" * 65)
    out.append("  数据来源: Hacker News API / Reddit / GitHub API")
    out.append("=" * 65)
    
    print("\n".join(out))

if __name__ == "__main__":
    show_results()
