-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenSecrets.py
199 lines (152 loc) · 8.78 KB
/
openSecrets.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import requests
from bs4 import BeautifulSoup
import ast
# Relevant functions for pulling information from OpenSecrets data stores (thanks guys love your work)
# Primarily applicable for federal-level committees and individuals.
"""
Donor Look up:
https://www.opensecrets.org/donor-lookup/results?name=buddy+bengel&order=desc&sort=D&zip=28562
https://www.opensecrets.org/donor-lookup/results? name = NAME &order=desc & sort=D & zip = ZIP
"""
# Methods for individual contributions
def collect_contributions(name, zipCode):
namePlus = name.replace('_', '+')
url = f"https://www.opensecrets.org/donor-lookup/results?name={namePlus}&order=desc&sorD&zip={zipCode}"
res = requests.get(url)
soup = BeautifulSoup(res.content, 'html.parser')
body = soup.body
data = body.find_all('tr')
ret=[]
for a in data[1:]:
item = str(a)
badstr = ['<tr>', '<td class="category orange">', '<td>',
'</td>', '/tr', '<td class="u-nowrap">', '<br/>',
'\t', '<>', '<td class="category green">', '<td class="c-red u-center" colspan="8" style="font-size: 12px;"><b>FEDERAL LAW PROHIBITS THE USE OF CONTRIBUTOR INFORMATION FOR THE PURPOSE OF SOLICITING CONTRIBUTIONS OR FOR ANY COMMERCIAL PURPOSE.</b>']
for char in badstr:
item = item.replace(char, "")
splitItem = item.split('\n')
"""
Set up as follows:
\n
\n
money to candidates
\n
\n
name
city, zip
\n
employer
occupation
date
money
recipient
state
\n
"""
if len(splitItem) > 5:
split_location = splitItem[6].split(',')
results = {'category':splitItem[2], 'contributor': splitItem[5], 'city': split_location[0], 'zipCode': split_location[1].strip(' '),
'employer': splitItem[8], 'occupation': splitItem[9], 'data': splitItem[10], 'amount':splitItem[11], 'recipient':splitItem[12], 'jurisdiction':splitItem[13]}
ret.append(results)
return(ret)
def parse_contributions(contribution):
amount_str = contribution['amount'].strip('$')
amount = int(amount_str.replace(',', ''))
target = contribution['recipient']
target_party = target[-2]
return amount, target_party
def indv_impact(name, zipCode):
contrs = collect_contributions(name, zipCode)
r = 0
d = 0
for contr in contrs:
amount, tp = parse_contributions(contr)
if tp == 'R':
r += amount
if tp == 'D':
d += amount
print(amount, tp)
print("Republican = " + str(r))
print("Democrat = " + str(d))
perc = max((r/(r+d)), (d/(r+d)))
party = 'R'
amt = r
if d > r:
party = 'D'
amt = d
return(perc, party, amt)
# Methods for federal PACs
"""
Searching: https://www.opensecrets.org/political-action-committees-pacs/lookup?txt=duke
<table class="DataTable-Partial dataTable no-footer" data-title="" data-paging="false" data-page-length="0" data-info="false" data-searching="false" data-filtercategories="null" data-filterindexes="null" data-checkboxfilters="null" data-selectfilters="null" data-colorcodeby="" data-uuid="32a58fe4-df75-48ac-9796-fa3d339ef023"
data-collection="[{"PAC Name":"\u003ca href=\"/political-action-committees-pacs/duke-energy/C00083535/summary/2024\"\u003eDuke Energy\u003c/a\u003e","Type":"PAC (#C00083535)","Most Recent Cycle Activity":"2024"},{"PAC Name":"\u003ca href=\"/political-action-committees-pacs/duke-energy/C00429662/summary/2008\"\u003eDuke Energy\u003c/a\u003e","Type":"PAC (#C00429662)","Most Recent Cycle Activity":"2008"},{"PAC Name":"\u003ca href=\"/political-action-committees-pacs/duke-energy/C00040907/summary/2000\"\u003eDuke Energy\u003c/a\u003e","Type":"PAC (#C00040907)","Most Recent Cycle Activity":"2000"},{"PAC Name":"\u003ca href=\"/political-action-committees-pacs/duke-pac/C00814004/summary/2022\"\u003eDuke PAC\u003c/a\u003e","Type":"","Most Recent Cycle Activity":"2022"}]" data-columnwidthlimits="{}" data-disablesearchinput="true" data-presortformat="[]" id="DataTables_Table_0" role="grid">
<thead>
<tr role="row"><th class="sorting" tabindex="0" aria-controls="DataTables_Table_0" rowspan="1" colspan="1" aria-label="PAC Name: activate to sort column ascending" style="width: 171.46875px;">PAC Name</th><th class="sorting" tabindex="0" aria-controls="DataTables_Table_0" rowspan="1" colspan="1" aria-label="Type: activate to sort column ascending" style="width: 214.984375px;">Type</th><th class="number-header sorting" tabindex="0" aria-controls="DataTables_Table_0" rowspan="1" colspan="1" aria-label="Most Recent Cycle Activity: activate to sort column ascending" style="width: 279.078125px;">Most Recent Cycle Activity</th></tr>
</thead>
<tbody>
<tr role="row" class="odd">
<td class=" color-category " style="height: 100%; vertical-align: middle;"><a href="/political-action-committees-pacs/duke-energy/C00083535/summary/2024">Duke Energy</a></td>
<td class=" " style="height: 100%; vertical-align: middle;">PAC (#C00083535)</td>
<td class="number " style="height: 100%; vertical-align: middle;">2024</td>
</tr><tr role="row" class="even">
<td class=" color-category " style="height: 100%; vertical-align: middle;"><a href="/political-action-committees-pacs/duke-energy/C00429662/summary/2008">Duke Energy</a></td>
<td class=" " style="height: 100%; vertical-align: middle;">PAC (#C00429662)</td>
<td class="number " style="height: 100%; vertical-align: middle;">2008</td>
</tr><tr role="row" class="odd">
<td class=" color-category " style="height: 100%; vertical-align: middle;"><a href="/political-action-committees-pacs/duke-energy/C00040907/summary/2000">Duke Energy</a></td>
<td class=" " style="height: 100%; vertical-align: middle;">PAC (#C00040907)</td>
<td class="number " style="height: 100%; vertical-align: middle;">2000</td>
</tr><tr role="row" class="even">
<td class=" color-category " style="height: 100%; vertical-align: middle;"><a href="/political-action-committees-pacs/duke-pac/C00814004/summary/2022">Duke PAC</a></td>
<td class=" " style="height: 100%; vertical-align: middle;"></td>
<td class="number " style="height: 100%; vertical-align: middle;">2022</td>
</tr></tbody>
</table>
"""
def PACcontributions(name):
namePlus = name.replace('_', '+')
url = f"https://www.opensecrets.org/political-action-committees-pacs/lookup?txt={namePlus}"
res = requests.get(url)
soup = BeautifulSoup(res.content, 'html.parser')
body = soup.body
table = body.find_all('table')
# If OpenSecrets redirects exactly to a PAC, pull URL for that. Should happen when exact pac name is searched
if len(table) == 3:
data = body.find_all('input', {'name':'utm_source'})
dataURL = data[0].get('value')
else:
return('No commmittees found.')
"""if len(table) == 1: (Can use if needed for a search that doesn't automatically redirect)
data = table[0].get('data-collection')
data = ast.literal_eval(data)
if len(data) >= 1:
entry = data[0]['PAC Name']
# print(test)
entries = entry.split('"')
# print(splittest)
n = entries[2]
n_url = entries[1]
nsplit = n.split('<')
n_actual = nsplit[0][1:]
print(n_actual)
print(n_url)
else:
return('No Commmittees found')
"""
pacURL = f'https://www.opensecrets.org{dataURL}'
res= requests.get(pacURL)
soup= BeautifulSoup(res.content, 'html.parser')
body = soup.body
tables = body.find_all('table')
target = tables[1].get('data-collection')
lst = ast.literal_eval(target)
selected = lst[0]['']
selspl = selected.split('<')
dem = float(((selspl[5].split('>'))[1])[:-1])
rep = float(((selspl[7].split('>'))[1])[:-1])
ret = {'dem': dem, 'rep': rep}
return ret
if __name__ == '__main__':
# name = input("name: ")
# zipCode = input('zipCode: ')
print(PACcontributions('charlotte mecklenburg'))