|
| 1 | +const https = require('https'); |
| 2 | + |
| 3 | +const username = 'pyramation'; // put your username here ;) |
| 4 | + |
| 5 | +function fetch(url) { |
| 6 | + return new Promise((resolve, reject) => { |
| 7 | + https |
| 8 | + .get(url, (resp) => { |
| 9 | + let data = ''; |
| 10 | + resp.on('data', (chunk) => { |
| 11 | + data += chunk; |
| 12 | + }); |
| 13 | + resp.on('end', () => { |
| 14 | + resolve(JSON.parse(data)); |
| 15 | + }); |
| 16 | + }) |
| 17 | + .on('error', (err) => { |
| 18 | + reject(err); |
| 19 | + }); |
| 20 | + }); |
| 21 | +} |
| 22 | + |
| 23 | +async function fetchPackagesByUser(user) { |
| 24 | + const url = `https://registry.npmjs.org/-/v1/search?text=maintainer:${user}&size=100`; |
| 25 | + const data = await fetch(url); |
| 26 | + return data.objects.map((pkg) => pkg.package.name); |
| 27 | +} |
| 28 | + |
| 29 | +async function fetchDownloadCounts(packageName, period) { |
| 30 | + const endDate = new Date(); |
| 31 | + const startDate = new Date(); |
| 32 | + startDate.setDate(endDate.getDate() - period); |
| 33 | + const start = `${startDate.getFullYear()}-${String( |
| 34 | + startDate.getMonth() + 1 |
| 35 | + ).padStart(2, '0')}-${String(startDate.getDate()).padStart(2, '0')}`; |
| 36 | + const end = `${endDate.getFullYear()}-${String( |
| 37 | + endDate.getMonth() + 1 |
| 38 | + ).padStart(2, '0')}-${String(endDate.getDate()).padStart(2, '0')}`; |
| 39 | + const url = `https://api.npmjs.org/downloads/point/${start}:${end}/${packageName}`; |
| 40 | + const result = await fetch(url); |
| 41 | + return result.downloads || 0; |
| 42 | +} |
| 43 | + |
| 44 | +async function getDownloadStats(packages) { |
| 45 | + const weeklyDownloads = await Promise.all( |
| 46 | + packages.map((pkg) => fetchDownloadCounts(pkg, 7)) |
| 47 | + ); |
| 48 | + const monthlyDownloads = await Promise.all( |
| 49 | + packages.map((pkg) => fetchDownloadCounts(pkg, 30)) |
| 50 | + ); |
| 51 | + const totalDownloads = await Promise.all( |
| 52 | + packages.map((pkg) => fetchDownloadCounts(pkg, 365)) |
| 53 | + ); |
| 54 | + |
| 55 | + const weeklyTotal = weeklyDownloads.reduce( |
| 56 | + (total, count) => total + count, |
| 57 | + 0 |
| 58 | + ); |
| 59 | + const monthlyTotal = monthlyDownloads.reduce( |
| 60 | + (total, count) => total + count, |
| 61 | + 0 |
| 62 | + ); |
| 63 | + const yearlyTotal = totalDownloads.reduce((total, count) => total + count, 0); |
| 64 | + |
| 65 | + return { |
| 66 | + weeklyTotal: weeklyTotal.toLocaleString(), |
| 67 | + monthlyTotal: monthlyTotal.toLocaleString(), |
| 68 | + yearlyTotal: yearlyTotal.toLocaleString() |
| 69 | + }; |
| 70 | +} |
| 71 | + |
| 72 | +(async () => { |
| 73 | + const packages = await fetchPackagesByUser(username); |
| 74 | + const { weeklyTotal, monthlyTotal, yearlyTotal } = await getDownloadStats( |
| 75 | + packages |
| 76 | + ); |
| 77 | + console.log( |
| 78 | + `Weekly downloads for packages maintained by ${username}: ${weeklyTotal}` |
| 79 | + ); |
| 80 | + console.log( |
| 81 | + `Monthly downloads for packages maintained by ${username}: ${monthlyTotal}` |
| 82 | + ); |
| 83 | + console.log( |
| 84 | + `Total downloads for the last year for packages maintained by ${username}: ${yearlyTotal}` |
| 85 | + ); |
| 86 | +})(); |
0 commit comments