Using Greasemonkey to Pimp the Telegraph’s MPs’ Expenses Data at paulcarvill.com, the home of Paul Carvill on the web

link: paulcarvill at flickr

paulcarvill.com

Hi, I'm Paul Carvill and I'm a web developer. I am Head of Interface Development at LBi, Europe's largest digital agency.

I also like walking, cooking, Bollywood and rock 'n' roll.

Using Greasemonkey to Pimp the Telegraph’s MPs’ Expenses Data

posted: Monday, July 13th, 2009 at 12:27 pm

You may have noticed that the Telegraph have some useful MPs’ expenses data on their site. To go with it they’ve also got some nice graphs, which show an MP’s historical expense claim data. The trend of those claims is, as you might expect, mostly upwards. No surprises so far. But, until recently, inflation has always been a positive number, so you would be probably be right to expect expense claims to rise in tandem with the prices of everyday goods and services.

Unpimped Telegraph MPs' expense data charts
Above: the unpimped image of Michael Ancram’s expenses data chart

But I don’t find the graphs useful enough. I can easily see the trend of the claims just by looking at the accompanying data table. What I really want to know is how closely the rises in MPs’ expenses matched the standard inflation rate. My hunch was that the rise in value of the MPs’ claims would be ahead of the inflation rate, so how best to check this? And how to check it for a wide number of claimants? In addition, I wanted to visualise a breakdown of the claim types – for travel, stationary, 2nd home etc.

Pimping the Telegraph's expense data charts
Above: an image of Michael Ancram’s expenses data chart following the addition of my Greasemonkey script.

I was interested enough to ask the Telegraph, via Twitter, if they had any plans to add some economic context to the data. They actually responded to let me know that the data was available in a spreadsheet, so why not go ahead and mash it up? Great! But on checking the spreadsheet I found that the data only went back to 2004, while the data on the Telegraph’s website went back to 2001. I’m aware that expenses data going back further are available from the Houses of Parliament website, but I didn’t really have to time to go searching for them, converting the format to what I needed etc. So I decided to write a quick Greasemonkey script to operate on the data already published by the Telegraph, on their website, which happened to be good enough markup to extract data from and use.

Greasemonkey is a Firefox plugin which enables you to write JavaScript scripts which can operate on the contents of a web page solely within your browser. Anything you do to the data does not alter the original in any way, and so it’s a great way of changing the way websites work, customising them to look how you like and generally mucking about with them.

Here’s some technical info on what I did:

First I checked the Office of National Statistics for the annual inflation data from the Retail Price Index, the broadest measure of inflation, which includes mortgage interest payments. I grabbed data for the years covered by the Telegraph data — 2001/02 to 2007/08 and added it to my script. Then I loaded in a couple of extra scripts which help to grab the data from the page, draw a new graph add it to the page in place of the original.

I extract the data from the MP’s expenses table, which is actually the same way the Telegraph are drawing the graph in the first place. The difference is that I extract each row of data rather than just the totals. I draw all the data to the graph, then add an extra line which extrapolates the MP’s first year total claim across the whole term of the graph, based on the inflation data for the subsequent years. Finally, I added a series of checkboxes to isolate different data sets within the graph, so you can compare, say, just an MP’s claims for food against her claims for stationery.

The script was fairly quickly put together, so if you’re handy with a bit of JavaScript you might be able to think of something I haven’t. For example I’d like to see a comparison of MPs’ claims against nursing or teaching salaries. Even if you just want to hack around you can download it and amend it as much as you like without any danger. But here’s the really great thing — I’ve actually maintained most of the structure of the Telegraph’s original graph code, so if they want to take my script back and use it on their page they’re free to do so, and it should work perfectly!

Was this hack useful? Yes. For example, it allowed me to see that Ian Austin, Labour MP for Dudley North, made below-inflation claims rises for 2006/07 and 2007/08. And that Labour MP for Leeds North East Fabian Hamilton’s 2007/08 total claim (£170,794.00) was almost double what it would have been if his first recorded claim (£75,437.00) from 2001/02 had risen in line with inflation.

You can download my Greasemonkey script here. If you don’t want to do that (or your IT department won’t let you) you can see from the images above how the script operates on the original website.

Some notes about using this script in Greasemonkey:

I wrote this script using Greasemonkey 0.8.2. As of version 0.8 you can use the @require metadata key to load JavaScript libraries into your user-script. Greasemonkey will download those scripts to your local directory, meaning your browser isn’t increasing network traffic every time you run the script. If for any reason you need to use an earlier version of the plugin you’ll find some useful information here on loading in scripts without the @require key.

We don’t want the script to execute except when we’re looking at a Telegraph MPs’ expenses page, so for best performance you should set this script to run on the following URLs:

http://parliament.telegraph.co.uk/mpsexpenses/expenses/*
http://parliament.telegraph.co.uk/mpsexpenses/expense-microsite/expenses/*

Some good links to try with the script installed:

Tags:

Comments are closed.