Skip to content

Instantly share code, notes, and snippets.

@tdlm
Created April 29, 2016 22:11
Show Gist options
  • Save tdlm/d8ce1fa7131525d87665ff0de4667779 to your computer and use it in GitHub Desktop.
Save tdlm/d8ce1fa7131525d87665ff0de4667779 to your computer and use it in GitHub Desktop.
'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
});
@tdlm
Copy link
Author

tdlm commented Apr 29, 2016

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment