Created
April 29, 2016 22:11
-
-
Save tdlm/d8ce1fa7131525d87665ff0de4667779 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var request = require('request'), | |
csv = require('fast-csv'), | |
validUrl = require('valid-url'), | |
baseRequest = request.defaults({ | |
followRedirect: false, | |
followAllRedirects: false | |
}); | |
if (process.argv.length < 2) { | |
console.log("Usage: status.js FILE [HOST] [STATUS]"); | |
process.exit(1); | |
} | |
var csvFile = process.argv[2], | |
hostName = process.argv[3] || 'cms.qa.vocativ.com', | |
expectedStatus = process.argv[4] || 200; | |
var read_csv_file = function(file, row_callback, done_callback) { | |
csv | |
.fromPath(file) | |
.on('data', row_callback) | |
.on('end', done_callback); | |
}; | |
var http_status_code = function(url, callback) { | |
if (validUrl.isUri(url) == false) { | |
return callback('Invalid URL', url, 0); | |
} | |
baseRequest.head(url, function(error, response, body) { | |
if (error) { | |
return callback(error.code, url, 0); | |
} | |
return callback(null, url, response.statusCode); | |
}); | |
} | |
read_csv_file(csvFile, function(row) { | |
var testUrl = 'http://' + hostName + row; | |
http_status_code(testUrl, function(error, url, statusCode) { | |
if (error == null) { | |
if (statusCode == expectedStatus) { | |
// Do nothing | |
} else { | |
console.log(url, statusCode); | |
} | |
} else { | |
console.error(error, url, statusCode); | |
} | |
}); | |
}, function() { | |
// Donesies | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's how I've been using this:
$ time node status.js final.csv www.qa.vocativ.com 200
And
$ time node status.js 301.csv www.qa.vocativ.com 301