-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfollowbot.js
More file actions
190 lines (143 loc) · 4.66 KB
/
followbot.js
File metadata and controls
190 lines (143 loc) · 4.66 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
var Promise = require( 'bluebird' )
, Twit = require( 'twit' )
, Debug = require( './debug' )
, debug = new Debug( 'FollowBot' );
var API_LIMIT = 15;
var FollowBot = function FollowBot( config ) {
this.account = new Twit(config.twitter);
this.allowProtected = config.allowProtected || false;
this.strict = config.strict || false;
};
FollowBot.prototype = {
followRandom: function followRandom( amount ) {
amount = amount || 1;
if( amount > 50 ) {
// Because of API limitations, it is only save to work with up to 50 random followers.
return Promise.reject( new Error( 'Because of api limitations, you can follow up to 50 random followers.' ) );
}
var bot = this;
return bot.getFollowers().then(function(ids) {
return bot.getRandomAccounts(ids, amount);
}).then(function(randomAccounts) {
return bot.follow(randomAccounts);
}).catch(Promise.reject);
},
getFollowers: function getFollowers() {
debug.log( 'Get all followers' );
var defer = Promise.pending()
, fullStack = []
, bot = this
, requests = 0
, reachedLimit
, next_cursor;
function getBatch( cursor, cb ) {
debug.log( 'Get batch' );
++requests;
bot.account.get( 'followers/ids', {
count: 5000
, stringify_ids: true
, cursor: cursor
}, function( err, resp ) {
if( err ) {
defer.reject( err );
return;
}
fullStack = fullStack.concat( resp.ids || [] );
next_cursor = String( resp.next_cursor_str || resp.next_cursor || -1 );
reachedLimit = ( bot.strict === true && requests >= API_LIMIT - 1 );
if( next_cursor && ( next_cursor !== '-1' && next_cursor !== '0' ) && !reachedLimit ) {
return getBatch( resp.next_cursor_str || resp.next_cursor, cb );
}
cb();
});
}
getBatch( -1, function done() {
debug.log( 'Received full stack' );
defer.resolve(fullStack);
});
return defer.promise;
},
convertIDs: function convertIDs( ids, ignoreOverflow ) {
debug.log( 'Convert ' + ids.length + ' ids to user objects' );
if( ids.length > 100 && !ignoreOverflow ) {
return Promise.reject( new Error( 'Can only convert up to 100 ids at a time' ) );
}
var defer = Promise.pending()
, bot = this;
bot.account.get( 'users/lookup', {
user_id: ids.slice( 0, 100 ).join( ',' )
}, function( err, resp ) {
if( err ) {
defer.reject( err );
return;
}
defer.resolve( resp || [] );
});
return defer.promise;
},
getRandomAccounts: function getRandomAccounts( ids, max ) {
debug.log( 'Get ' + max + ' random account' );
var defer = Promise.pending()
, bot = this
, randomIds = [];
// Get random IDs
if( ids.length <= max ) {
debug.warn( 'Account has not enough followers' );
randomIds = ids;
}
else {
// Shuffle the ids
randomIds = privates.shuffleArray(ids).slice( 0, ( max * 2 ) );
}
// Convert them into user objects
bot.convertIDs(randomIds).then(function(accounts) {
// Filter invalid picks
var randomAccounts = [];
for( var i = 0; i < accounts.length && randomAccounts.length != max; ++i ) {
// @TODO - Protected
if( accounts[i].following !== true && ( !accounts[i].protected || bot.allowProtected ) ) {
randomAccounts.push( accounts[i] );
}
}
defer.resolve( randomAccounts );
}).catch(defer.reject);
return defer.promise;
},
follow: function follow( accounts ) {
debug.log( 'Follow ' + accounts.length + ' accounts' );
var bot = this;
var followPromises = accounts.map(function followSingle(account) {
var followDefer = Promise.pending();
bot.account.post( 'friendships/create', {
user_id: account.id
, follow: true
}, function( err, resp ) {
if( err ) {
followDefer.reject( err );
return;
}
followDefer.resolve( resp );
});
return followDefer.promise;
});
return Promise.all(followPromises);
}
};
var privates = {
shuffleArray: function shuffleArray( array ) {
var currentIndex = array.length
, shuffled = array
, temporaryValue
, randomIndex;
while( 0 !== currentIndex ) {
randomIndex = Math.floor( Math.random() * currentIndex );
currentIndex -= 1;
temporaryValue = shuffled[currentIndex];
shuffled[currentIndex] = shuffled[randomIndex];
shuffled[randomIndex] = temporaryValue;
}
return shuffled;
}
};
FollowBot.prototype.__privates = privates;
module.exports = FollowBot;