Created
April 8, 2010 13:38
-
-
Save hns/360076 to your computer and use it in GitHub Desktop.
A RingoJS script that tracks one or more topics via Twitter's streaming API
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 | |
// | |
// NOTE: I started working on a package for asynchronous JSON parsing that contains | |
// a better working version of this script: http://github.com/hns/jetson | |
// | |
// Run with: | |
// ringo twitterstream.js KEYWORD[,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