1
1
const npm = require ( 'npm-stats' ) ( )
2
2
const json = require ( 'JSONStream' )
3
3
const request = require ( 'request' )
4
- const cheerio = require ( 'cheerio' )
5
4
const after = require ( 'after' )
6
5
const path = require ( 'path' )
7
6
const fs = require ( 'fs' )
8
7
9
8
const stats = module . exports = { }
10
- const next = after ( 4 , done )
9
+ const next = after ( 6 , done )
11
10
12
- crawlNpm ( next )
11
+ getTotal ( next )
12
+ getDownloadsWeek ( next )
13
+ getDownloadsDay ( next )
13
14
countPublishes ( next )
14
15
getQualityCount ( next )
15
16
getDownloadStarContrast ( next )
16
17
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 ) {
19
32
if ( err ) return next ( err )
20
33
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 )
24
44
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
28
46
47
+ stats [ 'download-day' ] = Number ( count )
29
48
next ( )
30
49
} )
31
50
}
@@ -104,9 +123,11 @@ function getDownloadStarContrast(next) {
104
123
return pkg . ratio !== Infinity
105
124
} )
106
125
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
+ }
110
131
111
132
var topInflated = downloads . filter ( function ( pkg ) {
112
133
return pkg . name . indexOf ( 'lodash' ) // some of these are single-repo, multi-package
@@ -124,9 +145,11 @@ function getDownloadStarContrast(next) {
124
145
return a . ratio - b . ratio
125
146
} )
126
147
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
+ }
130
153
131
154
next ( )
132
155
}
0 commit comments