-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
409 lines (333 loc) · 13.3 KB
/
app.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
var MongoClient = require('mongodb').MongoClient;
var classifier = require('./classifier');
var config = require('./config');
var helper = require('./helper');
var words = require('./words');
var async = require('async');
var _ = require('lodash');
var Twit = require('twit');
var T = new Twit(config);
// Change these, depending on what you want your
// database, collection, and tracking hash tag to
// look like.
var dbName = 'twitter-bot';
var collectionName = 'inputs';
var tweetDb, tweetColl;
var tweetsID = [];
var HTML_VALUE = 4;
var MEAN_VALUE = 3;
var START_TIME_TO_RETWEET = 1000 * 60 * 60 * 0.3;
var END_TIME_TO_RETWEET = 1000 * 60 * 60 * 0.4;
// var START_TIME_TO_RETWEET = 20;
// var END_TIME_TO_RETWEET = 30;
var stream = T.stream('statuses/filter', {track: words.program.concat(words.html) });
stream.on('tweet', function(tweet) {
console.log('Incoming Tweet');
// Log the tweet no matter what, as log as a connection to Mongo has been established
if(!tweetColl) {
console.log('**** Sem tweetColl');
return ;
}
// Don't Fav/RT the middle of a convo
if(tweet.in_reply_to_user_id) {
return ;
}
var screen_name = tweet.user.screen_name;
var text = tweet.text;
var valueSpecialUser = null;
_.some(words.specialUsers, function(value, key) {
var XKey = '^' + key + '$';
if(screen_name.match(XKey)) {
valueSpecialUser = value;
console.log('** Special User --', key + ': ' + value);
return true;
}
});
if(
_.isNumber(valueSpecialUser) ||
// Se nao e retweet
// !tweet.retweeted_status &&
tweet.lang === 'en' &&
classifier.isTextWhitelist(text) &&
// Não precisa mais
// !words.blacklist.test(tweet.text.toLowerCase()) &&
// Tem que ter link dentro da propriedade urls
(tweet.entities && tweet.entities.urls && tweet.entities.urls.length !== 0)
) {
console.log('** screen_name --', screen_name);
console.log('** text --', text);
console.log('*** id_str', tweet.id_str);
// Rejeitar stream repetidos
if(!_.some(tweetsID, tweet.id_str)) {
tweetsID.push(tweet.id_str);
} else {
console.log('** tweetsID já tem --', tweet.id);
return;
}
var postActual = helper.isActualPost(tweet);
if(!postActual) {
console.log('** !postActual --', postActual);
return ;
}
// userBlackList
if (classifier.isUserBlacklist(screen_name)) {
console.log('** UserBlackList --', screen_name);
return ;
}
// tweet.text.match(words.userBlacklistRTPattern)
if(classifier.isUserBlacklistRT(tweet)) {
console.log('*** RT Blacklist ', tweet.text);
return;
}
// tweet.text.match(words.textBlacklistPattern)
if(classifier.isTextBlacklist(tweet)){
console.log('isTextBlacklist --> ', tweet.text);
return;
}
// console.log('******* validUrl', validUrl);
if(!classifier.isValidUrls(tweet.entities.urls)) {
console.log('** URL Inválida -->', tweet.entities.urls);
return ;
}
// }
// Roll to retweet
if(words.htmlPattern.test(text)) {
if(!tweet.retweeted) {
console.log(' ** Scheduling html RT');
scheduleFuture(retweetTweet, tweet, HTML_VALUE, valueSpecialUser);
// scheduleFuture(retweetTweet, tweet, 0);
// favorite(tweet);
}
} else {
if (!tweet.retweeted) {
console.log('** Scheduling MEAN RT');
scheduleFuture(retweetTweet, tweet, MEAN_VALUE, valueSpecialUser);
// scheduleFuture(retweetTweet, tweet, 0);
// favorite(tweet);
// follow(tweet);
}
}
}
});
function favorite(tweet) {
if(!tweet.favorited && helper.isLucky(1/13)) {
console.log('Scheduling Favorite');
scheduleFuture(favoriteTweet, tweet);
}
}
function follow(tweet) {
if(!tweet.user.following && helper.isLucky(1/40)) {
console.log('Scheduling Follow');
scheduleFuture(followUser, tweet.user);
}
}
// Instead of Fav/RT/Follow immediately, do it after a variable delay.
var scheduleFuture = function(fn, tweet, resistValue, valueSpecialUser) {
// Pode reduzir resitencia (aumentar chance dele ser postado)
var resistValueWhithBias = classifier.biasRetweetValue(resistValue, tweet);
// Pode aumentar resistencia (reduzir chance dele ser postado)
var hours = helper.tweetedAtHours(tweet);
if(hours > 1) {
if(hours < 2) {
resistValueWhithBias += 1;
} else if(hours < 4) {
resistValueWhithBias += 2;
} else if(hours < 6) {
resistValueWhithBias += 3;
} else if(hours < 8) {
resistValueWhithBias += 4;
} else if(hours < 10) {
resistValueWhithBias += 5;
} else if(hours < 12) {
resistValueWhithBias += 6;
} else if(hours < 14) {
resistValueWhithBias += 7;
} else if(hours < 16) {
resistValueWhithBias += 8;
} else if(hours < 18) {
resistValueWhithBias += 9;
} else if(hours < 20) {
resistValueWhithBias += 10;
} else if(hours < 22) {
resistValueWhithBias += 11;
} else if(hours < 24) {
resistValueWhithBias += 12;
}
}
if(valueSpecialUser) {
resistValueWhithBias += valueSpecialUser;
}
console.log('*** special user?', valueSpecialUser);
console.log('*** diff hours -- ', hours);
console.log('*** Bias hours? --', resistValueWhithBias);
setTimeout(fn.bind(this, tweet, resistValueWhithBias), randomMsBetween(START_TIME_TO_RETWEET, END_TIME_TO_RETWEET));
};
//Choose a random number between two given numbers
var randomMsBetween = function(low, high) {
return Math.floor(Math.random() * (high - low)) + low;
};
var favoriteTweet = function(tweet, count) {
console.log('Favoriting tweet:' + tweet.text);
T.post('favorites/create', { id: tweet.id_str }, cb);
// T.post('favorites/create', { id: tweet.id_str }, cb, count);
};
var retweetTweet = function(tweet, count) {
console.log('Try retweet tweet:', tweet.text);
T.get('statuses/show/:id', {id: tweet.id_str}, function(err, data, response) {
if (!data || err) {
console.log(err);
return ;
}
// console.log('Date:', created_at);
console.log('count (arg) -->', count);
console.log('data.retweet_count -->', data.retweet_count);
if(data.retweet_count < count) {
return ;
}
console.log(1);
async.waterfall([
async_findDb,
async_matchText,
async_sendManager
],
function(err, result) {
if(err) {
new Error(err);
}
console.log(result);
});
console.log(2);
function async_findDb(callback) {
console.log(3);
var allTweetTexts = tweetColl.find().toArray(function(err, docsList) {
if(err) {
console.log(err);
}
// console.log('*** DOCS', docsList);
callback(null, docsList);
});
}
function async_matchText(docsList, callback) {
// console.log('docsList', docsList);
// console.log('!docsList', !docsList);
var isSimilarTweet = false;
var wordsList = data.text.split(' ');
if(!docsList || docsList.length <= 0) {
return callback(null, wordsList);
}
_.some(docsList, function(doc) {
var wordsListDb = doc.text;
// console.log('****** wordsListDb', wordsListDb);
// console.log('****** data.text.split(" ")', data.text.split(' '));
// wordsList : Array Words of text
var textItemMatched = _.filter(wordsList, function(word){
// console.log('*** Primeiro --> Filter');
// wordsListDb : Array Words of Text (in DB)
return _.some(wordsListDb, function(wordDb){
// console.log('*** Segundo --> Filter');
// console.log('word', word);
// console.log('wordDb', wordDb);
// o ultimo word é indefinido
if(word === undefined || wordDb === undefined) return false;
if(word.toLowerCase() === wordDb.toLowerCase()) {
// console.log('IGUAL', word);
// Remove wordDb do wordsListDb
var index = wordsListDb.indexOf(wordDb);
wordsListDb.splice(index, 1);
// _.some wordsListDb
return true;
}
// _.some wordsListDb
return false;
});
});
// console.log('wordsList.length', wordsList.length);
var percent = 60;
var qtdPerm = wordsList.length * percent / 100;
// console.log('qtdPerm', qtdPerm);
var qtdMatched = textItemMatched.length;
if (qtdMatched >= qtdPerm) {
isSimilarTweet = true;
console.log(' *** qtdMatched', qtdMatched);
console.log(' *** qtdMatched >= qtdPerm', qtdMatched >= qtdPerm);
// _.some docsList - break
return true;
}
//_.some docsList - continue
return false;
});
if(isSimilarTweet) {
// isSimilarTweet = false;
return callback('*** Já possui SIMILAR');
} else {
return callback(null, wordsList);
}
}
function async_sendManager(wordsList, callback) {
console.log(4);
// var wordsList = tweet.text.split(' ');
console.log('wordsList', wordsList );
tweetColl.insert({text: wordsList}, function(err, result) {
if(err) {
console.log('ERRO de INSERÇÃO', err);
return callback('Erro de INSERÇÃO');
}
});
console.log('******** Retweeting !!!');
console.log('retweet_count -->', data.retweet_count);
// console.log('******** Search alternative date', data);
var userLucky = classifier.userLucky(data);
console.log('*** app.js screen_name' , data.screen_name)
var wasUserLucky = helper.isLucky(userLucky / 10);
var textLucky = classifier.textLucky(data);
var wasTextLucky = helper.isLucky(textLucky / 10);
console.log('Text -->', data.text);
console.log('Text Lucky -->', textLucky);
console.log('Was Text Lucky --> helper.isLucky(textLucky / 10)', wasTextLucky);
console.log('User -->', data.screen_name);
console.log('User Lucky -->', userLucky);
console.log('Was User Lucky --> helper.isLucky(userLucky / 10)', wasUserLucky);
if(wasUserLucky && wasTextLucky) {
T.post('statuses/retweet/:id', { id: tweet.id_str }, cb);
} else {
console.log('docker -- ext not ', data.text + ' --> ' + textLucky);
console.log('docker -- ext not ', data.text + ' --> ' + userLucky);
}
return callback(null, 'Tweet Inserted');
}
});
};
var followUser = function(user) {
console.log('Following user: ' + user.screen_name);
T.post('friendships/create', {screen_name: user.screen_name, follow: true}, cb);
};
// Simple callback to swallow successes and log errors
var cb = function(err, data, response) {
if(err) {
console.log(err);
return;
}
// console.log(data);
};
//Connect to a local Mongo Instance
MongoClient.connect('mongodb://localhost:27019/' + dbName, function(err, db) {
if(err) {
console.log('ERROR: Cannot connect to mongo, tweets will not be logged');
console.log(err);
} else {
console.log('Connected to mongo');
tweetDb = db;
tweetColl = db.collection(collectionName);
}
});
//Gracefully handle SIGINT
process.on( 'SIGINT', function() {
console.log( "\nGracefully shutting down from SIGINT (Ctrl-C)" );
if(tweetDb) {
tweetDb.close();
}
if(stream){
stream.stop();
}
process.exit();
});