-
Notifications
You must be signed in to change notification settings - Fork 2
/
parse_hb_sponsors.py
258 lines (210 loc) · 6.98 KB
/
parse_hb_sponsors.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from models import *
from model_helpers import parse_name, get_or_create_person
import re
import requests
from bs4 import BeautifulSoup
from time import sleep
def get_hb_sponsor(bill, sponsor_type):
""" Takes a bill and a sponsor type.
Parse out the name, and queries to find the member's record.
Returns a Bill_Sponsor object, ready to save.
"""
bill_sponsor = Bill_Sponsor(
bill = bill
, sponsor_type = sponsor_type
)
if sponsor_type == 'S':
bill_sponsor.raw_name = bill.sponsor_string
elif sponsor_type == 'C':
bill_sponsor.raw_name = bill.co_sponsor_string
# parse the district from the raw_name
try:
district = re.search(r'\((\d+)\)', bill_sponsor.raw_name).group(1)
except AttributeError:
# if this fails, set up the first query to fail and deal with it downstream
district = 0
# query for member by district, assembly and chamber
member_query = (Assembly_Member
.select()
.where(
(Assembly_Member.district == district)
& (Assembly_Member.assembly == bill.session.assembly)
& (Assembly_Member.chamber == 'H')
)
)
# if there's only one result in the member query, use that member
if member_query.count() == 1:
bill_sponsor.sponsor = member_query.get()
# if there are more than one members...
elif member_query.count() != 1:
# for some reason, these two legislator names are sometimes formatted differently
if 'Stacey Newman' in bill_sponsor.raw_name:
last = 'Newman'
elif 'Scharnhorst' in bill_sponsor.raw_name:
last = 'Scharnhorst'
else:
last = re.search(r'^([\w-]+)(?:\s\w+)?,', bill_sponsor.raw_name).group(1)
if district > 0:
# query for member by last_name, district, assembly and chamber
member_query = (Assembly_Member
.select()
.join(Person)
.where(
(Person.last_name == last)
& (Assembly_Member.district == district)
& (Assembly_Member.assembly == bill.session.assembly)
& (Assembly_Member.chamber == 'H')
)
)
elif district == 0:
# query for member by last_name, assembly and chamber
member_query = (Assembly_Member
.select()
.join(Person)
.where(
(Person.last_name == last)
& (Assembly_Member.assembly == bill.session.assembly)
& (Assembly_Member.chamber == 'H')
)
)
if member_query.count() == 1:
bill_sponsor.sponsor = member_query.get()
else:
# remove the 'etal' from the raw_name
name_string = re.sub(r'[\.\s]+et\s*al\.*', '', bill_sponsor.raw_name).strip()
# parse the name
parsed_name = parse_name(name_string)
# remove the district
district = parsed_name['name_dict'].pop('district')
# get or create a person
person = get_or_create_person(parsed_name['name_dict'])['person']
# get or create an assembly_member
with db.atomic():
bill_sponsor.sponsor = Assembly_Member.get_or_create(
person = person
, district = district
, assembly = bill.session.assembly
, chamber = 'H'
)[0]
# save the new bill_sponsor record
return bill_sponsor
# query to get all house bills where the sponsor string is not null
hb_q = (Bill.select()
.join(Bill_Type)
.where(
(Bill_Type.chamber == 'H')
& (Bill.bill_type != 'HEC')
& (
(
(Bill.sponsor_string.is_null(False))
& (fn.Char_Length(Bill.sponsor_string) > 2)
)
| (
(Bill.co_sponsor_string.is_null(False))
& (fn.Char_Length(Bill.co_sponsor_string) > 2)
& ~(Bill.co_sponsor_string.regexp(r'^[.\s]+etal\.*$'))
)
)
)
)
# # loop over all the house bills...
# for bill in hb_q:
# print ' Working on {0.bill_type_id} {0.number}'.format(bill)
# print bill.source_doc.url
# if bill.sponsor_string == None or len(bill.sponsor_string) <= 2:
# print ' No sponsor.'
# else:
# print ' Getting sponsor...'
# hb_spon = get_hb_sponsor(bill, 'S')
# with db.atomic():
# hb_spon.save()
# if bill.co_sponsor_string == None or len(bill.co_sponsor_string) <= 2 or re.match(r'^[.\s]+etal\.*$', bill.co_sponsor_string) != None:
# print ' No co-sponsor.'
# else:
# print ' Getting co-sponsor...'
# hb_co_spon = get_hb_sponsor(bill, 'C')
# try:
# with db.atomic():
# hb_co_spon.save()
# except IntegrityError as e:
# # sometimes the sponsor appears in the list of co-sponsors
# if 'duplicate' in e.message:
# pass
# print '--------------'
# some of the more recent house bills have a link to a page that lists all the co-sponsors
hb_w_co_spon_link_q = (Bill.select()
.join(Bill_Type)
.where(
(Bill_Type.chamber == 'H')
& (Bill.bill_type != 'HEC')
& (Bill.co_sponsor_link.contains('CoSponsors.aspx'))
)
)
# set up a requests session for pages we don't have yet:
with requests.session() as requests_session:
for bill in hb_w_co_spon_link_q:
print 'http://www.house.mo.gov/' + bill.co_sponsor_link
r = None
while r == None:
try:
r = requests_session.get('http://www.house.mo.gov/' + bill.co_sponsor_link)
except requests.exceptions.ConnectionError as e:
print e
print ' Connection failed. Retrying...'
requests_session = requests.session()
except Exception as e:
print e
soup = BeautifulSoup(r.content, 'html5lib')
table = soup.find(id = 'ContentPlaceHolder1_grdCoSponsors_DXMainTable')
for tr in soup.find_all('tr', class_ = 'dxgvDataRow'):
tds = tr.find_all('td')
bill_co_sponsor = Bill_Sponsor(
bill = bill
, sponsor_type = 'C'
, raw_name = tds[0].text.strip()
)
district = tds[1].text.strip()
# query for member by district, assembly and chamber
member_query = (Assembly_Member
.select()
.where(
(Assembly_Member.district == district)
& (Assembly_Member.assembly == bill.session.assembly)
& (Assembly_Member.chamber == 'H')
)
)
# if there's only one result in the member query, use that member
if member_query.count() == 1:
bill_co_sponsor.sponsor = member_query.get()
# if there are more than one members...
elif member_query.count() != 1:
parsed_name = parse_name(bill_co_sponsor.raw_name)['name_dict']
# query for member by last_name, district, assembly and chamber
member_query = (Assembly_Member
.select()
.join(Person)
.where(
(Person.last_name == parsed_name['last_name'])
& (Assembly_Member.district == district)
& (Assembly_Member.assembly == bill.session.assembly)
& (Assembly_Member.chamber == 'H')
)
)
if member_query.count() == 1:
bill_co_sponsor.sponsor = member_query.get()
else:
print 'Error: Could not find co-sponsor!'
print bill_co_sponsor.raw_name
print bill_co_sponsor.sponsor.person.last_name
try:
with db.atomic():
bill_co_sponsor.save()
except IntegrityError as e:
# sometimes the sponsor appears in the list of co-sponsors
if 'duplicate' in e.message:
pass
sleep(3)
print '=========='
print 'fin.'