11const npm = require ( 'npm-stats' ) ( )
22const json = require ( 'JSONStream' )
33const request = require ( 'request' )
4- const cheerio = require ( 'cheerio' )
54const after = require ( 'after' )
65const path = require ( 'path' )
76const fs = require ( 'fs' )
87
98const 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 )
1314countPublishes ( next )
1415getQualityCount ( next )
1516getDownloadStarContrast ( 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