Skip to content

Instantly share code, notes, and snippets.

@creationix
Created January 1, 2011 08:57
Show Gist options
  • Save creationix/761639 to your computer and use it in GitHub Desktop.
Save creationix/761639 to your computer and use it in GitHub Desktop.
A sample client for creationix/jsonparse that consumes the twitter feed and filters out messages and names
var Parser = require('./jsonparse');
var Http = require('http');
var p = new Parser();
// IMPORTANT, put your username and password in here
var username = "yourTwitterUsername", password = "yourPassword";
var client = Http.createClient(80, "stream.twitter.com");
var request = client.request("GET", "/1/statuses/sample.json", {
"Host": "stream.twitter.com",
"Authorization": (new Buffer(username + ":" + password)).toString("base64")
});
request.on('response', function (response) {
console.log(response.statusCode);
console.dir(response.headers);
response.on('data', function (chunk) {
p.write(chunk);
});
response.on('end', function () {
console.log("END");
});
});
request.end();
var text, name;
p.onValue = function (value) {
if (this.stack.length === 1 && this.key === 'text') { text = value; }
if (this.stack.length === 2 && this.key === 'name' && this.stack[1].key === 'user') { name = value; }
if (this.stack.length === 0) {
console.log("%s - %s", text, name);
}
};
@creationix
Copy link
Author

Note, this is the node v0.2.x http client API, but the streaming JSON library can be used with node v0.4.x just the same (over https even)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment