Yes, she could use a smartphone to see buses on an MTA map. Yes, she could get location information by texting the code for her stop (she lives near the route). But none of this was simple enough.
"I want a phone number that will TELL me when the next bus is coming," she said.
I'll have it for you by the end of the day, I replied.
It was a bit of a gamble. More of a challenge to myself, really. But if "before midnight" counts as the end of the day, then I succeeded. Try it:
Dial 646-480-7193. When prompted, enter 308333 or any of the bus stop codes for the B63 line.
It's not journalism, but it is a working example of how someone can take public data and turn it into a useful tool, quickly. And at almost no cost.
How I Did It
First, I wrote a little program in Sinatra that sends a 6-digit bus stop code to the NYC Metropolitan Transit Authority API -- or application programming interface -- whenever someone hits my program's web address. (The API is a public portal to the live bus data. All you need is a little programmer know-how and a free key from the MTA. The technical details are right here.)
The API sends back 77 lines of information about the stop and the buses approaching it, including the one detail I want:
<StopsFromCall>3</StopsFromCall>
The next bus is three stops away. I use a Ruby tool called Nokogiri to find and extract just this number, which I drop into an amazingly simple web page. The page's entire output looks like this:
The next bus to arrive at 14th Street and Fifth Avenue heading north is 3 stops away.
That accomplished, I bought a phone number from Twilio for $1 a month, and $0.01/call. Twilio provides a telephone connection to web-based programs, which I first heard about during a demo at a TimesOpen event.
I set my new phone number to hit my program's URL whenever someone calls. By wrapping the text in special tags, Twilio recognizes it as a cue to talk:
<Response>
<Say>
The next bus to arrive at 14th Street and Fifth Avenue heading north is 3 stops away.
</Say>
</Response>
Voila.
I later used Twilio's <Gather> tags to build what's essentially a web form to capture digits entered on a phone for any bus stop. I also added error catching, for when no buses are coming, and programmed it to announce the location of the following bus if the next bus is arriving.
Some Hiccups
Turns out that Twilio reads text a little too fast to be understood on an NYC street corner. So I rewrote the output to introduce pauses:
The next bus to arrive at ... 14th Street ... and ... Fifth Avenue ... heading ... north ... is ... 3 ... stops ... away.
Also, there's a bug in the API system that sends back server errors in certain conditions. Word is that the MTA has actually fixed this on their development servers, and that fix is being pushed to the public system pretty soon.
More to Come
I have a few enhancements up my sleeve, which should be done in a week or two. If you'd like to know when new tricks roll out, drop a note with "Bus Talk" in the subject line to john (at) johnkeefe.net. I'd love to hear your thoughts on how it could work better, too.
I'll also release the source code after those nifty updates. Let me know, too, if you're interested in that.
Photo by jbrau13 on flickr.