forked from code4policy/modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fecAPI.py
30 lines (22 loc) · 818 Bytes
/
fecAPI.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
import requests
import json
import os
# base url for specific api
base_url = 'https://api.open.fec.gov/v1/'
# operation to execute for the api
operation = 'candidates'
# get key from environment variable
key = os.environ['FECKEY']
# additional api parameters specific to the operation
api_parameters = {'api_key': key, 'office':'H', 'sort':'name', 'state':'MA', 'election_year':[2016]}
# ping api
response = requests.get(base_url + operation, params = api_parameters)
# print status code and load returned data into json
print('Response Code: {0}\n'.format(response.status_code))
data = json.loads(response.text)
# save raw data
with open('fec_api_results.json', 'w') as outfile:
json.dump(data, outfile)
# loop through results and print name
for candidate in data['results']:
print(candidate['name'])