-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
86 lines (64 loc) · 2.21 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import requests
from urllib import unquote_plus
from chalice import Chalice, BadRequestError
app = Chalice(app_name='what-reps')
app.debug = True
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 = 'https://maps.googleapis.com/maps/api/geocode/json'
def get_coordinates(address):
params = {'sensor': 'false', 'address': address}
r = requests.get(geocoding, params=params)
results = r.json().get('results')
location = results[0]['geometry']['location']
return location['lat'], location['lng']
def feds(lat, lng):
url = sunlight_t.format(lat=lat, long=lng)
federal_reps = requests.get(url).json().get('results')
return federal_reps
def state_reps(lat, lng):
url = openstates_t.format(lat=lat, long=lng)
state_reps = requests.get(url).json()
return state_reps
def search(address):
"""
Return all the legislators you got, based on address.
"""
address = unquote_plus(address)
lat, lng = get_coordinates(address)
# These could be done in parallel...somehow?
s_reps = state_reps(lat, lng)
federal_reps = feds(lat, lng)
return {
'location': (lat, lng),
'state': s_reps,
'federal': federal_reps
}
@app.route('/')
def index():
return {
'endpoints': {
'/': 'This page.',
'/state-reps/?lat={lat}&?lng={lng}': 'Just the state representatives',
'/fed-reps/?lat={lat}&?lng={lng}': 'Just the federal representatives',
'/search/{address}': "Find all Federal and State representatives for this address."
}
}
@app.route('/search/{address}')
def searchview(address):
try:
return search(address)
except:
raise BadRequestError('Cannot find address: ' + address)
@app.route('/fed-reps/')
def fedview():
params = app.current_request.query_params
lat = params.get('lat')
lng = params.get('lng')
return feds(lat, lng)
@app.route('/state-reps/')
def fedview():
params = app.current_request.query_params
lat = params.get('lat')
lng = params.get('lng')
return state_reps(lat, lng)