Introducing @whensmybus

3 October 2011

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.


32 Responses

Yeah, erm… sorry about that :-)

It’s very brilliant.

[...] some people have created iPhone apps including Next Bus London, Bus Checker and the awesome Twitter account which tweets you back with your bus arrival [...]

Allie

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?

Ade Bradley

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.

@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.

Edgemaster

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?

“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.

@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

[...] It’s been nine days since @whensmybus was released and the feedback has by and large been positive. It’s not all been plain sailing – the [...]

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?

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.

[...] ([email protected]) e twitter (@vixbus), entramos em contato com o o Chris Applegate – do @whensmybus, do Reino Unido – e com o Metzen, de Vitória, que criou o excelente No Ponto e iniciamos a [...]

@Marcos – that’s absolutely fantastic! So pleased to see what I’ve inspired

[...] year I introduced a service called @whensmybus, a Twitter bot that you could ask for real-time bus times from anywhere in London. It proved to be [...]

[...] ([email protected]) e twitter (@vixbus), entramos em contato com o o Chris Applegate – do @whensmybus, do Reino Unido – e com o Metzen, de Vitória, que criou o excelente No Ponto e iniciamos a [...]

It’s going to be ending of mine day, but before finish I
am reading this great piece of writing to increase my knowledge.

Also visit my web-site – care home audit report (nanceealbert.mywapblog.com)

I’ve been browsing online more than 3 hours today, yet I
never found any interesting article like yours.

It is pretty worth enough for me. In my view, if all website owners and bloggers made good content
as you did, the internet will be much more useful
than ever before.

Hi there, after reading this remarkable article i am also delighted to share my know-how here
with friends.

Hey! Quick question that’s completely off topic. Do you know
how to make your site mobile friendly? My web site looks weird when browsing from my iphone4.
I’m trying to find a theme or plugin that might be able to resolve this issue.
If you have any recommendations, please share.
Thanks!

Hi everyone, it’s my first go to see at this web site, and piece of writing is truly fruitful designed for me,
keep up posting such articles.

Right now it sounds like Expression Engine is the preferred blogging platform available right now.
(from what I’ve read) Is that what you’re using on your blog?

Also visit my weblog; website

Quality posts is the key to invite the viewers to pay a quick
visit the web site, that’s what this web page is providing.

Feel free to visit my web site … download youtube videos mp4 (http://www.youtube.com/watch?v=0hTj5Qw694I)

Hi there! I simply wish to offer you a huge thumbs up for your excellent information you’ve got here on this post.

I’ll be returning to your web site for more soon.

Hi, i feel that i noticed you visited my site thus i got here to go back the choose?.I am attempting to in finding things to improve my site!I assume its ok to use a few of your concepts!!

Here is my web blog :: download youtube downloader latest version

I am not sure where you are getting your information, but great topic.
I needs to spend some time learning more or understanding more.
Thanks for great info I was looking for this info for my
mission.

I’m amazed, I have to admit. Rarely do I come across a blog that’s both equally educative and interesting, and let me tell you, you’ve hit the nail on the head.

The problem is something that not enough men and women are speaking intelligently about.

I am very happy I found this during my hunt for something relating
to this.

Here is my web page – free youtube downloader

What’s ?p to all, ho? is all, I think every on? is
getti?g mo?? from this web page, and ?ou? views ar? nice for ne? visitors.

Hello, I do think your web site may be having internet browser compatibility issues.
Whenever I look at your blog in Safari, it looks fine however, if opening in I.E., it has some overlapping issues.

I merely wanted to provide you with a quick heads up! Aside from that, excellent website!

When I originally left a comment I seem to have clicked on the -Notify me when new
comments are added- checkbox and from now on whenever a comment is added I
recieve 4 emails with the same comment. Perhaps there is an easy method you are able to remove me from that service?
Many thanks!

my web page; how to download a song from youtube

I am sure this piece of writing has touched all the internet viewers, its really really pleasant piece of writing on building up new website.

Here is my homepage; youtube to mp4 (http://www.dicastia.com/2011/07/5765-plants-vs-zombies-e.html)

Spot on with this write-up, I honestly think this website needs a lot more attention.
I’ll probably be back again to read more, thanks for the
information!

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>