-
Notifications
You must be signed in to change notification settings - Fork 521
/
Copy pathcontact_forms.py
executable file
·74 lines (52 loc) · 2.08 KB
/
contact_forms.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
#!/usr/bin/env python
'''Gets contact webform URLs for the intersection of members with bioguide ids
and with correlating contact form steps in unitedstates/contact-congress:
args:
<bioguide_id bioguide_id ...>
A list of bioguide ids to import.
options:
--debug[=True]
Whether or not verbose output should be printed to the command line
'''
import yaml
from urllib.request import urlopen
import utils
from utils import load_data, save_data
# These members have forms in iframes, and Contact-Congress has different
# needs than human users might.
SKIP_BIOGUIDES = ['M000312']
def run():
options = utils.flags()
debug = options.get('debug', False)
filename = "legislators-current.yaml"
args = utils.args()
legislators = load_data(filename)
if len(args) != 0:
bioguides = args
print("Fetching contact forms for %s..." % ', '.join(bioguides))
else:
bioguides = [member['id']['bioguide'] for member in legislators]
print("Fetching contact forms for all current members...")
for legislator in legislators:
bioguide = legislator['id']['bioguide']
if bioguide not in bioguides: continue
if bioguide in SKIP_BIOGUIDES: continue
if debug: print("Downloading form for %s" % bioguide, flush=True)
try:
steps = contact_steps_for(bioguide)
except LegislatorNotFoundError as e:
if debug: print("skipping, %s..." % e, flush=True)
continue
legislator['terms'][-1]['contact_form'] = steps['contact_form']['steps'][0]['visit']
print("Saving data to %s..." % filename)
save_data(legislators, filename)
def contact_steps_for(bioguide):
base_url = "https://raw.githubusercontent.com/unitedstates/contact-congress/main/members/{bioguide}.yaml"
response = urlopen(base_url.format(bioguide=bioguide))
if response.code == 404:
raise LegislatorNotFoundError("%s not found in unitedstates/contact-congress!" % bioguide)
return yaml.load(response.read())
class LegislatorNotFoundError(Exception):
pass
if __name__ == '__main__':
run()