Skip to content

Instantly share code, notes, and snippets.

@adv0r
Forked from JohnGreenaway/Tweets.js
Created December 13, 2011 15:06
Show Gist options
  • Save adv0r/1472449 to your computer and use it in GitHub Desktop.
Save adv0r/1472449 to your computer and use it in GitHub Desktop.
var sys=require('sys'), http = require('http');
var query = "Winter Olympics";
var twitter = http.createClient(80, "search.twitter.com");
var since = 0;
function scanTweet() {
// the httpclient "twitter" we created above - here we build a
// query to Twitter, requesting a JSON response. It's not
// fired off until we call the "finish" method on it.
var tweetRequest = twitter.request("/search.json?q="
+ query + "&since_id="+since,
{"host": "search.twitter.com",
"User-Agent": "NodeJS HTTP Client"});
// Note we pass in a callback to handle the response when it
// comes back - after the request is fired processing continues,
// it doesn't wait for the response. Very similar to how you call
// Ajax from a browser.
tweetRequest.addListener("response", function(response) {
var responseBody = "";
response.setBodyEncoding("utf8");
// the "body" event is fired as chunks come back in the
// response so streaming can be handled.
// Here we just collect it all up.
response.addListener("body",
function(chunk) {
responseBody += chunk;
});
// the "complete" event is fired once all body returned
response.addListener("complete",
function() {
tweets = JSON.parse(responseBody);
var results = tweets["results"];
var length = results.length;
for (var i = (length-1); i >= 0; i--) {
if (results[i].id > since) {
since = results[i].id;
}
sys.puts("@"+results[i].from_user
+": "+results[i].text);
}
});
});
// fire off the request above
tweetRequest.finish();
// call this every 10 seconds, then sleep
setTimeout(gettweets, 10000);
}
// kick it all off
scanTweet();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment