-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
145 lines (111 loc) · 3.85 KB
/
run.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
from flask import Flask, render_template
from flask import redirect
from flask import request, url_for
from pyzipcode import ZipCodeDatabase
import requests
import urllib2
import keys
# opensecrets
# ----------------
# download and install https://code.google.com/p/python-crpapi/downloads/list
# docs - https://www.opensecrets.org/resources/create/api_doc.php
from crpapi import CRP, CRPApiError
CRP.apikey = keys.opensecrets_key
# sunlight
# ----------------
# install pip install sunlight
# docs - http://python-sunlight.readthedocs.org/en/latest/#usage
import sunlight
from sunlight import openstates
from sunlight import congress
sunlight.config.API_KEY = keys.sunlight_key
# Transparency Data
# ----------------
# python setup.py install - from python-transparencydata-master
from transparencydata import TransparencyData
td = TransparencyData(keys.sunlight_key)
# Influence Explorer
# ----------------
from influenceexplorer import InfluenceExplorer
ie = InfluenceExplorer(keys.sunlight_key)
app = Flask(__name__)
# @app.template_filter('format_currency')
# def format_currency(value):
# value = float(value)
# return "${:,.2f}".format(value)
@app.template_filter('format_currency')
def format_currency(value):
value = float(value)
return "${:,.2f}".format(value)
# Get Amount of Super Pac Expenditures from OpenSecrets widget
def superpac_money():
url = 'http://www.opensecrets.org/widgets/outsidespending2012.js'
url = urllib2.urlopen(url).read()
url_split = url.split()
length = len(url_split)
amount = url_split[length-1]
# removes quotes and semicolon
amount = amount[1:-2]
return amount
def get_state_from_zip(zip):
z = ZipCodeDatabase()
zipcode = z[zip]
return zipcode.state
@app.route('/')
def index():
return render_template('index.html')
#http://127.0.0.1:5000/campaign/pelosi
@app.route('/campaign/')
@app.route('/campaign/<politician>/<salary>')
def campaign(politician=None, salary=None):
if politician == None:
return render_template('choose_location.html')
else:
# congress
# politician_info = congress.legislators(lastname=politician)[0]
#td.contributions(recipient_ft=politician)
# openstates
# politician_info = sunlight.openstates.legislators(last_name=politician)
# influence explorer
politician_info = ie.entities.search(politician)[0]
id = politician_info['id']
top_industries = ie.pol.industries(id, limit=10)
return render_template('campaign.html', salary=salary, politician=politician_info, top_industries=top_industries)
@app.route('/choose_politician')
def choose_politician():
if 'location' in request.args:
location = request.args.get('location', '')
else:
location = '91101'
if location.isdigit():
state = get_state_from_zip(location)
# Congress API doesn't have as many entries
url = 'http://congress.api.sunlightfoundation.com/legislators/locate?apikey='+str(keys.sunlight_key)+'&zip='+str(location)
r = requests.get(url)
r = r.json()
congress = r['results']
# openstates
legislators = openstates.legislators(state=state)
else:
# openstates
legislators = openstates.legislators(state=location)
congress=0
return render_template('choose_politician.html', legislators=legislators, congress=congress)
@app.route('/lobbying')
def lobbying():
#top_10 = ie.entities.top_n_lobbyist_bundlers(limit=10)
top_orgs = ie.entities.top_n_organizations(limit=15, cycle='-1')
top_industries = ie.entities.top_n_industries(limit=15)
return render_template('lobbying.html', top_orgs=top_orgs, top_industries=top_industries)
@app.route('/organizations')
def organizations():
return render_template('organizations.html')
@app.route('/super_pacs')
def super_pacs():
# TODO - get dynamic data for 2014
return render_template('super_pacs.html')
@app.route('/take_action')
def take_action():
return render_template('take_action.html')
if __name__ == '__main__':
app.run(debug=True)