-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter.js
318 lines (294 loc) · 8.52 KB
/
twitter.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
const Twitter = require('twitter');
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 tweetStream;
let concoursTimer;
let concoursTimeoutTab = [];
let apiCall = true;
let retweet = (tweet, cb) => {
twitter.post('statuses/retweet/' + tweet.id_str, (err, tweet, response) => {
if (err) {
logger.error("RT " + tweet ? tweet.id_str : "" + " failed cause:" + JSON.stringify(err));
cb(err);
} else {
logger.info("RT " + tweet ? tweet.id_str : "" + " done.");
cb();
}
});
}
let getInstructions = (tweet) => {
let txt = "";
if(!tweet.retweeted_status)
return;
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 enterConcours = () => {
let concoursTweets = db.get().collection("concoursTweets");
concoursTweets.findOne({ concoursEnter: null, concoursFailed: null }, (err, tweet) => {
if (err) {
logger.error("concoursTweets findOne : " + JSON.stringify(err));
return;
}
if(!tweet){
logger.error("concoursTweets no mode concours tweet available in db");
return;
}
let idDb = tweet._id;
payloadLogger.info(tweet.id_str + " " + JSON.stringify(tweet));
let msg = printTweetUrl(tweet);
let rtTweet = tweet.retweeted_status;
if (!rtTweet) {
logger.warn(msg);
concoursTweets.updateOne({ _id: idDb }, { $set: { concoursFailed: true } }, (err, result) => {
if (err) {
logger.error("concoursTweets updateOne");
} else {
enterConcours();
return;
}
});
return;
}
else {
msg = printTweetUrl(rtTweet);
}
let instructions = getInstructions(tweet);
if (!instructions || (!instructions.rt && !instructions.follow)) {
logger.warn(msg);
concoursTweets.updateOne({ _id: idDb }, { $set: { concoursFailed: true } }, (err, result) => {
if (err) {
logger.error("concoursTweets updateOne");
} else {
enterConcours();
return;
}
});
return;
}
if (apiCall && rtTweet && instructions.rt && !rtTweet.retweeted) {
retweet(rtTweet, (err) => {
if (err) {
concoursTweets.updateOne({ _id: idDb }, { $set: { concoursFailed: true } }, (err, result) => {
if (err) {
logger.error("concoursTweets updateOne");
} else {
enterConcours();
return;
}
});
return;
}
concoursTweets.updateOne({ _id: idDb }, { $set: { concoursEnter: true } }, (err, result) => {
if (err)
logger.error("concoursTweets updateOne");
else
updateBotStats("concoursRT");
});
if (apiCall && instructions.follow && tweet.entities.user_mentions) {
tweet.entities.user_mentions.forEach(user => {
followUser(tweet, user);
});
}
logger.info(msg + " " + instructions.str);
});
}
else {
logger.warn("!rt " + msg);
concoursTweets.updateOne({ _id: idDb }, { $set: { concoursFailed: true } }, (err, result) => {
if (err) {
logger.error("concoursTweets updateOne");
} else {
enterConcours();
return;
}
});
}
});
}
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, date: new Date() };
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, callback) => {
twitter.post('statuses/update', { status: status }, (error, tweet, response) => {
if (error)
logger.error("Random RT: " + JSON.stringify(error));
logger.info("Random RT: Tweet done.");
callback();
});
}
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, () => {
updateBotStats("randomRT");
});
});
}
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 errorStreamTweet = (error) => {
logger.error("Problem with stream ! " + JSON.stringify(error));
process.exit(1);
}
let processStreamTweet = (tweet) => {
let concoursTweets = db.get().collection("concoursTweets");
concoursTweets.findOne({ id_str: tweet.id_str }, (err, result) => {
if (err) {
logger.error("concoursTweets findOne : " + JSON.stringify(err));
return;
}
if (!result) {
concoursTweets.insertOne(tweet, (err, result) => {
if (err) {
logger.error("concoursTweets insertOne : " + JSON.stringify(err));
return;
}
logger.info(printTweetUrl(tweet) + " inserted in db.");
});
}
else {
logger.warn(printTweetUrl(tweet) + " already present in db.");
}
});
}
let printTweetUrl = (tweet) => {
return "https://twitter.com/" + tweet.user.screen_name + "/status/" + tweet.id_str;
}
let processTweet = () => {
let nbConcoursTweets = config.nbConcoursTweetsPerInterval;
let nbRandomTweets = config.nbRandomTweetsPerInterval;
let interval = (config.concoursInterval * 60 * 1000) / (nbConcoursTweets + nbRandomTweets);
let offset = 0;
for (let i = 0; i < nbConcoursTweets; i++) {
let to = setTimeout(() => {
logger.info("Concours tweet");
enterConcours();
}, offset);
concoursTimeoutTab.push(to);
offset += interval;
}
for (let i = 0; i < nbRandomTweets; i++) {
let to = setTimeout(() => {
logger.info("Random tweet");
postRandomTweet();
}, offset);
concoursTimeoutTab.push(to);
offset += interval;
}
}
let startConcours = () => {
logger.info("Start concours (interval: " + config.concoursInterval + " min, pause " + config.pause + " min)");
config.concours = true;
if (concoursTimer)
clearInterval(concoursTimer);
processTweet();
concoursTimer = setInterval(processTweet, (config.concoursInterval * 60 * 1000) + (config.pause * 60 * 1000));
}
let stopConcours = () => {
logger.info("Stop concours");
config.concours = false;
clearInterval(concoursTimer);
concoursTimeoutTab.forEach((to) => {
clearTimeout(to);
});
concoursTimeoutTab = [];
concoursTimer = null;
}
let startConcoursStream = () => {
logger.info("Start concours twitter stream");
if (!config.stream) {
config.stream = true;
twitter.stream('statuses/filter', { track: '#CONCOURS, CONCOURS' }, stream => {
tweetStream = stream;
stream.on('data', processStreamTweet);
stream.on('error', errorStreamTweet);
});
}
}
let stopConcoursStream = () => {
logger.info("Stop concours twitter stream");
config.stream = false;
if (tweetStream)
tweetStream.destroy();
}
TwitterModule = {
startConcoursStream: startConcoursStream,
stopConcoursStream: stopConcoursStream,
startConcours: startConcours,
stopConcours: stopConcours
};
module.exports = TwitterModule;