-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
64 lines (51 loc) · 1.54 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
from flask import Flask, request, make_response, render_template
from flask.ext.cors import CORS
import json
import requests
import re
import os
import operator
import tweet-your-rep
app = Flask(__name__)
cors = CORS(app)
sentry = None
try:
from raven.contrib.flask import Sentry
app.config['SENTRY_DSN'] = os.environ['SENTRY_DSN']
sentry = Sentry(app)
except ImportError:
pass
except KeyError:
pass
# ROUTES
@app.route('/api/')
def api():
input_addr = request.args.get('address')
# load in CSV to memory
# geocode address
# look up the lat/long to get reps https://sunlightlabs.github.io/openstates-api/legislators.html#methods/geo-lookup
# match open states ID to our local list
# return legislator info
parse_resp = {'input-address': input_addr}
parse_result = tweet-your-rep.parse(input_addr)
result = []
for t in parse_result:
result.append( {'value': t[0], 'tag': t[1]} )
parse_resp['result'] = result
if sentry:
sentry.captureMessage("%s\n%s" % (input_addr, json.dumps(parse_resp, indent=4)))
resp = make_response(json.dumps(parse_resp))
resp.headers['Content-Type'] = 'application/json'
return resp
@app.route('/')
def index():
return render_app_template('index.html')
# UTILITY
def render_app_template(template, **kwargs):
'''Add some goodies to all templates.'''
if 'config' not in kwargs:
kwargs['config'] = app.config
return render_template(template, **kwargs)
# INIT
if __name__ == "__main__":
app.run(debug=True, port=9999)