-
-
Save Uvacoder/8674bf7d4267594656ce91c7526b19a7 to your computer and use it in GitHub Desktop.
Get npm download count by period
This file contains hidden or 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
| const path = require('path') | |
| const fetch = require('node-fetch') | |
| const flatcache = require('flat-cache') | |
| /* Download API | |
| period: Joi.string().regex(/(\d{4}-\d{2}-\d{2}(:\d{4}-\d{2}-\d{2})?|[\w-]+)/).required(), | |
| Package "package-name", last week: | |
| /downloads/point/last-week/package-name | |
| Package "package-name", given 7-day period: | |
| /downloads/point/2014-02-01:2014-02-08/package-name | |
| Package "package-name", last 30 days: | |
| /downloads/point/last-month/package-name | |
| Package "package-name", specific month: | |
| /downloads/point/2014-01-01:2014-01-31/package-name | |
| */ | |
| const lastYear = `${getDateRange(-365)}:${getDateRange()}` | |
| const TIMEFRAMES = { | |
| 'last-day': 'last-day', | |
| '1d': 'last-day', | |
| 'last-week': 'last-week', | |
| '1w': 'last-week', | |
| 'last-month': 'last-month', | |
| '1m': 'last-month', | |
| 'last-year': lastYear, | |
| '1y': lastYear, | |
| } | |
| async function getStats(opts) { | |
| if (!opts.pkg) { | |
| throw new Error('No pkg name specified') | |
| } | |
| if (!opts.timeframe) { | |
| throw new Error('No timeframe specified') | |
| } | |
| let cache = flatcache.load('npm-downloads', path.resolve('./cache')) | |
| let key = getCacheKey() | |
| let cachedData = cache.getKey(key) | |
| if (cachedData) { | |
| console.log(`Returning cached results for ${opts.timeframe}...`) | |
| return cachedData | |
| } | |
| const range = TIMEFRAMES[opts.timeframe] || opts.timeframe | |
| console.log(`Fetching new npm download count for ${opts.timeframe}...`) | |
| try { | |
| // let newData = await fetch("https://api.npmjs.org/downloads/point/last-month/${pkg}") | |
| let newData = await fetch(`https://api.npmjs.org/downloads/point/${range}/${opts.pkg}`) | |
| .then(res => res.json()) | |
| .then(json => { | |
| return { | |
| downloads: json.downloads | |
| } | |
| }) | |
| cache.setKey(key, newData) | |
| cache.save() | |
| return newData | |
| } catch (e) { | |
| console.log('Failed, returning 0') | |
| return { | |
| downloads: 0 | |
| } | |
| } | |
| } | |
| function getCacheKey() { | |
| let date = new Date() | |
| return `${date.getUTCFullYear()}-${date.getUTCMonth() + 1}-${date.getUTCDate()}` | |
| } | |
| function pad(num) { | |
| return `${num < 10 ? '0' : ''}${num}` | |
| } | |
| function getDateRange(daysOffset) { | |
| let date = new Date() | |
| if (daysOffset) { | |
| date.setTime(date.getTime() + daysOffset * 1000 * 60 * 60 * 24) | |
| } | |
| return `${date.getUTCFullYear()}-${pad(date.getUTCMonth() + 1)}-${pad(date.getUTCDate())}` | |
| } | |
| getStats({ | |
| pkg: 'analytics', | |
| timeframe: 'last-year', | |
| }).then((data) => { | |
| console.log('d', data) | |
| }).catch((e) => { | |
| console.log('e', e) | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment