-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
168 lines (130 loc) · 3.83 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
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
let request = require('request'),
store = require('data-store')('spacex-takeoffs'),
express = require('express'),
app = express(),
Twit = require('twit'),
config = {
twitter: {
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
}
},
T = new Twit(config.twitter);
app.use(express.static('public'));
app.all('/' + process.env.BOT_ENDPOINT, function (req, res) {
let json = {};
try {
fetchLaunchesJson((json) => {
let launches = [];
for (let i in json) {
let launchData = json[i];
let launch = {
flight_number: launchData.flight_number,
launch_date_utc: launchData.launch_date_utc,
rocket_name: launchData.rocket.rocket_name,
site_name_long: launchData.launch_site.site_name_long,
reddit_campaign: launchData.links.reddit_campaign
};
launches[i] = launch;
}
checkLaunches(launches);
res.json(json);
});
} catch(err) {
console.log(err.message);
}
});
function fetchLaunchesJson(callback) {
request('https://api.spacexdata.com/v2/launches/upcoming', (err, res, body) => {
if (err) {
throw err;
}
let json = JSON.parse(body);
callback(json);
});
}
async function checkLaunches(launches) {
for (let i = 0; i < launches.length; i++) {
let launch = launches[i];
if (isLaunchNew(launch)) {
await new Promise((resolve, reject) => {
setTimeout(() => {
try {
tweet(getTweetText(launch));
resolve();
} catch (err) {
console.log(err.message);
}
}, 5000);
});
} else {
console.log("Not tweeting launch " + launch.flight_number);
}
}
}
function isLaunchNew(launch) {
let flightNumber = launch.flight_number;
let lastTweetedFlightNumber = store.get('last-tweeted-flight-number');
if (flightNumber > lastTweetedFlightNumber || lastTweetedFlightNumber === undefined) {
store.set('last-tweeted-flight-number', flightNumber);
return true;
}
return false;
}
function tweet(text) {
console.log('Tweeting: ' + text);
T.post(
'statuses/update',
{ status: text },
(err, data, resp) => {
if (err) {
throw err;
}
}
);
}
function formatDateString(utc) {
let date = new Date(utc);
let dd = date.getDate();
if (dd < 10) {
dd = '0' + dd;
}
let mm = date.getMonth() + 1;
if (mm < 10) {
mm = '0' + mm;
}
let yyyy = date.getFullYear();
let hour = date.getHours();
if (hour < 9) {
hour = '0' + hour;
}
let min = date.getMinutes();
if (min < 9) {
min = '0' + min;
}
let dateString = dd + '.' + mm + '.' + yyyy + ' ' + hour + ':' + min;
return dateString;
}
function getTweetText(launch) {
let name = launch.rocket_name;
let date = formatDateString(launch.launch_date_utc) + ' UTC';
let place = launch.site_name_long;
let link = launch.reddit_campaign;
let tags = '#SpaceX #' + name.replace(/ /g,'');
let end = `${tags}` + (link?` Read more: ${link}`:``);
let templates = [
`${name} will launch on ${date} at the ${place}. ${end}`,
`${name} will take off at the ${place} on ${date}. ${end}`,
`New launch targeted for ${date}: ${name} starting from ${place}. ${end}`,
`${date} ${name} takes off at the ${place}. ${end}`
];
return templates[getRandomIntInRange(0, templates.length-1)];
}
function getRandomIntInRange(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
let listener = app.listen(process.env.PORT, () => {
console.log('Bot running on port ' + listener.address().port);
});