-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
105 lines (95 loc) · 4.6 KB
/
example.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
# August Guang, February 2013
# corpSearch.py
# Takes in an input term (corporation name) and scrapes the InfluenceExplorer
# to run: python corpSearch.py -i <input> -o <output>
from transparencydata import TransparencyData
import sys, getopt
import requests
import pprint
import itertools
import json
api = TransparencyData('8f0d91c66d4e428da018c0eb0fa571fc')
# reads CRP_Categories.txt
def readIn(inFile):
with open(inFile, 'r') as fin:
# format is:
# Catcode Catname Catorder Industry Sector Sector_Long
tmp = fin.readlines()
sectData = tmp[1:]
return sectData
# sorts CRP_Categories.txt so that all codes are associated with a sector in a dictionary
# this allows us to look up a code in the dictionary
def sectorDict(sectData):
sectDict = {}
for i in sectData:
sector = i.split()[4]
code = i.split()[0]
if sector in sectDict:
sectDict[sector].append(code)
else:
sectDict[sector] = [code]
return sectDict
# writes output from API in format needed for Javascript/D3
# I AM SO UNAMUSED
def writeJSON(APIout):
jsonList = []
for i in APIout:
jsonList.append(
{ 'cycle' : {
"contributor" : {
"contributor_name" : i["contributor_name"],
"contributor_ext_id" : i["contributor_ext_id"],
"contributor_type":
{
"Corporation" : {
"organization_name" : i["organization_name"],
"organization_ext_id" : i["organization_ext_id"],
"parent_organization_name" : i["parent_organization_name"],
"parent_organization_ext_id" : i["parent_organization_ext_id"],
"transaction" : {
"transaction_namespace" : i["transaction_namespace"],
"transaction_id" : i["transaction_id"],
"transaction_type" : i["transaction_type"],
"transaction_type_description" : i["transaction_type_description"],
"filing_id" : i["filing_id"],
"is_amendment" : i["is_amendment"],
"amount" : i["amount"],
"date" : i["date"],
"Recipient": {
"recipient_name" : i["recipient_name"],
"recipient_ext_id" : i["recipient_ext_id"],
"recipient_party" : i["recipient_party"],
"recipient_type" : i["recipient_type"],
"recipient_state" : i["recipient_state"],
"recipient_state_held" : i["recipient_state_held"],
"recipient_category" : i["recipient_category"],
"district" : {
"district" : i["district"],
"district_held" : i["district_held"],
"seat" : i["seat"],
"seat_held" : i["seat_held"],
"seat_status" : i["seat_status"],
"seat_result" : i["seat_result"],
}
}
}
},
"Individual" : {
"contributor_occupation" : i["contributor_occupation"],
"contributor_employer" : i["contributor_employer"],
"contributor_gender" : i["contributor_gender"],
"contributor_address" : i["contributor_address"],
"contributor_city" : i["contributor_city"],
"contributor_state" : i["contributor_state"],
"contributor_zipcode" : i["contributor_zipcode"],
"contributor_category" : i["contributor_category"]
}
}
}}})
jsonData = json.dumps(jsonList)
print jsonData
# f = open(outFile)
# f.write(jsonData)
return jsonData
data = api.contributions(cycle='2012', contributor_ft="Corrections Corp of America")
jsonData = writeJSON(data)