-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
85 lines (75 loc) 路 2.39 KB
/
Copy pathindex.js
File metadata and controls
85 lines (75 loc) 路 2.39 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
import fs from 'fs';
import download from './lib/avatar.js';
import image from './lib/image.js';
async function request(repo, query = '') {
const res = await fetch(`https://api.github.com/repos/${repo}/contributors?per_page=100${query}`, {
headers: {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.1.1 Safari/605.1.15'
}
});
const data = await res.json();
console.log("Request", repo, query, data.length);
// If more than 100 contributors, find them in the next page
const linkHeader = res.headers.get('link');
if (linkHeader) {
const links = linkHeader.split(',').reduce((acc, link) => {
const match = link.match(/<([^>]+)>; rel="([^"]+)"/);
if (match) acc[match[2]] = match[1];
return acc;
}, {});
if (links.next) {
return [data, links.next];
}
}
return [data, null];
}
function parse(contributors) {
const db = [];
contributors.forEach(({ login, avatar_url, type, contributions }) => {
if (type === 'Bot') return;
const target = db.find(item => item.login === login);
if (!target) {
db.push({
login,
avatar_url,
contributions
});
} else {
target.contributions += contributions;
}
});
const iissnanIndex = db.findIndex(user => user.login === 'iissnan');
const iissnan = iissnanIndex > -1 ? db.splice(iissnanIndex, 1)[0] : null;
const ivanNginxIndex = db.findIndex(user => user.login === 'ivan-nginx');
const ivanNginx = ivanNginxIndex > -1 ? db.splice(ivanNginxIndex, 1)[0] : null;
db.sort((a, b) => b.contributions - a.contributions);
if (ivanNginx) db.unshift(ivanNginx);
if (iissnan) db.unshift(iissnan);
Promise.all(db.map(data => download(data.avatar_url))).then(avatars => {
avatars.forEach((avatar, index) => {
db[index].avatar = avatar;
fs.writeFileSync('./contributors.svg', image(db));
});
})
.catch(error => {
console.error(error);
});
}
async function main() {
const repos = [
'iissnan/hexo-theme-next',
'theme-next/hexo-theme-next',
'next-theme/hexo-theme-next'
];
const contributors = [];
for (let repo of repos) {
let data, next;
do {
[data, next] = await request(repo, next ? `&${next.split('?')[1]}` : '');
contributors.push(...data);
} while (next);
}
console.log(contributors.length);
parse(contributors);
}
main();