|
14 | 14 |
|
15 | 15 | API_ROOT = "http://congress.api.sunlightfoundation.com"
|
16 | 16 |
|
| 17 | +LEGISLATOR_ID_TYPES = ( |
| 18 | + 'bioguide', |
| 19 | + 'thomas', |
| 20 | + 'lis', |
| 21 | + 'govtrack', |
| 22 | + 'votesmart', |
| 23 | + 'crp', |
| 24 | + 'fec', |
| 25 | +) |
| 26 | + |
17 | 27 |
|
18 | 28 | class Congress(sunlight.service.Service):
|
19 | 29 | """
|
| 30 | + Bindings to the `Congress API <http://sunlightlabs.github.io/congress/>`_. |
| 31 | + Keep in mind this is a thin wrapper around the API so the API documentation |
| 32 | + is the place to look for help on field names and examples. |
20 | 33 | """
|
21 | 34 |
|
22 | 35 | def legislators(self, **kwargs):
|
| 36 | + """ |
| 37 | + Search and filter for members of Congress. |
| 38 | +
|
| 39 | + For details see `Legislators API docs |
| 40 | + <http://sunlightlabs.github.io/congress/legislators.html>` |
| 41 | + """ |
| 42 | + return self.get('legislators', **kwargs) |
| 43 | + |
| 44 | + def legislator(self, identifier, id_type=LEGISLATOR_ID_TYPES[0], **kwargs): |
| 45 | + """ |
| 46 | + Retrieve a member of Congress by a unique identifier. Defaults to |
| 47 | + bioguide. Choices are: |
| 48 | +
|
| 49 | + * bioguide |
| 50 | + * thomas |
| 51 | + * lis |
| 52 | + * govtrack |
| 53 | + * votesmart |
| 54 | + * crp |
| 55 | + * fec |
| 56 | +
|
| 57 | + For details see `Legislators API docs |
| 58 | + <http://sunlightlabs.github.io/congress/legislators.html>` |
| 59 | + """ |
| 60 | + if id_type not in LEGISLATOR_ID_TYPES: |
| 61 | + id_type = LEGISLATOR_ID_TYPES[0] |
| 62 | + id_arg = {} |
| 63 | + if id_type == 'fec': |
| 64 | + id_arg['fec_ids'] = identifier |
| 65 | + else: |
| 66 | + id_key = '{0}_id'.format(id_type) |
| 67 | + id_arg[id_key] = identifier |
| 68 | + |
| 69 | + kwargs.update(id_arg) |
| 70 | + results = self.get('legislators', **kwargs) |
| 71 | + if len(results): |
| 72 | + return results[0] |
| 73 | + return None |
| 74 | + |
| 75 | + |
| 76 | + def all_legislators_in_office(self, **kwargs): |
| 77 | + """ |
| 78 | + Returns all legislators currently in office (non-paginated response). |
| 79 | +
|
| 80 | + For details see `Legislators API docs |
| 81 | + <http://sunlightlabs.github.io/congress/legislators.html>` |
| 82 | + """ |
| 83 | + kwargs.update({ |
| 84 | + "per_page": "all" |
| 85 | + }) |
23 | 86 | return self.get('legislators', **kwargs)
|
24 | 87 |
|
25 | 88 | def locate_legislators_by_lat_lon(self, lat, lon, **kwargs):
|
| 89 | + """ |
| 90 | + Find members of Congress by a latitude and longitude. |
| 91 | +
|
| 92 | + For details see `Legislators API docs |
| 93 | + <http://sunlightlabs.github.io/congress/legislators.html#methods/legislators-locate>` |
| 94 | + """ |
26 | 95 | kwargs.update({
|
27 | 96 | "latitude": lat,
|
28 | 97 | "longitude": lon
|
29 | 98 | })
|
30 | 99 | return self.get('legislators/locate', **kwargs)
|
31 | 100 |
|
32 | 101 | def locate_legislators_by_zip(self, zipcode, **kwargs):
|
| 102 | + """ |
| 103 | + Find members of Congress by zip code. |
| 104 | +
|
| 105 | + For details see `Legislators API docs |
| 106 | + <http://sunlightlabs.github.io/congress/legislators.html#methods/legislators-locate>` |
| 107 | + """ |
33 | 108 | kwargs.update({
|
34 | 109 | "zip": zipcode
|
35 | 110 | })
|
36 | 111 | return self.get('legislators/locate', **kwargs)
|
37 | 112 |
|
| 113 | + def bills(self, **kwargs): |
| 114 | + """ |
| 115 | + Search and filter through bills in Congress. |
| 116 | +
|
| 117 | + For details see `Bills API docs |
| 118 | + <http://sunlightlabs.github.io/congress/bills.html>` |
| 119 | + """ |
| 120 | + return self.get('bills', **kwargs) |
| 121 | + |
| 122 | + def bill(self, bill_id, **kwargs): |
| 123 | + """ |
| 124 | + Retrieve a bill by bill_id. |
| 125 | +
|
| 126 | + For details see `Bills API docs |
| 127 | + <http://sunlightlabs.github.io/congress/bills.html>` |
| 128 | + """ |
| 129 | + kwargs.update({ |
| 130 | + "bill_id": bill_id |
| 131 | + }) |
| 132 | + results = self.get('bills', **kwargs) |
| 133 | + if len(results): |
| 134 | + return results[0] |
| 135 | + return None |
| 136 | + |
| 137 | + def search_bills(self, query, **kwargs): |
| 138 | + """ |
| 139 | + Search the full text of legislation, and other fields. |
| 140 | +
|
| 141 | + For details see `Bills API docs |
| 142 | + <http://sunlightlabs.github.io/congress/bills.html#methods/bills-search>` |
| 143 | + """ |
| 144 | + kwargs.update({ |
| 145 | + "query": query |
| 146 | + }) |
| 147 | + return self.get('bills/search', **kwargs) |
| 148 | + |
| 149 | + def upcoming_bills(self, **kwargs): |
| 150 | + """ |
| 151 | + Search and filter through upcoming bills in the House and Senate. |
| 152 | +
|
| 153 | + This will return bills that have been scheduled by |
| 154 | + party leadership for upcoming House and Senate floor action. |
| 155 | +
|
| 156 | + For details see `Upcoming Bills API docs |
| 157 | + <http://sunlightlabs.github.io/congress/upcoming_bills.html>` |
| 158 | + """ |
| 159 | + return self.get('upcoming_bills', **kwargs) |
| 160 | + |
| 161 | + def locate_districts_by_lat_lon(self, lat, lon, **kwargs): |
| 162 | + """ |
| 163 | + Find congressional districts by a latitude and longitude. |
| 164 | +
|
| 165 | + For details see `Districts API docs |
| 166 | + <http://sunlightlabs.github.io/congress/districts.html>` |
| 167 | + """ |
| 168 | + kwargs.update({ |
| 169 | + "latitude": lat, |
| 170 | + "longitude": lon |
| 171 | + }) |
| 172 | + return self.get('/districts/locate', **kwargs) |
| 173 | + |
| 174 | + def locate_districts_by_zip(self, zipcode, **kwargs): |
| 175 | + """ |
| 176 | + Find congressional districts by a latitude and longitude. |
| 177 | +
|
| 178 | + For details see `Districts API docs |
| 179 | + <http://sunlightlabs.github.io/congress/districts.html>` |
| 180 | + """ |
| 181 | + kwargs.update({ |
| 182 | + "zip": zipcode, |
| 183 | + }) |
| 184 | + return self.get('/districts/locate', **kwargs) |
| 185 | + |
| 186 | + def committees(self, **kwargs): |
| 187 | + """ |
| 188 | + Search and filter through committees in the House and Senate. |
| 189 | +
|
| 190 | + For details see `Bill API docs |
| 191 | + <http://sunlightlabs.github.io/congress/committees.html>` |
| 192 | + """ |
| 193 | + return self.get('committees', **kwargs) |
| 194 | + |
| 195 | + def amendments(self, **kwargs): |
| 196 | + """ |
| 197 | + Search and filter through committees in Congress. |
| 198 | +
|
| 199 | + For details see `Amendments API docs |
| 200 | + <http://sunlightlabs.github.io/congress/committees.html>` |
| 201 | + """ |
| 202 | + return self.get('amendments', **kwargs) |
| 203 | + |
| 204 | + def votes(self, **kwargs): |
| 205 | + """ |
| 206 | + Search and filter through votes in Congress. |
| 207 | +
|
| 208 | + For details see `Votes API docs |
| 209 | + <http://sunlightlabs.github.io/congress/votes.html>` |
| 210 | + """ |
| 211 | + return self.get('votes', **kwargs) |
| 212 | + |
| 213 | + def floor_updates(self, **kwargs): |
| 214 | + """ |
| 215 | + Search and filter through floor updates in the House and Senate. |
| 216 | +
|
| 217 | + For details see `Floor Updates API docs |
| 218 | + <http://sunlightlabs.github.io/congress/floor_updates.html>` |
| 219 | + """ |
| 220 | + return self.get('floor_updates', **kwargs) |
| 221 | + |
| 222 | + def hearings(self, **kwargs): |
| 223 | + """ |
| 224 | + Search and filter through committee hearings in the House and Senate. |
| 225 | +
|
| 226 | + For details see `Hearings API docs |
| 227 | + <http://sunlightlabs.github.io/congress/hearings.html>` |
| 228 | + """ |
| 229 | + return self.get('hearings', **kwargs) |
| 230 | + |
38 | 231 | # implementation methods
|
39 | 232 | def _get_url(self, endpoint, apikey, **kwargs):
|
40 | 233 | return "%s/%s?apikey=%s&%s" % (
|
41 | 234 | API_ROOT, endpoint, apikey,
|
42 | 235 | sunlight.service.safe_encode(kwargs)
|
43 | 236 | )
|
44 | 237 |
|
45 |
| - |
46 | 238 | def _decode_response(self, response):
|
47 | 239 | return json.loads(response)['results']
|
0 commit comments