-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
198 lines (170 loc) · 4.3 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
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
const Axios = require('axios');
const CTable = require('console.table');
const Fs = require('fs');
const Mailer = require('nodemailer');
const Path = require('path');
const Config = require('./config')
const Stations = require('./stations');
/**
* Abfrage der Abfahrtszeiten von der API.
*/
async function getDeparture() {
try {
const options = {
params: {
limit: Config.LIMIT
},
data: {
station: Config.DEPARTURE,
offset: Config.OFFSET
}
}
const result = await Axios.get(Config.URL, options);
return result.data;
} catch (err) {
console.log(err);
return null;
}
}
/**
* Status der abgefragten Abfahrtszeiten.
* @param {Array} departures
* @returns {Boolean}
*/
function isDelayed(departures) {
if (!departures) return false;
for (const el in departures) {
if (el.status !== "puenktlich") {
return true;
}
}
return false;
}
/**
* Liefert die Abfahrtszeiten im HTML-Format zurück.
* @param {Array} departures
*/
function getHtml(departures = []) {
const station = Stations.find(st => st.ds100 === Config.DEPARTURE);
let table = "";
if (departures) {
departures.forEach((el) => {
let trStyle = "";
if (el.departure_time === Config.DEPARTURE_TIME) {
trStyle = "background-color: lightblue;";
}
let delayStyle = "font-weight: bold;";
if (el.delay > 0) {
delayStyle += "color: red;";
} else {
delayStyle += "color: green;";
}
table += `
<tr style="${trStyle}">
<td>
${el.departure_time} <span style="${delayStyle}">+${el.delay}</span>
</td>
<td>
${el.direction}
</td>
<td>
${el.transport}
</td>
<td>
${el.cancellation ? '<b>Ausgefallen</b>' : el.status}
</td>
</tr>
`;
});
}
const html = `
<html>
<head></head>
<body>
<h1>${station ? station.name : Config.DEPARTURE}</h1>
<table style="table-layout: fixed;">
<thead>
<th style="width:100px;">Abfahrt</th>
<th style="width:300px;">Zielbahnhof</th>
<th style="width:100px;">Zug</th>
<th style="width:100px;">Status</th>
</thead>
<tbody>
${table}
</tbody>
</table>
</body>
</html>
`;
return html;
}
/**
* Speichert die Abfahrtszeiten als HTML-File.
* @param {String} html
*/
function saveHtml(html) {
if (!Fs.existsSync(Config.DIRECTORY)) {
Fs.mkdirSync(Config.DIRECTORY);
}
const filePath = Path.join(Config.DIRECTORY, Config.FILENAME);
Fs.writeFileSync(filePath, html);
}
/**
* Versendet eine E-Mail mit den Ergebnissen.
* @param {String} html E-Mail-Text
* @param {Boolean} delayed verspätet?
*/
async function sendMail(html, delayed) {
const transportOptions = {
host: Config.HOST,
port: Config.PORT,
auth: {
user: Config.USER,
pass: Config.PASS
}
};
let subject = 'pünktliche';
if (delayed) {
subject = 'verspätete';
}
const mailOptions = {
from: Config.FROM,
to: Config.TO,
subject: Config.SUBJECT.replace('##STATUS##', subject),
html: html
};
const transporter = Mailer.createTransport(transportOptions);
await transporter.sendMail(mailOptions);
}
/**
* Darstellung der Ergebnisse als Tabelle auf der Konsole.
* @param {Array} departures
*/
function printAsTable(departures = []) {
const table = departures.map((el) => {
return {
Abfahrt: `${el.departure_time} +${el.delay}`,
Zielbahnhof: el.direction,
Zug: el.transport,
Status: `${el.cancellation ? 'Ausgefallen' : el.status}`,
}
});
console.table(table);
}
/**
* MAIN
*/
async function main() {
const zeiten = await getDeparture();
const html = getHtml(zeiten.departures);
if (Config.PRINT_CONSOLE) {
printAsTable(zeiten.departures);
}
if (Config.SAVE_HTML) {
saveHtml(html);
}
if (Config.SEND_MAIL) {
await sendMail(html, isDelayed(zeiten.departures));
}
}
main();