-
Notifications
You must be signed in to change notification settings - Fork 4
/
TrumpBot.js
113 lines (91 loc) · 3.9 KB
/
TrumpBot.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
process.env.UV_THREADPOOL_SIZE = 10;
let DataManager = require('./Includes/DataManager.js');
let DataAPI = require('./Includes/DataAPI.js');
let Schedule = require('node-schedule');
let config = require("./data/config.json");
let Facebook = require("./Includes/Facebook.js");
//Ende imports
let appToken = config.appId + "|" + config.appSecret;
let trumpNewsId = config.trumpNewsId;
let trumpNewsToken = config.trumpNewsToken;
let fb = new Facebook(config.appId, config.appSecret);
//fb.extendToken(trumpNewsToken);
let storage = require("node-persist");
let lastPosts = [];
let dm = new DataManager(config.redisPort, function()
{
let api = new DataAPI(dm.client);
let updateBot = function(){
fb.downloadPosts(trumpNewsId, function(data, prev, next){
let pagePosts = data;
let postable = [];
let done = 0;
api.getLinksToWords(["trump"], function(res){ //Other words too?
let todo = (res.length > 200 ? 200 : res.length);
for(let r = 0; r < todo; r++)
{
api.getLink(res[r], function(link){
if(link.sourceId != "dm") //Links from that site are buggy
{
let found = false;
for(let l in pagePosts)
{
if(pagePosts[l].message == link.title)
{
found = true;
break;
}
}
for(let l in lastPosts)
{
if(lastPosts[l].message == link.title || lastPosts[l].url == link.url)
{
found = true;
break;
}
}
if(found == false)
postable.push(link); //Postable
}
done++;
if(done >= todo)
{
postable = postable.sort(function(a, b){return parseInt(b.date) - parseInt(a.date)});
if(postable.length >= 1)
{
console.log("Posting: ");
console.log(postable[0]);
fb.postTo(trumpNewsId, postable[0].title, postable[0].url, trumpNewsToken);
lastPosts.push({message:postable[0].title, url:postable[0].url});
while(lastPosts.length > 200)
lastPosts.shift();
storage.setItem('trumpNewsLastPosts',JSON.stringify(lastPosts))
.then(function() {
console.log("Saved lastPosts");
});
}
else
{
console.log("Nothing to post anymore :(");
}
}
});
}
});
});
}
storage.init().then(function() {
storage.getItem('trumpNewsLastPosts')
.then(function(value) {
lastPosts = JSON.parse(value);
updateBot();
});
});
//Every 10 minutes
Schedule.scheduleJob('10 * * * *', updateBot);
Schedule.scheduleJob('20 * * * *', updateBot);
Schedule.scheduleJob('30 * * * *', updateBot);
Schedule.scheduleJob('40 * * * *', updateBot);
Schedule.scheduleJob('50 * * * *', updateBot);
Schedule.scheduleJob('0 * * * *', updateBot);
});