|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Fetch static data at build time to avoid runtime API rate limits. |
| 5 | + * Writes results to static/data/ so they are served as static assets. |
| 6 | + * |
| 7 | + * Data fetched: |
| 8 | + * - Docker Hub pulls (via shields.io) → static/data/downloads.json |
| 9 | + * - GitHub contributors across all repos → static/data/contributors.json |
| 10 | + */ |
| 11 | + |
| 12 | +const https = require('https'); |
| 13 | +const fs = require('fs'); |
| 14 | +const path = require('path'); |
| 15 | + |
| 16 | +const DATA_DIR = path.join(__dirname, '..', 'static', 'data'); |
| 17 | + |
| 18 | +const REPOS = [ |
| 19 | + 'm3ue/m3u-editor', |
| 20 | + 'm3ue/m3u-proxy', |
| 21 | + 'm3ue/m3u-editor-docs-v2', |
| 22 | +]; |
| 23 | + |
| 24 | +/** |
| 25 | + * Perform a GET request and resolve with the parsed JSON body. |
| 26 | + */ |
| 27 | +function fetchJson(url, headers = {}) { |
| 28 | + return new Promise((resolve, reject) => { |
| 29 | + const options = { headers: { 'User-Agent': 'docusaurus-prebuild', ...headers } }; |
| 30 | + https.get(url, options, (res) => { |
| 31 | + let raw = ''; |
| 32 | + res.on('data', (chunk) => { raw += chunk; }); |
| 33 | + res.on('end', () => { |
| 34 | + if (res.statusCode >= 200 && res.statusCode < 300) { |
| 35 | + try { |
| 36 | + resolve(JSON.parse(raw)); |
| 37 | + } catch (e) { |
| 38 | + reject(new Error(`JSON parse error for ${url}: ${e.message}`)); |
| 39 | + } |
| 40 | + } else { |
| 41 | + reject(new Error(`HTTP ${res.statusCode} for ${url}`)); |
| 42 | + } |
| 43 | + }); |
| 44 | + }).on('error', reject); |
| 45 | + }); |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Fetch Docker Hub pull count and write to static/data/downloads.json |
| 50 | + */ |
| 51 | +async function fetchDownloads() { |
| 52 | + const data = await fetchJson( |
| 53 | + 'https://img.shields.io/docker/pulls/sparkison/m3u-editor.json' |
| 54 | + ); |
| 55 | + const formatted = data.value || '0'; |
| 56 | + const result = { formatted: `${formatted}+`, fetchedAt: new Date().toISOString() }; |
| 57 | + fs.writeFileSync( |
| 58 | + path.join(DATA_DIR, 'downloads.json'), |
| 59 | + JSON.stringify(result, null, 2) |
| 60 | + ); |
| 61 | + console.log(` ✓ Downloads cached: ${result.formatted}`); |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Fetch contributors from all repos and write to static/data/contributors.json |
| 66 | + */ |
| 67 | +async function fetchContributors() { |
| 68 | + const allContributors = new Map(); |
| 69 | + |
| 70 | + for (const repo of REPOS) { |
| 71 | + console.log(` → Fetching contributors for ${repo}...`); |
| 72 | + const data = await fetchJson( |
| 73 | + `https://api.github.com/repos/${repo}/contributors?per_page=100`, |
| 74 | + { Accept: 'application/vnd.github.v3+json' } |
| 75 | + ); |
| 76 | + |
| 77 | + data.forEach((contributor) => { |
| 78 | + if (contributor.login.endsWith('[bot]') || contributor.login === 'Copilot') { |
| 79 | + return; |
| 80 | + } |
| 81 | + if (allContributors.has(contributor.login)) { |
| 82 | + allContributors.get(contributor.login).contributions += contributor.contributions; |
| 83 | + } else { |
| 84 | + allContributors.set(contributor.login, { |
| 85 | + login: contributor.login, |
| 86 | + avatar_url: contributor.avatar_url, |
| 87 | + html_url: contributor.html_url, |
| 88 | + contributions: contributor.contributions, |
| 89 | + }); |
| 90 | + } |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + const sorted = Array.from(allContributors.values()) |
| 95 | + .sort((a, b) => b.contributions - a.contributions); |
| 96 | + |
| 97 | + const result = { contributors: sorted, fetchedAt: new Date().toISOString() }; |
| 98 | + fs.writeFileSync( |
| 99 | + path.join(DATA_DIR, 'contributors.json'), |
| 100 | + JSON.stringify(result, null, 2) |
| 101 | + ); |
| 102 | + console.log(` ✓ Contributors cached: ${sorted.length} unique contributors`); |
| 103 | +} |
| 104 | + |
| 105 | +(async () => { |
| 106 | + console.log('Fetching static data...'); |
| 107 | + fs.mkdirSync(DATA_DIR, { recursive: true }); |
| 108 | + |
| 109 | + const results = await Promise.allSettled([fetchDownloads(), fetchContributors()]); |
| 110 | + |
| 111 | + results.forEach((r) => { |
| 112 | + if (r.status === 'rejected') { |
| 113 | + console.warn(` ⚠ Warning: ${r.reason.message}`); |
| 114 | + } |
| 115 | + }); |
| 116 | + |
| 117 | + console.log('Static data fetch complete.'); |
| 118 | +})(); |
0 commit comments