-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
252 lines (220 loc) · 6.86 KB
/
bot.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
const Twitter = require('twitter');
const express = require('express');
const app = express();
const port = 3000;
const logger = require('./utils/logger').logger;
const payloadLogger = require('./utils/logger').payloadLogger;
const db = require('./utils/db');
const config = require('./config');
const twitter = new Twitter({
consumer_key: process.env.consumer_key,
consumer_secret: process.env.consumer_secret,
access_token_key: process.env.access_token_key,
access_token_secret: process.env.access_token_secret
});
let apiCall = true;
let concoursStream = null;
let nbProcessedTweet = 0;
let maxTweets = 2;
let pauseConcours = 5 * 60 * 1000; //5m
let concoursMaxDuration = 24 * 60 * 60 * 1000; //24h
logger.info("Starting twitter bot...");
let retweet = (tweet, cb) => {
twitter.post('statuses/retweet/' + tweet.id_str, (err, tweet, response) => {
if (err) {
logger.error("RT " + tweet.id_str + " failed cause:" + JSON.stringify(err));
cb(err);
} else {
logger.info("RT " + tweet.id_str + " done.");
cb();
}
});
}
let getInstructions = (tweet) => {
let txt = "";
if (tweet.retweeted_status.extended_tweet)
txt = tweet.retweeted_status.extended_tweet.full_text.toUpperCase();
else
txt = tweet.retweeted_status.text.toUpperCase();
let rt = txt.includes("RT");
let follow = txt.includes("FOLLOW") || txt.includes("SUIVRE");
let like = txt.includes("LIKE") || txt.includes("AIME");
let inst = "";
if (rt) inst += "RT";
if (follow) {
if (inst.length > 0) inst += ", ";
inst += "Follow";
}
if (like) {
if (inst.length > 0) inst += ", ";
inst += "Like";
}
return {
"str": "[" + inst + "]",
"rt": rt,
"follow": follow,
"like": like
};
}
let followUser = (tweet, user) => {
twitter.post('friendships/create', { user_id: user.id_str, follow: true }, (err, resp) => {
if (err)
logger.error("Follow " + user.id_str + " failed cause:" + JSON.stringify(err));
logger.info("Follow " + user.id_str + " done (" + tweet.id_str + ")");
});
}
let errorTweet = (error) => {
logger.error("Problem with stream ! " + JSON.stringify(error));
process.exit(1);
}
let printTweetUrl = (tweet) => {
return "https://twitter.com/" + tweet.user.screen_name + "/status/" + tweet.id_str;
}
let processTweet = (tweet) => {
if (nbProcessedTweet == maxTweets) {
logger.info("------------------------------------------------------------------------------");
logger.info(" Stop stream. Listen stream again after " + pauseConcours / 1000 / 60 + "m");
logger.info("------------------------------------------------------------------------------");
nbProcessedTweet = 0;
concoursStream.destroy();
postRandomTweet();
setTimeout(() => {
postRandomTweet();
}, 2000);
setTimeout(() => {
startConcoursStream();
}, pauseConcours);
}
payloadLogger.info(tweet.id_str + " " + JSON.stringify(tweet));
let msg = printTweetUrl(tweet);
let rtTweet = tweet.retweeted_status;
if (!rtTweet) {
logger.warn(msg);
return;
}
if (rtTweet) {
msg = printTweetUrl(rtTweet);
}
let instructions = getInstructions(tweet);
if (!instructions.rt && !instructions.follow) {
logger.warn(msg);
return;
}
if (apiCall && rtTweet && instructions.rt && !rtTweet.retweeted) {
retweet(rtTweet, (err) => {
if (err) return;
updateBotStats("concoursRT");
if (apiCall && instructions.follow && tweet.entities.user_mentions) {
tweet.entities.user_mentions.forEach(user => {
followUser(tweet, user);
});
}
logger.info(msg + " " + instructions.str);
nbProcessedTweet++;
});
}
}
let updateBotStats = (rtType) => {
let statsCol = db.get().collection("stats");
statsCol.findOne({ id: rtType }, (err, result) => {
if (err) {
logger.error(rtType + " stats update findOne : " + JSON.stringify(err));
}
else if (!result) {
let botStats = { id: rtType, value: 1 };
statsCol.insertOne(botStats, (err, result) => {
if (err)
logger.error(rtType + " stats update insertOne : " + JSON.stringify(err));
else
logger.info(rtType + " stats created.");
});
}
else {
statsCol.updateOne({ id: rtType }, { $inc: { value: 1 } }, (err, result) => {
if (err)
logger.error(rtType + " stats update value +1 : " + JSON.stringify(err));
else
logger.info(rtType + " stats updated.");
});
}
});
}
let postTweet = (status) => {
twitter.post('statuses/update', { status: status }, (error, tweet, response) => {
if (error)
logger.error("Random RT: " + JSON.stringify(error));
logger.info("Random RT: Tweet done.");
updateBotStats("randomRT");
});
}
let postRandomTweet = () => {
let tweetCollection = db.get().collection("tweets");
tweetCollection.aggregate([
{ $sample: { size: 1 } }
]).toArray((err, docs) => {
if (err)
logger.error("Random RT: " + JSON.stringify(err));
let tweet = docs[0];
payloadLogger.info(JSON.stringify(tweet));
let status = tweet.full_text;
if (tweet.retweeted_status) {
status = tweet.retweeted_status.full_text;
if (tweet.retweeted_status.entities && tweet.retweeted_status.entities.urls) {
tweet.retweeted_status.entities.urls.forEach(urlElem => {
if (!status.includes(urlElem.url))
status += urlElem.url;
});
}
}
logger.info("Random RT: " + printTweetUrl(tweet));
postTweet(status);
});
}
let startConcoursStream = () => {
logger.info("------------------------------------------------------------------");
logger.info(" Start concours stream... ");
logger.info("------------------------------------------------------------------");
twitter.stream('statuses/filter', { track: '#CONCOURS, CONCOURS' }, stream => {
concoursStream = stream;
setTimeout(() => {
logger.info("Bot stopped after " + (concoursMaxDuration / 1000) + "s");
concoursStream.destroy();
process.exit(1);
}, concoursMaxDuration);
stream.on('data', processTweet);
stream.on('error', errorTweet);
});
}
app.get('/stats', (req, res) => {
res.setHeader('Content-Type', 'application/json');
let statsCol = db.get().collection("stats");
statsCol.find({}).toArray((err, result) => {
if (err) {
res.status(500).send(err);
return;
}
res.status(200).send(result);
});
});
app.get('/stats/reset', (req, res) => {
res.setHeader('Content-Type', 'application/json');
let statsCol = db.get().collection("stats");
statsCol.drop((err, result) => {
if (err) {
res.status(500).send(err);
return;
}
res.status(200).send(result);
});
});
app.listen(port, () => {
logger.info(`Bot app listening on port ${port} !`);
db.connect(config.dbUrl, config.dbName, (err) => {
if (err) {
logger.error("Problem to connect to mongodb.");
process.exit(1);
} else {
startConcoursStream();
}
});
});