Skip to content

Instantly share code, notes, and snippets.

@eledroos
Created July 10, 2017 20:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eledroos/7874872ecb20e12cc5c7be6e7188e7cb to your computer and use it in GitHub Desktop.
Save eledroos/7874872ecb20e12cc5c7be6e7188e7cb to your computer and use it in GitHub Desktop.
A script to take a list of addresses from NationBuilder and find their State Senator and Representative using the Google Civic API.
import pandas as pd
import json, requests, logging, pprint
# Setup logging
lfh = logging.FileHandler('civicInfoScript.log')
lfh.setFormatter(logging.Formatter('%(levelname)s %(asctime)s %(message)s'))
log = logging.getLogger('civicInfoScript')
log.setLevel(logging.INFO)
log.addHandler(lfh)
# Load NationBuilder sample file
df = pd.read_csv("volunteers.csv")
# Columns that will have content in the final CSV
# df['json_dump'] = '' # debugging purposes, comment when prod
df['senator_1_name'] = ''
df['senator_1_number'] = ''
df['senator_1_website'] = ''
df['senator_2_name'] = ''
df['senator_2_number'] = ''
df['senator_2_website'] = ''
df['rep_district'] = ''
df['rep_name'] = ''
df['rep_number'] = ''
df['rep_website'] = ''
# Assemble id column in DF, you'll iterate through this
nb_id = df['nationbuilder_id']
addresses = pd.DataFrame()
# Make call to Google, taking in address and
# returning a JSON object
def civicInfoCall(address):
result = {}
url = 'https://www.googleapis.com/civicinfo/v2/representatives?'
apiKey = '' #API KEY GOES HERE
try:
response = requests.get(url, params={"key":apiKey,"address":address})
response.raise_for_status()
except requests.exceptions.RequestException as e:
# this will log the whole traceback
log.exception("Call failed with %s", e)
# here you either re-raise the exception, raise your own exception
# or return anything
# in our case we'll just return NOTHING. YOU GET NOTHING. GOOD DAY SIR.
return None
data = response.json() # Set JSON to a data object
log.info(data) # Log the received JSON object
pprint.pprint(data) # pretty print the JSON to terminal
return data
i = 0 # Index Counter
for id in nb_id:
# Assemble address
address = df.iloc[i]['primary_address1']
city = df.iloc[i]['primary_city']
state = df.iloc[i]['primary_state']
zipCode = df.iloc[i]['primary_zip']
fullAddress = str(address) + " " + str(city) + " " + str(state) + " 0" + str(zipCode)
if address is None:
fullAddress = str(city) + " " + str(state) + " 0" + str(zipCode)
# Send address to civicInfoCall() with the address
print("Checking: " + fullAddress)
dataCall = civicInfoCall(fullAddress) # set JSON method call to a new object for this scope
#df['json_dump'][i] = dataCall # debugging purposes, comment out when prod.
# Set values
try:
# Senator 1
df['senator_1_name'][i] = dataCall['officials'][2]['name'] # Ed J. Markey
df['senator_1_number'][i] = dataCall['officials'][2]['phones'] # (202) 224-2742
df['senator_1_website'][i] = dataCall['officials'][2]['urls'] # http://markey.senate.gov/
# Senator 2
df['senator_2_name'][i] = dataCall['officials'][3]['name'] # Elizabeth Warren
df['senator_2_number'][i] = dataCall['officials'][3]['phones'] # (202) 224-4543
df['senator_2_website'][i] = dataCall['officials'][3]['urls'] # http://warren.senate.gov/
# Representative
df['rep_district'][i] = dataCall['offices'][3]['name'] # Massachusetts's 7th congressional district
df['rep_name'][i] = dataCall['officials'][4]['name'] # Michael E. Capuano
df['rep_number'][i] = dataCall['officials'][4]['phones'] # (202) 225-5111
df['rep_website'][i] = dataCall['officials'][4]['urls'] # https://www.house.gov/capuano/
except Exception as e:
print(e) # for debugging purposes
log.info(e)
pass
i += 1 # Increment Counter to iterate
print("Writing DF to file...")
df.to_csv('volunteers-civicinfo.csv', encoding='utf-8', index=False)
print("Saved. Program will now exit.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment