Skip to content

Instantly share code, notes, and snippets.

@robtuley
Created January 4, 2015 15:06
Show Gist options
  • Select an option

  • Save robtuley/edf31e657f5b035939b9 to your computer and use it in GitHub Desktop.

Select an option

Save robtuley/edf31e657f5b035939b9 to your computer and use it in GitHub Desktop.
#!/usr/bin/nodejs
var http = require('http');
var latestCaptchaJson = JSON.stringify({
'q':'Is ice hot or cold?',
'a':['75e52a0ecfafeda17a34fc60111c1f0b'] });
var apiUrl = 'http://api.textcaptcha.com/mirror.json';
var refreshCaptchaEveryXSeconds = 5;
var app = http.createServer(function(req,res){
res.setHeader('Content-Type','application/json');
res.end(latestCaptchaJson);
});
app.listen(3000);
continuallyRefreshCaptcha();
function continuallyRefreshCaptcha(){
requestNewCaptcha(function(err,captcha){
setTimeout(continuallyRefreshCaptcha,
refreshCaptchaEveryXSeconds*1000);
if(err) return console.log(err);
latestCaptchaJson = captcha;
console.log(latestCaptchaJson);
});
}
function requestNewCaptcha(next){
http.get(apiUrl,function(res){
var body = '';
if(res.statusCode!=200){
return next(new Error('status code '+res.statusCode),null);
}
res.on('data',function(chunk){ body+=chunk; });
res.on('end',function(){
next(null,body);
});
}).on('error',next);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment