forked from obsidianmd/obsidian-releases
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-stats.js
111 lines (91 loc) · 2.75 KB
/
update-stats.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
let fs = require('fs');
let https = require('https');
let plugins = JSON.parse(fs.readFileSync('./community-plugins.json', 'utf8'));
let stats = JSON.parse(fs.readFileSync('./community-plugin-stats.json', 'utf8'));
let githubToken = process.env.gh_token;
console.log(`Updating stats for ${Object.keys(plugins).length} plugins`);
let newStats = {};
for (let plugin of plugins) {
let key = plugin.id;
if (stats.hasOwnProperty(key)) {
newStats[key] = stats[key];
}
}
let saveStats = () => fs.writeFileSync('./community-plugin-stats.json', JSON.stringify(newStats, null, 2), 'utf8');
saveStats();
(async() => {
for (let key in plugins) {
if (!plugins.hasOwnProperty(key)) {
continue;
}
for (let attempt = 0; attempt < 5; attempt++) {
try {
let plugin = plugins[key];
let id = plugin.id;
console.log(`Downloading stats for ${id}`);
let stats = newStats[id] = newStats[id] || {};
let repo = plugin.repo;
let data = await download(repo);
let releases = JSON.parse(data);
// stats is Array<{tag_name: string, assets: Array<{name: string, download_count}>}>
console.log(`Received ${releases.length} releases`);
let updated = 0;
for (let release of releases) {
let version = release.tag_name;
let assets = release.assets;
let downloads = 0;
let publishTs = new Date(release.published_at).getTime();
if (publishTs > updated) {
updated = publishTs;
}
for (let asset of assets) {
if (asset.name === 'manifest.json') {
downloads = asset.download_count;
}
}
if (downloads) {
stats[version] = downloads;
}
}
let total = 0;
for (let version in stats) {
if (stats.hasOwnProperty(version) && version !== 'downloads' && version !== 'updated' && version !== 'latest') {
total += stats[version];
}
}
console.log(`Total downloads: ${total}`);
stats['downloads'] = total;
stats['updated'] = updated;
saveStats();
break;
} catch (e) {
console.log('Failed', e.message);
await new Promise(resolve => setTimeout(resolve, 1000));
}
}
}
console.log('All done!');
})();
function download(repo) {
return new Promise((resolve, reject) => {
let req = https.get(
{
host: 'api.github.com',
port: 443,
path: '/repos/' + repo + '/releases',
headers: {
'User-Agent': 'Obsidian-Script',
// Generate a permission-less token here https://github.com/settings/tokens
// This raises the API rate limit from 60/hr to 5000/hr
'Authorization': 'Basic ' + Buffer.from(githubToken).toString('base64')
}
},
function (res) {
let body = [];
res.on('data', (data) => body.push(data));
res.on('end', () => resolve(body.join('')));
}
);
req.on('error', reject);
});
}