Created
January 1, 2011 08:57
-
-
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
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
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); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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)