This repository has been archived by the owner on Feb 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
services.js
402 lines (371 loc) · 13 KB
/
services.js
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
const Twitter = require('twitter');
const mongoURL = process.env.MONGODB_URI;
const Services = {};
Services.UpdateFollowersData = {};
const debugStatus = typeof v8debug === 'object'
|| /--debug|--inspect/.test(process.execArgv.join(' '));
/** Compares two arrays and returns the overlapping values
* @param {array} one The array to be compared from
* @param {array} two The array to be compared to
* @returns {array} Array including only common values in both one and two.
*/
const arrayDiff = (one, two) => one.filter(i => two.indexOf(i) < 0);
if (!debugStatus) {
(function AutoLiker() {
/** Twitter Accounts Tracker
* @description Keeps track of all accounts and
* whether the Tweet-Liker is on or off.
* @type {{accounts: {id: {accessToken: string, accessTokenSecret: string, enabled: boolean}}}}
*/
const tracker = {
accounts: {},
/** Reset Accounts List
* @desc Reset the accounts list in the tracker.
* @return {true} Returns true
*/
reset: () => {
this.accounts = {};
return true;
}
};
/** Initiate Liker Service
* @desc Initiates the autoliker service for a specified user.
* @param {{accessToken: string, accessTokenSecret: string, id: number}} credentials
* An object consisting of properties accessToken, accessTokenSecret, and the Twitter ID.
* @param {boolean} useStream Choose whether to use the stream or not (not supported)
* @return {null} Doesn't return anything.
*/
function initLiker(credentials, useStream) {
const client = new Twitter({
consumer_key: process.env.TWITTER_CONSUMER_KEY,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
access_token_key: credentials.accessToken,
access_token_secret: credentials.accessTokenSecret
});
let interval;
let lastTweet;
/** Check Twitter Timeline
* @return {null} Doesn't return anything.
*/
function checkTL() {
client.get('statuses/home_timeline', {
count: 15, since_id: lastTweet, include_entities: false
}, (error, tweets, response) => {
if (!error && response) {
const tweet = tweets[0];
lastTweet = tweet.id_str;
if (tweet.user.id_str !== credentials.id
&& tweet.favorited !== true) {
client.post('favorites/create', {
id: tweet.id_str, include_entities: false
}, (err, likedTweets, rawdata) => {
if (rawdata && likedTweets);
});
}
} else {
switch (error.code) {
case 89: // Invalid/expired token, delete from DB
clearInterval(interval);
break;
case 32: // Could not authenticate, delete from DB
clearInterval(interval);
break;
case 326: // Locked account, delete from DB
clearInterval(interval);
break;
case 88: // Rate limit exceeded
break;
default: // other errors
console.error(error);
break;
}
}
});
}
if (useStream === false) {
interval = setInterval(() => { checkTL(); }, 900000); // every 9 minutes
if (tracker.accounts[credentials.id] && tracker.accounts[credentials.id].enabled === true) {
if (tracker.accounts[credentials.id].instances) {
tracker.accounts[credentials.id].instances[0] += 1;
clearInterval(interval);
} else if (!tracker.accounts[credentials.id].instances) {
tracker.accounts[credentials.id].instances = [0];
checkTL();
}
} else {
clearInterval(interval);
}
} else if (useStream === true) {
if (tracker.accounts[credentials.id] && tracker.accounts[credentials.id].enabled === true) {
if (tracker.accounts[credentials.id].instances) {
tracker.accounts[credentials.id].instances[0] += 1;
} else if (!tracker.accounts[credentials.id].instances) {
tracker.accounts[credentials.id].instances = [0];
// OK to go
client.stream('statuses/filter', {
});
}
}
}
}
/** MongoDB filtering query
* @description This filter selects only
* users with the autolike service turned on.
*/
const query = { 'services.autolike': true };
MongoClient.connect(mongoURL, (err, db) => {
/** @desc Ensures no errors occurred */
assert.equal(null, err);
/** Opens users collection in database */
db.collection('users', (err2, collection) => {
setInterval(() => {
collection.find(query, (err3, docs) => {
tracker.reset();
docs.forEach((doc) => {
tracker.accounts[doc.twitter.id] = {
accessToken: doc.twitter.accessToken,
accessTokenSecret: doc.twitter.accessTokenSecret,
enabled: true
};
initLiker({
accessToken: doc.twitter.accessToken,
accessTokenSecret: doc.twitter.accessTokenSecret,
id: doc.twitter.id
}, false);
});
});
}, 15e3);
});
});
}());
} else {
console.log('Services failed to start due to debug status.');
}
Services.UpdateFollowersData.filterUserData = (user, callback1) => {
const $finalUser = {};
const allowedKeys = [
'id_str',
'name',
'screen_name',
'protected',
'followers_count',
'friends_count',
'created_at',
'favourites_count',
'lang',
'profile_image_url_https',
'following',
'follow_request_sent',
'muting',
'blocking',
'blocked_by'
];
Object.keys(user).forEach((property) => {
if (allowedKeys.indexOf(property) > -1) {
$finalUser[property] = user[property];
}
if (Object.keys($finalUser).length >= allowedKeys.length) {
callback1($finalUser);
}
});
};
Services.UpdateFollowersData.Struct = class Struct {
constructor() {
this.struct = {
user_id: 0,
counts: {
followers: 0,
new_followers: 0,
unfollowers: 0
},
unfollowers: [],
followers: []
};
}
};
Services.UpdateFollowersData.connectDB = (callback) => {
MongoClient.connect(mongoURL, (err, dbObject) => {
if (err) throw err;
else callback(dbObject);
});
};
Services.UpdateFollowersData.twitterCollection = (dbObject, callback) => {
dbObject.createCollection('twitter-stats', (err, collectionObject) => {
if (err) throw err;
else callback(collectionObject);
});
};
Services.UpdateFollowersData.findInDatabase = (collectionObject, findOptions, callback) => {
collectionObject.findOne(findOptions, (err, doc) => {
if (err) throw err;
else callback(doc);
});
};
Services.UpdateFollowersData.DBHandler = (findOptions, callback) => {
const dbHandlers = Services.UpdateFollowersData;
dbHandlers.connectDB((dbObject) => {
dbHandlers.twitterCollection(dbObject, (collectionObject) => {
dbHandlers.findInDatabase(collectionObject, findOptions, (doc) => {
callback(doc, collectionObject, dbObject);
});
});
});
};
/** Gets followers list from Twitter
* @param {object[]} client A new instance of a Twitter object.
* @param {number} userID The user ID of a Twitter user.
* @param {function} callback The callback function
* @returns {void} Does not return anything
*/
Services.UpdateFollowersData.getFollowers = (client, struct, userID, callback) => {
Services.UpdateFollowersData.getFollowerCount(client, struct, userID, (newStruct) => {
Services.UpdateFollowersData.getFollowerList(client, newStruct, userID, (followerList,
struct2) => {
callback(followerList, struct2);
});
});
};
Services.UpdateFollowersData.getFollowerCount = (client, struct, userID, callback) => {
client.get('users/show', { user_id: userID }, (err, data) => {
struct.counts.followers = data.followers_count;
callback(struct);
});
};
Services.UpdateFollowersData.getFollowerList = (client, newStruct, userID, callback) => {
let cursor = -1;
const repeatList = () => {
const options = {
user_id: userID,
cursor,
count: 200,
skip_status: true,
include_user_entities: false
};
client.get('followers/list', options, (err, data) => {
if (err) throw err;
const followerList = newStruct.followers;
newStruct.followers = followerList.concat(data.users);
cursor = data.next_cursor_str;
// Checks to see if there's no more pages of followers left
if (parseInt(cursor, 10) === 0) callback(newStruct.followers, newStruct);
else repeatList();
});
};
repeatList();
};
Services.UpdateFollowersData.metaFollowerSort = (doc, struct, callback) => {
const NewFollowers = struct.followers.map((item) => {
if (item.id_str) return item.id_str;
return null;
});
const OldFollowers = doc.followers.map((item) => {
if (item.id_str) return item.id_str;
return null;
});
const unfollowerIDs = arrayDiff(OldFollowers, NewFollowers);
const newFollowerIDs = arrayDiff(NewFollowers, OldFollowers);
Services.UpdateFollowersData.createMeta(unfollowerIDs, newFollowerIDs, doc, struct,
(followerList, unfollowerList, newStruct) => {
callback(followerList, unfollowerList, newFollowerIDs.length, unfollowerIDs.length, newStruct);
});
};
Services.UpdateFollowersData.createMeta = (unfollowerIDs, newFollowerIDs, doc, struct, callback) => {
const followerList = struct.followers.map((item) => {
let $item;
Services.UpdateFollowersData.filterUserData(item, (user) => {
const $this = user;
if (!$this.metadata) $this.metadata = {};
if (newFollowerIDs.indexOf(item.id_str) >= 0) {
$this.metadata.new_follower = true;
$this.metadata.followed_at = (new Date()).toString();
$this.metadata.unfollower = false;
$item = $this;
return $this;
}
$this.metadata.unfollower = false;
$this.metadata.new_follower = false;
$item = $this;
return $this;
});
return $item;
});
const unfollowerList = doc.followers.filter((item) => {
let $item = null;
if (unfollowerIDs.indexOf(item.id_str) >= 0) {
Services.UpdateFollowersData.filterUserData(item, (user) => {
const $this = user;
if (!$this.metadata) $this.metadata = {};
$this.metadata.unfollower = true;
$this.metadata.new_follower = false;
$this.metadata.unfollowed_at = (new Date()).toString();
$item = $this;
return $this;
});
}
return $item;
});
callback(followerList, unfollowerList, struct);
};
Services.UpdateFollowersData.followSort = (doc, struct, collectionObject, callback) => {
const userID = struct.user_id;
Services.UpdateFollowersData.metaFollowerSort(doc, struct, (followerList, unfollowerList,
newFollowerIDs, unfollowerIDs, newStruct) => {
newStruct.counts.new_followers = newFollowerIDs;
newStruct.counts.unfollowers = unfollowerIDs;
newStruct.unfollowers = unfollowerList;
newStruct.followers = followerList;
collectionObject.findOneAndUpdate({ user_id: userID }, { $set: newStruct }, (err, response) => {
if (err) throw err;
callback(newStruct, response);
});
});
};
Services.UpdateFollowersData.followSortNew = (collectionObject, struct, callback) => {
const metaList = struct.followers.map((item) => {
let $item;
Services.UpdateFollowersData.filterUserData(item, (user) => {
const $this = user;
if (!$this.metadata) $this.metadata = {};
$this.metadata.unfollower = false;
$this.metadata.new_follower = true;
$this.metadata.followed_at = (new Date()).toString();
$item = $this;
return $this;
});
return $item;
});
struct.followers = metaList;
struct.counts.unfollowers = 0;
struct.counts.new_followers = 0;
collectionObject.insertOne(struct, (err, response) => {
if (err) throw err;
else callback(struct, response);
});
};
Services.UpdateFollowers = (userID, token, secret, callback) => {
const client = new Twitter({
consumer_key: process.env.TWITTER_CONSUMER_KEY,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
access_token_key: token,
access_token_secret: secret
});
const struct = (new Services.UpdateFollowersData.Struct()).struct;
struct.user_id = userID;
// Use connect method to connect to the server
Services.UpdateFollowersData.DBHandler({ user_id: userID }, (doc, collectionObject) => {
Services.UpdateFollowersData.getFollowers(client, struct, userID, (followerList, newStruct) => {
if (doc) {
Services.UpdateFollowersData.followSort(doc, newStruct, collectionObject, () => {
callback();
});
} else {
Services.UpdateFollowersData.followSortNew(collectionObject, newStruct, () => {
callback();
});
}
});
});
};
module.exports = Services;