Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

evil, disgusting, horrible first run at a fake user stream call using…

… rest updates behind the scenes
  • Loading branch information...
commit 26f88261710ab665d52873a5d568227966c18d7b 1 parent 18c19ee
@jdub jdub authored
Showing with 89 additions and 1 deletion.
  1. +89 −1 lib/twitter.js
View
90 lib/twitter.js
@@ -1,7 +1,8 @@
var http = require('http'),
querystring = require('querystring'),
oauth = require('oauth'),
- streamparser = require('./parser');
+ streamparser = require('./parser'),
+ events = require('events');
function merge(defaults, options) {
defaults = defaults || {};
@@ -211,6 +212,93 @@ Twitter.prototype.stream = function(method, params, callback) {
/*
+ * FAKE STREAM (not API stable!)
+ */
+Twitter.prototype.fakeStream = function(method, params, callback) {
+ if ( method !== 'user' )
+ throw "Invalid fake stream method, must be 'user'.";
+
+ if ( typeof params === 'function' ) {
+ callback = params;
+ params = null;
+ }
+
+ var stream = new FakeStream(this);
+
+ if ( typeof callback === 'function' ) callback(stream);
+ return this;
+}
+
+// ... and the FakeStream object
+function FakeStream(twit) {
+ events.EventEmitter.call(this);
+
+ var self = this;
+
+ self.twit = twit;
+ self.interval = null;
+ self.hparams = {
+ count: 5, // for the first call only
+ include_entities: 1
+ };
+ self.dparams = {
+ count: 1, // for the first call only
+ include_entities: 1
+ };
+
+ // Update straight away
+ self.updateHome();
+ self.updateDirects();
+
+ // Thence every 60s
+ self.interval = setInterval(function() {
+ self.updateHome();
+ self.updateDirects();
+ }, 30000);
+}
+require('util').inherits(FakeStream, events.EventEmitter);
+
+FakeStream.prototype.updateHome = function() {
+ var self = this;
+ self.twit.getHomeTimeline(self.hparams, function(data) {
+ if ( data.length > 0 && data[0].id_str ) {
+ self.hparams['since_id'] = data[0].id_str;
+ }
+ // Only limit number of tweets on first call
+ if ( self.hparams.count ) delete self.hparams.count;
+ // Emit tweets in reverse order
+ for ( var i = data.length - 1; i >= 0; i-- ) {
+ self.emit('data', data[i]);
+ }
+ });
+}
+
+FakeStream.prototype.updateDirects = function() {
+ var self = this;
+ self.twit.getDirectMessages(self.dparams, function(data) {
+ // Grab the latest id to fetch fewer in future
+ if ( data.length > 0 && data[0].id_str ) {
+ self.dparams['since_id'] = data[0].id_str;
+ }
+ // We only care about getting since_id on the first call
+ if ( self.dparams.count ) {
+ delete self.dparams.count;
+ return;
+ }
+ // Emit tweets in reverse order
+ for ( var i = data.length - 1; i >= 0; i-- ) {
+ self.emit('data', {direct_message:data[i]});
+ }
+ });
+}
+
+FakeStream.prototype.destroy = function() {
+ var self = this;
+ clearInterval(self.interval);
+};
+
+
+/*
* CONVENIENCE FUNCTIONS (not API stable!)
*/
Please sign in to comment.
Something went wrong with that request. Please try again.