33// Get image sizes for images on a given url
44var Scraper = require ( 'image-scraper' ) ,
55 request = require ( 'request' ) ,
6+ async = require ( 'async' ) ,
67 Filesize = require ( 'filesize' ) ,
78 colors = require ( 'colors/safe' ) ,
89 sprintf = require ( "sprintf-js" ) . sprintf ,
910 argv = require ( 'minimist' ) ( process . argv . slice ( 2 ) ) ;
1011
11- var usage = "Usage: image_check -u URL [-b MIN_BYTES_TO_ALERT_ON]\n" +
12+ var usage = "Usage: image_check -u URL [-b MIN_BYTES_TO_ALERT_ON] [-j|-json] \n" +
1213 "Ex: " + colors . grey ( "image_check -u http://www.google.com -b 1k" ) ;
1314
15+
1416if ( ! argv . u ) {
1517 console . log ( usage ) ;
1618 process . exit ( 1 ) ;
1719}
1820
19- var test_url = argv . u ,
20- max_size_bytes = argv . b || 0 ;
21+ var url = argv . u ,
22+ max_size_bytes = argv . b || 0 ,
23+ json_output = argv . j || argv . json || false ;
2124
2225if ( typeof ( max_size_bytes ) === "string" && max_size_bytes . match ( / k / i) ) {
2326 max_size_bytes = max_size_bytes . replace ( / k / i, "" ) ;
@@ -30,46 +33,80 @@ if (isNaN(max_size_bytes)){
3033 process . exit ( 1 ) ;
3134}
3235
33- if ( ! test_url . match ( / h t t p / i) && ! test_url . match ( / h t t p s / i) ) {
34- test_url = "http://" + test_url ;
35- }
36-
37- if ( max_size_bytes ) {
38- console . log ( colors . bold ( "Image files >" + Filesize ( max_size_bytes ) + " (" + max_size_bytes + " bytes)" ) ) ;
36+ if ( ! url . match ( / h t t p / i) && ! url . match ( / h t t p s / i) ) {
37+ url = "http://" + url ;
3938}
4039
41- var scraper = new Scraper ( test_url ) ;
40+ var scraper = new Scraper ( url ) ;
4241
42+ var asyncTasks = [ ] ;
4343scraper . on ( "image" , function ( image ) {
44- processImage ( image ) ;
44+ asyncTasks . push ( function ( callback ) {
45+ processImage ( image , callback ) ;
46+ } ) ;
4547} ) ;
4648
4749scraper . on ( "end" , function ( ) {
48- console . log ( "END" ) ;
50+ async . parallel ( asyncTasks , function ( ) {
51+ // Sort the output by bytes descending
52+ json . images . sort ( function ( a , b ) {
53+ return ( b . bytes - a . bytes ) ;
54+ } ) ;
55+
56+ if ( json_output ) {
57+ console . log ( json ) ;
58+ } else {
59+ if ( max_size_bytes ) {
60+ console . log ( colors . bold ( "Image files >" + Filesize ( max_size_bytes ) + " (" + max_size_bytes + " bytes)" ) ) ;
61+ }
62+
63+ if ( json . images . length > 0 ) {
64+ json . images . forEach ( function ( image_data ) {
65+ var image_url = image_data . image_url ,
66+ file_size_bytes = image_data . bytes ,
67+ file_size = Filesize ( file_size_bytes ) ,
68+ formatted_file_size = sprintf ( "%11s" , file_size ) ;
69+
70+ // Images 3x the max size get highlighted in red
71+ var formatted_output = colors . yellow ( formatted_file_size ) ;
72+ if ( file_size_bytes > ( 3 * max_size_bytes ) ) {
73+ formatted_output = colors . red ( formatted_file_size ) ;
74+ }
75+ formatted_output += " " + colors . cyan ( image_url ) ;
76+
77+ console . log ( formatted_output ) ;
78+ } ) ;
79+ } else {
80+ console . log ( "No images found." ) ;
81+ }
82+ }
83+ } ) ;
4984} ) ;
5085
51- function processImage ( image ) {
86+ var formatted_output_arr = { } ,
87+ json = {
88+ url : url ,
89+ byte_threshold : max_size_bytes ,
90+ images : [ ]
91+ } ;
92+ function processImage ( image , callback ) {
5293 var image_url = image . address ;
5394 request . head ( image_url , function ( err , res ) {
5495 if ( err ) {
5596 return console . error ( colors . red ( "Error" ) , err ) ;
5697 }
5798
5899 if ( res && res . headers [ 'content-length' ] ) {
59- var file_size_bytes = + ( res . headers [ 'content-length' ] ) ,
60- file_size = Filesize ( file_size_bytes ) ;
100+ var file_size_bytes = + ( res . headers [ 'content-length' ] ) ;
61101
62102 if ( file_size_bytes > max_size_bytes ) {
63- var formatted_file_size = sprintf ( "%11s" , file_size ) ;
64- // Images 3x the max size get highlighted in red
65- var formatted_output = colors . yellow ( formatted_file_size ) ;
66- if ( file_size_bytes > ( 3 * max_size_bytes ) ) {
67- formatted_output = colors . red ( formatted_file_size ) ;
68- }
69- formatted_output += " " + colors . cyan ( image_url ) ;
70- console . log ( formatted_output ) ;
103+ json . images . push ( {
104+ image_url : image_url ,
105+ bytes : file_size_bytes
106+ } ) ;
71107 }
72108 }
109+ callback ( ) ;
73110 } ) ;
74111}
75- scraper . scrape ( ) ;
112+ scraper . scrape ( ) ;
0 commit comments