-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
81 lines (70 loc) · 2.58 KB
/
server.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
/* Setting things up. */
var path = require('path'),
fs = require('fs'),
express = require('express'),
app = express(),
rp = require('request-promise'),
storage = require('node-persist'),
cheerio = require('cheerio'),
Twit = require('twit'),
twit;
try {
twit = 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
});
console.log("Ready to tweet! Send a GET or POST request to {GLITCH_PROJECT_URL}/tweet");
} catch(err) {
console.error(err);
console.error("Sorry, your .env file does not have the correct settings in order to tweet");
}
storage.initSync();
app.use(express.static('public'));
/* You can use uptimerobot.com or a similar site to hit your /BOT_ENDPOINT to wake up your app and make your Twitter bot tweet. */
var allText = "";
const options = {
uri: `http://powerlisting.wikia.com/wiki/Special:Random`,
transform: function (body) {
return cheerio.load(body);
}
};
rp(options)
.then(($) => {
var text = $('#mw-content-text').children('p').text().split("");
var finalText = text[0];
var i = 1;
while(i < 100 && text[i] != "."){
finalText += text[i];
i++;
}
finalText += ".";
var title = $('.page-header__title').text();
allText = (title + ": " + finalText + " (" + 'http://powerlisting.wikia.com/wiki/' + title.replace(/ /g, "_") + ")");
})
.catch((err) => {
console.log(err);
});
function postTweet(){
console.log("Tweeting!");
twit.post('statuses/update', { status: allText }, function(err, data, response) {
console.log(`Posted status: ` + allText);
});
}
app.all("/" + process.env.BOT_ENDPOINT, function (request, response) {
var now = Date.now(), // time since epoch in millisecond
lastRun = storage.getItemSync("lastRun") || 0, // last time we were run in milliseconds
postDelay = process.env.POST_DELAY_IN_MINUTES || 60;// time to delay between tweets in minutes
if (now - lastRun <= (1000 * 60 * postDelay)) { // Only post every process.env.POST_DELAY_IN_MINUTES or 60 minutes
console.error(`It's too soon, we only post every ${postDelay} minutes. It's only been ${ Math.floor((now - lastRun) / 60 / 1000 ) } minutes`);
return false;
}
storage.setItemSync("lastRun", now);
var resp = response;
postTweet();
return true;
});
var listener = app.listen(process.env.PORT, function () {
console.log('Your bot is running on port ' + listener.address().port);
});