| 9 | API = "https://api.github.com/users/{}" |
| 10 | |
| 11 | def fetch_user(username: str) -> dict: |
| 12 | headers = { |
| 13 | "Accept": "application/vnd.github+json", |
| 14 | "User-Agent": "coauthor-line-script", |
| 15 | } |
| 16 | token = os.getenv("GITHUB_TOKEN") or os.getenv("GH_TOKEN") |
| 17 | if token: |
| 18 | headers["Authorization"] = f"Bearer {token}" |
| 19 | |
| 20 | req = urllib.request.Request(API.format(username), headers=headers) |
| 21 | try: |
| 22 | with urllib.request.urlopen(req) as resp: |
| 23 | return json.load(resp) |
| 24 | except urllib.error.HTTPError as e: |
| 25 | msg = e.read().decode("utf-8", "replace") |
| 26 | print(f"error: GitHub API returned {e.code} {e.reason} for '{username}'", file=sys.stderr) |
| 27 | if msg.strip(): |
| 28 | print(msg, file=sys.stderr) |
| 29 | raise SystemExit(2) |
| 30 | |
| 31 | def main(argv: list[str]) -> int: |
| 32 | if len(argv) != 2 or argv[1] in ("-h", "--help"): |