Skip to content

Instantly share code, notes, and snippets.

@jetpks
Created May 23, 2012 07:17
Show Gist options
  • Save jetpks/2773663 to your computer and use it in GitHub Desktop.
Save jetpks/2773663 to your computer and use it in GitHub Desktop.
ed's node app
(function() {
"use strict";
var http = require('http')
, url = require('url')
, cached = {}
;
function server(req, res) {
var now = Date.now()
, twitter = url.parse(req.url).pathname.slice(1)
;
// deal with favicon etc
if(twitter == 'favicon.ico' || twitter == '') {
res.writeHead(501);
res.end();
return;
}
if(typeof cached[twitter] != 'undefined'
&& cached[twitter].grabbed > now - 600) {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(cached[twitter].json);
console.log(now + ' [H] ' + twitter);
return;
}
// get the json from twitter api
http.get({ host: 'api.twitter.com'
, port: 80
, path: '/1/statuses/user_timeline/' + twitter + '.json'
}, handleTwats);
function handleTwats(twSock) {
var twat = {grabbed: now, json: ''}
;
twSock.on('data', function(chunk) {
twat.json += chunk;
});
twSock.on('end', function() {
cached[twitter] = twat;
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(twat.json);
console.log(now + ' [m] ' + twitter);
});
}
}
http.createServer(server).listen(10001);
console.log('Twitter proxy running at on port 10001');
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment