twitst4tz

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

rtd2.js (2030B)


      1 //
      2 //  RTD2 - Twitter bot that tweets about the most popular github.com news
      3 //  Also makes new friends and prunes its followings.
      4 //
      5 var Bot = require('./bot')
      6   , config1 = require('../config1');
      7 
      8 var bot = new Bot(config1);
      9 
     10 console.log('RTD2: Running.');
     11 
     12 //get date string for today's date (e.g. '2011-01-01')
     13 function datestring () {
     14   var d = new Date(Date.now() - 5*60*60*1000);  //est timezone
     15   return d.getUTCFullYear()   + '-'
     16      +  (d.getUTCMonth() + 1) + '-'
     17      +   d.getDate();
     18 };
     19 
     20 setInterval(function() {
     21   bot.twit.get('followers/ids', function(err, reply) {
     22     if(err) return handleError(err)
     23     console.log('\n# followers:' + reply.ids.length.toString());
     24   });
     25   var rand = Math.random();
     26 
     27   if(rand <= 0.10) {      //  tweet popular github tweet
     28     var params = {
     29         q: 'github.com/'
     30       , since: datestring()
     31       , result_type: 'mixed'
     32     };
     33     bot.twit.get('search/tweets', params, function (err, reply) {
     34       if(err) return handleError(err);
     35 
     36       var max = 0, popular;
     37 
     38       var tweets = reply.statuses
     39         , i = tweets.length;
     40 
     41       while(i--) {
     42         var tweet = tweets[i]
     43           , popularity = tweet.retweet_count;
     44 
     45         if(popularity > max) {
     46           max = popularity;
     47           popular = tweet.text;
     48         }
     49       }
     50 
     51       bot.tweet(popular, function (err, reply) {
     52         if(err) return handleError(err);
     53 
     54         console.log('\nTweet: ' + (reply ? reply.text : reply));
     55       })
     56     });
     57   } else if(rand <= 0.55) { //  make a friend
     58     bot.mingle(function(err, reply) {
     59       if(err) return handleError(err);
     60 
     61       var name = reply.screen_name;
     62       console.log('\nMingle: followed @' + name);
     63     });
     64   } else {                  //  prune a friend
     65     bot.prune(function(err, reply) {
     66       if(err) return handleError(err);
     67 
     68       var name = reply.screen_name
     69       console.log('\nPrune: unfollowed @'+ name);
     70     });
     71   }
     72 }, 40000);
     73 
     74 function handleError(err) {
     75   console.error('response status:', err.statusCode);
     76   console.error('data:', err.data);
     77 }