Repository navigation
Expand file tree
/
Copy pathupdate_stats.py
More file actions
86 lines (71 loc) · 2.96 KB
/
Copy pathupdate_stats.py
File metadata and controls
86 lines (71 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""Generate profile cards from public GitHub data using the authenticated gh CLI."""
import json
import subprocess
from datetime import datetime, timezone
from html import escape
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
USER = "specode"
def api(endpoint):
result = subprocess.run(
["gh", "api", endpoint], check=True, capture_output=True, text=True
)
try:
return json.loads(result.stdout)
except json.JSONDecodeError as error:
raise RuntimeError(f"GitHub returned invalid JSON for {endpoint}") from error
def public_repos():
repos = []
page = 1
while True:
batch = api(f"users/{USER}/repos?type=owner&per_page=100&page={page}")
repos.extend(repo for repo in batch if not repo["fork"] and not repo["private"])
if len(batch) < 100:
return repos
page += 1
def text(x, y, value, size=14, color="var(--text)", weight=400):
return f'<text x="{x}" y="{y}" font-size="{size}" fill="{color}" font-weight="{weight}">{escape(str(value))}</text>'
def card(width, title, body, description):
return f'''<svg xmlns="http://www.w3.org/2000/svg" width="{width}" height="260" viewBox="0 0 {width} 260" role="img" aria-labelledby="title desc">
<title id="title">{escape(title)}</title>
<desc id="desc">{escape(description)}</desc>
<style>
svg {{ --bg: #ffffff; --text: #24292f; --muted: #57606a; --border: #d0d7de; --accent: #2563eb; --track: #eaeef2; }}
@media (prefers-color-scheme: dark) {{ svg {{ --bg: #0d1117; --text: #e6edf3; --muted: #9da7b3; --border: #30363d; --accent: #79a8ff; --track: #21262d; }} }}
text {{ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; }}
</style>
<rect x="0.5" y="0.5" width="{width - 1}" height="259" rx="10" fill="var(--bg)" stroke="var(--border)"/>
{text(24, 38, title, 20, "var(--accent)", 600)}
{body}
</svg>
'''
def main():
repos = public_repos()
user = api(f"users/{USER}")
stats = [
("Stars earned", sum(repo["stargazers_count"] for repo in repos)),
("Public original repositories", len(repos)),
("Followers", user["followers"]),
("Forks of original repositories", sum(repo["forks_count"] for repo in repos)),
]
body = []
for index, (label, value) in enumerate(stats):
y = 84 + index * 39
body.append(text(24, y, label, 15))
body.append(text(382, y, f"{value:,}", 18, weight=600))
updated = datetime.now(timezone.utc).strftime("%Y-%m-%d")
body.append(
text(24, 244, f"Public data only · updated {updated} UTC", 11, "var(--muted)")
)
stats_svg = card(
470,
"Specode's GitHub Stats",
"\n".join(body),
"; ".join(f"{label}: {value}" for label, value in stats),
)
# Fetch everything successfully before replacing the last good card.
assets = ROOT / "assets"
assets.mkdir(exist_ok=True)
(assets / "stats.svg").write_text(stats_svg, encoding="utf-8")
if __name__ == "__main__":
main()