Created
May 30, 2011 11:29
-
-
Save callumacrae/998758 to your computer and use it in GitHub Desktop.
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 http = require('http'), | |
| events = require('events'); | |
| function Twitter(options, track) | |
| { | |
| var user, pass, authstring, | |
| __self, req, emitter; | |
| __self = this; | |
| emitter = events.EventEmitter.call(this); | |
| authstring = (new Buffer(options.user + ':' + options.pass, 'ascii')).toString('base64'); | |
| options = { | |
| host: 'stream.twitter.com', | |
| port: 80, | |
| path: '/1/statuses/filter.json', | |
| method: 'POST', | |
| headers: { | |
| 'Authorization': 'Basic ' + authstring, | |
| 'Host': 'stream.twitter.com', | |
| 'Content-Type': 'application/x-www-form-urlencoded' | |
| } | |
| }; | |
| req = http.request(options, function(res) | |
| { | |
| if (res.statusCode === 200) | |
| { | |
| console.log('Streaming API connected'); | |
| res.setEncoding('utf8'); | |
| res.on('data', function(chunk) | |
| { | |
| chunk = /"text":"((?:\\.|[^"])+)",/gm.exec(chunk); | |
| if (chunk) | |
| { | |
| __self.emit('tweet', chunk[1]); | |
| } | |
| }); | |
| } | |
| else | |
| { | |
| console.log('Streaming API error: ' + res.statusCode); | |
| } | |
| }); | |
| // Start request | |
| req.end(track, 'ascii'); | |
| } | |
| module.exports = Twitter; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment