-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild_cids.py
73 lines (59 loc) · 2.13 KB
/
build_cids.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
"""
Retrieves the OpenSecrets.org CIDs (Candidate IDs) for every US legislator and
write them to data/cids.txt.
The cids.txt file is used by TweetText to select a random legislator for each
tweet.
"""
import os
import json
from crpapi import CRP
try:
from config import secrets
except ImportError:
secrets = {
"OPENSECRETS_API_KEY" : os.environ['OPENSECRETS_API_KEY']
}
crp = CRP(secrets['OPENSECRETS_API_KEY'])
states = [
"AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DC", "DE", "FL", "GA",
"HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD",
"MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ",
"NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC",
"SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY"
]
def progress_bar(count, total, bar_len, status=''):
"""
Prints a progress bar to the console.
:param count: the current item iteration
:param total: the total number of items to iterate
:param bar_len: the length of the progress bar
:param status: optional message regarding progress bar status
"""
filled_len = int(round(bar_len * count / float(total)))
percents = round(100.0 * count / float(total), 1)
progress = '=' * filled_len + '-' * (bar_len - filled_len)
print('[%s] %s%s ...%s\r' % (progress, percents, '%', status))
def build_cids():
"""
Retrieve all CIDs for current congressional members and
write the list to data/cids.txt, replacing the previous file.
"""
cids = []
i = 0
progress_bar(i, len(states), 50, 'Starting...')
for state in states:
i += 1
cands = crp.candidates.get(state)
if len(cands) > 1:
for cand in cands:
cids.append(cand['@attributes']['cid'])
else:
cids.append(cands['@attributes']['cid'])
progress_bar(i, len(states), 50, 'Retrieving legislators from %s' % state)
f = open('data/cids.txt', 'wb')
json.dump(cids, f)
f.close()
result = 'Retrieved {num} CIDs from OpenSecrets.org.'.format(num=len(cids))
print(result)
if __name__ == "__main__":
build_cids()