Created
March 31, 2019 16:46
-
-
Save olihawkins/99044b7ece130a1af3bfd8a0b01dd56f to your computer and use it in GitHub Desktop.
How to download Members' email addresses from the Parliamentary Data Platform using pdpy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import pdpy | |
def fetch_mps_email(): | |
return fetch_members_email(house=pdpy.constants.PDP_ID_HOUSE_OF_COMMONS) | |
def fetch_lords_email(): | |
return fetch_members_email(house=pdpy.constants.PDP_ID_HOUSE_OF_LORDS) | |
def fetch_members_email(house=None): | |
"""Fetch emails for Members.""" | |
# Initialise house constraint | |
house_constraint = '' | |
# If a house is specified set the house constraint | |
if house == pdpy.constants.PDP_ID_HOUSE_OF_COMMONS or \ | |
house == pdpy.constants.PDP_ID_HOUSE_OF_LORDS: | |
house_constraint = 'BIND(d:{0} AS ?house)'.format(house) | |
# Build the query | |
members_email_query = """ | |
PREFIX : <https://id.parliament.uk/schema/> | |
PREFIX d: <https://id.parliament.uk/> | |
SELECT DISTINCT | |
?person_id | |
?mnis_id | |
?given_name | |
?family_name | |
?other_names | |
?display_name | |
?full_title | |
?gender | |
?date_of_birth | |
?date_of_death | |
WHERE {{ | |
# House constraint | |
{0} | |
?person_id :memberMnisId ?mnis_id ; | |
:personGivenName ?given_name ; | |
:personFamilyName ?family_name ; | |
<http://example.com/F31CBD81AD8343898B49DC65743F0BDF> ?display_name ; | |
<http://example.com/D79B0BAC513C4A9A87C9D5AFF1FC632F> ?full_title ; | |
:personHasGenderIdentity/:genderIdentityHasGender/:genderName ?gender ; | |
:memberHasParliamentaryIncumbency/:seatIncumbencyHasHouseSeat/:houseSeatHasHouse ?house . | |
OPTIONAL {{ ?person_id :personOtherNames ?other_names . }} | |
OPTIONAL {{ ?person_id :personDateOfBirth ?date_of_birth . }} | |
OPTIONAL {{ ?person_id :personDateOfDeath ?date_of_death . }} | |
OPTIONAL {{ ?person_id :memberHasParliamentaryIncumbency/:parliamentaryIncumbencyHasContactPoint/:email ?email . }} | |
}} | |
""".format(house_constraint) | |
return pdpy.sparql_select(members_email_query) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment