|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Small GitHub API radar for public profile signals.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import json |
| 7 | +import os |
| 8 | +import sys |
| 9 | +import urllib.error |
| 10 | +import urllib.parse |
| 11 | +import urllib.request |
| 12 | +from collections import Counter |
| 13 | +from datetime import datetime, timezone |
| 14 | + |
| 15 | + |
| 16 | +API_ROOT = "https://api.github.com" |
| 17 | + |
| 18 | + |
| 19 | +def github_get(path: str): |
| 20 | + token = os.environ.get("GITHUB_TOKEN") |
| 21 | + request = urllib.request.Request(f"{API_ROOT}{path}") |
| 22 | + request.add_header("Accept", "application/vnd.github+json") |
| 23 | + request.add_header("X-GitHub-Api-Version", "2022-11-28") |
| 24 | + if token: |
| 25 | + request.add_header("Authorization", f"Bearer {token}") |
| 26 | + |
| 27 | + try: |
| 28 | + with urllib.request.urlopen(request, timeout=30) as response: |
| 29 | + payload = response.read().decode("utf-8") |
| 30 | + return json.loads(payload) |
| 31 | + except urllib.error.HTTPError as exc: |
| 32 | + detail = exc.read().decode("utf-8", errors="replace") |
| 33 | + raise SystemExit(f"GitHub API error {exc.code}: {detail}") from exc |
| 34 | + |
| 35 | + |
| 36 | +def list_public_repos(username: str) -> list[dict]: |
| 37 | + repos: list[dict] = [] |
| 38 | + page = 1 |
| 39 | + while page <= 4: |
| 40 | + query = urllib.parse.urlencode( |
| 41 | + { |
| 42 | + "per_page": 100, |
| 43 | + "page": page, |
| 44 | + "sort": "updated", |
| 45 | + "direction": "desc", |
| 46 | + } |
| 47 | + ) |
| 48 | + batch = github_get(f"/users/{username}/repos?{query}") |
| 49 | + if not batch: |
| 50 | + break |
| 51 | + repos.extend(batch) |
| 52 | + if len(batch) < 100: |
| 53 | + break |
| 54 | + page += 1 |
| 55 | + return repos |
| 56 | + |
| 57 | + |
| 58 | +def render(username: str) -> str: |
| 59 | + user = github_get(f"/users/{username}") |
| 60 | + repos = list_public_repos(username) |
| 61 | + languages = Counter(repo.get("language") for repo in repos if repo.get("language")) |
| 62 | + recent = sorted(repos, key=lambda repo: repo.get("pushed_at") or "", reverse=True)[:8] |
| 63 | + total_stars = sum(repo.get("stargazers_count", 0) for repo in repos) |
| 64 | + |
| 65 | + lines = [ |
| 66 | + f"# GitHub Radar: {username}", |
| 67 | + "", |
| 68 | + f"Generated: {datetime.now(timezone.utc).isoformat(timespec='seconds')}", |
| 69 | + "", |
| 70 | + "## Profile", |
| 71 | + "", |
| 72 | + f"- Name: {user.get('name') or username}", |
| 73 | + f"- Bio: {user.get('bio') or '(empty)'}", |
| 74 | + f"- Blog: {user.get('blog') or '(empty)'}", |
| 75 | + f"- Public repos: {user.get('public_repos')}", |
| 76 | + f"- Followers: {user.get('followers')}", |
| 77 | + f"- Following: {user.get('following')}", |
| 78 | + f"- Total public stars: {total_stars}", |
| 79 | + "", |
| 80 | + "## Language Signals", |
| 81 | + "", |
| 82 | + ] |
| 83 | + |
| 84 | + if languages: |
| 85 | + for language, count in languages.most_common(8): |
| 86 | + lines.append(f"- {language}: {count}") |
| 87 | + else: |
| 88 | + lines.append("- No language metadata found.") |
| 89 | + |
| 90 | + lines.extend(["", "## Recent Public Repositories", ""]) |
| 91 | + if recent: |
| 92 | + for repo in recent: |
| 93 | + description = repo.get("description") or "(no description)" |
| 94 | + lines.append( |
| 95 | + f"- [{repo['name']}]({repo['html_url']}): {description} " |
| 96 | + f"({repo.get('language') or 'unknown'}, " |
| 97 | + f"{repo.get('stargazers_count', 0)} stars)" |
| 98 | + ) |
| 99 | + else: |
| 100 | + lines.append("- No repositories found.") |
| 101 | + |
| 102 | + return "\n".join(lines) + "\n" |
| 103 | + |
| 104 | + |
| 105 | +def main(argv: list[str]) -> int: |
| 106 | + username = argv[1] if len(argv) > 1 else "myshkin451" |
| 107 | + print(render(username)) |
| 108 | + return 0 |
| 109 | + |
| 110 | + |
| 111 | +if __name__ == "__main__": |
| 112 | + raise SystemExit(main(sys.argv)) |
0 commit comments