-
Notifications
You must be signed in to change notification settings - Fork 4
/
transparencydata.py
206 lines (173 loc) · 5.38 KB
/
transparencydata.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
"""
The transparencydata module provides API wrappers for the data found on
transparencydata.com.
"""
__author__ = "Jeremy Carbaugh ([email protected])"
__version__ = "0.1"
__copyright__ = "Copyright (c) 2010 Sunlight Labs"
__license__ = "BSD"
import sys
if sys.version_info[0] == 3:
from urllib.parse import urlencode, urljoin
from urllib.request import urlopen
from urllib.error import HTTPError
else:
from urllib import urlencode
from urlparse import urljoin
from urllib2 import HTTPError, urlopen
try:
import json
except ImportError:
import simplejson as json
DEFAULT_URL = "http://transparencydata.com/api/1.0/"
DEFAULT_PARAMETERS = ('apikey','page','per_page')
DEFAULT_HANDLERS = {}
class TransparencyDataError(Exception):
pass
# base client
class Client(object):
def __init__(self, key, base_url=DEFAULT_URL):
self.apikey = key
self.apiurl = base_url
self.debug = False
def __call__(self, **kwargs):
kwargs['apikey'] = self.apikey
params = {}
handlers = {}
handlers.update(DEFAULT_HANDLERS)
if hasattr(self, 'handlers'):
handlers.update(self.handlers)
for param, value in kwargs.iteritems():
(name, operator) = param.split('__') if '__' in param else (param, None)
if name not in self.parameters and name not in DEFAULT_PARAMETERS:
raise TransparencyDataError('%s is not a valid parameter' % param)
if operator == 'in':
if isinstance(value, (list, tuple)):
value = "|".join(str(v) for v in value)
else:
operator = None
elif operator == 'gt':
value = ">|%s" % value
elif operator == 'lt':
value = "<|%s" % value
elif operator == 'between':
if not isinstance(value, (list, tuple)):
raise TransparencyDataError('%s__%s must be a tuple or list' % (name, operator))
start = value[0].strftime("%Y-%m-%d")
end = value[1].strftime("%Y-%m-%d")
value = "><|%s|%s" % (start, end)
handler = handlers.get(name, None)
if handler:
value = handler(name, value, operator)
params[name] = value.encode('utf8')
url = "%s?%s" % (urljoin(self.apiurl, self.endpoint), urlencode(params))
if self.debug:
print url
return
try:
response = urlopen(url).read().decode()
return json.loads(response)
except HTTPError, e:
raise TransparencyDataError(e.read())
except (ValueError, KeyError), e:
raise TransparencyDataError('Invalid Response')
# base types
class ContributionsClient(Client):
endpoint = 'contributions.json'
parameters = (
'contributor_state',
'recipient_state',
'cycle',
'for_against',
'contributor_industry',
'seat',
'transaction_namespace',
'transaction_type',
'contributor_ext_id',
'recipient_ext_id',
'organization_ext_id',
'parent_organization_ext_id',
'committee_ext_id',
'contributor_type',
'recipient_type',
'date',
'amount',
'committee_ft',
'contributor_ft',
'employer_ft',
'organization_ft',
'recipient_ft',
)
class LobbyingClient(Client):
endpoint = 'lobbying.json'
parameters = (
'lobbyist_is_rep',
'industry',
'transaction_id',
'transaction_type',
'filing_type',
'year',
'issue',
'client_ext_id',
'lobbyist_ext_id',
'candidate_ext_id',
'client_ft',
'client_parent_ft',
'lobbyist_ft',
'registrant_ft',
'issue_ft',
)
class EarmarkClient(Client):
endpoint = 'earmarks.json'
parameters = (
'year',
'state',
'member_party',
'member_state',
'bill',
'description',
'city',
'member',
'recipient',
)
class GrantsClient(Client):
endpoint = 'grants.json'
parameters = (
'assistance_type',
'fiscal_year',
'recipient_state',
'recipient_type',
'agency_ft',
'recipient_ft',
)
class ContractsClient(Client):
endpoint = 'contracts.json'
parameters = (
'agency_id',
'contracting_agency_id',
'fiscal_year',
'place_distrct',
'place_state',
'requesting_agency_id',
'vendor_state',
'vendor_zipcode',
'vendor_district',
'vendor_duns',
'vendor_parent_duns',
'agency_name',
'contracting_agency_name',
'requesting_agency_name',
'vendor_name',
'vendor_city',
'obligated_amount',
'current_amount',
'maximum_amount',
)
# main wrapper
class TransparencyData(object):
def __init__(self, key):
self.contributions = ContributionsClient(key)
self.lobbying = LobbyingClient(key)
self.earmarks = EarmarkClient(key)
self.grants = GrantsClient(key)
self.contracts = ContractsClient(key)