Skip to content

Instantly share code, notes, and snippets.

@rdmurphy
Created July 27, 2012 04:24
Show Gist options
  • Save rdmurphy/3186163 to your computer and use it in GitHub Desktop.
Save rdmurphy/3186163 to your computer and use it in GitHub Desktop.
Texas GOP U.S. Senate Super PAC Expenditures (For or Against)
import urllib
import json
import csv
import nytapikey
BASE_URI = "http://api.nytimes.com/svc/elections/us/v3/finances/2012"
API_KEY = nytapikey.key # keeping my key warm and safe
dewhurst_id = "S2TX00361"
cruz_id = "S2TX00312"
results_file = open("ussen_results.csv", "wb")
writer = csv.writer(results_file)
writer.writerow(["candidate",
"fec_committee_name",
"amount",
"support_or_oppose",
"date",
"payee",
"purpose",
"fec_uri",
"super_pac_status"])
superpac_store = {} # to prevent wasting API calls, store super PAC true/false
def check_for_superpac(id):
if not id in superpac_store:
request_url = BASE_URI + "/committees/" + id + ".json?api-key=" + API_KEY
response = urllib.urlopen(request_url).read()
try:
response = json.loads(response)["results"][0]["super_pac"]
except ValueError: # if the NYT API is missing something, you make the call
response = None
superpac_store[id] = response
payload = response
else:
payload = superpac_store[id]
return payload
def get_independ_expend(cand, id, offset):
request_url = BASE_URI + "/candidates/" + id + "/independent_expenditures.json?api-key=" + API_KEY
if offset > 0:
request_url += "&offset=" + str(offset)
response = json.loads(urllib.urlopen(request_url).read())["results"]
if not response:
return
else:
for entry in response:
check_results = check_for_superpac(entry["fec_committee"][entry["fec_committee"].rfind("/") + 1:][:-5])
if check_results == True or check_results == None:
result = []
result.append(cand)
result.append(entry["fec_committee_name"])
result.append(entry["amount"])
result.append(entry["support_or_oppose"])
result.append(entry["date"])
result.append(entry["payee"])
result.append(entry["purpose"])
result.append(entry["fec_uri"])
result.append(check_results)
writer.writerow(result)
if len(response) == 20:
new_offset = offset + 20
get_independ_expend(cand, id, new_offset)
def main():
get_independ_expend("Dewhurst", dewhurst_id, 0)
get_independ_expend("Cruz", cruz_id, 0)
results_file.close()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment