Created
June 30, 2016 21:39
-
-
Save robertbenjamin/1c5c58e8c79ac2f26d9d1ab856c1b450 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // requiring just allows me to use functionality or code from an external source | |
| // bitbar is a JavaScript module I'm using to make the menubar | |
| // i just structure an array properly and pass it to bitbar and it makes the application | |
| const bitbar = require('bitbar'); | |
| // superagent is a http request library | |
| // basically it allows me to easily make a call to the OneBusAway API | |
| // im setting it equal to "request", and you can see it later | |
| const request = require('superagent'); | |
| // moment is a small library that parses crazy time formats into human readable ones | |
| // eg: 1467317117000 (unix epoch time in milliseconds) to '15 minutes away' | |
| const moment = require('moment'); | |
| // this is an API key I got from the devs at OneBusAway | |
| // it's in a different directory on my copmuter (./src/API_KEY) and NOT on Github so no one can take it | |
| // if there is none, i use the 'TEST' that still works but is limited to less requests | |
| const busAPIKey = require('./src/API_KEY.js') || 'TEST'; | |
| // this is the number of the bus stop in front of my work | |
| var busStop = 18610; | |
| // this is the URL to the OneBusAway url | |
| var busAPI = `http://api.pugetsound.onebusaway.org/api/where/arrivals-and-departures-for-stop/1_${busStop}.json?key=${busAPIKey}`; | |
| // request is superagent, just assigned to a variable so i can use it more easily | |
| // init request | |
| request | |
| // GET (shoot a request to) the API url I set in a variable above | |
| .get(busAPI) | |
| // one you got it (end).. | |
| .end(function(err, res) { | |
| // ..parse the response (res) from JSON (which is the format I get it in) to a JavaScript object | |
| var response = JSON.parse(res.text); | |
| // dig into the response to the key we want (all arrivals and departures from my stop) | |
| var trips = response.data.entry.arrivalsAndDepartures | |
| // filter out just the arrivals for my bus number (32) | |
| .filter(function(trip) { | |
| return trip.routeShortName == '32'; | |
| }) | |
| // get the scheduled and predicted arrival times for the busses that were filtered above | |
| .map(function(trip) { | |
| var newTrip = {}; | |
| newTrip.scheduledArrival = moment(trip.scheduledArrivalTime).fromNow(); | |
| newTrip.predictedArrival = moment(trip.predictedArrivalTime).fromNow(); | |
| return newTrip; | |
| }); | |
| // menu is the array I will send to bitbar at the end to create my menubar applet | |
| var menu = []; | |
| // the first arrival is the text you see in the menu without opening it | |
| menu.push({ | |
| // add a bus emoji, then the predicted arrival time | |
| // if there is no predicted time, use the scheduled time | |
| text: ':bus: ' + trips[0].predictedArrival || trips[0].scheduledArrival, | |
| // set the color to white if it's predicted, otherwise make it red | |
| // this is because a scheduled bus might not actually be an accurate time | |
| color: trips[0].predictedArrival ? 'white' : 'red' | |
| }); | |
| // this is for the rest of the arrivals after the most recent one | |
| // for each arrival AFTER the first one.. | |
| trips.slice(1).forEach(function(trip) { | |
| // ..add a point_right emoji, and then the predicted or scheduled time like above | |
| menu.push({ | |
| text: ':point_right: ' + trip.predictedArrival || trip.scheduledArrival, | |
| color: trip.predictedArrival ? 'white' : 'red' | |
| }) | |
| }); | |
| // get the big menu array i just created and pass it to bitbar to make the menubar app! | |
| bitbar(menu); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment