[dont forget to read part one]

In the spirit of staying focused, I made a bit more progress on the Metro API wrapper with respect to parsing the HTTP response. I ended up with a neat little command-line utility that tells me the latest arrival information for any Metro station I pass in by name.

For those who are curious, I examined the response text for a while, and made use of Python’s simple string manipulation functions and HTML table tags to extract the destination, line (color), and minutes-to-arrival. This has proven to be a very naive approach; the blinking BOARDING css can cause an error on the very last line [notice how I cast Minutes to an int; this was to correct a phantom newline generated by WMATA’s HTTP response]

temp = tableRow.pop(0) color.append(temp[temp.index('alt="')+5:temp.index('" />')]) temp = temp[temp.index('</td>')+5:] temp = temp[temp.index('</td>')+5:] Destination.append(temp[temp.index('<td>')+4:temp.index('</td>')]) temp = temp[temp.index('/td>')+4:] Minutes.append(temp[temp.index('<td>')+4:temp.index('</td>')]) i=i+1 print "%s line train towards %s in %d minutes." % (color.pop(), Destination.pop(), int(Minutes.pop()))

With this preliminary parsing routine, the output is good enough to start building cool things with the API wrapper, but eventually I’ll need to either address the edge cases, or import a python package to deal with the DOM elements. With the Terminal bound to a hotkey combination, simple programs that interact with resource intensive (Asana task management) or difficult-to-navigate (WMATA) sites can save developers tons of time, and keeps them out of the web browser (where distractions are made).

I used the alias command to make a shortcut to my MetroWrapper.py script, but quitting Terminal and restarting it caused this shortcut to disappear.

alias metro="python ~/MetroWrapper/MetroWrapper.py"

As it turns out, this is a setting that should be set in a shell configuration file; I use zsh so this file is .zshrc

Code is on BitBucket