UNPKG

2.07 kBJavaScriptView Raw
1//
2// Bot
3// class for performing various twitter actions
4//
5var Twit = require('../lib/twitter');
6
7var Bot = module.exports = function(config) {
8 this.twit = new Twit(config);
9};
10
11//
12// post a tweet
13//
14Bot.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 > 140) {
18 return callback(new Error('tweet is too long: ' + status.length));
19 }
20 this.twit.post('statuses/update', { status: status }, callback);
21};
22
23//
24// choose a random friend of one of your followers, and follow that user
25//
26Bot.prototype.mingle = function (callback) {
27 var self = this;
28
29 this.twit.get('followers/ids', function(err, reply) {
30 if(err) { return callback(err); }
31
32 var followers = reply.ids
33 , randFollower = randIndex(followers);
34
35 self.twit.get('friends/ids', { user_id: randFollower }, function(err, reply) {
36 if(err) { return callback(err); }
37
38 var friends = reply.ids
39 , target = randIndex(friends);
40
41 self.twit.post('friendships/create', { id: target }, callback);
42 })
43 })
44};
45
46//
47// prune your followers list; unfollow a friend that hasn't followed you back
48//
49Bot.prototype.prune = function (callback) {
50 var self = this;
51
52 this.twit.get('followers/ids', function(err, reply) {
53 if(err) return callback(err);
54
55 var followers = reply.ids;
56
57 self.twit.get('friends/ids', function(err, reply) {
58 if(err) return callback(err);
59
60 var friends = reply.ids
61 , pruned = false;
62
63 while(!pruned) {
64 var target = randIndex(friends);
65
66 if(!~followers.indexOf(target)) {
67 pruned = true;
68 self.twit.post('friendships/destroy', { id: target }, callback);
69 }
70 }
71 });
72 });
73};
74
75function randIndex (arr) {
76 var index = Math.floor(arr.length*Math.random());
77 return arr[index];
78};
\No newline at end of file