-
Notifications
You must be signed in to change notification settings - Fork 1
/
randomWhisper.js
119 lines (98 loc) · 3.34 KB
/
randomWhisper.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
'use strict';
const Twitter = require('twitter');
const auth = require('./auth.json');
const bole = require('bole');
const dbHelper = require('./database.js');
const config = require('./config.json');
const messages = config.messages;
let users = config.usersToHaunt;
const chance = 1 / config.chance;
// Set up logging
const log = bole('cthunWhisper');
bole.output({
level: config.logging,
stream: process.stdout,
});
// We may be mad, but we don't want to spam.
let random = Math.random();
if (random > chance) {
log.debug('Maybe some other time...');
return;
}
// Set up the twitter client.
const client = new Twitter(auth);
// We should spread our madness, don't want arouse suspicion in our targets.
getFreshRandomUser(function (user) {
// Get the twitter handle for the account id first.
client.get('users/show', { user_id : user.id }, function (err, user, response) {
tweetWhisper(user.screen_name);
// I do not forget...
dbHelper.upsertUser(user.id, function () {
log.debug('Hey you, I remember you...');
});
});
});
/**
* Get a random user that has not been tweeted in the previous timeframe and pass
* it to the callback.
* @param {Function} callback What to do with the random user.
*/
function getFreshRandomUser(callback) {
// Should there be no users left to whisper to don't do anything.
if (users.length === 0) {
log.info("I crave fresh souls...");
return;
}
// Randomly select one of the haunted users.
let randomIndex = Math.floor(Math.random() * users.length);
let user = users[randomIndex];
// Verify that this user has not been tweeted recently.
dbHelper.findUser(user.id, function (userDoc) {
// No userdoc means the user has not yet been tweeted.
if (!userDoc) {
callback(user);
return;
}
// Calculate difference in hours and see if it is in range.
let now = new Date();
let difference = Math.abs(now - userDoc.lastTweetDate) / 3.6e6
// It has been a while, good to go.
if (difference > config.hoursDelay) {
callback(user);
return;
}
// This user has recently been whispered to.
log.debug('My last whisper to ' + user.name + ' still lingers...');
// Remove the selected user from the array. Since this script reruns
// every time we don't need to worry about putting it back
users.splice(randomIndex, 1);
// Recursively find a new user.
getFreshRandomUser(callback);
});
}
/**
* Whisper a tweet to a user.
* @param {string} userName Twitter handle of the user to tweet.
*/
function tweetWhisper(userName) {
// Direct it to the lucky individual.
let whisper = '@' + userName + ': ';
// Add a maddening whisper.
whisper += messages[Math.floor(Math.random() * messages.length)];
// Make sure we don't go over the 140 character limit.
whisper.substr(0, 140);
// Set up post object.
let data = {
status: whisper,
}
// Commenting code out is cumbersome.
if (config.testing) {
log.info(data);
return;
}
// Hey... Listen...
client.post('statuses/update', data, function (err, tweet, response) {
if (err) log.error(err);
else log.info('Whispered... ' + tweet.text);
});
};