Skip to content

Instantly share code, notes, and snippets.

@cjwinchester
Created June 9, 2020 16:10
Show Gist options
  • Save cjwinchester/1e2423445edec3a84c1de36322c482c4 to your computer and use it in GitHub Desktop.
Save cjwinchester/1e2423445edec3a84c1de36322c482c4 to your computer and use it in GitHub Desktop.
API_KEY = os.environ.get('FEC_API')
DONOR_URL = 'https://api.open.fec.gov/v1/schedules/schedule_a'
# for brevity, will skip the read-in-the-csv step
# `names` is a list of first initial/last name strings
# e.g. `J DOE`
# list of records to write out
data_out = []
for name in names:
# set up the request parameters
params = {
'api_key': API_KEY,
'contributor_name': name,
'per_page': 100,
'sort': 'contribution_receipt_date',
'is_individual': True
}
# make the request
current_response = requests.get(DONOR_URL, params=params)
# throw an error if you get an invalid HTTP status code
current_response.raise_for_status()
# turn the response into json
current_data = current_response.json()
# get a handle to the records and pagination data for this chunk of results
current_results = current_data['results']
current_pagination = current_data['pagination']
# this block will execute until the `break` statement is reached
while True:
# call the function to extract the data from this set of records
# `get_data_from_raw_response` returns a smaller version of the
# donation record with some additional info about the committee
for donation in current_results:
data = get_data_from_raw_response(donation)
data_out.append(data)
# let us know what's up
print(f'Nabbed {len(current_results)} records for search term "{name}"')
# check to see if we're at the last page of results, which
# doesn't have a `last_indexes` key
last_index_object = current_pagination.get('last_indexes')
# if we just processed the last page of results, we are done
if not last_index_object:
break
else:
# if not, keep trucking
# ⭐️ this is where i'm having difficulty figuring out which
# parameters to include in the next API call
# check for two parameters to append to the next request
last_index = last_index_object.get('last_index')
last_contribution_receipt_date = last_index_object.get('last_contribution_receipt_date')
# add `last_index` to the request parameters
params['last_index'] = last_index
# check if `last_contribution_receipt_date` exists
if last_contribution_receipt_date:
# if so, add to the params
params['last_contribution_receipt_date'] = last_contribution_receipt_date
else:
# if not, set that value as null
params['last_contribution_receipt_date'] = None
# per Paul's email, add a new param for `sub_id`
params['sub_id'] = f'>{last_index}'
# API call fails unless you include this as well?
params['sort_null_only'] = True
# make the request and check for HTTP errors
new_request = requests.get(DONOR_URL, params)
new_request.raise_for_status()
# turn the response into JSON
new_data = new_request.json()
# update the results and pagination values
current_pagination = new_data['pagination']
current_results = new_data['results']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment