-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
executable file
·62 lines (49 loc) · 2.07 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
#!/usr/bin/env /usr/local/bin/node
// <bitbar.title>BitBus</bitbar.title>
// <bitbar.version>v1.0</bitbar.version>
// <bitbar.author>Robert Benjamin</bitbar.author>
// <bitbar.author.github>robertbenjamin</bitbar.author.github>
// <bitbar.desc>A BitBar menubar app for seeing when the next bus arrives at my work.</bitbar.desc>
// <bitbar.image>http://i.imgur.com/zjuQRQF.png</bitbar.image>
// <bitbar.dependencies>node,superagent,momentjs</bitbar.dependencies>
// <bitbar.abouturl>https://github.com/robertbenjamin/bit-bus</bitbar.abouturl>
const bitbar = require('bitbar');
const request = require('superagent');
const moment = require('moment');
const busAPIKey = require('./src/API_KEY.js') || 'TEST';
let busStop = 18620;
let busAPI = `http://api.pugetsound.onebusaway.org/api/where/arrivals-and-departures-for-stop/1_${busStop}.json?key=${busAPIKey}&minutesAfter=70`;
let menuColor = bitbar.darkMode ? 'white' : 'black';
let warningColor = '#ff4136';
request
.get(busAPI)
.end((err, res) => {
if (err || !res.ok) bitbar([{ text: ':bus: no internet :(', color: warningColor }]);
let response = JSON.parse(res.text);
let trips = response.data.entry.arrivalsAndDepartures;
if (trips.length === 0) {
bitbar([{ text: ':bus: no more trips', color: warningColor }]);
} else {
let myTrips = trips
.filter(trip => trip.routeShortName === '32')
.map(trip => {
let newTrip = {};
newTrip.scheduledArrival = moment(trip.scheduledArrivalTime).fromNow();
if (trip.predictedArrivalTime) newTrip.predictedArrival = moment(trip.predictedArrivalTime).fromNow();
return newTrip;
});
let menu = [];
menu.push({
text: `:bus: ${myTrips[0].predictedArrival || myTrips[0].scheduledArrival}`,
color: myTrips[0].predictedArrival ? menuColor : warningColor
});
menu.push(bitbar.sep);
myTrips.slice(1).forEach(trip => {
menu.push({
text: `:point_right: ${trip.predictedArrival || trip.scheduledArrival}`,
color: trip.predictedArrival ? menuColor : warningColor
})
});
bitbar(menu);
}
});