-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
110 lines (93 loc) · 2.79 KB
/
index.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
const fs = require('fs');
const path = require('path');
const _ = require('lodash');
const Twit = require('twit');
const zalgo = require('./zalgo');
const WORDS_DIR = process.env.WORDS_DIR;
if (!WORDS_DIR) {
console.error('WORDS_DIR not specified');
process.exit(1);
}
if (!fs.existsSync(WORDS_DIR)) {
fs.mkdirSync(WORDS_DIR);
}
const SOURCE_WORDS = 'words.txt';
const SHUFFLED_WORDS = path.join(WORDS_DIR, 'words_shuffled.txt');
const LIVE = !!(process.env.LIVE || '').trim();
const client =
LIVE &&
new Twit({
consumer_key: process.env.CONSUMER_KEY,
consumer_secret: process.env.CONSUMER_SECRET,
access_token: process.env.ACCESS_TOKEN,
access_token_secret: process.env.ACCESS_TOKEN_SECRET,
});
function shiftShuffledWord() {
let wordsString;
if (!fs.existsSync(SHUFFLED_WORDS)) {
wordsString = '';
} else {
wordsString = fs.readFileSync(SHUFFLED_WORDS, 'utf8');
}
let words = wordsString.split('\n');
if (!wordsString) {
words = _.shuffle(fs.readFileSync(SOURCE_WORDS, 'utf8').split('\n'));
}
let word = words.shift();
fs.writeFileSync(SHUFFLED_WORDS, words.join('\n'));
return word;
}
function constructTweetText(word) {
// If the Zalgo'ed tweet is too long, don't try and tweet it.
for (let i = 0; i < 20; i++) {
let trialText = zalgo(`MAKE AMERICA ${word.toUpperCase()} AGAIN`);
if (trialText.length <= 140) return trialText;
}
}
function updateStatus(params) {
if (!LIVE) return;
return new Promise((resolve, reject) => {
client.post('statuses/update', params, function(err, data, response) {
if (err) return reject(err);
resolve(data);
});
});
}
function waitFor(msUntilTime) {
return new Promise((resolve, reject) => {
setTimeout(resolve, msUntilTime);
});
}
function waitUntilDueTime() {
// Wait until the next hour.
let tweetInterval = 1000 * 60 * 60;
let msUntilTime = tweetInterval - (Date.now() % tweetInterval) - 1000 * 15;
console.log('Waiting for next tweet time...');
return waitFor(LIVE ? msUntilTime : 5000);
}
function waitAndTweet() {
let status, word;
// Probably only topical for a couple of weeks
const covfefeTime = 1496120760000;
const daysAfterCovfefe = (new Date() - covfefeTime) / (1000 * 60 * 60 * 24);
if (Math.random() > daysAfterCovfefe / 14 && Math.random() > 0.5) {
word = 'covfefe';
status = constructTweetText('covfefe');
}
while (!status) {
word = shiftShuffledWord();
status = constructTweetText(word);
}
return waitUntilDueTime()
.then(() => updateStatus({ status }))
.then(() => {
console.log(`MAKE AMERICA ${word.toUpperCase()} AGAIN`);
return waitFor(LIVE ? 1000 * 60 : 0);
})
.then(() => waitAndTweet());
}
waitAndTweet().catch(err => {
console.error(err);
process.exit(1);
});
console.log('Bot started.');