What is this?
This python script downloads timetables (weekday, Saturday, Sunday) from Metlink for Melbourne's public transport system, and writes them out in an XML format. It was designed to be used with ADBTransit, a Pocket PC app for showing public transport timetables, so the XML schema was dictated by that.
Why?
Because silly Metlink doesn't have a mobile-accessible version of their website, and even if they did, the exorbitant mobile data rates here don't justify it. Using this script and ADBTransit, I get to have the timetables for my lines ready for reference anytime. Of course, I still need to check the Metlink website occasionally for timetable changes and updates, but it's much better. No nice 'journey planner' feature either.
To reiterate - this script only downloads the normal weekday, Saturday and Sunday timetables. It does not know about holiday timetables, or route updates.
It'd be cool if Metlink offered a XML web service to their data, make my life a lot easier too, but of course, government agency and latest-and-greatest tech don't match. They haven't even gotten their heads around RSS for the news and service updates! I wrote a post on Metlink's web shortcomings, a while ago, here.
I'm also thinking of different ways of presenting and using this data, to make it easier to fit public transport into our lives. First up is probably writing a nice desktop app, and a mobile app better suited to Metlink's intricacies. As it is an XML format, you can do cool things with the timetable data too - some kind of mash up?
I'm planning on improving the XML schema too - it doesn't suite the Metlink data quite right. Is there even a public transport timetable XML standard anyway?
What does Metlink think?
No idea, but if anything, I'm doing them a favour I reckon. All that said, this script is not endorsed or associated with Metlink in any way, and the data retrieved remains the copyright of Metlink.
Ok, so how do I get it and use it?
First off, I didn't write this script for ease-of-use. It was a nice exercise to learn python (a programming language). I will be coming out with a nicer app with this feature in the future though, because it's something Metlink lacks that should exist.
But if you still want it and are feeling adventurous, keep reading. Unlike most of my creations, this should work on all platforms python runs on, including Linux and Mac OSX; but I've only tested it on Windows Vista.
- Make sure you have Python 2.5 installed (a previous version may work, but not tested). If you're running Linux or Mac OSX, you should be fine. Open up the terminal and type in 'python --version' (without the quotes) and press ENTER to check. If it isn't there, use your package manager to install it in Linux, or your Mac CDs for a Mac.
If you're running Windows, you most likely do not have Python installed. To check, open up the Command Prompt (start -> run -> type in cmd -> press ENTER), then type in 'python --version' (without the quotes), followed by ENTER to check. If it's not recognised, download Python 2.5 from here, then double-click to install it.
- Now you need to install the XML parsing library (libxml2) and the python bindings (lxml). Sounds complicated, but it isn't.
- Go here, and download the ez-setup.py file (link in first sentence of section) on to your desktop.
- Run ez-setup.py by double-clicking it or by using the command prompt, change to your desktop directory, and type 'python ez-setup.py' (without quotes). It will download and install easy-install, a tool that helps install modules into python. You can delete ez-setup.py afterwards.
- In your terminal/command prompt, type 'easy_install lxml' (without quotes), and it'll download and install more stuff. If you get an unrecognised command error on Windows, type in 'cd\python25\scripts' (without quotes), press ENTER, and try again. If you're using Windows Vista, you'll need to open up the command prompt in adminstrator mode - start -> type in 'command prompt' (without quotes) in the search box -> right-click on Command Prompt -> select Run As Administrator.
- Now download the script I wrote, ttdownload.py, using the link below. Save the script in a new folder called 'metlink' (without quotes) on your desktop.
- Go back to your terminal/command prompt, and change to the new folder you created (on Windows XP: cd\documents and settings\username\desktop\metlink; on Windows Vista: cd\users\username\desktop; on Linux: cd /home/username/desktop; replacing username with your username).
- Type in 'python' (without quotes), and press ENTER.
- Once loaded, type in 'import ttdownload' (without quotes), and press ENTER.

- In order to tell the script which timetable to download, you need to find out the line ID. Open up your web browser and navigate to Metlink's website.
- On the right side, find the 'Transport Search' box, and type in the route you want to download.

- If you get a results page, select the route you wanted.
- Now on the route page, select the direction of the timetable you want to download.

- Look at the URL of the current page - it should look like this: http://www.metlinkmelbourne.com.au/line/view/2184. The line ID in this case is 2184 (highlighted in green below).

- Now return to the terminal/command prompt you were using in steps 5 and 6, and type in 'ttdownload.start(line_id)' (without quotes) and press ENTER, where line_id is the number you discovered earlier. It will work out the line information, then start downloading the timetables, before saving it as an XML file in the current folder.

- Repeat steps 7 to 12 for each line you want to download. Remember, each route has two lines, one for each direction, so you have to do these steps twice for each route.
- Each of the XML files produced when you download a line timetable is openable in ADBTransit, however it isn't very useful to have a different file for each line. To combine them into one file, type in 'ttdownload.combine_tml()' (without quotes) and press ENTER. The resultant file is called combined.tml in the folder.

- Now, the XML format isn't the most space-saving format out there, so you might want to make it smaller before putting it on your phone (also more efficient). ADBTransit can read both the XML format (.tml) and a binary format (.tdb). The binary format however, is much smaller and efficient. So to convert it over on your desktop, download ADBTransit Desktop, unzip it and load it up, click File -> Load Transit, change the file type to the XML format, navigate to your metlink folder and open up the combined.tml file (as in diagram below), then click File -> Save As and save it in .tdb format.

- Now copy the combined.tdb or combined.tml file over to your Pocket PC's My Documents folder, fire up ADBTransit on your Pocket PC and load it!
- To close the terminal/command prompt, type in 'exit()' (without quotes) to exit python, then type 'exit' (without quotes) to close the window.
Download my script
Metlink Timetable Downloader python script
As usual, let me know what you think, any issues you have, constructive criticism and anything else. If you have any improvements to the script, let me know too - this is my first foray into python.