forked from aershov24/ibotsunburn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
347 lines (297 loc) · 11.2 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
var twit = require('twit');
var config = require('./config.js');
var log4js = require('log4js');
var logger = log4js.getLogger();
var forEach = require('async-foreach').forEach;
var Twitter = new twit(config.twitter);
var dateFormat = require('dateformat');
var random = require('random-js')();
var redis = require('redis');
var fs = require('fs');
var path = require('path');
var request = require('request');
var giphy = require('giphy-api')(config.giphy.api);
var randomItem = require('random-item');
const uuidV1 = require('uuid/v1');
client = redis.createClient(process.env.REDIS_URL || '');
logger.level = 'all';
var TWITTER_SEARCH_PHRASE = 'i got sunburn AND -filter:replies AND -filter:retweets';
var TWITTER_SEARCH_TYPE = 'recent';
var TWITTER_SEARCH_COUNT = 7;
var SUNBURN_MEMES = [
'Hw r u doin guys?',
'The beach was gr8?',
'every1 gets a sunburn!!!',
'From taking a walk at nite!',
'Went outside on a cloudy day?',
'Sunburn level tomato!',
'nbdy called me a lobster!',
'DYK sunburn is caused by the sun?',
'Sunburn, Sunburn Everywhere!',
'Slaps it!',
'Happy peeling guys!',
'The sun is not our frnd!',
'r u ready 4 peeling?',
'wot duz sunburn feel like?']
var SUNBURN_TWEET = 'It seems {array} got sunburn 2day! {meme}'
var REPLY_TWEET = 'Hey @{screen_name}! Wanna 2 stay sunburn free anytime, anywhere? Get ur Sun Safety Coach app now => http://uvimate.com'
var WELCOME_TWEET = 'thx 4 following @{screen_name}! Wanna 2 stay sunburn free anytime, anywhere? Get ur Sun Safety Coach app now => http://uvimate.com'
var BOT_SCREEN_NAME = 'ibotsunburn'
var REDIS_SUNBURNED_KEY = 'sunburnedPeople';
var REDIS_MENTIONS_KEY = 'mentionedIn';
var DEBUG = false;
var sunburnScreenNames = []
var today = new Date();
var gifLimit = 100
var gifOffset = 0
// START: ====== POST TO WITTER WITH GIF ======
var postWithGif = function(gifTopic, text, callback)
{
// Search with options using promise
giphy.search({q: gifTopic, limit: gifLimit, offset: gifLimit*random.integer(0, gifOffset)}).then(function (res) {
var gifUrl = res.data[random.integer(0, gifLimit-1)].images.original.url
logger.trace('Random gif found: '+ gifUrl);
var filename = uuidV1()+'.gif'
var mp4stream = request(gifUrl).pipe(fs.createWriteStream('gifs/'+filename));
mp4stream.on('finish', function () {
console.log('Gif safed...');
var pathToVideo = path.join(__dirname, 'gifs/'+filename);
console.log(pathToVideo);
Twitter.postMediaChunked({ file_path: pathToVideo }, function (err, data, response) {
if (err)
return logger.error(err);
logger.trace('Gif uploaded...');
fs.unlinkSync(pathToVideo);
var opts = {
media_ids: [data.media_id_string],
status: text,
}
if (!DEBUG){
Twitter.post('statuses/update', opts, function(err, reply) {
callback(err);
});
}
})
});
})
}
var replyWithGif = function(gifTopic, toScreenName, toTwitId, text, callback)
{
// Search with options using promise
giphy.search({q: gifTopic, limit: gifLimit, offset: gifLimit*random.integer(0, gifOffset)}).then(function (res) {
var gifUrl = res.data[random.integer(0, gifLimit-1)].images.original.url
logger.trace('Random gif found: '+ gifUrl);
var filename = uuidV1()+'.gif'
var mp4stream = request(gifUrl).pipe(fs.createWriteStream('gifs/'+filename));
mp4stream.on('finish', function () {
console.log('Gif safed...');
var pathToVideo = path.join(__dirname, 'gifs/'+filename);
console.log(pathToVideo);
Twitter.postMediaChunked({ file_path: pathToVideo }, function (err, data, response) {
if (err)
return logger.error(err);
console.log('Gif uploaded...');
fs.unlinkSync(pathToVideo);
console.log(data)
var opts = {
media_ids: [data.media_id_string],
in_reply_to_status_id: toTwitId,
status: text.replace('{screen_name}', toScreenName),
}
if (!DEBUG){
Twitter.post('statuses/update', opts, function(err, reply) {
callback(err);
});
}
})
});
})
}
// POST: ====== POST TO WITTER WITH GIF ======
// START: ====== POST TO SUNBURN ======
var postSunburnTwit = function() {
if (sunburnScreenNames.length > 0){
logger.trace('Building post...');
if (!DEBUG){
postWithGif(
'sunburn',
SUNBURN_TWEET.replace('{array}', sunburnScreenNames.join(" ")).replace('{meme}', randomItem(SUNBURN_MEMES)),
function(err){
if (err)
return logger.error('err');
logger.trace('Replied to sunburned...');
});
}
} else {
logger.trace('Nothing to post...');
}
}
var checkScreenName = function(screenName, callback) {
logger.debug('Check screenName: '+screenName);
if (!DEBUG){
client.sadd(REDIS_SUNBURNED_KEY, screenName, function(err, reply) {
if (err)
return logger.error(err);
if (reply == 1) { // new screenName
logger.debug('Add screenName: '+screenName);
callback(true, screenName)
}
else{
logger.debug('Skip screenName: '+screenName);
callback(false, screenName)
}
});
} else {
logger.debug('Add screenName: '+screenName);
callback(true, screenName)
}
}
var collectScreenNames = function(tweets, index){
if (index < tweets.length) {
var tweet = tweets[index];
if (tweet.user.screen_name == BOT_SCREEN_NAME)
{
logger.debug('Skip own tweet...');
collectScreenNames(tweets, index+1);
}
else{
logger.trace('['+tweet.user.screen_name+' tweeted]: '+ tweet.text);
checkScreenName(tweet.user.screen_name, function(isNew, screenName){
if (isNew){
logger.debug('Push to array: '+screenName);
sunburnScreenNames.push('@'+screenName);
}
collectScreenNames(tweets, index+1);
});
}
}
else
{
postSunburnTwit();
}
}
// find latest tweet according the query 'q' in params
var searchSunburnTweets = function() {
// clear prev sunburnScreenNames
sunburnScreenNames = [];
var params = {
q: TWITTER_SEARCH_PHRASE, // REQUIRED
result_type: TWITTER_SEARCH_TYPE,
lang: 'en',
count: TWITTER_SEARCH_COUNT
}
logger.debug('Search "I Got Sunburn" tweets: '+TWITTER_SEARCH_PHRASE);
// for more parameters, see: https://dev.twitter.com/rest/reference/get/search/tweets
Twitter.get('search/tweets', params, function(err, data) {
if (err)
return logger.error(err);
if (data.statuses.length == 0)
return logger.debug("No tweets found");
logger.trace("Tweets found: "+ data.statuses.length);
collectScreenNames(data.statuses, 0);
});
}
// END: === POST TO SUNBURN
// START: === REPLY TO FOLLOW
var replyToFollow = function(event) {
var name = event.source.name;
var screenName = event.source.screen_name;
if (screenName != BOT_SCREEN_NAME) {
logger.trace('Bot was followed by: ' + screenName);
if (!DEBUG){
postWithGif(
'sunburn',
WELCOME_TWEET.replace('{screen_name}', screenName),
function(err){
if (err)
return logger.error('err');
logger.trace('Welcome...');
});
}
}
}
// END: === REPLY TO FOLLOW
// START: === REPLY TO REPLY
var replyToReplies = function(tweet) {
var reply_to = tweet.in_reply_to_screen_name;
// Check to see if this was, in fact, a reply to you
if (reply_to === BOT_SCREEN_NAME) {
// Get the username and content of the tweet
var screenName = tweet.user.screen_name;
if (!DEBUG){
postWithGif(
'sunburn',
REPLY_TWEET.replace('{screen_name}', screenName),
function(err){
if (err)
return logger.error(err);
logger.trace('Welcome...');
});
}
}
}
// END: === REPLY TO REPLY
// START: === POST TO MENTIONS
var searchMentions = function(){
Twitter.get('/statuses/mentions_timeline', { count: 2 }, function(err, data){
if(err)
return logger.error(err);
if (data.length){
for (var i = 0; i < data.length; i++){
var currentTweet = data[i];
//the object added to latestMentions array
var tweetObj = {};
tweetObj.screen_name = currentTweet.user.screen_name;
tweetObj.text = currentTweet.text;
tweetObj.id = currentTweet.id_str;
if (!DEBUG){
client.sadd(REDIS_MENTIONS_KEY, tweetObj.id , function(err, reply) {
if (err)
return logger.error(err);
if (reply == 1) { // new mentions_id
logger.trace('Mentioned in: ' + tweetObj.id);
logger.trace('Mentioned by: ' + tweetObj.screen_name);
logger.trace('Mentioned tweet: ' + tweetObj.text);
replyWithGif(
'sunburn',
tweetObj.screen_name,
tweetObj.id,
REPLY_TWEET,
function(err){
if (err)
return logger.error('err');
logger.trace('Replied...');
});
}
else {
logger.trace('Skip mention...');
}
});
}
else{
logger.trace('Mentioned in: ' + tweetObj.id);
logger.trace('Mentioned by: ' + tweetObj.screen_name);
logger.trace('Mentioned tweet: ' + tweetObj.text);
}
}
}
});
};
// END: === POST TO MENTIONS
// MAIN: Bot Loop
var dir = './gifs';
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
}
logger.debug("== @iBotSunburn Started ==");
logger.debug("DEBUG: "+DEBUG);
searchSunburnTweets();
setInterval(function() { searchSunburnTweets(); }, 60000*10); // check every 10 minutes
logger.debug('Listen sunburned users...');
var stream = Twitter.stream('user');
stream.on('follow', replyToFollow);
logger.debug('Listen followers...');
stream.on('tweet', replyToReplies);
logger.debug('Listen replies...');
setInterval(function() { searchMentions() }, 6000*10*10); // check every 10 minutes
logger.debug('Listen mentions...');