Fix leaderboard metadata (#99)

* fetch contributor metadata in leaderboard generator

* Refactor profile fetching
This commit is contained in:
guyeisenkot
2025-07-29 11:51:37 +03:00
committed by GitHub
parent 2bf4c7a32b
commit 762011f1f3
2 changed files with 53 additions and 3 deletions

View File

@@ -1,11 +1,14 @@
#!/usr/bin/env python3
"""Generate aggregated contributor leaderboard data."""
import json
import os
from pathlib import Path
from collections import defaultdict
from datetime import datetime
import yaml
from github_utils import fetch_profile
def parse_front_matter(md_path):
try:
text = md_path.read_text(encoding='utf-8')
@@ -68,6 +71,20 @@ def main():
top_users = {u['name'] for u in output}
# Load existing contributor data to reuse cached profiles
assets_dir = Path('assets/data')
assets_dir.mkdir(parents=True, exist_ok=True)
existing = {}
existing_path = assets_dir / 'contributors.json'
if existing_path.exists():
try:
with open(existing_path, 'r', encoding='utf-8') as f:
existing = json.load(f)
except Exception:
existing = {}
token = os.getenv('GITHUB_TOKEN')
contributors = {}
for user in top_users:
d = users[user]
@@ -75,11 +92,17 @@ def main():
{'slug': s, 'title': d['entry_titles'][s]}
for s in sorted(d['entry_titles'])
]
contributors[user] = {
info = {
'repos': sorted(d['repos']),
'entries': entries,
'comments': {k: v for k, v in d['comments'].items()}
}
profile = existing.get(user, {}).get('profile')
if profile is None:
profile = fetch_profile(user, token)
if profile is not None:
info['profile'] = profile
contributors[user] = info
data_dir = Path('_data')
data_dir.mkdir(exist_ok=True)
@@ -87,8 +110,6 @@ def main():
json.dump(output, f, indent=2, ensure_ascii=False)
print(f'Wrote {len(output)} contributors to _data/leaderboard.json')
assets_dir = Path('assets/data')
assets_dir.mkdir(parents=True, exist_ok=True)
with open(assets_dir / 'contributors.json', 'w', encoding='utf-8') as f:
json.dump(contributors, f, indent=2, ensure_ascii=False)
print(f'Wrote {len(contributors)} users to assets/data/contributors.json')

29
github_utils.py Normal file
View File

@@ -0,0 +1,29 @@
import json
from urllib.request import Request, urlopen
from urllib.error import URLError, HTTPError
PROFILE_FIELDS = [
'location',
'company',
'blog',
'twitter_username',
'email',
'site_admin',
'followers',
'following',
]
def fetch_profile(user, token=None):
"""Fetch GitHub profile data for a user and return selected fields."""
url = f'https://api.github.com/users/{user}'
headers = {'Accept': 'application/vnd.github+json'}
if token:
headers['Authorization'] = f'Bearer {token}'
req = Request(url, headers=headers)
try:
with urlopen(req, timeout=10) as resp:
data = json.load(resp)
except (HTTPError, URLError, OSError) as exc:
print(f'Failed to fetch profile for {user}: {exc}')
return None
return {k: data.get(k) for k in PROFILE_FIELDS if data.get(k) is not None}