-
-
Save robi42/360185 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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