Last active
April 25, 2023 11:32
-
-
Save gwire/045f98972b1e453a89966fef95d40bb7 to your computer and use it in GitHub Desktop.
List MP email addresses
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
#!/usr/bin/env python3 | |
# quick script to iterate through the parliament contact API and outputs email details | |
# in the from "name,party,email" for each current Commons member | |
import sys | |
import json | |
import requests | |
import re | |
house = "1" # 1=commons 2=lords | |
member_api_url = "https://members-api.parliament.uk/api" | |
house_list = "/Members/Search?House=" + house + "&IsCurrentMember=true&take=20" | |
guess_email = False | |
new_results = True | |
skip=0 | |
while new_results: | |
member_json = requests.get(member_api_url + house_list + f"&skip={skip}").json() | |
if len(member_json["items"])>0: | |
skip += 20 | |
else: | |
new_results = False | |
for i in member_json["items"]: | |
memberdetails = i["value"]["nameDisplayAs"] | |
memberdetails += "," + i["value"]["latestParty"]["name"] | |
if i["links"]: | |
for j in i["links"]: | |
if j["rel"] == "contactInformation": | |
contact_json = requests.get(member_api_url + j["href"]).json() | |
email_p = "" | |
email_c = "" | |
email_e = "" | |
for k in contact_json["value"]: | |
if k["type"].lower() == "parliamentary office": | |
if "email" in k: | |
email_p = k["email"] | |
if k["type"].lower() == "parliamentary": | |
if "email" in k: | |
email_p = k["email"] | |
if k["type"].lower() == "constituency office": | |
if "email" in k: | |
email_c = k["email"] | |
if k["type"].lower() == "external or private office": | |
if "email" in k: | |
email_e = k["email"] | |
if email_p: | |
memberdetails += "," + email_p | |
else: | |
if email_c: | |
memberdetails += "," + email_c | |
else: | |
if email_e: | |
memberdetails += "," + email_e | |
else: | |
memberdetails += ",none" | |
## we make a guess at Lords email addresses if not published | |
if guess_email and house=="2" and "[email protected]" or ",none" in memberdetails: | |
for j in i["links"]: | |
if j["rel"] == "synopsis": | |
synopsis_json = requests.get(member_api_url + j["href"]).json() | |
p = re.compile("name is (.*), and") ## do you even API? | |
extract_name = p.search(synopsis_json["value"]) | |
bname = extract_name.group(1) | |
firstinitial = bname[0][0].lower() | |
bname_split = bname.split() | |
surname = bname_split[-1].lower() | |
guessed_email = surname + firstinitial + "@parliament.uk" | |
if "[email protected]" in memberdetails: | |
memberdetails = memberdetails.replace("[email protected]",guessed_email) | |
else: | |
memberdetails = memberdetails.replace("none",guessed_email) | |
sys.stdout.write( memberdetails + "\n") | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment