Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /ctrl-gaurav_effGen /website /scripts /gen_github_data.py
| #!/usr/bin/env python3 | |
| """Snapshot the repository's public counts into ``data/github.json``. | |
| The site shows stars, forks and contributors in three places. It reads them from | |
| the GitHub API in the visitor's browser, which is the right source but not a | |
| reliable one: the unauthenticated API allows 60 requests an hour per address, and | |
| a visitor who is over that limit, offline, or behind something that blocks the | |
| call used to see zeros — or, in the hero, nothing at all. | |
| This writes the last known-good figures to a file the site ships, so the counts | |
| render before the network answers and stay rendered if it never does. The live | |
| lookup still wins whenever it succeeds; this is only the floor. | |
| python scripts/gen_github_data.py # write data/github.json | |
| python scripts/gen_github_data.py --stdout # print, write nothing | |
| Unlike ``gen_site_data.py`` this one needs the network, so it is deliberately a | |
| separate script: ``gen_site_data.py --check`` stays offline and deterministic. | |
| Re-run this when the numbers are worth refreshing; nothing breaks if it is stale. | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| import re | |
| import sys | |
| import urllib.request | |
| from datetime import datetime, timezone | |
| from pathlib import Path | |
| REPO = Path(__file__).resolve().parent.parent | |
| OUT = REPO / "data" / "github.json" | |
| SLUG = "ctrl-gaurav/effGen" | |
| API = "https://api.github.com" | |
| def _get(url: str): | |
| request = urllib.request.Request( | |
| url, headers={"Accept": "application/vnd.github+json", "User-Agent": "effgen-site-build"} | |
| ) | |
| with urllib.request.urlopen(request, timeout=30) as response: | |
| return json.loads(response.read()), dict(response.headers) | |
| def contributors_count() -> int: | |
| """Total contributors, read off the pagination header. | |
| Asking for one per page makes the last page number the total, which avoids | |
| walking every page just to count them. | |
| """ | |
| _, headers = _get(f"{API}/repos/{SLUG}/contributors?per_page=1&anon=false") | |
| link = headers.get("Link", "") | |
| match = re.search(r'[?&]page=(\d+)>;\s*rel="last"', link) | |
| if match: | |
| return int(match.group(1)) | |
| # No Link header means a single page; count what came back. | |
| people, _ = _get(f"{API}/repos/{SLUG}/contributors?per_page=100&anon=false") | |
| return len(people) | |
| def main() -> int: | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("--stdout", action="store_true", help="print, write nothing") | |
| args = parser.parse_args() | |
| repo, _ = _get(f"{API}/repos/{SLUG}") | |
| data = { | |
| "_comment": ( | |
| "Generated by scripts/gen_github_data.py. The last known-good repository " | |
| "counts, shipped so the site renders them without the network. The live " | |
| "GitHub API still wins when it answers." | |
| ), | |
| "derived_at": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"), | |
| "slug": repo["full_name"], | |
| "stars": repo["stargazers_count"], | |
| "forks": repo["forks_count"], | |
| "watchers": repo["subscribers_count"], | |
| "open_issues": repo["open_issues_count"], | |
| "contributors": contributors_count(), | |
| } | |
| text = json.dumps(data, indent=2) + "\n" | |
| if args.stdout: | |
| sys.stdout.write(text) | |
| return 0 | |
| OUT.write_text(text) | |
| print(f"{OUT.relative_to(REPO)}: {data['stars']} stars · {data['forks']} forks · " | |
| f"{data['contributors']} contributors") | |
| return 0 | |
| if __name__ == "__main__": | |
| raise SystemExit(main()) | |
Xet Storage Details
- Size:
- 3.53 kB
- Xet hash:
- 16099df6566f446ea142330e170808bc79aceaf6c30dfe200d779b293312ef02
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.