twitst4tz

twitter statistics web application
Log | Files | Refs | README | LICENSE

bot.js (3317B)


      1 //
      2 //  Bot
      3 //  class for performing various twitter actions
      4 //
      5 var Twit = require('../lib/twitter');
      6 
      7 var Bot = module.exports = function(config) { 
      8   this.twit = new Twit(config);
      9 };
     10 
     11 //
     12 //  post a tweet
     13 //
     14 Bot.prototype.tweet = function (status, callback) {
     15   if(typeof status !== 'string') {
     16     return callback(new Error('tweet must be of type String'));
     17   } else if(status.length > 280) {
     18     return callback(new Error('tweet is too long: ' + status.length));
     19   }
     20   this.twit.post('statuses/update', { status: status }, callback);
     21 };
     22 
     23 // choose a random tweet and follow that user
     24 Bot.prototype.searchFollow = function (params, callback) {
     25   var self = this;
     26  
     27   self.twit.get('search/tweets', params, function (err, reply) {
     28     if(err) return callback(err);
     29  
     30     var tweets = reply.statuses;
     31 	var rTweet = randIndex(tweets)
     32 	if(typeof rTweet != 'undefined')
     33 	{
     34 		var target = rTweet.user.id_str;
     35  
     36 		self.twit.post('friendships/create', { id: target }, callback);
     37 	}
     38   });
     39 };
     40 
     41 //
     42 // retweet
     43 //
     44 Bot.prototype.retweet = function (params, callback) {
     45   var self = this;
     46  
     47   self.twit.get('search/tweets', params, function (err, reply) {
     48     if(err) return callback(err);
     49  
     50     var tweets = reply.statuses;
     51     var randomTweet = randIndex(tweets);
     52 	if(typeof randomTweet != 'undefined')
     53 		self.twit.post('statuses/retweet/:id', { id: randomTweet.id_str }, callback);
     54   });
     55 };
     56  
     57 //
     58 // favorite a tweet
     59 //
     60 Bot.prototype.favorite = function (params, callback) {
     61   var self = this;
     62  
     63   self.twit.get('search/tweets', params, function (err, reply) {
     64     if(err) return callback(err);
     65  
     66     var tweets = reply.statuses;
     67     var randomTweet = randIndex(tweets);
     68 	if(typeof randomTweet != 'undefined')
     69 		self.twit.post('favorites/create', { id: randomTweet.id_str }, callback);
     70   });
     71 };
     72 
     73 //
     74 
     75 //  choose a random friend of one of your followers, and follow that user
     76 //
     77 Bot.prototype.mingle = function (callback) {
     78   var self = this;
     79   
     80   this.twit.get('followers/ids', function(err, reply) {
     81       if(err) { return callback(err); }
     82       
     83       var followers = reply.ids
     84         , randFollower  = randIndex(followers);
     85         
     86       self.twit.get('friends/ids', { user_id: randFollower }, function(err, reply) {
     87           if(err) { return callback(err); }
     88           
     89           var friends = reply.ids
     90             , target  = randIndex(friends);
     91             
     92           self.twit.post('friendships/create', { id: target }, callback); 
     93         })
     94     })
     95 };
     96 
     97 //
     98 //  prune your followers list; unfollow a friend that hasn't followed you back
     99 //
    100 Bot.prototype.prune = function (callback) {
    101   var self = this;
    102   
    103   this.twit.get('followers/ids', function(err, reply) {
    104       if(err) return callback(err);
    105       
    106       var followers = reply.ids;
    107       
    108       self.twit.get('friends/ids', function(err, reply) {
    109           if(err) return callback(err);
    110           
    111           var friends = reply.ids
    112             , pruned = false;
    113           
    114           while(!pruned) {
    115             var target = randIndex(friends);
    116             
    117             if(!~followers.indexOf(target)) {
    118               pruned = true;
    119               self.twit.post('friendships/destroy', { id: target }, callback);   
    120             }
    121           }
    122       });
    123   });
    124 };
    125 
    126 function randIndex (arr) {
    127   var index = Math.floor(arr.length*Math.random());
    128   return arr[index];
    129 };