-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
367 lines (345 loc) · 10.7 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
console.log('\033[2J');
var creds = require('./creds');
const DEV_MODE = true;
//MYSQL STUFF
var mysql = require('mysql');
var pool = mysql.createPool({
connectionLimit : 10,
host : creds.mysql.host,
port : creds.mysql.port,
database : creds.mysql.dbName,
user : creds.mysql.user,
password : creds.mysql.password
});
//init twitter
var Twitter = require('twitter');
var client = new Twitter({
consumer_key: creds.twitter.consumerKey,
consumer_secret: creds.twitter.consumerSecret,
access_token_key: creds.twitter.accessToken,
access_token_secret: creds.twitter.accessSecret
});
//stream all tweets matching 'track' var
var phrase = '#masterchef',
users = {
FunkeDope: {
name: 'FunkeDope',
id: 21332344
},
Radio1045: {
name: 'Radio1045',
id: 21720480
}
};
var user = users.Radio1045;
//listens for tweets from a user
/*
console.log('looking for tweets from: @' + user.name);
client.stream('statuses/filter', {follow: user.id}, function(stream) {
stream.on('data', function(tweet) {
//check if they tweeted something
if(tweet.text) {
//log it, as long as its not a RT
if(tweet.text.substring(0, 4) !== "RT @") {
//now, lets only care if its a tweet FROM this user, not just mentioning them
if(tweet.user.id === user.id) {
console.log(tweet.text);
pushOver(tweet.text, 'New tweet from @' + user.name);
console.log('------------------------------');
}
}
}
});
stream.on('error', function(error) {
console.log(error);
throw error;
});
});
*/
//listenes for all tweets with keywords
var keywords = [
'follow retweet win',
'follow retweet contest',
'follow rt win',
'follow rt contest',
'follow retweet giveaway',
'follow rt giveaway',
'rt contest',
'rt win',
'retweet win',
'retweet contest',
'retweet giveaway'
],
keywordsCSV = keywords.join();
console.log('looking for tweets with: ' + keywordsCSV);
console.log('------------------------------');
client.stream('statuses/filter', {track: keywordsCSV}, function(stream) {
stream.on('data', function(tweet) {
//check if they tweeted something
if(tweet.text) {
//log it, as long as its not a RT or @
if(!tweet.quoted_status_id &&
tweet.text.substring(0, 4) !== "RT @" &&
tweet.text.substring(0, 1) !== "@" &&
tweet.text.substring(0, 3) !== 'RT ' &&
tweet.text.substring(0, 4) !== 'RT: ' &&
tweet.text.substring(0, 2) !== '"@' &&
tweet.text.substring(0, 4) !== '#RT ' &&
tweet.text.substring(0, 5) !== '#RT: ' &&
tweet.text.toLowerCase().indexOf('vote') < 0 &&
tweet.text.toLowerCase().indexOf('voto') < 0 &&
tweet.text.toLowerCase().indexOf('stop by') < 0 &&
tweet.text.toLowerCase().indexOf('click here') < 0 &&
tweet.text.toLowerCase().indexOf('i\'ve entered') < 0 &&
tweet.text.toLowerCase().indexOf('i\'m entered') < 0
) {
//now make sure the actual text contains a phrase. twitter returns a lot of bs. card text matches the filter which we dont want
var excludeRegex = /.+( ?)(: RT(:?))/ig;
var match = tweet.text.match(excludeRegex);
if(!match) {
//console.log(tweet);
/*console.log('@' + tweet.user.screen_name + ': ' + tweet.text);
console.log('------------------------------');
//wait anywhere from half a second to 1mins to enter
var delay = Math.floor((Math.random() * 1) + 300);
delay = delay * 1000;
setTimeout(function() {
enterContest(tweet);
}, delay);*/
}
}
}
});
stream.on('error', function(error) {
console.log(error);
throw error;
});
});
//twitter helper methods
function sendTweet() {
'use strict';
//tweet something
var status = 'I want Pizza! #AMAZONDASH';
client.post('statuses/update', {status: status}, function(error, tweet, response){
if(error) {
console.log(error);
throw error;
}
else {
//console.log(tweet); // Tweet body.
console.log('Tweet sent: "' + status + '"');
}
});
}
function retweet(id) {
'use strict';
client.post('statuses/retweet', {id: id, trim_user: true}, function(error, tweet, response){
if(error) {
console.log(error);
console.log(id);
//throw error;
}
else {
// console.log('Retweeted');
}
});
}
function favorite(id) {
'use strict';
client.post('favorites/create', {id: id, trim_user: true}, function(error, tweet, response){
if(error) {
console.log(error);
console.log(id);
//throw error;
}
else {
//console.log('Faved');
}
});
}
function follow(id) {
'use strict';
client.post('friendships/create', {user_id: id}, function(error, tweet, response){
if(error) {
console.log(error);
console.log(id);
//throw error;
}
else {
// console.log('Followed');
}
});
}
function unFollow(id) {
'use strict';
client.post('friendships/destroy', {user_id: id}, function(error, tweet, response){
if(error) {
console.log(error);
console.log(id);
//throw error;
}
else {
console.log('Unfollowed: ' + id);
}
});
}
function checkIfDeleted(tweet) {
'use strict';
client.get('statuses/show', {id: tweet.id_str, trim_user: true, include_entities: false}, function(error, tw, response){
if(error) {
console.log(error);
console.log('deleted as fuck');
}
else {
console.log('Not deleted! Lets enter this contest...');
//enterContest(tweet);
}
});
}
//send a push notification to pushover
function pushOver(text, title) {
'use strict';
var Pushover = require('node-pushover');
var push = new Pushover({
token: creds.pushOver.token,
user: creds.pushOver.user
});
if(!title) {
title = 'TwitMonLee';
}
if(!text) {
text = 'New push notification!';
}
// A callback function is defined:
push.send(title, text, function (err, res){
if(err) {
console.log("Error sending push");
console.log(err);
console.log(err.stack);
}
else {
//console.log("DEV: Push notification sent successfully.");
console.log(res);
}
});
}
//this function handles entering the actual contest. just be on the safe side we will rt, fav, and follow everything
function enterContest(tweet) {
'use strict';
//first thing is first, dont do anything if its a contest we already entered. for now we will assume 1 userid = 1 contest
hasEntered(tweet)
}
//checks the db to see if a contest from the selected user has alread ybeen entered
function hasEntered(tweet) {
'use strict';
var query = pool.query('SELECT userID FROM Contests WHERE userID = ' + tweet.user.id, function(err, rows, fields) {
if(err) {
console.log(err);
throw err;
}
if(rows.length > 0) {
console.log('!!ALREADY ENTERED A CONTEST FROM THIS USER!! : ' + tweet.user.id);
return false;
}
else {
//add contest deets to the db
addToDB(tweet);
//rt and fav. delay a bit between both
retweet(tweet.id_str);
favorite(tweet.id_str);
follow(tweet.user.id);
updateDB(tweet, true, true, true);
console.log('!!CONTEST ENTERED!!');
}
});
}
function addToDB(tweet) {
'use strict';
var sql = 'INSERT INTO Contests SET userName=?, userID=?, entered=?, retweeted=?, faved=?, following=?, text=?';
var inserts = [
tweet.user.screen_name,
tweet.user.id,
0,
0,
0,
0,
tweet.text
];
sql = mysql.format(sql, inserts);
pool.query(sql, function(err, rows, fields) {
if(err) {
console.log(err);
throw err;
return false;
}
else {
return true;
}
});
}
function updateDB(tweet, rt, fav, follow) {
'use strict';
var sql = 'UPDATE Contests SET entered=?, retweeted=?, faved=?, following=? WHERE userID=?';
var inserts = [
true,
rt,
fav,
follow,
tweet.user.id
];
sql = mysql.format(sql, inserts);
pool.query(sql, function(err, rows, fields) {
if(err) {
console.log(err);
throw err;
return false;
}
else {
return true;
}
});
return true;
}
//unfollows ~50 - 150 users in random intervals
function getUsersToUnfollow() {
'use strict';
var unFollowNumber = Math.floor((Math.random() * 150) + 50);
var sql = 'SELECT userID FROM Contests WHERE following = 1 ORDER BY timeEntered ASC LIMIT ' + unFollowNumber;
pool.query(sql, function(err, rows, fields) {
if(err) {
console.log(err);
throw err;
}
else {
//console.log(sql);
//console.log(rows);
for(var i = 0; i < rows.length; i++) {
var delay = Math.floor((Math.random() * 1) + 300);
//delay = delay * 1000;
setInterval(function() {
console.log(i);
console.log(rows[i]);
//unFollow(rows[i].userID);
}, delay);
}
}
});
}
// ----- for dev: space bar = dash press --------- //
if(DEV_MODE === true) {
var keypress = require('keypress');
// make `process.stdin` begin emitting "keypress" events
keypress(process.stdin);
// listen for the "keypress" event
process.stdin.on('keypress', function (ch, key) {
'use strict';
if(key && key.ctrl && key.name === 'c') {
process.exit();
}
else if(key && key.name === 'space') {
getUsersToUnfollow();
}
});
process.stdin.setRawMode(true);
//process.stdin.resume();
}