-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbot.js
More file actions
78 lines (62 loc) · 2.01 KB
/
bot.js
File metadata and controls
78 lines (62 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//
// Bot
// class for performing various twitter actions
//
var Twit = require('twit');
var Bot = module.exports = function(config) {
this.twit = new Twit(config);
};
//
// post a tweet
//
Bot.prototype.tweet = function (status, callback) {
if(typeof status !== 'string') {
return callback(new Error('tweet must be of type String'));
} else if(status.length > 140) {
return callback(new Error('tweet is too long: ' + status.length));
}
this.twit.post('statuses/update', { status: status }, callback);
};
//
// choose a random friend of one of your followers, and follow that user
//
Bot.prototype.mingle = function (callback) {
var self = this;
this.twit.get('followers/ids', function(err, reply) {
if(err) { return callback(err); }
var followers = reply.ids
, randFollower = randIndex(followers);
self.twit.get('friends/ids', { user_id: randFollower }, function(err, reply) {
if(err) { return callback(err); }
var friends = reply.ids
, target = randIndex(friends);
self.twit.post('friendships/create', { id: target }, callback);
})
})
};
//
// prune your followers list; unfollow a friend that hasn't followed you back
//
Bot.prototype.prune = function (callback) {
var self = this;
this.twit.get('followers/ids', function(err, reply) {
if(err) return callback(err);
var followers = reply.ids;
self.twit.get('friends/ids', function(err, reply) {
if(err) return callback(err);
var friends = reply.ids
, pruned = false;
while(!pruned) {
var target = randIndex(friends);
if(!~followers.indexOf(target)) {
pruned = true;
self.twit.post('friendships/destroy', { id: target }, callback);
}
}
});
});
};
function randIndex (arr) {
var index = Math.floor(arr.length*Math.random());
return arr[index];
};