Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit 7bfaf29

Browse files
committed
fix stats script
1 parent be24598 commit 7bfaf29

File tree

2 files changed

+40
-18
lines changed

2 files changed

+40
-18
lines changed

Diff for: unpm-dat/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
"dependencies": {
1010
"JSONStream": "^0.10.0",
1111
"after": "^0.8.1",
12-
"cheerio": "^0.19.0",
1312
"concat-stream": "^1.4.8",
1413
"dat-npm": "^4.0.6",
1514
"ghauth": "^2.0.1",

Diff for: unpm-dat/stats.js

+40-17
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,50 @@
11
const npm = require('npm-stats')()
22
const json = require('JSONStream')
33
const request = require('request')
4-
const cheerio = require('cheerio')
54
const after = require('after')
65
const path = require('path')
76
const fs = require('fs')
87

98
const stats = module.exports = {}
10-
const next = after(4, done)
9+
const next = after(6, done)
1110

12-
crawlNpm(next)
11+
getTotal(next)
12+
getDownloadsWeek(next)
13+
getDownloadsDay(next)
1314
countPublishes(next)
1415
getQualityCount(next)
1516
getDownloadStarContrast(next)
1617

17-
function crawlNpm(next) {
18-
request.get('https://npmjs.com/', function(err, res, body) {
18+
19+
function getTotal(next) {
20+
request('https://skimdb.npmjs.com/registry', { json: true }, function (err, res, body) {
21+
if (err) return next(err)
22+
23+
const total = body && body.doc_count || 0
24+
25+
stats['total'] = Number(total)
26+
next()
27+
})
28+
}
29+
30+
function getDownloadsWeek(next) {
31+
request('https://api.npmjs.org/downloads/point/last-week', { json: true }, function (err, res, body) {
1932
if (err) return next(err)
2033

21-
const $ = cheerio.load(body+'')
22-
const dls = $('.icon-cal-7days .pretty-number')[0]
23-
const total = $('.icon-package-hex .total-packages')[0]
34+
const count = body && body.downloads || 0
35+
36+
stats['download-week'] = Number(count)
37+
next()
38+
})
39+
}
40+
41+
function getDownloadsDay(next) {
42+
request('https://api.npmjs.org/downloads/point/last-day', { json: true }, function (err, res, body) {
43+
if (err) return next(err)
2444

25-
stats['total'] = Number($(total).text())
26-
stats['download-week'] = Number($(dls).text())
27-
stats['download-day'] = Math.floor(stats['download-week'] / 7)
45+
const count = body && body.downloads || 0
2846

47+
stats['download-day'] = Number(count)
2948
next()
3049
})
3150
}
@@ -104,9 +123,11 @@ function getDownloadStarContrast(next) {
104123
return pkg.ratio !== Infinity
105124
})
106125

107-
stats['ignored-name'] = topIgnored[0].name
108-
stats['ignored-stars'] = topIgnored[0].stars
109-
stats['ignored-downloads'] = topIgnored[0].count
126+
if (topIgnored[0]) {
127+
stats['ignored-name'] = topIgnored[0].name
128+
stats['ignored-stars'] = topIgnored[0].stars
129+
stats['ignored-downloads'] = topIgnored[0].count
130+
}
110131

111132
var topInflated = downloads.filter(function(pkg) {
112133
return pkg.name.indexOf('lodash') // some of these are single-repo, multi-package
@@ -124,9 +145,11 @@ function getDownloadStarContrast(next) {
124145
return a.ratio - b.ratio
125146
})
126147

127-
stats['inflated-name'] = topInflated[0].name
128-
stats['inflated-stars'] = topInflated[0].stars
129-
stats['inflated-downloads'] = topInflated[0].count
148+
if (topInflated[0]) {
149+
stats['inflated-name'] = topInflated[0].name
150+
stats['inflated-stars'] = topInflated[0].stars
151+
stats['inflated-downloads'] = topInflated[0].count
152+
}
130153

131154
next()
132155
}

0 commit comments

Comments
 (0)