Sunday, April 27, 2008

How to find the cheapest train tickets by website scraping

One of my niggles with train timetable websites is that it can be very tricky to find the cheapest train ticket if you want to ask questions like "What is the cheapest ticket if I could travel on Sunday, Monday or Tuesday next month?" or even worse "What would be the cheapest 1st class ticket if can travel any weekend in June?". If you phone up a train hotline they would probably give up (and you are normally paying 10p/min while they try) or they would guess and might miss the best deal.

My solution was to use iMacro. This is a macro recording plugin for Firefox that can automate interactions with a website. I can now set up an initial script and set the browser to look for the cheapest possible ticket.

For example, below are the results for London to Nottingham travelling in the mornings (9am to 1pm) from 3rd to 5th May searching for the cheapest direct train ticket in any 2 hour slot. It took 20 seconds to configure the script (after taking a day to develop it) and my computer took 5 minutes to run the search as it has to enter data on the timetable website 12 times (3 days x 2 time slots x two classes of travel) and wait each time for the website to refresh with new data. I chose the Virgin Trains website as it seemed to have quite a nice interface which made it easy to pick the first radio button to find the cheapest ticket and seemed to return the most comprehensive results (I kept finding discrepancies on other sites, which surprised me as I assumed they shared the same underlying data).
Results for London to Nottingham 
[click to show longer example]

Searched on Sun, 27 Apr 2008 12:44:50 GMT

Saturday 3 May 2008
Slot | Time | Std | Time | 1st
09:00 | 08:55 | £ 15.00 | 08:55 | £ 18.00
11:00 | 12:55 | £ 15.00 | 11:55 | £ 18.00

Sunday 4 May 2008
Slot | Time | Std | Time | 1st
09:00 | 09:00 | £ 15.00 | 09:00 | £ 18.00
11:00 | 11:00 | £ 15.00 | 11:00 | £ 18.00

Monday 5 May 2008
Slot | Time | Std | Time | 1st
09:00 | 08:55 | £ 11.00 | 08:55 | £ 18.00
11:00 | 10:55 | £ 11.00 | 10:55 | £ 18.00
I have tested with very long queries taking more than an hour to run and the only problem may be time-outs from the website. In these cases iMacro appears to lock but by pressing the Pause/Resume button will continue without losing data. Note it is best not to use the browser when running an iMacro script but I have successfully used a different browser (Safari) at the same time without any problems.

The macro is run from a javascript file in iMacro which calls iMacro commands (previously I passed variables to an iim file but it seems easier to put it all in one file) and I tweak the "Set up query" section of the javascript to query a particular train route, this could easily be set from user prompts. For the time being it is always cheapest to get two singles rather than a return so I've only bothered writing the macro for a single. Note that the slot hours (set in array myhour) are based on the maximum number of hours that Virgin trains will display in the particular train route. For London/Nottingham this is 2 hours and for London/Redruth this is 3 hours. Nice bonus functionality I've included are returning the date in long text form from the Virgin site and keeping the iMacro code display updated telling you the estimated time left.

The latest version opens a results window to show the data in an html table. You can use iimDisplay() but it is limited to a tiny window and was (at the time of writing) not resizeable by scripting.

Here's the source code. Note that the source code is not word wrapped and long lines may appear truncated, but if you cut & paste to your editor you should see all the text.

JavaScript source code Virgin-Trains.js (click to show)

Monday, April 14, 2008

How to use free GPS on a K750i mobile phone

Last year I bought a tiny bluetooth GPS device off ebay for £20. I set up my laptop with it and we managed to drive around Germany using a copy of Autoroute showing our planned route, current GPS coordinates and it even shouted helpful instructions such as "turn left in 400 meters"! We planned our route in the hotel the night before and as we are often looking for poorly road-signed ancient sites it is terribly handy to have the confidence of the computer pointing you down an empty track rather than guesswork.

I did get the GPS recognized by my Sony Ericsson K750i (due to a previous hack it runs using the W800i firmware) and used some trial software to show coordinates but little else. I spent some time yesterday further researching and managed to get it working quite beautifully yesterday.

The simple free java application is called TrekBuddy and it has an associated wiki site. After connecting to the GPS via bluetooth, the application shows where you are on a scrolling map that you define and store locally on the memory stick. I have it working with an almost A to Z street level map of London (spanning West Hammersmith through to Catford). You can load other maps as you go along so a later improvement would be to install a higher level road map of the UK. In the case of my map of London it was only 2mb in size (I use a 4gb stick!) so it would be no issue to carry around several maps.

I had to spend some time tweaking the configuration, in particular the initial location of the maps had to be entered by hand to point to the memory stick ("file:///E:/other/mapdata/london") and it took a while to work out that I needed to tick the option to enable a large atlas in order to display anything. The only other hard part was preparing the map, luckily someone has worked all this out and I imported maps from Open Street Map (setting default datum as WGS 84) using the tool listed on the wiki. I also managed to import using the rather neat Google Maps to TrekBuddy tool on the same page. The latter being slightly more limited as to the overall size of map but with the benefits of a better quality street map (just right for a pedestrian map in Central London).

Thursday, February 14, 2008

Talktalk (Carphone) compromised my password!

I had been getting mysterious emails to someone else that I thought was spam nonsense. This morning, after getting a few about membership of the same club, I emailed the club secretary back suggesting they had the wrong email (assuming some sort of typo).

This afternoon Talktalk Technical Support phoned me to apologise for giving out my password! Over Christmas my email had stopped working and I called to check why and they "reset" my account somehow and I agreed a new default password. I tried logging in afterwards to the online account manager but had lost the password they send you in the post to set up your account and after hanging on the 0870 number for a couple of minutes I gave up and left the password as their default random looking one. It turns out that a new customer in January had been given my email account details and the password as their account. Consequently that customer has been logging into my email account over the last few weeks, probably thinking that email to me was spam and sending email from my address!

I can't believe that a company can be so cavalier about customer privacy. Needless to say, I've gone online and changed the password again from the new one agreed on the phone.

Lessons learned:
  1. Never send anything confidential by email unencrypted (at least put a password on a document)
  2. Always change passwords from defaults or if given temporarily to someone else
  3. Stick to more secure mixed numbers and letters passwords of a decent length (8 characters or more), a tool like Sxipper can be helpful if you have to keep switching between accounts
  4. If it's important then digitally sign it
  5. Expect companies (like Carphone) to give away your details by accident and know who to contact if you think something has been compromised

Tuesday, February 12, 2008

Mac mini - six things they did not tell you about switching from PC to Macintosh

Last November, after my PC died for about the 4th time I knew there was something seriously wrong, probably something odd happening with the motherboard. I thought I'd treat myself to something different so I splashed out on a Mac mini (Core Duo).
Tip: If you are buying a Mac from Apple, consider the cost of memory upgrades. I would have opted for a 2GB Mac mini rather than the standard 1GB version but they wanted £80 for the privilege. The actual memory cost from Crucial.com is £32 and when I sold my old memory on eBay for £12 I realized I had saved myself £60!

Mostly it's worked as promised but the following painful annoyances rather caught me out:
  1. Parallels did not come as standard, something I really needed for running some of my old windows applications. My only other option would have been to reserve a major chunk of my hard disk and use dual booting, something that I've had real issues with in the past. I took a trial copy of Parallels and it works fairly well apart from being too slow. In fact so slow that in January I upgraded the memory (to 2GB), though as I'm techie enough to do this myself it was under 30 quid.

  2. Webcam did not work. This has been a headache. Not only did my Philips USB webcam not work, there are no valid drivers to make it work (I wasted hours trying to get a generic driver to work). Even worse, there are no guarantees that any other cheap camera I can find will actually work. I still don't have a replacement webcam.

  3. Scanner did not work. I have a rather expensive Fujitsu ScanSnap scanner. Unfortunately there are no drivers that will make it work natively in Leopard. I eventually got it running under Windows 2000 in Parallels, hardly ideal.

  4. EVE Online does not work. I tried out a free account with EVE Online (a well known and popular multi-player game for the Mac), sadly it doesn't run reliably on the Mac mini and freezes up randomly.

  5. Can't network with a PC. This is particularly annoying. I've been trying for several weeks to get this working. I have worked through inbuilt help and googled for solutions to no avail. I can get my Mac to recognize my home networked PC and even read/write files to its hard disk but not the reverse. I wanted this to work as I'm using a 500GB USB drive as my Time Machine and I've kept space on it for PC backups but as the Mac remains invisible to the PC my archive/backup process is frustratingly manual and I can't share a central file library.

  6. Can't use my microphone/headphone. This was not made clear in the summary specification and is a really stupid design flaw. The audio-in socket is actually only for digital microphones. This means that my headphone microphones will not work (though confusingly they fit the socket and it took me quite a while to find out this was not some sort of driver problem). Consequently since buying the Mac I have not been able to use Skype, a real problem as I use this as my home office number. I have been searching for a second-hand iMic device (they're a bit pricey new), a USB adaptor for microphones, I should be able to find one for under £15. The alternative would have been to get a bluetooth microphone, but I can't bear having yet another device to keep charged up.