Create a gist now

Instantly share code, notes, and snippets.

var sys = require("sys");
var http = require("http");
// http://search.twitter.com/search.json?q=
// http://search.twitter.com/search.json?q=%s&since_id=%s
var twitter_search = http.createClient(80, "search.twitter.com");
var since_id=0;
var timeo=2000;
var twits = new process.EventEmitter();
function searcht() {
var request="";
if (since_id == 0 ){
request = twitter_search.request("GET", "/search.json?q=fail", {"host": "search.twitter.com"});
} else {
request = twitter_search.request("GET", "/search.json?q=fail&since_id="+since_id, {"host": "search.twitter.com"});
}
request.finish(function (response) {
response.setBodyEncoding("utf8");
var body="";
response.addListener("body", function (chunk) {
body=body+chunk;
});
response.addListener("complete", function() {
res = JSON.parse(body);
sys.puts("timeo: "+timeo);
sys.puts("Since id: "+since_id);
sys.puts("Last id: "+res["max_id"]);
since_id=res["max_id"];
twits.emit('tws', body);
});
});
setTimeout(searcht, timeo);
};
http.createServer(function(req, res) {
if (req.url == '/twitter') {
req.connection.setTimeout(0);
res.sendHeader(200, {'Content-type':'text/plain'});
l = twits.addListener('tws', function(m) {
//res.sendBody(twits.join('\n');
res.sendBody("Last id: "+since_id+' | m: '+m+'\n');
twits.removeListener('tws', l);
});
var to = setTimeout(function() {
twits.removeListener('tws', l);
res.sendBody(JSON.stringify([]));
}, 3000);
} else {
res.sendHeader(404, {'Content-type':'text/plain'});
res.sendBody('not found');
res.finish();
}
}).listen(8888);
setTimeout(searcht, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment