-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmbta.js
36 lines (30 loc) · 944 Bytes
/
mbta.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
const mbtapi = require('mbtapi').create({
apiKey: process.env.MBTA_API_KEY,
});
function arrivalTimes(stopId) {
return mbtapi.predictionsByStop(stopId).then(function (predictions) {
const trips = predictions.mode[0].route[0].direction[0].trip;
return trips.map(trip => {
var predictionDate = new Date();
predictionDate.setSeconds(predictionDate.getSeconds() + Number(trip.pre_away));
return predictionDate;
});
});
}
export const massAveEta = { forestHills: [], oakGrove: [] };
function updateMassAveEta() {
arrivalTimes('70013').then(times => {
massAveEta.oakGrove = times;
}).catch(() => {
massAveEta.oakGrove = null;
});
arrivalTimes('70012').then(times => {
massAveEta.forestHills = times;
}).catch(() => {
massAveEta.forestHills = null;
});
}
// Update the eta on an interval
updateMassAveEta();
const interval = Number(20000);
setInterval(updateMassAveEta, interval);