-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
717 lines (566 loc) · 17.5 KB
/
server.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
const fox = require("fox-js"); // Some custom functions etc
const csv = require("csv-parser");
const fs = require("fs");
const low = require("lowdb"); // Database
const FileSync = require("lowdb/adapters/FileSync");
const fetch = require("node-fetch");
const dayjs = require("dayjs");
const today = dayjs().format("MM/DD/YYYY");
const daysPrevious = dayjs()
.subtract(2, "days")
.format("MM/DD/YYYY");
// Helper to add delay to async functions
const delay = duration => new Promise(resolve => setTimeout(resolve, duration));
const path = require("path"),
express = require("express"),
app = express(),
Twit = require("twit"),
config = {
twitter: {
consumer_key: process.env.CONSUMER_KEY,
consumer_secret: process.env.CONSUMER_SECRET,
access_token: process.env.ACCESS_TOKEN,
access_token_secret: process.env.ACCESS_TOKEN_SECRET,
timeout_ms: 60 * 1000
}
},
T = new Twit(config.twitter);
const adapter = new FileSync(".data/db.json");
const db = low(adapter);
// Set some defaults
db.defaults({ friends: [] }).write();
// Serve the public directory directly
app.use(express.static("public"));
let blockList = [];
function updateBlockList(callback) {
// To load up some data from disk
blockList = [];
fs.createReadStream("block-list.csv")
.pipe(csv({ headers: false }))
.on("data", row => {
blockList.push(row[0]);
})
.on("end", () => {
console.log("CSV file successfully processed");
if (callback) callback();
});
}
// updateBlockList();
let processingBlockList = false;
/*
*
* THE MAIN LOOP TRIGGERED EVERY 25 MINS
*
*/
app.get("/" + process.env.BOT_ENDPOINT, async function(request, response) {
console.log("The bot has been triggered!!!!");
// Get latest mentions and add to list
T.get("statuses/mentions_timeline", (error, data, response) => {
let users = [];
for (const record of data) {
users.push(record.user.id);
}
console.log("Found: " + users.length);
addUsersToList(users, "1280669347413848064");
});
// Get list of blocked users and unblock
// T.get("blocks/ids", async (err, data, res) => {
// console.log(data.ids.length);
// for (const id of data.ids) {
// console.log(id);
// await delay(500);
// T.post(
// "blocks/destroy",
// { user_id: id.toString() },
// (err, data, response) => {
// console.log(data);
// }
// );
// }
// });
// Unblock (more reliable than ids)
// T.get("blocks/list", async (err, data, res) => {
// if (err) {
// console.log(err);
// return;
// }
// console.log(data.users.length);
// for (const user of data.users) {
// await delay(500);
// T.post(
// "blocks/destroy",
// { screen_name: user.screen_name },
// (err, data, response) => {
// console.log(data.screen_name);
// }
// );
// }
// });
// Search for string and add to list
// addToList();
// fox.setIntervalX(
// muteCycle,
// 1 * 1000, // Milliseconds between calls
// 11 // How many times
// );
// fox.setIntervalX(
// checkFriendFollows,
// 1 * 1000, // Milliseconds between calls
// 15 // How many times
// );
response.sendStatus(200);
}); // app.all Express call
// Other endpoints
app.all("/update-blocklist1", async (request, response) => {
console.log("Update triggered...", today, daysPrevious);
blockList = [];
// Fetches the latest trollbots from Botsentinal and blocks them
fetch("https://botsentinel.com/blocklist", {
credentials: "include",
headers: {
accept:
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"accept-language": "en-AU,en;q=0.9,en-US;q=0.8",
"cache-control": "no-cache",
"content-type": "application/x-www-form-urlencoded",
pragma: "no-cache",
"sec-fetch-mode": "navigate",
"sec-fetch-site": "same-origin",
"sec-fetch-user": "?1",
"upgrade-insecure-requests": "1"
},
referrer: "https://botsentinel.com/blocklist",
referrerPolicy: "no-referrer-when-downgrade",
body: `categories%5B%5D=untrustworthy&categories%5B%5D=trollbot&date_pref=1&daterange=${daysPrevious}+-+${today}&zip=1&access-code=&download=1`,
method: "POST",
mode: "cors"
}).then(res =>
res.body
.pipe(csv({ headers: false }))
.on("data", row => {
blockList.push(row[0]);
})
.on("end", () => {
console.log("CSV file successfully processed");
// console.log(blockList);
processBlockList(blockList);
})
);
response.sendStatus(200);
});
app.all("/test", async (request, response) => {
// Process blocktogether list
// updateBlockList(() => {
// processBlockList(blockList);
// });
// unfollowId("87540272064304330");
// fox.setIntervalX(
// unUnRetweetCycle,
// 1 * 1000, // Milliseconds between calls
// 100// How many times
// );
// fox.setIntervalX(
// checkFriendFollows,
// 1 * 1000, // Milliseconds between calls
// 10 // How many times
// );
// T.get("application/rate_limit_status", (error, data, response) => {
// console.log(data);
// });
response.sendStatus(200);
});
async function processBlockList(list) {
if (processingBlockList) {
console.log("Already processing...");
return;
}
processingBlockList = true;
// Add troll accounts to blocked list
for (let account of list) {
await delay(500);
T.post("blocks/create", { user_id: account }, (err, data, response) => {
console.log("Blocking:", account);
if (err) console.log(err);
});
}
processingBlockList = false;
}
app.get("/loadfriends", (request, response) => {
T.get("friends/ids", { screen_name: "phocks", stringify_ids: true }, function(
err,
data,
res
) {
var friendIds = data.ids;
friendIds.reverse();
db.set("friends", friendIds).write();
response.json(data);
});
});
app.get("/loadmutes", (request, response) => {
T.get("mutes/users/ids", { stringify_ids: true }, function(err, data, res) {
var friendIds = data.ids;
friendIds.reverse();
db.set("friends", friendIds).write();
response.json(data);
});
});
app.get("/loadunretweeters", (request, response) => {
T.get("friendships/no_retweets/ids", { stringify_ids: true }, function(
err,
data,
res
) {
console.log(data);
var friendIds = data;
friendIds.reverse();
db.set("friends", friendIds).write();
response.json(data);
});
});
var listener = app.listen(process.env.PORT, function() {
console.log("Your bot is running on port " + listener.address().port);
});
// Functions below here ay
async function muteCycle() {
console.log("");
let friends = db.get("friends").value() || [];
if (!friends[0]) {
console.log("No friends left to process...");
} else {
let friend = String(friends[0]); // Strings work better than integers in Twitter
console.log("muting: " + friend);
muteUser(friend);
console.log("User muted: " + friend);
friends.shift(); // Remove first item in array
db.set("friends", friends) // Write changes to db
.write();
console.log("On to next one. Process seemed to go OK...");
console.log("Accounts to go: " + friends.length);
}
}
async function unmuteCycle() {
console.log("");
let friends = db.get("friends").value() || [];
if (!friends[0]) {
console.log("No friends left to process...");
} else {
let friend = String(friends[0]); // Strings work better than integers in Twitter
console.log("unmuting: " + friend);
await unmuteUser(friend);
console.log("User unmuted: " + friend);
friends.shift(); // Remove first item in array
db.set("friends", friends) // Write changes to db
.write();
console.log("On to next one. Process seemed to go OK...");
console.log("Accounts to go: " + friends.length);
}
}
function addToList() {
var query = {
q:
'"JavaScript" -filter:nativeretweets -filter:replies min_faves:1 lang:en',
result_type: "recent",
lang: "en",
count: 100
};
T.get("search/tweets", query, function(error, data, response) {
if (error) {
console.log("Bot could not find latest tweets, - " + error);
} else {
var userList = "";
data.statuses.forEach(function(d, i) {
if (userList === "") userList = userList + d.user.screen_name;
else userList = userList + "," + d.user.screen_name;
});
console.log(userList);
var params = {
screen_name: userList,
owner_screen_name: "phocks",
slug: "javascript"
};
// Uncomment below to process
T.post("lists/members/create_all", params, function(error, response) {
if (error) {
console.log("Bot could not do it, - " + error);
} else {
console.log("Completed...");
// console.log(response);
}
});
}
});
}
async function unUnRetweetCycle() {
console.log("");
let friends = db.get("friends").value() || [];
if (!friends[0]) {
console.log("No friends left to process...");
} else {
let friend = String(friends[0]); // Strings work better than integers in Twitter
console.log("ununretweeting: " + friend);
await unUnRetweetUser(friend);
friends.shift(); // Remove first item in array
db.set("friends", friends) // Write changes to db
.write();
console.log("On to next one. Process seemed to go OK...");
console.log("Accounts to go: " + friends.length);
}
}
async function searchAndFollow() {
var result = await searchTweets(
'"try going vegan" -filter:nativeretweets -filter:replies'
);
console.log("About to fav this tweet: " + result.id_str);
favTweet(result.id_str);
var screenName = getScreenName(result);
console.log("Found new tweet: " + screenName + ": " + result.text);
if (screenName !== getLastFollowed()) {
await followUser(screenName);
await removeRetweets(screenName);
await muteUser(screenName);
} else {
console.log("Already followed...");
}
setLastFollowed(screenName);
}
async function favTweet(tweetId) {
T.post("favorites/create", { id: tweetId }, function(error, response) {
if (error) {
console.log("Bot could not fav, - " + error);
} else {
console.log("Bot faved : " + tweetId);
}
});
}
async function checkFriendFollows() {
console.log("");
let friends = db.get("friends").value() || [];
if (!friends[0]) {
console.log("No friends left to process...");
} else {
let friend = String(friends[0]); // Strings work better than integers in Twitter
console.log("Checking if this friend follows: " + friend);
let isFollowing = await isFollowingMe(friend);
console.log("Are they following? " + isFollowing);
if (!isFollowing) {
console.log("Unfollowing: " + friend);
unfollowId(friend);
}
friends.shift(); // Remove first item in array
db.set("friends", friends) // Write changes to db
.write();
console.log("Process seemed to go OK...");
console.log("Accounts to go: " + friends.length);
}
}
function unfollowId(userId) {
T.post(
"friendships/destroy",
{ user_id: userId },
(error, data, response) => {
if (error) console.log(error.message);
}
);
}
function getLastFollowed() {
var x = db.get("lastFollowed").value();
return x;
}
function setLastFollowed(screenName) {
db.set("lastFollowed", screenName).write();
}
async function followUser(screenName) {
try {
var response = await T.post("friendships/create", {
screen_name: screenName
});
console.log("Sending follow request: " + screenName); //, response.data);
} catch (error) {
console.log(error);
}
}
// async function muteUser(screenName) {
// try {
// var response = await T.post("mutes/users/create", {
// screen_name: screenName
// });
// console.log("Sending mute request: " + screenName);
// } catch (error) {
// console.log(error);
// }
// }
async function removeRetweets(screenName) {
try {
var response = await T.post("friendships/update", {
screen_name: screenName,
retweets: "false"
});
console.log("Removing retweets from: " + screenName); //, response.data);
} catch (error) {
console.log(error);
}
}
async function searchTweets(query) {
var query = {
q: query,
result_type: "recent",
lang: "en",
count: 100
};
var response = await T.get("search/tweets", query);
// var index = randomNumber(100);
var index = 0;
return response.data.statuses[index];
}
// app.set('json spaces', 4);
function getScreenName(tweet) {
return tweet.user.screen_name;
}
async function isFollowingMe(userId) {
var response = await T.get("friendships/lookup", { user_id: userId }); // 300 per 15 min windows allowed
if (response.data.errors) {
// console.log("there was an error, most likely user doesn't exist any more");
console.log(response.data.errors);
return true;
}
if (
response.data[0] &&
response.data[0].connections.indexOf("followed_by") !== -1
) {
return true;
} else {
return false;
}
}
function randomNumber(lessThan) {
return Math.floor(Math.random() * lessThan);
}
function muteUser(userId) {
T.post("mutes/users/create", { user_id: userId }, (err, data, res) => {
// if (err) response.send(err);
if (err) console.log(err);
console.log("Muted user " + userId);
// friends.shift();
// db.set('friends', friends)
// .write();
// console.log("Left to do: " + friends.length)
});
}
function unmuteUser(userId) {
T.post("mutes/users/destroy", { user_id: userId }, (err, data, res) => {
// if (err) response.send(err);
if (err) {
console.log("Error: " + err.message);
return;
} else {
console.log("Unmuted user " + userId);
}
});
}
function unUnRetweetUser(userId) {
T.post(
"friendships/update",
{ user_id: userId, retweets: true },
(err, data, res) => {
// if (err) response.send(err);
if (err) {
console.log("Error: " + err.message);
return;
} else {
console.log("ununretweeted user " + userId);
}
}
);
}
// app.all("/" + process.env.BOT_ENDPOINT, function (request, response) {
// // Put recurring stuff here
// let friends = db.get('friends').value() || [];
// if (!friends[0]) {
// response.status(200);
// console.log('No friends left...');
// response.send('No friends left...');
// return false;
// }
// // console.log(friends);
// console.log('starting operation')
// fox.setIntervalX(muteUser,
// 1 * 1000, // Milliseconds between calls
// 20 // How many times
// );
// function removeRetweet () {
// T.post('friendships/update', { user_id: friends[0], retweets: 'false' }, (err, data, res) => {
// // if (err) response.send(err);
// if (err) console.log(err);
// console.log('Removed retweets from ' + friends[0]);
// friends.shift();
// db.set('friends', friends)
// .write();
// console.log("Left to do: " + friends.length)
// });
// }
// function muteUser (userId) {
// T.post('mutes/users/create', { user_id: userId }, (err, data, res) => {
// // if (err) response.send(err);
// if (err) console.log(err);
// console.log('Muted user ' + userId);
// // friends.shift();
// // db.set('friends', friends)
// // .write();
// // console.log("Left to do: " + friends.length)
// });
// }
// function addRetweet () {
// T.post('friendships/update', { user_id: 5703342, retweets: 'true' }, (err, data, res) => {
// // if (err) response.send(err);
// if (err) console.log(err);
// console.log('Added retweets from ' + 5703342, data);
// // friends.shift();
// // db.set('friends', friends)
// // .write();
// // console.log("Left to do: " + friends.length)
// });
// }
// response.send('ok');
// }); // app.all Express call
// THIS WAS JUST TO MOVE FOLLOWING TO A LIST SO I HAVE SOME ORIGINAL STARTING POINT
// T.get("friends/ids", async (err, data, res) => {
// console.log(data.ids.length);
// let following = data.ids;
// while (following.length > 1) {
// let firstHundred = following.splice(0, 100);
// console.log(firstHundred.length);
// console.log(following.length);
// T.post(
// "lists/members/create_all",
// { list_id: "1276362310487953410", user_id: firstHundred.join() },
// (err, data, response) => {
// // console.log(data);
// console.log("Done one group");
// }
// );
// await delay(500);
// }
// });
function addUsersToList(userIds, listId) {
var userList = "";
userIds.forEach(function(userId, iteration) {
if (userList === "") userList = userList + userId;
else userList = userList + "," + userId;
});
console.log(userList);
var params = {
screen_name: userList,
owner_screen_name: "phocks",
list_id: listId
};
// Uncomment below to process
T.post("lists/members/create_all", params, function(error, response) {
if (error) {
console.log("Bot could not do it, - " + error);
} else {
console.log("Completed...");
// console.log(response);
}
});
}