Skip to content

Commit 32ac2cf

Browse files
committed
JSON output format. Sorting by size
1 parent 7f07990 commit 32ac2cf

File tree

3 files changed

+83
-32
lines changed

3 files changed

+83
-32
lines changed

README.md

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,31 @@
11
node-image-size-scanner
22
=======================
3-
For a given URL, report image file sizes and paths with optional minimum file size to report on.
3+
For a given URL, report image file sizes and paths. Optionally you may specify a minimum file size to report on. Formatted output or json output.
44

55
## Installation ##
66
`npm install -g node-image-size-scanner`
77

88
## Usage ##
99
```
1010
$ image_check
11-
Usage: image_check -u URL [-b MIN_BYTES_TO_ALERT_ON]
11+
Usage: image_check -u URL [-b MIN_BYTES_TO_ALERT_ON] [-j|-json]
1212
Ex: image_check -u http://www.google.com -b 1k
13-
13+
```
14+
### Formatted Output ###
15+
```
1416
$ image_check -u http://www.google.com -b 1k
1517
Image files > 1.00 kB (1000 bytes)
16-
1.83 kB http://www.google.com/images/icons/product/chrome-48.png
17-
209.03 kB http://www.google.com/logos/doodles/2014/halloween-2014-5647569745084416.3-hp.gif
18+
8.23 kB https://www.google.com/images/srpr/logo9w.png
19+
1.83 kB https://www.google.com/images/icons/product/chrome-48.png
20+
```
21+
### JSON output
22+
```
23+
$ image_check -u https://www.google.com -b 1k -j
24+
{ url: 'https://www.google.com',
25+
byte_threshold: 1000,
26+
images:
27+
[ { image_url: 'https://www.google.com/images/srpr/logo9w.png',
28+
bytes: 8228 },
29+
{ image_url: 'https://www.google.com/images/icons/product/chrome-48.png',
30+
bytes: 1834 } ] }
1831
```

image_check.js

Lines changed: 61 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,24 @@
33
// Get image sizes for images on a given url
44
var 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+
1416
if (!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

2225
if (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(/http/i) && !test_url.match(/https/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(/http/i) && !url.match(/https/i)) {
37+
url = "http://" + url;
3938
}
4039

41-
var scraper = new Scraper (test_url);
40+
var scraper = new Scraper (url);
4241

42+
var asyncTasks = [];
4343
scraper.on("image", function(image){
44-
processImage(image);
44+
asyncTasks.push(function(callback) {
45+
processImage(image, callback);
46+
});
4547
});
4648

4749
scraper.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();

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
{
22
"name": "node-image-size-scanner",
3-
"version": "0.0.9",
4-
"description": "For a given URL, report image file sizes and paths with optional minimum file size to report on.",
3+
"version": "0.0.10",
4+
"description": "For a given URL, report image file sizes and paths that exceed a threshold value.",
55
"main": "image_check.js",
66
"author": "Kip Gebhardt <[email protected]>",
77
"license": "ISC",
88
"bugs": {
99
"url": "https://github.com/rv-kip/node-image-size-scanner/issues"
1010
},
1111
"dependencies": {
12+
"async": "^0.9.0",
1213
"colors": "^1.0.2",
1314
"filesize": "^2.0.4",
14-
"image-scraper": "^0.1.2",
15+
"image-scraper": "git://github.com/rv-kip/node-image-scraper.git#v0.1.2.1",
1516
"minimist": "^1.1.0",
1617
"request": "^2.45.0",
1718
"sprintf-js": "^1.0.2"

0 commit comments

Comments
 (0)