Skip to content

Instantly share code, notes, and snippets.

@callumacrae
Created May 30, 2011 11:29
Show Gist options
  • Select an option

  • Save callumacrae/998758 to your computer and use it in GitHub Desktop.

Select an option

Save callumacrae/998758 to your computer and use it in GitHub Desktop.
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