Catching the bus to class with Python
For the past month I have been studying abroad in London. One of the first things I noticed when arriving here was how much better the public transportation system was compared to what I am used to in Philadelphia. Whether you are taking the tube or the bus it is clean, quick, and the easiest way to get around the city. It took a few weeks to become fully acclimated with all the stops and routes but when everything starts connecting it becomes very easy to get around.
Depending on traffic my commute to class is about a fifteen-minute bus ride from my dorm. A few days ago while waiting for the bus in the morning and looking at the LED board at the stop which displays the upcoming buses and when they are due to arrive I began to wonder where this information was coming from. Surely enough after some searching I came across the Transport for London API. The API has quite a few feeds for the buses, tube, and even the Barclays bike share program. After some reading I set out to optimize my morning commute to class.
After some fiddling around I was able to get a JSON response that included all of the data sent to the bus stop outside my building. The board at this stop has been offline quite a few times the past week so it was nice to see that its a problem with the board and not the API.
Now that I had access to all of the data I needed to narrow it down to just the bus I was interested in taking to class in the morning. This is where python comes in, I wrote a quick function that takes in the JSON from the API and then parses out just the bus I take to class.
import requests def getTime(): r = requests.get('http://countdown.tfl.gov.uk/stopBoard/58984') json_result = r.json() all_stops = json_result['arrivals'] my_stops =  for x in all_stops: if x['isRealTime']== True and x['destination'] == 'White City': x = str(x['routeName']) + ' ' + str(x['destination']) + ' ' + str(x['estimatedWait']) my_stops.append(x) print my_stops
It worked perfectly and only took ten lines of code. I decided to take the idea a step further and wrap it in a quick flask application. I came up with this single page app that displays all of the upcoming buses.
For now it is still just local on my laptop but has been working well saving me some time waiting outside in the morning. This was a quick and fun learning experience that helped me learn quite a bit more about flask applications.
If you have any questions, feedback, advice, or corrections please get in touch with me on Twitter or email me at email@example.com.