-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerator.py
More file actions
375 lines (321 loc) · 14.6 KB
/
generator.py
File metadata and controls
375 lines (321 loc) · 14.6 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
"""GitHub profile analyzer and card generator."""
import math
import hashlib
from datetime import datetime, timezone
from typing import Any, Optional
import requests
# Developer class definitions
CLASSES = {
"Night Owl": {
"description": "Codes best under moonlight",
"icon": "🦉",
"color_primary": "#1a1a2e",
"color_secondary": "#16213e",
"color_accent": "#e94560",
"gradient_start": "#0f0c29",
"gradient_end": "#302b63",
},
"Early Bird": {
"description": "First commit before sunrise",
"icon": "🐥",
"color_primary": "#ff9a3c",
"color_secondary": "#ff6f3c",
"color_accent": "#ffc93c",
"gradient_start": "#f7971e",
"gradient_end": "#ffd200",
},
"Polyglot": {
"description": "Master of many tongues",
"icon": "🌐",
"color_primary": "#6c5ce7",
"color_secondary": "#a29bfe",
"color_accent": "#fd79a8",
"gradient_start": "#667eea",
"gradient_end": "#764ba2",
},
"Specialist": {
"description": "One language to rule them all",
"icon": "🎯",
"color_primary": "#00b894",
"color_secondary": "#00cec9",
"color_accent": "#fdcb6e",
"gradient_start": "#11998e",
"gradient_end": "#38ef7d",
},
"PR Speedrunner": {
"description": "Ships faster than light",
"icon": "⚡",
"color_primary": "#e17055",
"color_secondary": "#d63031",
"color_accent": "#ffeaa7",
"gradient_start": "#f12711",
"gradient_end": "#f5af19",
},
"Code Warrior": {
"description": "Unstoppable commit streak",
"icon": "⚔️",
"color_primary": "#2d3436",
"color_secondary": "#636e72",
"color_accent": "#ff7675",
"gradient_start": "#434343",
"gradient_end": "#000000",
},
"Weekend Warrior": {
"description": "Saturday and Sunday hero",
"icon": "🏖️",
"color_primary": "#0984e3",
"color_secondary": "#74b9ff",
"color_accent": "#dfe6e9",
"gradient_start": "#4facfe",
"gradient_end": "#00f2fe",
},
"Open Source Champion": {
"description": "Building for everyone",
"icon": "🏆",
"color_primary": "#6c5ce7",
"color_secondary": "#a29bfe",
"color_accent": "#ffeaa7",
"gradient_start": "#a18cd1",
"gradient_end": "#fbc2eb",
},
}
DEFAULT_CLASS = "Code Warrior"
def fetch_github_data(username: str) -> Optional[dict[str, Any]]:
"""Fetch GitHub profile, repos, and events data."""
headers = {"Accept": "application/vnd.github.v3+json"}
# Fetch user profile
user_resp = requests.get(f"https://api.github.com/users/{username}", headers=headers, timeout=10)
if user_resp.status_code != 200:
return None
user = user_resp.json()
# Fetch repos (up to 100, sorted by stars)
repos_resp = requests.get(
f"https://api.github.com/users/{username}/repos",
headers=headers,
params={"per_page": 100, "sort": "stars", "direction": "desc"},
timeout=10,
)
repos = repos_resp.json() if repos_resp.status_code == 200 else []
# Fetch recent events (for commit time analysis)
events_resp = requests.get(
f"https://api.github.com/users/{username}/events/public",
headers=headers,
params={"per_page": 100},
timeout=10,
)
events = events_resp.json() if events_resp.status_code == 200 else []
return {"user": user, "repos": repos, "events": events}
def calculate_stats(data: dict[str, Any]) -> dict[str, Any]:
"""Calculate developer stats from GitHub data."""
user = data["user"]
repos = data["repos"]
events = data["events"]
# Language distribution
languages: dict[str, int] = {}
total_stars = 0
total_forks = 0
for repo in repos:
if repo.get("fork"):
continue
lang = repo.get("language")
if lang:
languages[lang] = languages.get(lang, 0) + 1
total_stars += repo.get("stargazers_count", 0)
total_forks += repo.get("forks_count", 0)
# Sort languages by count
sorted_langs = sorted(languages.items(), key=lambda x: x[1], reverse=True)
top_languages = sorted_langs[:5]
# Commit time analysis
commit_hours: dict[int, int] = {h: 0 for h in range(24)}
weekend_commits = 0
weekday_commits = 0
push_events = [e for e in events if e.get("type") == "PushEvent"]
for event in push_events:
created = event.get("created_at", "")
if created:
try:
dt = datetime.fromisoformat(created.replace("Z", "+00:00"))
commit_hours[dt.hour] += 1
if dt.weekday() >= 5:
weekend_commits += 1
else:
weekday_commits += 1
except (ValueError, TypeError):
pass
# PR analysis
pr_events = [e for e in events if e.get("type") in ("PullRequestEvent", "PullRequestReviewEvent")]
pr_count = len([e for e in pr_events if e.get("type") == "PullRequestEvent"])
review_count = len([e for e in pr_events if e.get("type") == "PullRequestReviewEvent"])
# Calculate "power level" (XP)
xp = (
total_stars * 10
+ total_forks * 5
+ user.get("public_repos", 0) * 3
+ user.get("followers", 0) * 2
+ len(push_events) * 1
)
# Level from XP (logarithmic scaling)
level = max(1, min(99, int(10 * math.log2(1 + xp / 100))))
return {
"username": user.get("login", ""),
"name": user.get("name") or user.get("login", ""),
"avatar_url": user.get("avatar_url", ""),
"bio": user.get("bio") or "No bio yet",
"company": user.get("company") or "",
"location": user.get("location") or "",
"public_repos": user.get("public_repos", 0),
"followers": user.get("followers", 0),
"following": user.get("following", 0),
"created_at": user.get("created_at", ""),
"total_stars": total_stars,
"total_forks": total_forks,
"top_languages": top_languages,
"language_count": len(languages),
"commit_hours": commit_hours,
"weekend_commits": weekend_commits,
"weekday_commits": weekday_commits,
"pr_count": pr_count,
"review_count": review_count,
"push_events": len(push_events),
"xp": xp,
"level": level,
}
def determine_class(stats: dict[str, Any]) -> str:
"""Determine developer class from stats."""
scores: dict[str, float] = {cls: 0.0 for cls in CLASSES}
# Night Owl vs Early Bird
late_commits = sum(stats["commit_hours"].get(h, 0) for h in range(22, 24))
late_commits += sum(stats["commit_hours"].get(h, 0) for h in range(0, 4))
early_commits = sum(stats["commit_hours"].get(h, 0) for h in range(5, 9))
total_timed = late_commits + early_commits + 1
if late_commits / total_timed > 0.3:
scores["Night Owl"] += 10
if early_commits / total_timed > 0.3:
scores["Early Bird"] += 10
# Polyglot vs Specialist
if stats["language_count"] >= 5:
scores["Polyglot"] += 10
elif stats["language_count"] <= 2 and stats["language_count"] > 0:
scores["Specialist"] += 10
# PR Speedrunner
if stats["pr_count"] > 10:
scores["PR Speedrunner"] += 8
elif stats["pr_count"] > 5:
scores["PR Speedrunner"] += 4
# Weekend Warrior
total_commits = stats["weekend_commits"] + stats["weekday_commits"] + 1
if stats["weekend_commits"] / total_commits > 0.3:
scores["Weekend Warrior"] += 10
# Code Warrior (high push event count)
if stats["push_events"] > 50:
scores["Code Warrior"] += 10
elif stats["push_events"] > 20:
scores["Code Warrior"] += 5
# Open Source Champion (high stars + repos)
if stats["total_stars"] > 100:
scores["Open Source Champion"] += 10
elif stats["total_stars"] > 20:
scores["Open Source Champion"] += 5
# Return highest scoring class
best_class = max(scores, key=lambda k: scores[k])
if scores[best_class] == 0:
return DEFAULT_CLASS
return best_class
def generate_card_svg(stats: dict[str, Any]) -> str:
"""Generate an SVG trading card from developer stats."""
dev_class = determine_class(stats)
cls = CLASSES[dev_class]
# Calculate stat bars (0-100 scale)
stars_bar = min(100, stats["total_stars"])
repos_bar = min(100, stats["public_repos"] * 2)
followers_bar = min(100, stats["followers"])
langs_bar = min(100, stats["language_count"] * 15)
activity_bar = min(100, stats["push_events"] * 2)
# Top languages display
lang_items = ""
lang_colors = {
"Python": "#3572A5", "JavaScript": "#f1e05a", "TypeScript": "#3178c6",
"Go": "#00ADD8", "Rust": "#dea584", "Java": "#b07219",
"C++": "#f34b7d", "C": "#555555", "Ruby": "#701516",
"Swift": "#F05138", "Kotlin": "#A97BFF", "PHP": "#4F5D95",
"Shell": "#89e051", "HTML": "#e34c26", "CSS": "#563d7c",
"Dart": "#00B4AB", "Scala": "#c22d40", "Lua": "#000080",
}
for i, (lang, count) in enumerate(stats["top_languages"][:4]):
color = lang_colors.get(lang, "#888888")
y_pos = 340 + i * 22
bar_width = min(120, count * 20)
lang_items += f'''
<text x="30" y="{y_pos}" fill="#b0b0b0" font-size="11" font-family="monospace">{lang}</text>
<rect x="120" y="{y_pos - 10}" width="{bar_width}" height="12" rx="3" fill="{color}" opacity="0.8"/>
<text x="{125 + bar_width}" y="{y_pos}" fill="#888" font-size="9" font-family="monospace">{count}</text>'''
# Generate unique gradient ID from username
uid = hashlib.md5(stats["username"].encode()).hexdigest()[:8]
svg = f'''<svg xmlns="http://www.w3.org/2000/svg" width="400" height="500" viewBox="0 0 400 500">
<defs>
<linearGradient id="bg_{uid}" x1="0%" y1="0%" x2="100%" y2="100%">
<stop offset="0%" style="stop-color:{cls['gradient_start']};stop-opacity:1" />
<stop offset="100%" style="stop-color:{cls['gradient_end']};stop-opacity:1" />
</linearGradient>
<linearGradient id="bar_{uid}" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" style="stop-color:{cls['color_accent']};stop-opacity:1" />
<stop offset="100%" style="stop-color:{cls['color_accent']};stop-opacity:0.4" />
</linearGradient>
<clipPath id="avatar_clip_{uid}">
<circle cx="200" cy="90" r="45"/>
</clipPath>
<filter id="shadow_{uid}">
<feDropShadow dx="0" dy="2" stdDeviation="3" flood-opacity="0.3"/>
</filter>
</defs>
<!-- Card background -->
<rect width="400" height="500" rx="16" fill="url(#bg_{uid})" filter="url(#shadow_{uid})"/>
<rect x="2" y="2" width="396" height="496" rx="15" fill="none" stroke="{cls['color_accent']}" stroke-width="1" opacity="0.3"/>
<!-- Class badge -->
<rect x="10" y="10" width="120" height="24" rx="12" fill="{cls['color_accent']}" opacity="0.2"/>
<text x="70" y="26" fill="{cls['color_accent']}" font-size="11" font-family="monospace" text-anchor="middle" font-weight="bold">{dev_class}</text>
<!-- Level badge -->
<rect x="280" y="10" width="110" height="24" rx="12" fill="{cls['color_accent']}" opacity="0.2"/>
<text x="335" y="26" fill="{cls['color_accent']}" font-size="11" font-family="monospace" text-anchor="middle" font-weight="bold">LVL {stats['level']} | {stats['xp']:,} XP</text>
<!-- Avatar -->
<circle cx="200" cy="90" r="47" fill="{cls['color_accent']}" opacity="0.3"/>
<image href="{stats['avatar_url']}" x="155" y="45" width="90" height="90" clip-path="url(#avatar_clip_{uid})"/>
<!-- Name and username -->
<text x="200" y="160" fill="white" font-size="20" font-family="monospace" text-anchor="middle" font-weight="bold">{stats['name']}</text>
<text x="200" y="180" fill="#b0b0b0" font-size="13" font-family="monospace" text-anchor="middle">@{stats['username']}</text>
<!-- Class description -->
<text x="200" y="202" fill="{cls['color_accent']}" font-size="11" font-family="monospace" text-anchor="middle" font-style="italic">{cls['icon']} {cls['description']}</text>
<!-- Divider -->
<line x1="30" y1="215" x2="370" y2="215" stroke="{cls['color_accent']}" stroke-width="0.5" opacity="0.3"/>
<!-- Stats bars -->
<text x="30" y="240" fill="#b0b0b0" font-size="11" font-family="monospace">Stars</text>
<rect x="120" y="230" width="200" height="12" rx="3" fill="#1a1a2e" opacity="0.5"/>
<rect x="120" y="230" width="{stars_bar * 2}" height="12" rx="3" fill="url(#bar_{uid})"/>
<text x="330" y="240" fill="white" font-size="11" font-family="monospace">{stats['total_stars']}</text>
<text x="30" y="262" fill="#b0b0b0" font-size="11" font-family="monospace">Repos</text>
<rect x="120" y="252" width="200" height="12" rx="3" fill="#1a1a2e" opacity="0.5"/>
<rect x="120" y="252" width="{repos_bar * 2}" height="12" rx="3" fill="url(#bar_{uid})"/>
<text x="330" y="262" fill="white" font-size="11" font-family="monospace">{stats['public_repos']}</text>
<text x="30" y="284" fill="#b0b0b0" font-size="11" font-family="monospace">Followers</text>
<rect x="120" y="274" width="200" height="12" rx="3" fill="#1a1a2e" opacity="0.5"/>
<rect x="120" y="274" width="{followers_bar * 2}" height="12" rx="3" fill="url(#bar_{uid})"/>
<text x="330" y="284" fill="white" font-size="11" font-family="monospace">{stats['followers']}</text>
<text x="30" y="306" fill="#b0b0b0" font-size="11" font-family="monospace">Activity</text>
<rect x="120" y="296" width="200" height="12" rx="3" fill="#1a1a2e" opacity="0.5"/>
<rect x="120" y="296" width="{activity_bar * 2}" height="12" rx="3" fill="url(#bar_{uid})"/>
<text x="330" y="306" fill="white" font-size="11" font-family="monospace">{stats['push_events']}</text>
<!-- Divider -->
<line x1="30" y1="318" x2="370" y2="318" stroke="{cls['color_accent']}" stroke-width="0.5" opacity="0.3"/>
<!-- Languages -->
<text x="30" y="336" fill="white" font-size="12" font-family="monospace" font-weight="bold">Languages</text>
{lang_items}
<!-- Footer -->
<line x1="30" y1="440" x2="370" y2="440" stroke="{cls['color_accent']}" stroke-width="0.5" opacity="0.3"/>
<text x="30" y="460" fill="#666" font-size="9" font-family="monospace">Member since {stats['created_at'][:4]}</text>
<text x="200" y="480" fill="#555" font-size="9" font-family="monospace" text-anchor="middle">generated by git-aura</text>
<!-- Holographic overlay effect -->
<rect width="400" height="500" rx="16" fill="url(#bg_{uid})" opacity="0.05">
<animate attributeName="opacity" values="0.03;0.08;0.03" dur="3s" repeatCount="indefinite"/>
</rect>
</svg>'''
return svg