Sitemap
Ryo Osawa

Ryo Osawa

Software Engineer. Live in San Francisco. Coffee addict.

BART Train Schedule Reminder

3 min readOct 17, 2014

--

If you’re a BART rider, don’t you think you’d want a little app that sends you a text message when your BART train is arriving at your nearest BART station? As part of MVP (Minium-Value-Product) project at Hack Reactor, I wrote a little Node.js app that does exactly that using Twilio and BART APIs.

If you know Javascript and want to know how to do that, please read on this blog post. Since this app is written in Node, you first need to install Node. If you don’t have Node.js installed in your computer, read this instruction to install Node.js.

Once Node is installed in your computer, create a package.json to install all the dependent modules that your app needs. Create your own package.json and include ‘twilio’ and ‘xml2js’ modules.

Now all setups are done. Let’s first create a file to fetch real-time departure time using BART API. BART provides a variety of APIs and the one we’re using is called Real-time Information API. Below is an example that fetches departure information at Powell station departing Northbound to Richmond station:

http://api.bart.gov/api/etd.aspx?cmd=etd&orig=RICH&key=MW9S-E7SL-26DU-VV8V

Get Ryo Osawa’s stories in your inbox

Join Medium for free to get updates from this writer.

And, here is the response to the above request. Notice that the response sent back from BART API is XML.

<?xml version="1.0" encoding="utf-8" ?> 
<root>
<uri><![CDATA[ http://api.bart.gov/api/etd.aspx?cmd=etd&orig=RICH ]]></uri>
<date>03/30/2011</date>
<time>02:43:27 PM PDT</time>
<station>
<name>Richmond</name>
<abbr>RICH</abbr>
<etd>
<destination>Fremont</destination>
<abbreviation>FRMT</abbreviation>
<estimate>
<minutes>5</minutes>
<platform>2</platform>
<direction>South</direction>
<length>6</length>
<color>ORANGE</color>
<hexcolor>#ff9933</hexcolor>
<bikeflag>1</bikeflag>
</estimate>
<estimate>
<minutes>20</minutes>
<platform>2</platform>
<direction>South</direction>
<length>6</length>
<color>ORANGE</color>
<hexcolor>#ff9933</hexcolor>
<bikeflag>1</bikeflag>
</estimate>
</etd>
<etd>
<destination>Millbrae</destination>
<abbreviation>MLBR</abbreviation>
<estimate>
<minutes>Leaving</minutes>
<platform>2</platform>
<direction>South</direction>
<length>10</length>
<color>RED</color>
<hexcolor>#ff0000</hexcolor>
<bikeflag>1</bikeflag>
</estimate>
</etd>
</station>
<message />
</root>

Now, here is a module called bart.js code that uses BART API and parses the response into JSON in Node.

var qs = require(‘querystring’);
var parseString = require(‘xml2js’).parseString;
var request = require(‘request’);
var _ = require(‘underscore’);
var bartApiEndpoint = ‘http://api.bart.gov/api/etd.aspx?cmd=etd&key=MW9S-E7SL-26DU-VV8V&';var fetchDepartureInfo = function(orig, dir, dest, cb) {
var querystring = qs.stringify({
orig: orig,
dir: dir
});
var url = bartApiEndpoint + querystring;
console.log(‘Sending a request to ‘, url);
request(url, function (err, resp, body) {
if (err) throw err;
parseString(body, function (err, json) {
var estimates = _.filter(json.root.station[0].etd, function (estimate) {
if (estimate.abbreviation[0] === dest.toUpperCase()) {
return estimate.estimate;
}
});
if (estimates && estimates[0] && estimates[0].estimate) {
estimates = estimates[0].estimate;
var minutes = _.map(estimates, function (estimate) {
return estimate.minutes[0];
});
cb(minutes);
}});
});
};
exports.fetchDepartureInfo = fetchDepartureInfo;

Next, let’s create another module to send you a simple text message using Twilio SMS API. If you don’t have your Twilio account, you can create a trial account from their site. The overall signup process is very easy. After you verify your phone number, you’re good to go. To send a text message, you can simply make a POST request. Here is a module called twilio.js to send a text message.

var _ = require(‘underscore’);
var accountSid = process.env.TWILIO_ACCOUNT_ID;
var authToken = process.env.AUTH_TOKEN;
var from = process.env.PHONE;
//require the Twilio module and create a REST client
var client = require(‘twilio’)(accountSid, authToken);
exports.sendText = function(to, dest, minutes, done) {minutes = _.map(minutes, function(minute) {
return minute === ‘Leaving’ ? ‘now’ : minute;
});
var body = ‘Your train (‘ + dest + ‘) ‘ +
‘is arriving at Powell station in [‘ +
minutes.join(‘, ‘) +
‘] minutes.’;
var message = {
to: to,
from: from,
body: body
};
client.messages.create(message, function(err, message) {
if (err) {
console.error(err);
} else {
console.log(message.sid);
}
done();
});
};

Using bart.js and twilio.js, you now can create a simple Node.js app to send you a quick reminder about your BART schedules. Here is the code that does this:

var bart = require(‘./lib/bart’);
var twilio = require(‘./lib/twilio’);
bart.fetchDepartureInfo(job.station, job.direction, job.destination, function (estimates) {
twilio.sendText(job.phone, job.destination, estimates, done);
});

There you have it. If you want to check out a fully working app, please check out my repo in GitHub. Thanks for reading my blog post.

--

--

Ryo Osawa
Ryo Osawa

Written by Ryo Osawa

Software Engineer. Live in San Francisco. Coffee addict.