Pull plugin stats #51
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 'admin@obsidian.md' | |
git add . | |
git commit -m "chore: Update plugin stats" | |
git push |