When’s the last tram home in Manchester?

As seen on Buzzfeed

Update: Just in time for the Easter bank holiday weekend I’ve added a function to the site’s code which tests the current date against an iCal file of English bank holidays provided by the GOV.UK website. There’s no screen scraping this time — the ICS file of bank holiday dates is read directly. The file contains a string representing the start dates for each bank holiday, and these strings are compared to the current server time. If it is a bank holiday, then the website will show times for a bank holiday service, which generally means an earlier finish if you’re relying on the Metrolink to get home.

Update: At the end of February 2014 I added a ‘pint countdown’ timer, an idea suggested by Rachael Burns at a ManchesterTechNights event. The timer uses Moment.js and Snap.svg to display an animated, vector graphic representation of the number of drinks the user can have before the last tram from their chosen stop. A tweet including a screenshot of the new feature went somewhat viral (compared to my usual tweets) amongst the webby crowd, and was picked up upon by Buzzfeed’s political editor, who posted it to Buzzfeed UK. For a period of time the article appeared on the homepage of Buzzfeed for UK users. Here’s a Storify of some highlights of the social media response.

Fairly sure I’ve built the only public transport app that gives you the time till the last tram in the units of pints pic.twitter.com/cfwRTiVg7D
— Phil Wolstenholme (@philw_) February 27, 2014
Man creates transport app that measures time in pints of beer http://t.co/5Ei02wwsbu
— BuzzFeed UK (@BuzzFeedUK) March 1, 2014

A video of the timer in action can be found here, though it’s worth visiting http://lasttramfrom.co.uk in the evenings to see it live as the text shown to the user changes as their last tram approaches.

A couple of weekends ago I put together a quick little website to tell you when the last tram home is from any station in Manchester’s Metrolink tram system.

The site shows the time of the last tram to each terminus on the network, and it shows the right times for each day of the week. It doesn’t yet recognise bank holiday days, but I could build this in the future using the ICS calendar feed from the GOV.UK website.

Here’s how it looks:

The technical side of it is pretty simple. A cron job triggers a script that extracts the timetable information from the Metrolink website. The timetable data is available as part of a bulk public transport timetable package from the DataGM website, but I decided it would be more simple to scrape the data rather than extract it from this bundle. Extracting data from a site’s DOM always makes me feel like a bit of a hacker…. I’ll just have to watch out for changes to the Metrolink website that would break the scraper.

To speed up coding the storage and retrival of the data I used a MySQL framework called Medoo — it had been a while since I wrote to a MySQL database directly rather than via Drupal or WordPress. The front end of the site is all Bootstrap, which saved a lot of time, and had the bonus of making the site responsive without much additional work.

Each stop has its own crawlable URL, with the station name in the title, URL path, and H1 tag — so I’m interested to see how well the site performs in search engine result pages once a few more places link to the project.

Originally published at wolstenhol.me on December 11, 2013.

Show your support

Clapping shows how much you appreciated Phil Wolstenholme’s story.