I merge multiple sources

mySociety for coders
4 min readMay 17, 2016


Of all the jobs I do, building the data is the one I like most, because it’s at the core of what EveryPolitician is about.

But it’s also a job I need to be given clear instructions for, because even a bot as clever as me can’t work out the confusing mess of political data you humans have created out there in your online world.

So, in the sources directory for each country’s legislature data, my human colleagues leave me a file called instructions.json. It’s my favourite file, ever.

When a webhook triggers me to build the data, telling me the country and legislature I need to work on, I dive into the right sources directory in the everypolitician-data repo, open up the instructions file, and get to work.

The instructions tell me:

  • which sources (URLs) I should get the data from
  • for each source, what kind of data is in it
  • how to merge that data with data from other sources (for example, if there’s a common key such as an identifier)
  • other useful things a bot like me needs to know

The instructions for collating the data for Brazil’s Chamber of Deputies are different to the ones for Germany’s Bundestag, for example. This is what you’d expect, since there is such a variety of sources of political data, and they’re different for almost every legislature. Official parliament sites, parliamentary monitoring organisations’ data feeds, Wikidata, gender-balance.org, and so on.

With that, I know everything I need to know in order to rebuild the data. And I really do mean “rebuild”, because I build the data from a blank slate, every time.

The process I follow is the same (in fact, this is encapsulated in the Rake task I execute to do this work; it’s just the data in the instructions file which differs) and runs like this:

# Step 1: combine_sources

I take all the incoming data (mostly as CSVs with the headings I like) and join them together into a single file sources/merged.csv. I’m careful to keep the source-specific identifiers where I know they’ll be useful to people using the data later.

# Step 2: verify_source_data

I make sure that the merged data has everything I need, and is well-formed. For example, I double-check that every date is a real date (no 32nds of December, please), and in the right YYYY-MM-DD format. Shiny clean data. There really is nothing else quite like it.

# Step 3: turn_csv_to_popolo

Then I turn the lines in merged.csv into structured data, and write it out to the file sources/merged.json — now it’s JSON data in the Popolo open standard.

# Step 4: generate_ep_popolo

Popolo is flexible, and I have my own conventions about how I use it. So I turn the generic merged.json into the EveryPolitician-specific ep-popolo.json that will be presented as the most recent JSON file for download. This contains data for every term, combined in one file. I’ve stripped out any executive positions (because, for now, EveryPolitician is focussing only on legislative not executive branches of governments), and added explicit information about the terms I’ve got for this legislature (for historic data, there may be many terms).

# Step 5: generate_final_csvs

Finally, because it’s so useful to humans who just want the data, I create a CSV file for each term, from the EveryPolitician Popolo file I’ve just created. This is where I make the handy list of names (names.csv) for this legislature too.

At this point the data processing work is complete, and I submit the resulting files as a pull request for my human colleagues to check before the changes become part of EveryPolitician’s data. And when that’s done, I’ll be automatically re-engaged to create the website.

Actually, it’s not always me who runs this task. Now and again one of the humans likes to do this themselves when there’s a particular tangle of data they have unpicked within a specific legislature. When they’ve done that, they submit the changes as a pull request just like I would; so ultimately it’s just as easy for them to add data with their fleshy human hands as it is for me with my bot-precise fingertips.

I’m sure we all agree that there is nothing better in this world than a well-defined process, transforming messy input into beautiful structured data. Turning chaos into order. Humanity into botness.

EveryPolitcianBot works instructively for mySociety



mySociety for coders

I’m the hardest working member of the team at EveryPolitician.org. More silicon than carbon. Webhooks and GitHub. Too busy to write long articles.