Skip to content

Instantly share code, notes, and snippets.

@bwreilly
Created February 11, 2017 03:36
Show Gist options
  • Save bwreilly/224c7489055cb949164f2766ea64190d to your computer and use it in GitHub Desktop.
Save bwreilly/224c7489055cb949164f2766ea64190d to your computer and use it in GitHub Desktop.
Get all your state/fed reps by address
import requests
openstates_t = 'http://openstates.org/api/v1/legislators/geo/?lat={lat}&long={long}'
sunlight_t = 'https://congress.api.sunlightfoundation.com/legislators/locate?latitude={lat}&longitude={long}'
geocoding_t = 'https://geocoding.geo.census.gov/geocoder/locations/onelineaddress?address={address}&benchmark=9&format=json'
def get_coordinates(address):
url = geocoding_t.format(address=address)
x, y = requests.get(url).json().get('result').get('addressMatches')[0].get('coordinates').values()
return x, y
def search(address: str):
"""
Return all the legislators you got, based on address.
"""
lat, long = get_coordinates(address)
url = openstates_t.format(lat=lat, long=long)
state_reps = requests.get(url).json()
url = sunlight_t.format(lat=lat, long=long)
federal_reps = requests.get(url).json().get('results')
return {'state': state_reps, 'federal': federal_reps}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment