Created
August 1, 2014 15:51
-
-
Save JosePedroDias/8821b6b54d6f99c35951 to your computer and use it in GitHub Desktop.
node uriChanged.js <URI_GOES_HERE>
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
var request = require('request'); | |
var timer, firstBody, deltaT, t = 0; | |
var fetchURI = function(uri, cb) { | |
request({uri:uri}, function (error, response, body) { | |
if (error) { | |
cb(error); | |
} | |
else if (response.statusCode !== 200) { | |
cb(t + ': status was ' + response.statusCode); | |
} | |
else { | |
cb(null, body); | |
} | |
}); | |
t += deltaT/1000; | |
}; | |
var testBody = function(err, body) { | |
if (err) { | |
//clearInterval(timer); | |
console.log(err); | |
} | |
if (!firstBody) { | |
firstBody = body; | |
} | |
else if (body === firstBody) { | |
console.log(t + ': hasn\'t changed'); | |
} | |
else { | |
clearInterval(timer); | |
console.log(t + ': changed!'); | |
} | |
}; | |
var scriptName = process.argv[1]; | |
var args = process.argv.slice(2); | |
var uri = args[0]; | |
deltaT = args[1]; | |
deltaT = (deltaT ? parseInt(deltaT, 10) * 1000 : 2000); | |
if (!uri) { | |
console.log('Syntax: node ' + scriptName + ' <uri goes here> [<every n seconds>]'); | |
} | |
else { | |
console.log('Checking every ' + (deltaT/1000) + ' seconds for changes in URI ' + uri); | |
fetchURI(uri, testBody); | |
timer = setInterval(function() { | |
fetchURI(uri, testBody); | |
}, deltaT); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment