-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·119 lines (103 loc) · 3.85 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
const fs = require('fs');
const axios = require('axios');
const dateFormat = require('dateformat');
const moment = require('moment')
const PushBullet = require('pushbullet');
const CONFIG_FILE_NAME = 'config.json';
const LAST_DELAYS_FILE_NAME = 'last-delays.json';
const DEFAULT_ENCODING = 'utf8';
const MINUTES_DELTA = 30;
console.log("Calling API...")
fs.readFile(CONFIG_FILE_NAME, DEFAULT_ENCODING, (err, data) => {
if (err) throw err;
const configData = JSON.parse(data)
if (!fs.existsSync(LAST_DELAYS_FILE_NAME)) {
createInitialDelayCacheFile(configData)
}
JSON.parse(data).forEach(entry => {
if (isConfiguredWeekday(entry))
sendDelays(entry.from, entry.to, entry.time)
})
function isConfiguredWeekday(entry) {
return entry.weekdays.includes(dateFormat(new Date(), 'ddd').toLowerCase());
}
});
function sendDelays(from, to, time) {
let timeToCheck = moment(time, 'HH:mm');
let minutesDifference = timeToCheck.diff(moment.now(), 'minutes');
if (minutesDifference > MINUTES_DELTA || minutesDifference < -MINUTES_DELTA) {
return;
}
axios.get('http://transport.opendata.ch/v1/connections', {
params: {
from: from,
to: to,
date: dateFormat(new Date(), 'yyyy-mm-dd'),
time: time,
limit: 1
}
}).then(response => {
let delay = response.data.connections[0].from.delay;
if (delay) {
fs.readFile(LAST_DELAYS_FILE_NAME, DEFAULT_ENCODING, (err, data) => {
if (err) throw err;
let lastDelays = JSON.parse(data);
let hasNewDelay = lastDelays.some(connectionEntry => {
return isSameConnection(connectionEntry, from, to, time) &&
connectionEntry.lastDelay != delay;
});
if (hasNewDelay) {
if (err) throw err;
let pusher = new PushBullet(process.env.PUSHBULLET_API_KEY);
pushMessage(pusher, {
from: from,
to: to,
time: time,
}, {
delay: delay,
time: response.data.connections[0].from.prognosis.departure,
platform: response.data.connections[0].from.prognosis.platform,
arrival: response.data.connections[0].from.prognosis.arrival
});
lastDelays.forEach(connectionEntry => {
if (isSameConnection(connectionEntry, from, to, time)) {
connectionEntry.lastDelay = delay;
}
});
fs.writeFile(LAST_DELAYS_FILE_NAME, JSON.stringify(lastDelays), DEFAULT_ENCODING, (err) => {
if (err) throw err;
});
}
});
}
});
}
function isSameConnection(connectionEntry, from, to, time) {
return connectionEntry.from == from &&
connectionEntry.to == to &&
connectionEntry.time == time;
}
function pushMessage(pusher, connection, prognosis) {
pusher.note(
{},
`${connection.from} -> ${connection.to} Delay at ${connection.time}`,
`Delay is ${prognosis.delay} min\n` +
`Prognosis: ${prognosis.time}\n` +
`Platform: ${prognosis.platform}\n` +
`Arrival: ${prognosis.arrival}\n`
,
function (error, response) {
if (error) throw error;
});
}
function createInitialDelayCacheFile(configData) {
const delayFormat = configData.map(c => {
return {
from: c.from,
to: c.to,
time: c.time,
lastDelay: null
}
})
fs.writeFileSync(LAST_DELAYS_FILE_NAME, JSON.stringify(delayFormat))
}