Created
January 10, 2012 23:22
-
-
Save jfrazee/1591854 to your computer and use it in GitHub Desktop.
Twitter Streaming API with Node.js Request module
This file contains hidden or 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 request = require('request'); | |
function filter(options, callback){ | |
var params = { | |
uri: "https://stream.twitter.com/1/statuses/filter.json", | |
} | |
if (typeof options['oauth'] !== 'undefined'){ | |
params.oauth = options.oauth; | |
delete options.oauth; | |
} | |
else if (typeof options['basic'] !== 'undefined') { | |
params.uri = params.uri.replace(/^https?:\/\//, | |
'https://' + options.basic.username + ':' + options.basic.password + '@' | |
); | |
delete options.basic; | |
} | |
params.form = options; | |
var req = request.post(params, function(err, response, body){ | |
if (err) console.error(err); | |
}); | |
// The callback for request.post is called when the response returns so you | |
// have to save the post object and add a callback for the data event. | |
req.on('data', function(buffer){ | |
callback(JSON.parse(buffer.toString())); | |
}); | |
}; | |
filter({ basic: { username: '', password: '' }, track: 'new hampshire' }, | |
function(tweet) { console.log(tweet); } | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
4 years on and still really helpful - thanks.
Here's an updated version for the new Twitter API
See the repository readme for documentation :)