From 1f427f10b70329404617ca6abe2dd5d5e0a35e77 Mon Sep 17 00:00:00 2001 From: shay Date: Wed, 18 Feb 2026 12:18:47 -0500 Subject: [PATCH] feat(generate): added initial generate script --- src/generate.py | 1050 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 1050 insertions(+) create mode 100644 src/generate.py diff --git a/src/generate.py b/src/generate.py new file mode 100644 index 0000000..bd30a16 --- /dev/null +++ b/src/generate.py @@ -0,0 +1,1050 @@ +""" +GitHub Profile Widget Generator +Generates polished SVG mini-widgets from GitHub profile data. +Each widget is composable into a single unified profile card. +""" + +import requests +import math +import base64 +from datetime import datetime, timedelta +from collections import defaultdict +from io import BytesIO +from typing import Optional +from dataclasses import dataclass, field + +# ─── Theme ──────────────────────────────────────────────────────────────────── + +THEMES = { + "dark": { + "bg": "#121820", + "card_bg": "#1a2230", + "card_border": "#2a3444", + "text": "#d1d9e0", + "text_secondary": "#7d8895", + "accent": "#58a6ff", + "green": "#3fb950", + "orange": "#d29922", + "red": "#f85149", + "purple": "#bc8cff", + "pink": "#f778ba", + "grid": "#1e2836", + }, + "light": { + "bg": "#ffffff", + "card_bg": "#f6f8fa", + "card_border": "#d8dee4", + "text": "#24292f", + "text_secondary": "#656d76", + "accent": "#0969da", + "green": "#1a7f37", + "orange": "#9a6700", + "red": "#cf222e", + "purple": "#8250df", + "pink": "#bf3989", + "grid": "#eaeef2", + }, +} + +# ─── Data Classes ───────────────────────────────────────────────────────────── + +@dataclass +class GradeData: + grade: str # e.g. "A++", "S+", "B-" + score: float # 0-100 + stats: dict # {"commits": 1423, "prs": 87, "stars": 342, "repos": 28, "followers": 156} + tags: list # list of TagData + breakdown: dict = field(default_factory=dict) # optional bar breakdown + +@dataclass +class TagData: + tag: str + source: str = "earned" # "earned" | "chosen" + confidence: float = 1.0 + +@dataclass +class ImpactWeek: + week_start: str # ISO date + commits: int = 0 + additions: int = 0 + deletions: int = 0 + +@dataclass +class CollaboratorData: + username: str + avatar_b64: str = "" # base64 encoded avatar + shared_repos: int = 0 + shared_commits: int = 0 + +@dataclass +class FocusCategory: + category: str + percentage: float + commit_count: int = 0 + +@dataclass +class LanguageData: + language: str + percentage: float + loc: int = 0 # lines of code or repo count + +@dataclass +class AchievementData: + title: str + subtitle: str = "" + event_date: str = "" + icon: str = "trophy" # trophy, medal, star, hackathon + + +# ─── SVG Helpers ────────────────────────────────────────────────────────────── + +def _escape(text: str) -> str: + return text.replace("&", "&").replace("<", "<").replace(">", ">").replace('"', """) + + +def _card_wrapper(inner_svg: str, width: int, height: int, theme: dict, title: str = "") -> str: + """Wraps content in a styled card with rounded corners and border.""" + return f''' + + + + + + + + {f'{_escape(title.upper())}' if title else ''} + + {inner_svg} + +''' + + + +# ─── SVG stat icons (14x14) ────────────────────────────────────────────────── + +def _icon_svg(kind: str, color: str) -> str: + icons = { + "commits": f'', + "prs": f'', + "stars": f'', + "repos": f'', + "followers": f'', + } + return f'{icons.get(kind, "")}' + + +TAG_COLORS = { + "ml-engineer": "#bc8cff", "frontend": "#58a6ff", "backend": "#3fb950", + "fullstack": "#d29922", "devops": "#f0883e", "database": "#f778ba", + "mobile": "#ff6b9d", "security": "#f85149", "data-science": "#79c0ff", + "systems": "#7ee787", "cloud": "#58a6ff", "open-source": "#3fb950", +} + +GRADE_COLORS = { + "S": "#ff6b9d", "A": "#3fb950", "B": "#58a6ff", "C": "#d29922", + "D": "#f0883e", "F": "#f85149", +} + +def render_grade_widget(data: GradeData, theme_name: str = "dark") -> str: + t = THEMES[theme_name] + base = data.grade[0] + color = GRADE_COLORS.get(base, t["accent"]) + score = data.score + + radius = 36 + circumference = 2 * math.pi * radius + offset = circumference * (1 - score / 100) + + grade_font = 30 if len(data.grade) <= 2 else 22 + + # ── Stats row with icons ── + stat_labels = {"commits": "Commits", "prs": "PRs", "stars": "Stars", "repos": "Repos", "followers": "Followers"} + stat_keys = [k for k in ["commits", "prs", "stars", "repos", "followers"] if k in data.stats] + stats_svg = "" + stat_w = 340 / max(len(stat_keys), 1) + for i, key in enumerate(stat_keys): + val = data.stats[key] + sx = i * stat_w + stat_w / 2 + val_str = f"{val:,}" if val < 100000 else f"{val/1000:.0f}k" + stats_svg += f''' + + {_icon_svg(key, t["text_secondary"])} + {val_str} + {stat_labels.get(key, key)} + ''' + + # ── Tag pills (no dots) ── + tags_svg = "" + tx, ty = 0, 0 + max_tw = 340 + for tag in (data.tags or []): + tag_color = TAG_COLORS.get(tag.tag, t["accent"]) + label = tag.tag.replace("-", " ").title() + tw = int(len(label) * 6.6 + 18) + pill_opacity = 0.9 if tag.source == "earned" else 0.55 + + if tx + tw > max_tw: + tx = 0 + ty += 30 + + tags_svg += f''' + + + + {_escape(label)} + ''' + tx += tw + 6 + + tags_h = ty + 24 if data.tags else 0 + + # ── Layout ── + stats_y = 100 + tags_y = stats_y + 54 + # Add extra padding if there are multiple rows of tags + tag_padding = 18 if tags_h > 24 else 14 + card_h = (tags_y + tags_h + tag_padding) if data.tags else (stats_y + 54) + + inner = f''' + + + + + + {_escape(data.grade)} + {score:.0f} / 100 + + + + Developer Profile + Grade {_escape(data.grade)} · {score:.0f}/100 + + + + {stats_svg} + ''' + + if data.tags: + inner += f''' + + + {tags_svg} + ''' + + return _card_wrapper(inner, 380, card_h, t, "") + + +# ─── Impact Timeline Widget ────────────────────────────────────────────────── + +def render_impact_widget(weeks: list[ImpactWeek], theme_name: str = "dark", period: str = "6mo") -> str: + t = THEMES[theme_name] + if not weeks: + return "" + + w, h = 380, 200 + chart_x, chart_y = 40, 8 + chart_w, chart_h = 316, 82 + + max_commits = max((wk.commits for wk in weeks), default=1) or 1 + n = len(weeks) + + # Build area path + points = [] + for i, wk in enumerate(weeks): + px = chart_x + (i / max(n - 1, 1)) * chart_w + py = chart_y + chart_h - (wk.commits / max_commits) * chart_h + points.append((px, py)) + + if not points: + return "" + + # Smooth curve using cardinal spline + path_d = f"M {points[0][0]:.1f} {points[0][1]:.1f}" + for i in range(1, len(points)): + cx = (points[i-1][0] + points[i][0]) / 2 + path_d += f" C {cx:.1f} {points[i-1][1]:.1f}, {cx:.1f} {points[i][1]:.1f}, {points[i][0]:.1f} {points[i][1]:.1f}" + + area_d = path_d + f" L {points[-1][0]:.1f} {chart_y + chart_h} L {points[0][0]:.1f} {chart_y + chart_h} Z" + + # Y-axis labels + y_labels = "" + for i in range(3): + val = int(max_commits * (1 - i / 2)) + yy = chart_y + (i / 2) * chart_h + y_labels += f'{val}' + y_labels += f'' + + # X-axis labels (first, mid, last) + x_labels = "" + label_indices = [0, n // 2, n - 1] if n > 2 else list(range(n)) + for idx in label_indices: + if idx < len(weeks): + px = chart_x + (idx / max(n - 1, 1)) * chart_w + x_labels += f'{weeks[idx].week_start[:7]}' + + # Total commits - aligned with left edge, stacked layout + total = sum(wk.commits for wk in weeks) + summary_y = chart_y + chart_h + 46 + + inner = f''' + + + + + + + {y_labels} + {x_labels} + + + + {total:,} + commits over {period} + ''' + + return _card_wrapper(inner, w, h, t, "Impact Timeline") + +# ─── Collaborators Widget ───────────────────────────────────────────────────── + +def render_collaborators_widget(collabs: list[CollaboratorData], theme_name: str = "dark") -> str: + t = THEMES[theme_name] + items = "" + for i, c in enumerate(collabs[:5]): + y = i * 50 + avatar_el = "" + if c.avatar_b64: + avatar_el = f'''''' + else: + hue = hash(c.username) % 360 + avatar_el = f''' + + {_escape(c.username[0].upper())}''' + + bar_max = max((x.shared_commits for x in collabs[:5]), default=1) or 1 + bar_w = c.shared_commits / bar_max * 120 + + items += f''' + + {avatar_el} + {_escape(c.username)} + {c.shared_repos} repos · {c.shared_commits} commits + + + + + ''' + + total_h = len(collabs[:5]) * 50 + 48 + return _card_wrapper(items, 380, total_h, t, "Top Collaborators") + + + +# ─── Focus Score Widget ─────────────────────────────────────────────────────── + + +# ─── Focus Areas Widget (horizontal bars) ──────────────────────────────────── + +FOCUS_COLORS = ["#58a6ff", "#3fb950", "#bc8cff", "#d29922", "#f0883e", "#f778ba", "#f85149", "#79c0ff"] + +def render_focus_widget(categories: list[FocusCategory], theme_name: str = "dark", period: str = "1y") -> str: + t = THEMES[theme_name] + cats = sorted(categories, key=lambda c: -c.percentage)[:6] + if not cats: + return "" + + # Format period for display + period_map = { + "1w": "last week", "1m": "last month", "3m": "last 3 months", + "6m": "last 6 months", "1y": "last year", "all": "all time" + } + period_display = period_map.get(period, period) + + max_pct = cats[0].percentage if cats else 100 + bar_max_w = 210 + items = "" + + for i, cat in enumerate(cats): + color = FOCUS_COLORS[i % len(FOCUS_COLORS)] + y = i * 36 + bar_w = (cat.percentage / max_pct) * bar_max_w + + items += f''' + + {_escape(cat.category)} + + + + + {cat.commit_count} commits + {cat.percentage:.0f}% + ''' + + # Add period subtitle at the bottom + subtitle_y = len(cats) * 36 + 8 + items += f''' + Recent activity · {period_display}''' + + card_h = len(cats) * 36 + 54 + return _card_wrapper(items, 380, card_h, t, "Recent Focus") + + +# ─── Languages Widget (donut chart) ────────────────────────────────────────── + +LANG_COLORS = { + "Python": "#3572A5", "JavaScript": "#f1e05a", "TypeScript": "#3178c6", + "Go": "#00ADD8", "Rust": "#dea584", "Java": "#b07219", "C++": "#f34b7d", + "C": "#555555", "Ruby": "#701516", "Shell": "#89e051", "HTML": "#e34c26", + "CSS": "#563d7c", "Kotlin": "#A97BFF", "Swift": "#F05138", + "Jupyter Notebook": "#DA5B0B", "Dockerfile": "#384d54", +} + +def render_languages_widget(languages: list[LanguageData], theme_name: str = "dark") -> str: + t = THEMES[theme_name] + langs = sorted(languages, key=lambda l: -l.percentage)[:6] + if not langs: + return "" + + # Calculate center position to align with legend + legend_start = 20 + legend_height = len(langs) * 22 + cy = legend_start + legend_height / 2 + cx, r = 60, 44 + inner_r = 28 + circumference = 2 * math.pi * r + + arcs = "" + legend = "" + offset = 0 + + for i, lang in enumerate(langs): + color = LANG_COLORS.get(lang.language, FOCUS_COLORS[i % len(FOCUS_COLORS)]) + dash = circumference * lang.percentage / 100 + gap = circumference - dash + + arcs += f''' + + + ''' + offset += dash + + ly = i * 22 + legend += f''' + + + {_escape(lang.language)} + {lang.percentage:.0f}% + ''' + + # Center: just the top language name, centered vertically + top_lang = langs[0].language if langs else "" + center = f''' + + {_escape(top_lang)}''' + + inner = f''' + + {arcs} + {center} + {legend} + ''' + + rows_h = max(len(langs) * 22 + 40, 120) + return _card_wrapper(inner, 380, rows_h + 36, t, "Languages") + + +# ─── Achievements Widget ───────────────────────────────────────────────────── + +def _achievement_icon_svg(icon_type: str, color: str) -> str: + """Returns SVG path for achievement icons (24x24 centered).""" + icons = { + "trophy": f''' + + ''', + "medal": f''' + + + + + ''', + "star": f''' + + ''', + "hackathon": f''' + + + + ''', + } + return icons.get(icon_type, icons["trophy"]) + +def render_achievements_widget(achievements: list[AchievementData], theme_name: str = "dark", max_items: int = 5) -> str: + t = THEMES[theme_name] + items = "" + accent_colors = [t["orange"], t["green"], t["accent"], t["purple"], t["pink"]] + + # Limit achievements to max_items + shown = achievements[:max_items] + if not shown: + return "" + + for i, ach in enumerate(shown): + y = i * 56 + color = accent_colors[i % len(accent_colors)] + icon_svg = _achievement_icon_svg(ach.icon, color) + + items += f''' + + + + + {icon_svg} + + {_escape(ach.title)} + {_escape(ach.subtitle)}{(" · " + ach.event_date) if ach.event_date else ""} + ''' + + # Adjust height based on actual number of items shown + total_h = len(shown) * 56 + 50 + return _card_wrapper(items, 380, total_h, t, "Achievements") + +# ─── Composite Widget Builder ───────────────────────────────────────────────── + +def compose_widget( + widgets: dict[str, str], + enabled: list[str], + theme_name: str = "dark", + username: str = "", + avatar_b64: str = "", +) -> str: + """ + Takes rendered SVG strings for each widget type and composes them + into a single unified profile widget. + """ + import re as _re + t = THEMES[theme_name] + total_w = 420 + header_h = 60 + padding = 20 + gap = 16 + + # Parse each widget's height from its SVG + parts = [] + for key in enabled: + if key in widgets and widgets[key]: + svg = widgets[key] + hm = _re.search(r'height="(\d+)"', svg) + h = int(hm.group(1)) if hm else 160 + parts.append((key, svg, h)) + + content_h = sum(h for _, _, h in parts) + gap * max(len(parts) - 1, 0) + total_h = header_h + content_h + padding * 2 + 10 + + # Header + avatar_svg = "" + if avatar_b64: + avatar_svg = f''' + + ''' + else: + avatar_svg = f'' + + header = f''' + {avatar_svg} + {_escape(username or "Developer")} + GitHub Profile Widget + ''' + + # Stack widgets via base64 data URI embedding + y_offset = header_h + padding + embedded = "" + for key, svg, h in parts: + svg_b64 = base64.b64encode(svg.encode("utf-8")).decode("ascii") + embedded += f''' + ''' + y_offset += h + gap + + return f''' + + + {header} + {embedded} + Generated with ♥ +''' + + +# ─── GitHub API Fetcher ─────────────────────────────────────────────────── + +# ─── GitHub API Fetcher ─────────────────────────────────────────────────────── + +def fetch_github_data(username: str, token: Optional[str] = None) -> dict: + """ + Fetches all relevant GitHub data for a user. + Returns a dict matching the structure expected by the DB / widget renderers. + """ + headers = {"Accept": "application/vnd.github.v3+json"} + if token: + headers["Authorization"] = f"token {token}" + + base = "https://api.github.com" + + # User profile + user = requests.get(f"{base}/users/{username}", headers=headers).json() + + # Repos (up to 100) + repos = requests.get( + f"{base}/users/{username}/repos", + headers=headers, + params={"per_page": 100, "sort": "pushed", "type": "owner"}, + ).json() + + # Events (recent activity) + events = requests.get( + f"{base}/users/{username}/events", + headers=headers, + params={"per_page": 100}, + ).json() + + # Avatar as base64 + avatar_b64 = "" + if user.get("avatar_url"): + try: + resp = requests.get(user["avatar_url"] + "&s=64", timeout=5) + if resp.ok: + avatar_b64 = base64.b64encode(resp.content).decode("ascii") + except Exception: + pass + + return { + "user": user, + "repos": repos if isinstance(repos, list) else [], + "events": events if isinstance(events, list) else [], + "avatar_b64": avatar_b64, + } + + +# ─── Data Processing (GitHub API → Widget Data) ────────────────────────────── + +def compute_grade(github_data: dict) -> GradeData: + """Compute a developer grade from GitHub profile data.""" + user = github_data["user"] + repos = github_data["repos"] + events = github_data["events"] + + repo_count = min(len(repos), 50) + stars = sum(r.get("stargazers_count", 0) for r in repos) + forks = sum(r.get("forks_count", 0) for r in repos) + followers = user.get("followers", 0) + commits = sum(len(ev.get("payload", {}).get("commits", [])) for ev in events if ev.get("type") == "PushEvent") + prs = sum(1 for ev in events if ev.get("type") == "PullRequestEvent") + + scores = { + "repos": min(repo_count / 30 * 100, 100), + "stars": min(stars / 200 * 100, 100), + "forks": min(forks / 50 * 100, 100), + "followers": min(followers / 100 * 100, 100), + "activity": min(len(events) / 80 * 100, 100), + } + + total = sum(scores.values()) / len(scores) + + # Grade scale: S++/S+/S, A++/A+/A/A-, B++/B+/B/B-, C+/C/C-, D+/D/D-, F + if total >= 97: grade = "S++" + elif total >= 93: grade = "S+" + elif total >= 89: grade = "S" + elif total >= 86: grade = "A++" + elif total >= 82: grade = "A+" + elif total >= 78: grade = "A" + elif total >= 72: grade = "A-" + elif total >= 68: grade = "B++" + elif total >= 64: grade = "B+" + elif total >= 58: grade = "B" + elif total >= 50: grade = "B-" + elif total >= 42: grade = "C+" + elif total >= 35: grade = "C" + elif total >= 28: grade = "C-" + elif total >= 20: grade = "D+" + elif total >= 12: grade = "D" + elif total >= 5: grade = "D-" + else: grade = "F" + + stats = { + "commits": commits, + "prs": prs, + "stars": stars, + "repos": repo_count, + "followers": followers, + } + + tags = compute_tags(github_data) + + return GradeData( + grade=grade, + score=round(total, 1), + stats=stats, + tags=tags, + breakdown={k: round(v, 1) for k, v in scores.items()}, + ) + +def compute_tags(github_data: dict) -> list[TagData]: + """Infer developer tags from repo languages and topics.""" + repos = github_data["repos"] + lang_counts = defaultdict(int) + topic_set = set() + + for r in repos: + lang = r.get("language") + if lang: + lang_counts[lang] += 1 + for topic in r.get("topics", []): + topic_set.add(topic.lower()) + + tags = [] + total = sum(lang_counts.values()) or 1 + + # Language-based tags + lang_map = { + "Python": ["ml-engineer", "backend"], "JavaScript": ["frontend"], + "TypeScript": ["frontend"], "Go": ["backend", "systems"], + "Rust": ["systems"], "Java": ["backend"], "C++": ["systems"], + "Swift": ["mobile"], "Kotlin": ["mobile"], + "Dockerfile": ["devops"], "HCL": ["devops", "cloud"], + } + inferred = defaultdict(float) + for lang, count in lang_counts.items(): + pct = count / total + for tag in lang_map.get(lang, []): + inferred[tag] = max(inferred[tag], pct) + + # Topic-based + topic_map = { + "machine-learning": "ml-engineer", "deep-learning": "ml-engineer", + "frontend": "frontend", "react": "frontend", "vue": "frontend", + "backend": "backend", "api": "backend", "database": "database", + "devops": "devops", "docker": "devops", "kubernetes": "devops", + "security": "security", "fullstack": "fullstack", + } + for topic in topic_set: + if topic in topic_map: + inferred[topic_map[topic]] = max(inferred.get(topic_map[topic], 0), 0.7) + + # Fullstack heuristic + has_fe = any(lang_counts.get(l, 0) > 0 for l in ["JavaScript", "TypeScript"]) + has_be = any(lang_counts.get(l, 0) > 0 for l in ["Python", "Go", "Java", "Rust", "C++"]) + if has_fe and has_be: + inferred["fullstack"] = max(inferred.get("fullstack", 0), 0.6) + + for tag, conf in sorted(inferred.items(), key=lambda x: -x[1])[:6]: + tags.append(TagData(tag=tag, source="earned", confidence=round(conf, 2))) + + return tags + + +def compute_impact_timeline(github_data: dict) -> list[ImpactWeek]: + """Aggregate events into weekly impact data.""" + events = github_data["events"] + weekly = defaultdict(lambda: {"commits": 0, "additions": 0, "deletions": 0}) + + for ev in events: + if ev.get("type") == "PushEvent": + created = ev.get("created_at", "")[:10] + if created: + dt = datetime.fromisoformat(created) + week_start = (dt - timedelta(days=dt.weekday())).isoformat()[:10] + commits = len(ev.get("payload", {}).get("commits", [])) + weekly[week_start]["commits"] += commits + + weeks = [] + for ws in sorted(weekly.keys()): + d = weekly[ws] + weeks.append(ImpactWeek(week_start=ws, commits=d["commits"])) + + return weeks + + +def compute_collaborators(github_data: dict) -> list[CollaboratorData]: + """Find top collaborators from events.""" + events = github_data["events"] + me = github_data["user"].get("login", "").lower() + collab_stats = defaultdict(lambda: {"repos": set(), "commits": 0, "avatar": ""}) + + for ev in events: + actor = ev.get("actor", {}).get("login", "") + if actor.lower() == me: + # Look at mentions in PR/issue events + if ev.get("type") in ("PullRequestEvent", "IssuesEvent"): + payload = ev.get("payload", {}) + pr = payload.get("pull_request", {}) or payload.get("issue", {}) + assignees = pr.get("assignees", []) + for a in assignees: + login = a.get("login", "") + if login.lower() != me: + collab_stats[login]["repos"].add(ev.get("repo", {}).get("name", "")) + collab_stats[login]["avatar"] = a.get("avatar_url", "") + collab_stats[login]["commits"] += 1 + else: + repo = ev.get("repo", {}).get("name", "") + collab_stats[actor]["repos"].add(repo) + collab_stats[actor]["avatar"] = ev.get("actor", {}).get("avatar_url", "") + collab_stats[actor]["commits"] += 1 + + collabs = [] + for username, stats in sorted(collab_stats.items(), key=lambda x: -x[1]["commits"])[:5]: + collabs.append(CollaboratorData( + username=username, + shared_repos=len(stats["repos"]), + shared_commits=stats["commits"], + )) + + return collabs + +def compute_focus(github_data: dict) -> list[FocusCategory]: + """Classify commits into focus categories.""" + repos = github_data["repos"] + events = github_data["events"] + + lang_to_focus = { + "Python": "Python", "JavaScript": "Frontend", "TypeScript": "Frontend", + "HTML": "Frontend", "CSS": "Frontend", "Go": "Backend", + "Rust": "Systems", "Java": "Backend", "C++": "Systems", "C": "Systems", + "Ruby": "Backend", "PHP": "Backend", "Shell": "DevOps", + "Dockerfile": "DevOps", "Jupyter Notebook": "ML", + } + + focus_counts = defaultdict(int) + repo_langs = {} + for r in repos: + lang = r.get("language") + if lang: + repo_langs[r["full_name"]] = lang + + for ev in events: + if ev.get("type") == "PushEvent": + repo_name = ev.get("repo", {}).get("name", "") + lang = repo_langs.get(repo_name) + focus = lang_to_focus.get(lang, "Other") + commits = len(ev.get("payload", {}).get("commits", [])) + focus_counts[focus] += commits + + total = sum(focus_counts.values()) or 1 + return [ + FocusCategory(category=cat, percentage=round(count / total * 100, 1), commit_count=count) + for cat, count in sorted(focus_counts.items(), key=lambda x: -x[1]) + ] + + +def compute_languages(github_data: dict) -> list[LanguageData]: + """Compute language distribution from repos.""" + repos = github_data["repos"] + lang_counts = defaultdict(int) + for r in repos: + lang = r.get("language") + if lang: + lang_counts[lang] += 1 + total = sum(lang_counts.values()) or 1 + return [ + LanguageData(language=lang, percentage=round(count / total * 100, 1), loc=count) + for lang, count in sorted(lang_counts.items(), key=lambda x: -x[1]) + ] + + +# ─── Main Entry Points ─────────────────────────────────────────────────────── + +def generate_widgets_from_github(github_data: dict, theme: str = "dark") -> dict[str, str]: + """ + Takes raw GitHub API data (as stored in DB) and returns rendered SVG strings + for each widget type. + """ + grade = compute_grade(github_data) + impact = compute_impact_timeline(github_data) + collabs = compute_collaborators(github_data) + focus = compute_focus(github_data) + languages = compute_languages(github_data) + + return { + "grade": render_grade_widget(grade, theme), + "impact": render_impact_widget(impact, theme), + "collaborators": render_collaborators_widget(collabs, theme), + "focus": render_focus_widget(focus, theme, period="1y"), + "languages": render_languages_widget(languages, theme), + } + + +def generate_full_widget( + username: str, + token: Optional[str] = None, + theme: str = "dark", + enabled: Optional[list[str]] = None, + achievements: Optional[list[AchievementData]] = None, +) -> str: + if enabled is None: + enabled = ["grade", "impact", "collaborators", "focus", "languages", "achievements"] + + print(f"Fetching GitHub data for {username}...") + github_data = fetch_github_data(username, token) + + print("Generating widgets...") + widgets = generate_widgets_from_github(github_data, theme) + + if achievements: + widgets["achievements"] = render_achievements_widget(achievements, theme) + + for name, svg in widgets.items(): + if svg: + with open(f"widget_{name}.svg", "w") as f: + f.write(svg) + print(f" Saved widget_{name}.svg") + + print("Composing final widget...") + composite = compose_widget( + widgets=widgets, + enabled=[e for e in enabled if e in widgets and widgets[e]], + theme_name=theme, + username=username, + avatar_b64=github_data.get("avatar_b64", ""), + ) + + with open(f"widget_{username}.svg", "w") as f: + f.write(composite) + print(f" Saved widget_{username}.svg") + + return composite + + +# ─── CLI Usage ──────────────────────────────────────────────────────────────── + +if __name__ == "__main__": + import sys + import os + + username = sys.argv[1] if len(sys.argv) > 1 else None + token = os.environ.get("GITHUB_TOKEN") + theme = sys.argv[2] if len(sys.argv) > 2 else "dark" + + if username: + achievements = [ + AchievementData("MIT IQuHACK Winner", "1st Place · Quantum Track", "2025-01", "trophy"), + AchievementData("MLH Top 50", "Global Hackathon League", "2025", "medal"), + ] + generate_full_widget(username, token, theme, achievements=achievements) + else: + print("Demo mode (no username). Generating with mock data...") + + grade = GradeData( + grade="A+", + score=86.2, + stats={"commits": 1423, "prs": 87, "stars": 342, "repos": 28, "followers": 156}, + tags=[ + TagData("ml-engineer", "earned", 0.92), + TagData("backend", "earned", 0.85), + TagData("fullstack", "earned", 0.7), + TagData("devops", "earned", 0.65), + TagData("systems", "earned", 0.55), + TagData("open-source", "chosen", 0.8), + ], + ) + + weeks = [ + ImpactWeek(f"2025-{m:02d}-{d:02d}", commits=c) + for m, d, c in [ + (7,7,12),(7,14,18),(7,21,25),(7,28,15),(8,4,30),(8,11,22), + (8,18,35),(8,25,28),(9,1,40),(9,8,33),(9,15,45),(9,22,38), + (9,29,50),(10,6,42),(10,13,55),(10,20,48),(10,27,60),(11,3,52), + (11,10,65),(11,17,58),(11,24,70),(12,1,62),(12,8,75),(12,15,68), + ] + ] + + collabs = [ + CollaboratorData("torvalds", "", 3, 142), + CollaboratorData("gaearon", "", 5, 98), + CollaboratorData("karpathy", "", 2, 76), + CollaboratorData("nat", "", 4, 51), + ] + + focus = [ + FocusCategory("ML", 32.1, 192), + FocusCategory("Frontend", 22.5, 135), + FocusCategory("Backend", 18.8, 113), + FocusCategory("DevOps", 14.2, 85), + FocusCategory("Systems", 8.4, 50), + FocusCategory("Data", 4.0, 24), + ] + + languages = [ + LanguageData("Python", 38.5, 42), + LanguageData("TypeScript", 22.0, 24), + LanguageData("Go", 14.2, 15), + LanguageData("Rust", 10.8, 12), + LanguageData("Shell", 8.0, 9), + LanguageData("Dockerfile", 6.5, 7), + ] + + achievements = [ + AchievementData("MIT IQuHACK Winner", "1st Place · Quantum Track", "2025-01", "trophy"), + AchievementData("MLH Top 50", "Global Hackathon League 2025", "2025", "medal"), + AchievementData("BostonHacks Champion", "Best AI/ML Project", "2024-11", "hackathon"), + AchievementData("CoreWeave Intern Hackathon", "Infrastructure Innovation Award", "2024-08", "star"), + ] + + for t_name in ["dark", "light"]: + widgets = { + "grade": render_grade_widget(grade, t_name), + "impact": render_impact_widget(weeks, t_name, "6mo"), + "collaborators": render_collaborators_widget(collabs, t_name), + "focus": render_focus_widget(focus, t_name, period="1y"), + "languages": render_languages_widget(languages, t_name), + "achievements": render_achievements_widget(achievements, t_name, max_items=4), + } + + for name, svg in widgets.items(): + suffix = "" if t_name == "dark" else f"_{t_name}" + path = f"widget_{name}{suffix}.svg" + with open(path, "w") as f: + f.write(svg) + print(f" Saved {path}") + + composite = compose_widget( + widgets=widgets, + enabled=["grade", "impact", "collaborators", "focus", "languages", "achievements"], + theme_name=t_name, + username="demo_user", + ) + suffix = "" if t_name == "dark" else f"_{t_name}" + with open(f"widget_full{suffix}.svg", "w") as f: + f.write(composite) + print(f" Saved widget_full{suffix}.svg") \ No newline at end of file