Skip to content

Commit

Permalink
JSON output format. Sorting by size
Browse files Browse the repository at this point in the history
  • Loading branch information
rv-kip committed Mar 4, 2015
1 parent 7f07990 commit 32ac2cf
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 32 deletions.
23 changes: 18 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
node-image-size-scanner
=======================
For a given URL, report image file sizes and paths with optional minimum file size to report on.
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.

## Installation ##
`npm install -g node-image-size-scanner`

## Usage ##
```
$ image_check
Usage: image_check -u URL [-b MIN_BYTES_TO_ALERT_ON]
Usage: image_check -u URL [-b MIN_BYTES_TO_ALERT_ON] [-j|-json]
Ex: image_check -u http://www.google.com -b 1k
```
### Formatted Output ###
```
$ image_check -u http://www.google.com -b 1k
Image files > 1.00 kB (1000 bytes)
1.83 kB http://www.google.com/images/icons/product/chrome-48.png
209.03 kB http://www.google.com/logos/doodles/2014/halloween-2014-5647569745084416.3-hp.gif
8.23 kB https://www.google.com/images/srpr/logo9w.png
1.83 kB https://www.google.com/images/icons/product/chrome-48.png
```
### JSON output
```
$ image_check -u https://www.google.com -b 1k -j
{ url: 'https://www.google.com',
byte_threshold: 1000,
images:
[ { image_url: 'https://www.google.com/images/srpr/logo9w.png',
bytes: 8228 },
{ image_url: 'https://www.google.com/images/icons/product/chrome-48.png',
bytes: 1834 } ] }
```
85 changes: 61 additions & 24 deletions image_check.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@
// Get image sizes for images on a given url
var Scraper = require('image-scraper'),
request = require('request'),
async = require('async'),
Filesize = require('filesize'),
colors = require('colors/safe'),
sprintf=require("sprintf-js").sprintf,
argv = require('minimist')(process.argv.slice(2));

var usage = "Usage: image_check -u URL [-b MIN_BYTES_TO_ALERT_ON]\n" +
var usage = "Usage: image_check -u URL [-b MIN_BYTES_TO_ALERT_ON] [-j|-json]\n" +
"Ex: " + colors.grey("image_check -u http://www.google.com -b 1k");


if (!argv.u) {
console.log(usage);
process.exit(1);
}

var test_url = argv.u,
max_size_bytes = argv.b || 0;
var url = argv.u,
max_size_bytes = argv.b || 0,
json_output = argv.j || argv.json || false;

if (typeof(max_size_bytes) === "string" && max_size_bytes.match(/k/i)){
max_size_bytes = max_size_bytes.replace(/k/i, "");
Expand All @@ -30,46 +33,80 @@ if (isNaN(max_size_bytes)){
process.exit(1);
}

if (!test_url.match(/http/i) && !test_url.match(/https/i)) {
test_url = "http://" + test_url;
}

if (max_size_bytes) {
console.log(colors.bold("Image files >" + Filesize(max_size_bytes) + " (" + max_size_bytes + " bytes)"));
if (!url.match(/http/i) && !url.match(/https/i)) {
url = "http://" + url;
}

var scraper = new Scraper (test_url);
var scraper = new Scraper (url);

var asyncTasks = [];
scraper.on("image", function(image){
processImage(image);
asyncTasks.push(function(callback) {
processImage(image, callback);
});
});

scraper.on("end", function(){
console.log("END");
async.parallel(asyncTasks, function(){
// Sort the output by bytes descending
json.images.sort(function(a, b){
return (b.bytes - a.bytes);
});

if (json_output) {
console.log(json);
} else {
if (max_size_bytes) {
console.log(colors.bold("Image files >" + Filesize(max_size_bytes) + " (" + max_size_bytes + " bytes)"));
}

if (json.images.length > 0) {
json.images.forEach(function(image_data){
var image_url = image_data.image_url,
file_size_bytes = image_data.bytes,
file_size = Filesize(file_size_bytes),
formatted_file_size = sprintf("%11s", file_size);

// Images 3x the max size get highlighted in red
var formatted_output = colors.yellow(formatted_file_size);
if (file_size_bytes > (3 * max_size_bytes)) {
formatted_output = colors.red(formatted_file_size);
}
formatted_output += " " + colors.cyan(image_url);

console.log(formatted_output);
});
} else {
console.log("No images found.");
}
}
});
});

function processImage(image) {
var formatted_output_arr = {},
json = {
url : url,
byte_threshold : max_size_bytes,
images : []
};
function processImage(image, callback) {
var image_url = image.address;
request.head(image_url, function(err, res){
if (err) {
return console.error(colors.red("Error"), err);
}

if (res && res.headers['content-length']){
var file_size_bytes = +(res.headers['content-length']),
file_size = Filesize(file_size_bytes);
var file_size_bytes = +(res.headers['content-length']);

if (file_size_bytes > max_size_bytes) {
var formatted_file_size = sprintf("%11s", file_size);
// Images 3x the max size get highlighted in red
var formatted_output = colors.yellow(formatted_file_size);
if (file_size_bytes > (3 * max_size_bytes)) {
formatted_output = colors.red(formatted_file_size);
}
formatted_output += " " + colors.cyan(image_url);
console.log(formatted_output);
json.images.push({
image_url: image_url,
bytes: file_size_bytes
});
}
}
callback();
});
}
scraper.scrape();
scraper.scrape();
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
{
"name": "node-image-size-scanner",
"version": "0.0.9",
"description": "For a given URL, report image file sizes and paths with optional minimum file size to report on.",
"version": "0.0.10",
"description": "For a given URL, report image file sizes and paths that exceed a threshold value.",
"main": "image_check.js",
"author": "Kip Gebhardt <[email protected]>",
"license": "ISC",
"bugs": {
"url": "https://github.com/rv-kip/node-image-size-scanner/issues"
},
"dependencies": {
"async": "^0.9.0",
"colors": "^1.0.2",
"filesize": "^2.0.4",
"image-scraper": "^0.1.2",
"image-scraper": "git://github.com/rv-kip/node-image-scraper.git#v0.1.2.1",
"minimist": "^1.1.0",
"request": "^2.45.0",
"sprintf-js": "^1.0.2"
Expand Down

0 comments on commit 32ac2cf

Please sign in to comment.