|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const POSTHOG_KEY = process.env.POSTHOG_KEY || process.env.POSTHOG_API_KEY |
| 4 | +const POSTHOG_HOST = process.env.POSTHOG_HOST || "https://us.i.posthog.com" |
| 5 | +const POSTHOG_EVENT = process.env.POSTHOG_EVENT || "download" |
| 6 | +const POSTHOG_DISTINCT_ID = process.env.POSTHOG_DISTINCT_ID || "openwork-download" |
| 7 | +const GITHUB_REPO = process.env.GITHUB_REPO || "different-ai/openwork" |
| 8 | +const STATS_FILE = process.env.STATS_FILE || "STATS.md" |
| 9 | + |
| 10 | +async function sendToPostHog(event, properties) { |
| 11 | + if (!POSTHOG_KEY) { |
| 12 | + console.warn("POSTHOG_KEY not set, skipping PostHog event") |
| 13 | + return |
| 14 | + } |
| 15 | + |
| 16 | + const response = await fetch(`${POSTHOG_HOST}/i/v0/e/`, { |
| 17 | + method: "POST", |
| 18 | + headers: { |
| 19 | + "Content-Type": "application/json", |
| 20 | + }, |
| 21 | + body: JSON.stringify({ |
| 22 | + distinct_id: POSTHOG_DISTINCT_ID, |
| 23 | + api_key: POSTHOG_KEY, |
| 24 | + event, |
| 25 | + properties, |
| 26 | + }), |
| 27 | + }).catch(() => null) |
| 28 | + |
| 29 | + if (response && !response.ok) { |
| 30 | + console.warn(`PostHog API error: ${response.status}`) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +async function fetchReleases() { |
| 35 | + const releases = [] |
| 36 | + let page = 1 |
| 37 | + const perPage = 100 |
| 38 | + const headers = { |
| 39 | + Accept: "application/vnd.github+json", |
| 40 | + "User-Agent": "openwork-download-stats", |
| 41 | + } |
| 42 | + const token = process.env.GITHUB_TOKEN |
| 43 | + |
| 44 | + if (token) { |
| 45 | + headers.Authorization = `Bearer ${token}` |
| 46 | + } |
| 47 | + |
| 48 | + while (true) { |
| 49 | + const url = `https://api.github.com/repos/${GITHUB_REPO}/releases?page=${page}&per_page=${perPage}` |
| 50 | + const response = await fetch(url, { headers }) |
| 51 | + |
| 52 | + if (!response.ok) { |
| 53 | + throw new Error(`GitHub API error: ${response.status} ${response.statusText}`) |
| 54 | + } |
| 55 | + |
| 56 | + const batch = await response.json() |
| 57 | + if (!Array.isArray(batch) || batch.length === 0) break |
| 58 | + |
| 59 | + releases.push(...batch) |
| 60 | + console.log(`Fetched page ${page} with ${batch.length} releases`) |
| 61 | + |
| 62 | + if (batch.length < perPage) break |
| 63 | + page += 1 |
| 64 | + await new Promise((resolve) => setTimeout(resolve, 1000)) |
| 65 | + } |
| 66 | + |
| 67 | + return releases |
| 68 | +} |
| 69 | + |
| 70 | +function calculate(releases) { |
| 71 | + let total = 0 |
| 72 | + const stats = [] |
| 73 | + |
| 74 | + for (const release of releases) { |
| 75 | + let downloads = 0 |
| 76 | + const assets = [] |
| 77 | + |
| 78 | + for (const asset of release.assets ?? []) { |
| 79 | + downloads += asset.download_count |
| 80 | + assets.push({ |
| 81 | + name: asset.name, |
| 82 | + downloads: asset.download_count, |
| 83 | + }) |
| 84 | + } |
| 85 | + |
| 86 | + total += downloads |
| 87 | + stats.push({ |
| 88 | + tag: release.tag_name, |
| 89 | + name: release.name, |
| 90 | + downloads, |
| 91 | + assets, |
| 92 | + }) |
| 93 | + } |
| 94 | + |
| 95 | + return { total, stats } |
| 96 | +} |
| 97 | + |
| 98 | +async function save(githubTotal) { |
| 99 | + const date = new Date().toISOString().split("T")[0] |
| 100 | + const total = githubTotal |
| 101 | + |
| 102 | + let previousGithub = 0 |
| 103 | + let previousTotal = 0 |
| 104 | + let content = "" |
| 105 | + |
| 106 | + try { |
| 107 | + const file = await import("node:fs/promises") |
| 108 | + content = await file.readFile(STATS_FILE, "utf8") |
| 109 | + const lines = content.trim().split("\n") |
| 110 | + |
| 111 | + for (let i = lines.length - 1; i >= 0; i -= 1) { |
| 112 | + const line = lines[i].trim() |
| 113 | + if (line.startsWith("|") && !line.includes("Date") && !line.includes("---")) { |
| 114 | + const match = line.match( |
| 115 | + /\|\s*[\d-]+\s*\|\s*([\d,]+)\s*(?:\([^)]*\))?\s*\|\s*([\d,]+)\s*(?:\([^)]*\))?\s*\|/, |
| 116 | + ) |
| 117 | + if (match) { |
| 118 | + previousGithub = parseInt(match[1].replace(/,/g, ""), 10) |
| 119 | + previousTotal = parseInt(match[2].replace(/,/g, ""), 10) |
| 120 | + break |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + } catch { |
| 125 | + content = |
| 126 | + "# Download Stats\n\n| Date | GitHub Downloads | Total |\n|------|------------------|-------|\n" |
| 127 | + } |
| 128 | + |
| 129 | + const githubChange = githubTotal - previousGithub |
| 130 | + const totalChange = total - previousTotal |
| 131 | + |
| 132 | + const githubChangeStr = |
| 133 | + githubChange > 0 |
| 134 | + ? ` (+${githubChange.toLocaleString()})` |
| 135 | + : githubChange < 0 |
| 136 | + ? ` (${githubChange.toLocaleString()})` |
| 137 | + : " (+0)" |
| 138 | + const totalChangeStr = |
| 139 | + totalChange > 0 |
| 140 | + ? ` (+${totalChange.toLocaleString()})` |
| 141 | + : totalChange < 0 |
| 142 | + ? ` (${totalChange.toLocaleString()})` |
| 143 | + : " (+0)" |
| 144 | + const line = `| ${date} | ${githubTotal.toLocaleString()}${githubChangeStr} | ${total.toLocaleString()}${totalChangeStr} |\n` |
| 145 | + |
| 146 | + if (!content.includes("# Download Stats")) { |
| 147 | + content = |
| 148 | + "# Download Stats\n\n| Date | GitHub Downloads | Total |\n|------|------------------|-------|\n" |
| 149 | + } |
| 150 | + |
| 151 | + const file = await import("node:fs/promises") |
| 152 | + await file.writeFile(STATS_FILE, content + line, "utf8") |
| 153 | + |
| 154 | + console.log( |
| 155 | + `\nAppended stats to ${STATS_FILE}: GitHub ${githubTotal.toLocaleString()}${githubChangeStr}, Total ${total.toLocaleString()}${totalChangeStr}`, |
| 156 | + ) |
| 157 | +} |
| 158 | + |
| 159 | +console.log(`Fetching GitHub releases for ${GITHUB_REPO}...\n`) |
| 160 | + |
| 161 | +const releases = await fetchReleases() |
| 162 | +console.log(`\nFetched ${releases.length} releases total\n`) |
| 163 | + |
| 164 | +const { total: githubTotal } = calculate(releases) |
| 165 | + |
| 166 | +await save(githubTotal) |
| 167 | + |
| 168 | +await sendToPostHog(POSTHOG_EVENT, { |
| 169 | + count: githubTotal, |
| 170 | + source: "github", |
| 171 | + repo: GITHUB_REPO, |
| 172 | +}) |
| 173 | + |
| 174 | +console.log("=".repeat(60)) |
| 175 | +console.log(`TOTAL DOWNLOADS: ${githubTotal.toLocaleString()}`) |
| 176 | +console.log(` GitHub: ${githubTotal.toLocaleString()}`) |
| 177 | +console.log("=".repeat(60)) |
0 commit comments