forked from obsidianmd/obsidian-releases
-
Notifications
You must be signed in to change notification settings - Fork 0
102 lines (85 loc) · 3.6 KB
/
plugin-stat.yml
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
name: Pull plugin stats
on:
schedule:
- cron: "0 0 * * *"
workflow_dispatch: # Put here!!
jobs:
pull-stats:
if: github.repository == 'obsidianmd/obsidian-releases'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v2
- uses: actions/github-script@v6
with:
github-token: ${{ secrets.PLUGIN_STAT_TOKEN }}
script: |
let fs = require('fs');
let plugins = JSON.parse(fs.readFileSync('./community-plugins.json', 'utf8'));
let stats = JSON.parse(fs.readFileSync('./community-plugin-stats.json', 'utf8'));
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];
}
}
(async() => {
console.log('Rate limit', (await github.rest.rateLimit.get()).data.rate);
for (let key in plugins) {
if (!plugins.hasOwnProperty(key)) {
continue;
}
try {
let plugin = plugins[key];
let id = plugin.id;
console.log(`Downloading stats for ${id} (${plugin.repo})`);
let stats = newStats[id] = newStats[id] || {};
let [owner, repo] = plugin.repo.split('/');
const releases = [];
const releasesData = github.paginate.iterator(github.rest.repos.listReleases, {owner, repo, per_page: 100});
for await (const { data: data } of releasesData) {
releases.push(...data);
}
// stats is Array<{tag_name: string, assets: Array<{name: string, download_count}>}>
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(`Downloads: ${total} Releases: ${releases.length}`);
stats['downloads'] = total;
stats['updated'] = updated;
} catch (e) {
console.log('Failed', e.message);
}
}
fs.writeFileSync('./community-plugin-stats.json', JSON.stringify(newStats, null, 2), 'utf8')
console.log('All done!');
})();
- run: |
git config --local user.name 'Obsidian Bot'
git config --local user.email '[email protected]'
git add .
git commit -m "chore: Update plugin stats"
git push