Repository navigation
Expand file tree
/
Copy pathgenerate-images.js
More file actions
165 lines (138 loc) · 5.54 KB
/
Copy pathgenerate-images.js
File metadata and controls
165 lines (138 loc) · 5.54 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
const fs = require('fs');
const path = require('path');
const API_URL = process.env.API_URL || 'https://openreadme.vercel.app/api/openreadme';
// Every image costs the API a headless browser launch, so this is the real throttle.
// Turn it down if the API starts timing out or returning 429.
const CONCURRENCY = Number(process.env.CONCURRENCY) || 6;
const GITHUB_TOKEN = process.env.GITHUB_TOKEN;
const REPO_OWNER = 'Open-Dev-Society';
const REPO_NAME = 'openreadme';
// Read user profiles from JSON file
function readUserProfiles() {
try {
const profilesPath = path.join(process.cwd(), 'data', 'user-profiles.json');
const data = fs.readFileSync(profilesPath, 'utf-8');
return JSON.parse(data);
} catch (error) {
console.error('⚠️ Error reading user profiles:', error.message);
return {};
}
}
async function generateProfileImage(username, userId) {
try {
console.log(`🎨 Generating image for ${username} (${userId})...`);
// Read stored user profile data first
const userProfiles = readUserProfiles();
const storedProfile = userProfiles[username] || {};
console.log(`📦 Stored profile data:`, storedProfile);
// Get user data from GitHub API as fallback
const userResponse = await fetch(`https://api.github.com/users/${username}`, {
headers: {
'Authorization': `token ${GITHUB_TOKEN}`,
'Accept': 'application/vnd.github.v3+json',
'User-Agent': 'OpenReadme-Workflow'
}
});
if (!userResponse.ok) {
console.warn(`⚠️ GitHub API warning for ${username}: ${userResponse.statusText}`);
}
const userData = userResponse.ok ? await userResponse.json() : { login: username };
// Prioritize stored profile data over GitHub API data
const name = storedProfile.name || userData.name || username;
const profilePic = storedProfile.profilePic || userData.avatar_url || '';
const twitterUsername = storedProfile.twitterUsername || userData.twitter_username || '';
const linkedinUsername = storedProfile.linkedinUsername || '';
const portfolioUrl = storedProfile.portfolioUrl || userData.blog || userData.html_url || '';
console.log(`✅ Using data - Name: ${name}, Twitter: ${twitterUsername}, LinkedIn: ${linkedinUsername}`);
// Build API URL with parameters
const params = new URLSearchParams({
username: username, // Required for API validation
n: name,
i: profilePic,
g: username,
x: twitterUsername,
l: linkedinUsername,
p: portfolioUrl,
t: 'classic'
});
const apiUrl = `${API_URL}?${params.toString()}`;
console.log(`📡 Calling API: ${apiUrl}`);
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'User-Agent': 'OpenReadme-Workflow'
}
});
console.log(`📊 Response status: ${response.status} ${response.statusText}`);
if (!response.ok) {
const errorText = await response.text();
console.error(`❌ API error response: ${errorText}`);
throw new Error(`API error (${response.status}): ${errorText}`);
}
const result = await response.json();
console.log(`✅ Successfully generated image for ${username}`);
console.log(`🔗 Image URL: ${result.url}`);
console.log(`📝 Method: ${result.method}`);
return result.url;
} catch (error) {
console.error(`❌ Error generating image for ${username}:`, error.message);
console.error(`🔍 Stack trace:`, error.stack);
return null;
}
}
// Process all users
(async () => {
try {
const mappingsString = process.env.MAPPINGS || '';
const mappings = mappingsString.split(' ').filter(m => m.trim());
console.log(`📋 Found ${mappings.length} users to process`);
console.log(`🔧 Using API URL: ${API_URL}`);
if (mappings.length === 0) {
console.log('⚠️ No user mappings found to process');
return;
}
let successCount = 0;
let errorCount = 0;
// Serial, this took 16s per profile: 21 minutes for 81 users, and longer with
// every user added. The queue is shared, so a slow profile holds up one worker
// instead of all of them.
const queue = mappings.slice();
console.log(`⚡ Processing ${CONCURRENCY} at a time`);
await Promise.all(Array.from({ length: CONCURRENCY }, async () => {
while (queue.length > 0) {
const mapping = queue.shift();
const [username, userId] = mapping.split('=');
if (!username || !userId) {
console.warn(`⚠️ Invalid mapping format: ${mapping}`);
continue;
}
console.log(`🔄 Processing ${username} (${userId})`);
try {
const imageUrl = await generateProfileImage(username, userId);
if (imageUrl) {
successCount++;
console.log(`✅ Success for ${username}: ${imageUrl}`);
} else {
errorCount++;
console.log(`❌ Failed for ${username}`);
}
} catch (error) {
errorCount++;
console.error(`💥 Error processing ${username}:`, error.message);
}
}
}));
console.log(`\n${'='.repeat(60)}`);
console.log(`📊 WORKFLOW SUMMARY`);
console.log(`${'='.repeat(60)}`);
console.log(`✅ Successful: ${successCount}`);
console.log(`❌ Failed: ${errorCount}`);
console.log(`📋 Total: ${successCount + errorCount}`);
console.log(`${'='.repeat(60)}`);
} catch (error) {
console.error('💥 Workflow failed:', error.message);
process.exit(1);
}
})();