Skip to content

Instantly share code, notes, and snippets.

@robi42
Forked from hns/twitterstream.js
Created April 8, 2010 15:29
Show Gist options
  • Save robi42/360185 to your computer and use it in GitHub Desktop.
Save robi42/360185 to your computer and use it in GitHub Desktop.
// Test for Ringo's new async HTTP client with streaming Twitter API
// Run with:
// ringo twitterstream.js KEYWORD
// needs valid user credentials
var username = "user";
var password = "password";
include("ringo/term");
var {Client} = require("ringo/httpclient");
var client = new Client(100000000); // need a way to disable timeout
var buffer = "";
client.request({
url: "http://stream.twitter.com/1/statuses/filter.json",
username: username,
password: password,
data: {track: system.args[1]},
part: function(content, status, contentType, exchange) {
buffer += content;
try {
var tweet = JSON.parse(buffer);
writeln(BOLD, RED, tweet.user.name + ":",
GREEN, tweet.text);
buffer = "";
} catch (error) {
// pull emergency brake - we need a streaming JSON API!
if (buffer.length > 500) {
writeln(BOLD, YELLOW, "Discarding", buffer.length, "bytes");
buffer = "";
}
// else wait for more bytes
}
return;
},
error: function(error) {
writeln(BOLD, RED, "ERROR:", error);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment