-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
411 lines (352 loc) · 13.1 KB
/
index.js
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// Project Structure:
// /project
// ├── package.json
// ├── server.js
// ├── public/
// │ ├── index.html
// │ └── script.js
// └── views/
const express = require('express');
const axios = require('axios');
const cors = require('cors');
const path = require('path');
const crypto = require('crypto');
const app = express();
const port = 8000;
app.use(cors());
app.use(express.json());
app.use(express.static('public'));
// In-memory cache to store generated stats
const statsCache = new Map();
// New route to generate a unique badge URL
app.post('/generate-badge', async (req, res) => {
const { platforms } = req.body;
if (!platforms || platforms.length === 0) {
return res.status(400).json({ error: 'Platform data is required' });
}
try {
const statsPromises = platforms.map(async (platformInfo) => {
const fullData = await fetchPlatformData(platformInfo.platform, platformInfo.username);
// Filter data based on selected features
const filteredData = {};
if (platformInfo.features) {
platformInfo.features.forEach(feature => {
if (fullData[feature] !== undefined) {
filteredData[feature] = fullData[feature];
}
});
}
return {
platform: platformInfo.platform,
...filteredData
};
});
const statsResults = await Promise.all(statsPromises);
// Generate a unique hash for this set of stats
const statsKey = crypto.randomBytes(16).toString('hex');
// Cache the stats with an expiration (e.g., 1 hour)
statsCache.set(statsKey, {
stats: statsResults,
timestamp: Date.now()
});
// Return the badge URL
res.json({
badgeUrl: `http://localhost:8000/badge/${statsKey}`,
statsKey: statsKey
});
} catch (error) {
res.status(500).json({
error: 'Failed to generate badge',
details: error.message
});
}
});
// Route to dynamically serve badge as SVG
app.get('/badge/:statsKey', (req, res) => {
const { statsKey } = req.params;
const cachedStats = statsCache.get(statsKey);
// Check if stats exist and are not expired (1 hour)
if (!cachedStats || (Date.now() - cachedStats.timestamp > 3600000)) {
return res.status(404).send('Badge not found or expired');
}
// Generate SVG dynamically
const svg = generateStatsBadgeSVG(cachedStats.stats);
res.setHeader('Content-Type', 'image/svg+xml');
res.send(svg);
});
// Function to generate SVG badge with dynamic sizing
function generateStatsBadgeSVG(stats) {
// Constants for sizing
const PLATFORM_WIDTH = 350;
const BASE_HEIGHT = 100; // Reduced base height
const FEATURE_HEIGHT = 25; // Height per feature
const PADDING_TOP = 20;
const PADDING_BOTTOM = 30;
let svgContent = `
<svg xmlns="http://www.w3.org/2000/svg" width="${PLATFORM_WIDTH * stats.length}" height="auto" viewBox="0 0 ${PLATFORM_WIDTH * stats.length} auto">
<style>
.badge-bg { fill: #f6f8fa; }
.platform-title { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif; }
.title { font-size: 16px; font-weight: bold; fill: #24292e; }
.subtitle { font-size: 12px; fill: #6a737d; }
.stat-label { font-size: 12px; fill: #6a737d; }
.stat-value { font-size: 14px; font-weight: bold; fill: #24292e; }
</style>
`;
stats.forEach((platformStats, index) => {
const platform = platformStats.platform;
const xOffset = index * PLATFORM_WIDTH;
// Calculate dynamic height based on features
let featureCount = Object.keys(platformStats).length - 1; // Subtract 'platform'
const platformHeight = BASE_HEIGHT + (featureCount * FEATURE_HEIGHT) + PADDING_TOP + PADDING_BOTTOM;
// Badge background
svgContent += `
<rect
x="${xOffset}" y="0"
width="${PLATFORM_WIDTH}"
height="${platformHeight}"
class="badge-bg"
rx="6"
ry="6"
stroke="#e1e4e8"
stroke-width="1"
/>
<!-- Platform Icon (placeholder) -->
<rect
x="${xOffset + 20}" y="${PADDING_TOP}"
width="40" height="40"
rx="6"
ry="6"
fill="#${getColorForPlatform(platform)}"
/>
<!-- Platform Title -->
<text
x="${xOffset + 70}" y="${PADDING_TOP + 20}"
class="title platform-title"
font-size="16"
font-weight="600"
>
${platform.replace(/_/g, ' ')}
</text>
<text
x="${xOffset + 70}" y="${PADDING_TOP + 40}"
class="subtitle platform-title"
font-size="12"
>
Profile Statistics
</text>
`;
// Render all selected features
featureCount = 0;
for (const [key, value] of Object.entries(platformStats)) {
if (key !== 'platform') {
const formattedKey = key.replace(/_/g, ' ')
.replace(/\b\w/g, l => l.toUpperCase());
const yPosition = PADDING_TOP + 60 + (featureCount * FEATURE_HEIGHT);
svgContent += `
<text
x="${xOffset + 20}"
y="${yPosition}"
class="stat-label platform-title"
>
${formattedKey}
</text>
<text
x="${xOffset + 320}"
y="${yPosition}"
class="stat-value platform-title"
text-anchor="end"
>
${value}
</text>
<line
x1="${xOffset + 20}"
y1="${yPosition + 5}"
x2="${xOffset + 320}"
y2="${yPosition + 5}"
stroke="#e1e4e8"
stroke-width="0.5"
/>
`;
featureCount++;
}
}
// Last updated timestamp
svgContent += `
<text
x="${xOffset + 20}"
y="${platformHeight - 10}"
class="subtitle platform-title"
font-size="10"
>
Last Updated: ${new Date().toLocaleString()}
</text>
`;
});
svgContent += `</svg>`;
return svgContent;
}
// Helper function to generate consistent colors for platforms
function getColorForPlatform(platform) {
const platformColors = {
'github': '24292e',
'leetcode_stats': '0a84ff',
'leetcode_contests': 'ffd700'
};
return platformColors[platform] || '4a4a4a';
}
// Clean up expired stats periodically
setInterval(() => {
const now = Date.now();
for (const [key, value] of statsCache.entries()) {
if (now - value.timestamp > 3600000) {
statsCache.delete(key);
}
}
}, 3600000); // Run every hour
// Comprehensive Platforms Configuration
const PLATFORMS = {
github: {
name: 'GitHub',
features: [
'avatar_url', 'name', 'login', 'bio', 'public_repos',
'followers', 'following', 'created_at', 'total_contributions',
'html_url', 'company', 'blog', 'location', 'email', 'hireable'
]
},
leetcode_stats: {
name: 'LeetCode Stats',
features: [
'totalSolved', 'totalQuestions', 'easySolved', 'totalEasy',
'mediumSolved', 'totalMedium', 'hardSolved', 'totalHard',
'acceptanceRate', 'ranking', 'contributionPoints', 'reputation'
]
},
leetcode_contests: {
name: 'LeetCode Contests',
features: [
'attendedContestsCount', 'rating', 'globalRanking',
'totalParticipants', 'topPercentage'
]
}
};
// Utility function to fetch data from different platforms
async function fetchPlatformData(platform, username) {
try {
switch(platform) {
case 'github':
const githubResponse = await axios.get(`https://api.github.com/users/${username}`);
// Attempt to get total contributions
let totalContributions = 0;
try {
const contributionsResponse = await axios.get(`https://api.github.com/users/${username}/events`);
totalContributions = contributionsResponse.data.length;
} catch (contribError) {
console.warn('Could not fetch total contributions');
}
return {
...githubResponse.data,
total_contributions: totalContributions
};
case 'leetcode_stats':
const leetcodeStatsResponse = await axios.get(`https://leetcode-stats-api.herokuapp.com/${username}`);
return leetcodeStatsResponse.data;
case 'leetcode_contests':
const leetcodeContestsResponse = await axios.post('https://leetcode.com/graphql', {
query: `{
userContestRanking(username:"${username}") {
attendedContestsCount
rating
globalRanking
totalParticipants
topPercentage
}
}`
});
return leetcodeContestsResponse.data.data.userContestRanking;
default:
throw new Error('Unsupported platform');
}
} catch (error) {
console.error(`Error fetching data for ${platform}:`, error);
throw error;
}
}
// Generate markdown snippet
function generateMarkdownSnippet(platformData) {
let markdown = '## My Profile Stats\n\n';
Object.entries(platformData).forEach(([platform, data]) => {
markdown += `### ${PLATFORMS[platform].name}\n`;
Object.entries(data).forEach(([feature, value]) => {
if (value !== null && value !== undefined) {
markdown += `- ${PLATFORMS[platform].features[feature] || feature}: ${value}\n`;
}
});
markdown += '\n';
});
return markdown;
}
// Routes
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index'));
});
// Get available platforms and their features
app.get('/platforms', (req, res) => {
res.json(PLATFORMS);
});
// Generate profile stats
app.post('/generate-stats', async (req, res) => {
const { platforms } = req.body;
if (!platforms || platforms.length === 0) {
return res.status(400).json({ error: 'Platform data is required' });
}
try {
const platformResults = {};
const statsPromises = platforms.map(async (platformInfo) => {
const fullData = await fetchPlatformData(platformInfo.platform, platformInfo.username);
// Filter data based on selected features
const filteredData = {};
if (platformInfo.features) {
platformInfo.features.forEach(feature => {
if (fullData[feature] !== undefined) {
filteredData[feature] = fullData[feature];
}
});
} else {
// If no specific features selected, use all data
return fullData;
}
return {
platform: platformInfo.platform,
...filteredData
};
});
const statsResults = await Promise.all(statsPromises);
const markdownSnippet = generateMarkdownSnippet(
statsResults.reduce((acc, stat) => {
acc[stat.platform] = {...stat};
delete acc[stat.platform].platform;
return acc;
}, {})
);
res.json({
stats: statsResults,
markdownSnippet: markdownSnippet
});
} catch (error) {
res.status(500).json({
error: 'Failed to fetch profile stats',
details: error.message
});
}
});
// Add error handling middleware
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send('Something broke! Check the server logs.');
});
app.listen(port, () => {
console.log(`Server started at http://localhost:${port}`);
});
// Export for potential testing
module.exports = app;