OUseful.Info, the blog…

Trying to find useful things to do with emerging technologies in open education

Viewing Campaign Finance Data In a Google Spreadsheet via the New York Times Campaign Data API

It’s all very well when someone like the New York Times opens up an API to campaign finance data (announced here), but only the geeks can play, right? It’s not like they’re making the data available in a spreadsheet so that more people have a chance to try and do something with it, is it? Err, well, maybe this’ll help? A way of getting data out from the NYT API and into an online Google spreadsheet, where you can do something with it… (you can see an example here: campaign data in a Google Spreadsheet (via The New York Times Campaign Data API)).

Following up on a comment to Visualising Financial Data In a Google Spreadsheet Motion Chart by Dan Mcquillan: “FWIW, there’s a lot of campaigning potential in these tools”, I just had a quick tinker with another Google spreadsheet function – importXML – that pulls XML data into a Google spreadsheet, and in particular pointed it at the newly released New York Times Campaign Finance API.

To use this API you need to get yourself a key, which requires you to register with the New York Times, and also provide a URL for your application: create a new Google spreadsheet, and use the URL for it (it’ll look something like http://spreadsheets.google.com/ccc?key=p1rHUqg4g4223vNKsJW8GcQ&hl=en_GB) as the URL for your NYT API app.

First up, enter “API KEY” as a label in a cell – I used G22 – and paste your New York Times API Campaign Finance API key for that spreadsheet into the cell next to it – e.g. H32. (My spreadsheet was cobbled together in scratchpad mode, so the cells are all over the place!)

In cell G23 enter the label “Names URL” and in cell H23 the following formula, which constructs a URL for the “Candidate Summaries”:
=CONCATENATE(“http://api.nytimes.com/svc/elections/us/v2/president/2008/finances/totals.xml?api-key=”,H22)

[aaargghhh - crappy styylesheet... the formula is this, but all on one line:
=CONCATENATE("http://api.nytimes.com/svc/elections/us/v2/
president/2008/finances/totals.xml?api-key=",H22)

]

(The formula concatenates a NYT API calling URL with your API key.)

We’re now going to pull in the list of candidates: in e.g. cell A24 enter the formula:
=ImportXML(H23,”//candidate”)

H23, remember, is a reference to the cell that contains the URL for the API call that grabs all the candidate summaries. The =importXML grabs the XML result from the API call, and the “//candidate” bit looks through the XML file till it finds a “candidate” element. If you look at the XML from the webservice, you’ll see it actually contains several of these:

The spreadsheet actually handles all of these and prints out a summary table for you:

All we did was configure one cell to grab all this information, remember: cell A24 which contains =ImportXML(H23,”//candidate”).

We’re now going to pull in more detailed campaign info for each candidate using the “Candidate Details” API request, which looks something like http://api.nytimes.com/svc/elections/us/v2/president/2008/finances/candidates/romney.xml

(The (lack of) capitalisation of the surname of the candidate in the URL is not necessary – it seems to work okay with “Romney.xml” for example).

To construct the URLs for each candidate, let’s write a formula to construct the URL for one of them.

In my spreadsheet, the Candidate Summary import in cell A24 built a table that filled cells A24:E42, so we’re going to start working in additional columns alongside that table.

You’ll notice that the URL for the detailed report just requires the candidate’s surname, whereas the candidate summary provides the forename as well.

In cell F24, add the following formula: SPLIT(A24, “,”)

What this does is split the contents of cell A24 (e.g. Obama, Barack) at the comma, and populate two cells (in columns F and G) as a result:

If you highlight the cell (F24), click on the little blue square in the bottom right hand corner, and drag down, you should be able to magically copy the formula and so spilt all the candidates’ names.

Now what we need to do is construct the URL that will pull back the detailed campaign information for each candidate. We’ll build the URLs in the next free column along (column H in my spreadsheet – the split name is across columns F and G).

In cell H24, enter the following formula (all on one line – split here for convenience):

=CONCATENATE(“http://api.nytimes.com/svc/elections/us/v2/president/2008/
finances/candidates/”,F24,”.xml?api-key=”,H$22)

H$22 refers to the cell that contains your NYT API key. The $ is there to anchor the row number in the formula, so when you drag the cell to copy it, the copied formula still refers to that exact same cell.

Highlight cell H24 and drag the little blue square down to complete the table and construct URLs for each candidate.

Good stuff :-)

Now we can construct another formula to import the full campaign details for each candidate.

in cell A2 in my spreadsheet, I entered the following:

=ImportXML(H24,”//candidate”)

H24 is the URL for the candidate details API call for the first candidate (Barack Obama in my case).

Hopefully, you should get a row of data pulled into the spreadsheet:

The XML file responsible looks something like this:

Use your eyes and work out what the contents of each cell might refer to:

Click on cell A2, and drag the blue square down to A20. The contents of cell A2 will be copied in a relative way, and should now be importing detailed information for all the candidates.

Now you have the data in a form you can play with :-)

And it should update whenever the New York Times updates the data it exposes through the API.

(I would publish the spreadsheet in all its glory, but then I’d have to give away my API key… Ah, what the heck = here it is: Spreadsheet of Campaign Finances, courtesy of the New York Times. (Maybe I should read the T&C to check this is a fair use…!))

For other New York Times Campaign Data API functions, see the documentation. You should know enough now to be able to work out how to use it…

PS the “//candidate” XPATH stuff can be tricky to get your head round. If anyone wants to post as a comment a list of XPATH routines to pull out different data elements, feel free to do so :-)

If you make use of the data in any interesting ways, please link back here so we can see what you’re getting up to and how you’re using the data…

Written by Tony Hirst

October 17, 2008 at 11:03 pm

Posted in Pipework, Tinkering

8 Responses

Subscribe to comments with RSS.

  1. Awesome – this is truly xml kung fu and makes me want to learn how to do similar stuff.

    As i understand it, it’s exactly this kind of trickster approach to tech that was the origin of http://www.mysociety.org/ and it’s interesting offspring.

    cheers
    dan

    dan mcquillan

    October 20, 2008 at 10:54 pm

  2. Another interesting thing you can do with data like this is with the Sunlight Labs API to clean up the names in a spreadsheet using the search method. Check this out:

    http://blog.sunlightlabs.com/2008/08/21/google-spreadsheet-and-the-sunlight-labs-api/

    Clay Johnson

    October 21, 2008 at 2:55 pm

  3. [...] far example, in Viewing Campaign Finance Data In a Google Spreadsheet via the New York Times Campaign Data API I showed how to do what it says on the [...]

  4. That’s a great way to use Google Spreadsheets, I’m sure I could find all sorts of interesting uses for it.

    PuReWebDev

    purewebdev

    October 24, 2008 at 2:31 am

  5. I used the same data directly from the FEC to do this: http://whocontributed.com

    You can download the whole dataset from the FEC right here: http://www.fec.gov/DisclosureSearch/MapAppDownload.do

    Jesse Farmer

    November 3, 2008 at 7:21 am

  6. Very, very cool. I’ve been playing around a lot with Docs but haven’t used importXML yet, thanks for the great example.

    Jason

    November 3, 2008 at 7:35 am

  7. [...] Viewing Campaign Finance Data In a Google Spreadsheet via the New York Times Campaign Data API « OU… Viewing Campaign Finance Data In a Google Spreadsheet via the New York Times Campaign Data API (tags: Mashup Politics Campaign Contributions) This entry was written by Govwonk, posted on December 2, 2008 at 11:06 pm, filed under Uncategorized. Bookmark the permalink. Follow any comments here with the RSS feed for this post. Post a comment or leave a trackback: Trackback URL. « links for 2008-12-01 [...]

  8. [...] Savvy voters care about who is supporting the candidates. The New York Times’ Campaign Finance API has a plethora of data on the largest financiers of parties, PACs and politicians. Again, a sortable and searchable table related to your local official would be useful for voters looking to dive deep into the information to turn it into a spreadsheet. [...]


Comments are closed.

Follow

Get every new post delivered to your Inbox.

Join 768 other followers

%d bloggers like this: