# -*- coding: utf-8 -*- """GITHUB SCANNER — ищет свежие репы по темам (анонимный REST API). Опционально: env GITHUB_TOKEN (rate limit 60/ч анон -> 5000/ч с токеном). Результат: output/github_new.jsonl (инкрементально, дедуп по full_name). Запуск: python gh_scan.py Cron: 0 */2 * * * """ import json import os import sys import time import urllib.parse import urllib.request from pathlib import Path try: sys.stdout.reconfigure(encoding="utf-8", errors="replace") except Exception: pass BASE = Path(__file__).resolve().parent OUT_DIR = BASE / "output" OUT_DIR.mkdir(exist_ok=True) OUT_FILE = OUT_DIR / "github_new.jsonl" QUERIES = [ ("telegram spam", "stars"), ("telegram scraper", "stars"), ("telegram inviter", "stars"), ("telegram mass dm", "stars"), ("telegram autoreg", "stars"), ("telethon", "updated"), ("telegram bot", "updated"), ("telegram proxy", "updated"), ] def load_seen(): seen = set() for fn in os.listdir(OUT_DIR): if fn.endswith(".jsonl"): try: with open(OUT_DIR / fn, encoding="utf-8") as f: for line in f: line = line.strip() if not line: continue try: rec = json.loads(line) except Exception: continue for key in ("fullName", "full_name", "repo", "name"): if key in rec and isinstance(rec[key], str) and "/" in rec[key]: seen.add(rec[key].lower()) except Exception: pass return seen def search(q, sort, per_page=20): url = ("https://api.github.com/search/repositories?q=" + urllib.parse.quote(q) + f"&sort={sort}&order=desc&per_page={per_page}") headers = { "User-Agent": "gh-scan", "Accept": "application/vnd.github+json", } tok = os.environ.get("GITHUB_TOKEN", "") if tok: headers["Authorization"] = "Bearer " + tok req = urllib.request.Request(url, headers=headers) try: with urllib.request.urlopen(req, timeout=30) as r: return json.loads(r.read().decode("utf-8", "ignore")).get("items", []) except Exception as e: print(f"ERR {q}: {e}") return [] def main(): seen = load_seen() print(f"previously seen: {len(seen)}") new_recs = [] batch_new = set() for q, sort in QUERIES: items = search(q, sort) added = 0 for it in items: fn = it["full_name"] low = fn.lower() if low in seen or low in batch_new: continue batch_new.add(low) rec = { "fullName": fn, "stargazersCount": it.get("stargazers_count", 0), "updatedAt": it.get("updated_at", ""), "description": (it.get("description") or "")[:300], "query": q, "foundAt": time.strftime("%Y-%m-%dT%H:%M:%S"), } new_recs.append(rec) added += 1 print(f"{q!r} ({sort}): {len(items)} results, {added} new") time.sleep(4 if not os.environ.get("GITHUB_TOKEN") else 1.5) with open(OUT_FILE, "a", encoding="utf-8") as f: for rec in new_recs: f.write(json.dumps(rec, ensure_ascii=False) + "\n") print(f"TOTAL NEW: {len(new_recs)}") for r in new_recs: print(f" + {r['fullName']} ({r['stargazersCount']}*)") if __name__ == "__main__": main()