Introducing @whensmybus

A few weeks ago TfL put all their information from Countdown, the service they use to provide bus arrival times, online. There’s a TfL Countdown website and you can enter a bus stop name, or ID number, and find out the latest buses from the stop.

But, it’s a bit fiddly. The main website doesn’t automatically redirect you to the mobile version if you are on a phone. If you type in a location, (e.g. my local Tube station, “Limehouse Station”), you have to pick a match for the location first (from two identically-named options), then a second screen asking you to find a bus stop, and then you get the relevant times. On a phone, it’s just feels fiddly and frustrating especially when I know my phone has GPS in it and knows my location anyway.

Update/correction There is, as it turns out, the ability to find by geolocation on the mobile site, it’s just on a mobile browser I just get the main website and don’t get redirected to the special mobile site, which means I never knew about it (thanks to Ade in the comments for pointing this out).

If only there was a mobile-friendly, geolocation aware, real-time way of fetching information. Oh wait. There is. It’s called Twitter. Twitter has geolocation allowed on Tweets (if you opt in) and an API to fetch and send messages, so we have a system set up already in place for our needs.

I owe a big debt of gratitude to Adrian Short, who wrote a Ruby script to pull bus times from TfL. TfL have not officially released an API for Countdown just yet, but Adrian found it, and it’s there and accessible – providing the data in JSON format for each stop. That use got me thinking – if that data is available and can be parsed quickly and easily, why not make a Twitter bot for it?

With that, @whensmybus was born, and is now in beta. Try it out now if you like. Make sure your Tweet has geolocation turned on (for which you’ll need a GPS-capable smartphone), and send a message like:

@whensmybus 135

Or whatever bus you are looking for. Within 60 seconds, you’ll get a Tweet back with the times of the next buses for that route, in each direction, from the stops closest to your location.

Why each direction? Specifying a direction is fiddly and ambiguous; bus routes wind and twist, and some of them are even circular, so “northbound” and “southbound” are not easy things to parse. The name of your destination can have ambiguous spellings, and I haven’t yet got round to tying it in with a geocoding service like Google Maps. So, at the moment the bot simply tells you buses in both directions from the stops nearest to you. I might change this in future, once I’ve got my head around geolocation services and fuzzy string matching and all that.

It’s still beta (thanks to an early unveiling by Sian ;) ) and I plan in future to add enhancements such as the ability to use without GPS. I also need to write some proper documentation for it, and stick the source code on Github later tonight once I am home. The source code is now available on github, but do bear in mind the codebase is a bit unstable right now. So, if you are a Londoner, please do use it and tell me what you think, either on the comments below or on Twitter. @ me, don’t @ the bot – it will think it’s a request for a bus service and get confused. :) All suggestions are welcome.

(And now, some tech stuff for the more interested)

The bot is a Python script, run every minute via a cronjob. It’s quite short – 350 lines including comments for the main bit. As well as the live data API, the service also uses two databases officially provided by TfL’s syndication service for free; one is of all the routes, and one for all the bus stop locations. I converted these from CSV format to sqlite so the bot can make SQL queries on the data. TfL use OS Easting and Northing locations for the bus stops, so I have to convert the GPS longitude and latitude; I am indebted to Chris Veness and his lat/lng to OS conversion script, which I translated from JavaScript to Python; I am also now much more educated on subtleties like the difference between OSGB36 and WGS84. Finally, I use the Tweepy library to receive and send the Tweets, which is really rather excellent and saves a lot of faff. Finally, the whole project would not be possible without the ideals of open data and open source software behind it, so if you’ve written even a single line of free software, then thank you as well.

16 thoughts on “Introducing @whensmybus

  1. One small thought – how can you get round the fact that twitter won’t let you send the same tweet more than once so if I want to find out when buses are coming every day, it will only work once?

    Or have you found a way round that?

  2. This is a great idea, but i’ve not had the problems with the mobile site that you seem to?

    When i use it it asks permission to find my location, then shows me the stop nearest to me. I’m at the list of busses within 2 taps.

    I’m going to give your one a go though.

  3. @Allie Just add a different random hashtag at the end of the Tweet, the bot will ignore it but it will make your Tweet unique

    @Ade Ah – hadn’t noticed that, because I’m not directed to the special mobile site when I go see it. But now I’m in, and I’ve allowed Safari to use location data, I can see that option there. Will correct the above

    @ottaky Nice site, though it doesn’t get my location that correct when I log in on a browser on my work computer – I’m based in Clerkenwell but it seems to think I am in Bank.

  4. Not everyone has data contracts with their phones, but do have free twitter texts, would it be possible to adapt this so that it can read location from the tweet text, if the geotag field is missing?

  5. “Nice site, though it doesn’t get my location that correct when I log in on a browser on my work computer – I’m based in Clerkenwell but it seems to think I am in Bank.”

    Using a PC will return times for buses in or around your ISP – not terribly useful ;-)

    My script is designed for mobile devices – hopefully it’s a little easier to use than the official TfL website.

  6. @Edgemaster I’m going to try to develop a non-GPS version that locates based on the text. There’s lots of things that can go wrong – spelling, misidentifying places etc. – but it is a planned feature

  7. hi mate… I’m from brazil and developing a project just like yours, except I’d also like to enable people to inform bus arrival times and confront them through statistics to government-provided values.

    in our city, we’ve got gps-tracked buses but the overall system is terryfing: no precision, no user-friendliness, no mobile version and specially no API for us to build upon.

    maybe we could get in touch and share some ideas on how to grab data from people as well. what do you say?

  8. hey mate,

    i finally managed to get a small service working here in Vitória, Brazil. it works by sending a tweet with the stop number and getting the estimated times (GPS-obtained) for that stop. We called it @vixbus and, though not as fancy as yours (GPS-tracked-tweet), it is a place to start, after all.

    thanks for the motivation of whensmybus. keep the nice work.

No new comments may be added.