forked from naomifox/mass-email-delivery-code
/
WriteYourRep.py
490 lines (424 loc) · 19.2 KB
/
WriteYourRep.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
import urllib2, re, subprocess, sys, os, time, urlparse
import browser, captchasolver, xmltramp
import web
from parseZipToDist import *
from wyrutils import *
from ClientForm import ParseResponse, ControlNotFoundError, AmbiguityError
import traceback
from BeautifulSoup import *
import socket; socket.setdefaulttimeout(30)
from DataForWriteYourRep import *
DEBUG = True
class WriteYourRep:
"""
Class to support writing to rep.
Example usage:
writeYourRep = WriteYourRep()
q = writeYourRep.writerep(i)
status = writeYourRep.getStatus(q)
"""
def __init__(self):
"""
initialize two dicts:
sendb has all the senate offices for each state
zipToDistDB has the list of districts for each zip code
"""
self.sendb = get_senate_offices()
self.zipToDistDB = get_zip_to_dist_db()
def getWyrContactLink(self, i):
"""
Use the Find Your Rep web site to look up the rep contact link
"""
b = browser.Browser()
b.open('http://www.house.gov/htbin/findrep?ADDRLK')
f = get_form(b, lambda f: f.find_control_by_id('state'))
f.fill_address(i.addr1, i.addr2)
f.fill_all(city=i.city, state=i.state.upper())
nextpage = b.open(f.click())
soup = BeautifulSoup(nextpage)
imageTags=soup.findAll("img", attrs={'alt' : 'Go to contact form'})
if len(imageTags) > 0:
fullEmailLink=imageTags[0].parent
else:
raise Exception("Could not find contact email at find your rep page")
print "Contact link:", fullEmailLink
contactUrl = fullEmailLink['href']
return contactUrl
def getWyrDistricts(self, zip):
"""
Reimplements functionality of getWyrContactLink, but doesn't
go through Wyr page
Will return a list of districts in the form ["AK-00"] or ["MA-01", "MA-02"]
"""
distList = self.zipToDistDB[zip]
return distList
def write_reps_in_dists(self, i, dists):
"""Attempt to submit the same data to specified districts."""
b = browser.Browser()
contact_links = [contact_congress_dict[dist] for dist in dists]
status=""
ctr=0
for contact_link in contact_links:
if DEBUG: print "contact_link selected: ", contact_link
q = self.writerep_general(contact_link, i)
status = self.getStatus(q)
if status.startswith("Thank"):
break
ctr += 1
if ctr >= len(dists):
ctr = 0
return (dists[ctr], q)
def writerep(self, i):
"""Looks up the right contact page and handles any simple challenges."""
b = browser.Browser()
# for some forms, we just need a direct link
# if i.dist in forms_with_frame:
# link = forms_with_frame[i.dist]
# elif i.dist in other_direct_forms:
# link = other_direct_forms[i.dist]
# else:
#dist will end in XX unless we are testing
dists = None
if (i.dist[3:]!='XX'):
contact_links = [contact_congress_dict[i.dist]]
print "contact_links list:", contact_links
else:
if DEBUG: print "Zip: %s" % i.zip5
dists = self.getWyrDistricts(i.zip5)
contact_links = [contact_congress_dict[dist] for dist in dists]
status=""
for (cnt, contact_link) in enumerate(contact_links):
if DEBUG: print "contact_link selected: ", contact_link
q = self.writerep_general(contact_link, i)
status = self.getStatus(q)
if status.startswith("Thank"):
if dists:
i.dist = dists[cnt]
break
if not status.startswith("Thank"):
contact_link = self.getWyrContactLink(i)
q = self.writerep_general(contact_link, i)
return q
def writesenator(self, senator, i):
"""Looks up the right contact page and handles any simple challenges."""
b = browser.Browser()
(state, link ) = self.getSenatorStateAndContactLink(senator)
if state == None:
raise Exception("Sorry, senator " + senator + " not found")
q = self.writerep_general(link, i)
return q
def getSenatorStateAndContactLink(self, sen2contact):
'''
for a given senator, get the contact link
senators names must match with what appears in their url.
For example, scottbrown, brown, boxer
'''
for state in self.sendb:
for contactlink in self.sendb.get(state, []):
sen = web.lstrips(web.lstrips(web.lstrips(contactlink, 'http://'), 'https://'), 'www.').split('.')[0]
if sen == sen2contact:
return (state,contactlink)
return (None, None)
def prepare_i(self, dist):
'''
get the fields for the email form.
the only thing that changes is the state
'''
i = web.storage()
i.id="jamesmith"
i.dist=dist
i.state = dist[:2]
if len(dist) == 2:
(i.zip5, i.zip4) = getzip(dist + '-00')
if "zip5" not in i or not i.zip5:
if len(dist) == 2:
i.zip5, i.zip4 = getzip(dist + '-01')
else:
i.zip5, i.zip4 = getzip(dist)
# stuff in some default values
i.prefix = 'Mr.'
i.fname = 'James'
i.lname = 'Smith'
i.addr1 = '12 Main St'
i.addr2 = ''
i.city = 'Franklin'
i.phone = '571-336-2637'
i.email = 'demandprogressoutreach@yahoo.com'
i.subject = 'Testing'
i.full_msg = 'Testing'
if dist in distsStreetAddresses.keys():
distAddress= distsStreetAddresses[dist]
i.addr1 = distAddress['addr1']
i.city = distAddress['city']
i.zip5 = distAddress['zip5']
i.zip4 = distAddress['zip4']
if DEBUG: print "contact info object:\n", i
return i
# these strings show up when successful at submitting form
confirmationStrings = ['thank',
'the following information has been submitted',
'your email has been successfully sent'
'your message has been sent',
'your message has been submitted',
'contact_opinion_ty.cfm', #weird one for harkin
'../free_details.asp?id=79', #weird one for sarbanes
'email confirmation',
'correspondence has been sent']
def getStatus(self, pagetxt):
confirmations=[cstr for cstr in confirmationStrings if cstr in pagetxt.lower()]
if confirmations:
return "Thanked"
errorStr = self.getError(pagetxt)
if errorStr:
return "Failed %s" % errorStr
return "Unknown"
def getError(self, pagetxt):
''' Attempt pattern matching on the pagetxt, to see if we can diagnose the error '''
for stringList in [zipIncorrectErrorStrs, addressMatchErrorStrs,
#jsRedirectErrorStrs,
#frameErrorStrs,
captchaStrs, generalErrorStrs]:
for errorStr in stringList:
if errorStr.lower() in pagetxt.lower():
if stringList == zipIncorrectErrorStrs:
error = "Zip incorrect: " + errorStr
return error
elif stringList == addressMatchErrorStrs:
error = "Address match problem: " + errorStr
return error
elif stringList == jsRedirectErrorStrs:
error = "Js redirect problem: " + errorStr
return error
elif stringList == frameErrorStrs:
error = "Frame problem: " + errorStr
return error
elif stringList == captchaStrs:
error = "Captcha problem: " + errorStr
return error
elif stringList == generalErrorStrs:
error = "General error: " + errorStr
return error
return None
def getSenators(self, state):
return self.sendb.get(state, [])
def is_thankyou_genuine(self, pagetxt):
negation_texts = ["Zip Authenticated"]
for string in negation_texts:
if string.lower() in pagetxt.lower():
return False
return True
def writerep_general(self, contact_link, i):
""" General function for filling in any form with signer data.
Works for the house's WYR form or just directly on any contact page.
Loops through 5 times attempting to fill in form details and clicking.
Stops either when 5 loops is complete, or has achieved success, or known failure
Returns the last successful page
"""
def get_challenge():
''' find captchas'''
labels = b.find_nodes('label', lambda x: x.get('for') == 'HIP_response')
if labels: return labels[0].string
def fill_inhofe_lgraham(f):
"""special function to fill in forms for inhofe and lgraham"""
if DEBUG: print "Filling special inhofe or lgraham form"
f.fill_all(A01=i.prefix, B01=i.fname, C01=i.lname, D01=i.addr1, E01=i.addr2, F01=i.city,
G01=i.state, H01=i.zip5, H02=i.phone, H03=i.phone, I01=i.email, J01="Communications", K01=i.full_msg)
f.fill(type='textarea', value=i.full_msg)
if DEBUG: print "f filled and ready to submit: ", f
def fill_form(f):
''' f is a form '''
if DEBUG: print "in fill_form, filling name"
f.fill_name(i.prefix, i.fname, i.lname)
if DEBUG: print "in fill_form, filling addr"
f.fill_address(i.addr1, i.addr2)
if DEBUG: print "in fill_form, filling phone"
f.fill_phone(i.phone)
if DEBUG: print "in fill_form, filling textarea"
textareacontrol = f.fill(type='textarea', value=i.full_msg)
if DEBUG: print 'filled textareacontrol' , textareacontrol
if DEBUG: print "in fill_form, filling all"
if DEBUG: print "Printing all controls"
for c in f.controls:
if DEBUG: print "control: ", c.name, " type: ", c.type
f.fill_all(city=i.city, zipcode=i.zip5, zip4=i.zip4, state=i.state.upper(),
email=i.email,
issue=['TECH', 'GEN', 'OTH'],
subject=i.subject, reply='yes',
Re='issue', #for billnelson
newsletter='noAction', aff1='Unsubscribe',
MessageType="Express an opinion or share your views with me")
# fill in all empty radio controls
empty_controls = [c for c in f.controls if c.type=="radio" and not c.value]
for c in empty_controls:
if DEBUG: print "empty ", c
try:
f.fill('OTH', control=c)
except:
if DEBUG: print "Failed to fill control %s" % c.name
# Solve captchas. I included this here because it was placed here by Aaron,
# but I haven't found a captcha that it works on. -NKF
#challenge = get_challenge()
#if challenge:
# print "Found challenge!"
# try:
# solution = captchasolver.solve(challenge)
# except Exception, detail:
# print >> sys.stderr, 'Exception in CaptchaSolve', detail
# print >> sys.stderr, 'Could not solve:"%s"' % challenge,
if DEBUG: print "f filled and ready to submit to ", b.url, "\n", f
b = browser.Browser()
if DEBUG: print "In writerep_general, opening contact_link", contact_link
b.open(contact_link)
# max loops
k = 6
# needed this from some weird error that I forgot to document.
# we only want to do the WYR form once,
# so it's a flag so we don't choose this one again.
completedWyrForm = False
for cnt in range(1,k):
if DEBUG: print "Loop ", cnt, ":\n", b.url, "\n" #, b.page, "\n Done with page ", cnt, "\n\n"
# check if this is a refresh page
# to do: see if we can get javascript window.location refreshes
# (would require some smart parsing or using a javascript interpreter module)
if 'http-equiv="refresh"' in b.page:
if DEBUG: print "Redirect to a new page:"
r_refresh = re.compile('[Uu][Rr][Ll]=([^"]+)')
newurl = r_refresh.findall(b.page)[0]
newurl = newurl.replace(' ', '%20')
newurl = newurl.replace('&', '&')
if DEBUG: print "\nNewurl:", newurl
try:
b.open(newurl)
continue #next loop
except:
print "Failed to open url ", newurl, " error: ", traceback.print_exc()
# some pages have multiple forms on them.
# For example, there may be a search tool in the sidebar.
# or there may be forms which are hidden by not displayed by the css.
# try to see what we can grab out the page, then we'll decide which one's the best to try
textareaform = get_form(b, lambda f: f.find_control_by_type('textarea'))
zipform = get_form(b, lambda f: f.has(name='zip'))
verificationform = get_form(b, lambda f: 'formproc' in f.action)
nameform = get_form(b, lambda f: 'wrep_const' in f.action) #see AL-06 for an example, has zip form in page too
wyrform = get_form(b, lambda f: f.find_control_by_id('state') and
f.find_control_by_name('zip') and
f.find_control_by_name('zip4'))
indexform = get_form(b, lambda f: f.has(name='Re')) # see billnelson for example
# choose which form we want to use
form = None
if textareaform:
if DEBUG: print "textareaform"
form = textareaform
elif wyrform and not completedWyrForm:
if DEBUG: print "wyrform"
form = wyrform
completedWyrForm = True
elif nameform:
if DEBUG: print "step2 contact form with name"
form = nameform
elif zipform:
if DEBUG: print "zipform"
form = zipform
elif verificationform:
if DEBUG: print "verification form"
form = verificationform
elif indexform:
if DEBUG: print "index form"
form = indexform
# if no redirect and no form was found, just return. can go no further
if not form:
if DEBUG: print "no form found"
return b.page
# look for captcha
if form.find_control_by_name('captcha') or form.find_control_by_name('validation'):
if DEBUG: print "captcha found"
# raise Captcha
return b.page
else:
if DEBUG: print "no captcha found"
if DEBUG: print "going to fill_form from ", b.url, " now \n", form, "\n End form", cnt, "\n"
if "inhofe" in contact_link or "lgraham" in contact_link:
fill_inhofe_lgraham(form)
else:
fill_form(form)
try:
if DEBUG: print "trying the next page"
nextpage = b.open(form.click())
except urllib2.HTTPError, error:
print >>sys.stderr, "caught a urllib2 http error"
print >>sys.stderr, "Failed to submit form for url ", b.url, " error: ", traceback.print_exc()
return "Failed to submit form for url "+ b.url+ " error: "+ traceback.format_exc()
except:
print >>sys.stderr, "caught an unexpected http error"
print >>sys.stderr, "Failed to submit form for url ", b.url, " error: ", traceback.print_exc()
return "Failed to submit form for url "+ b.url+ " error: "+ traceback.format_exc()
# Now, look for common errors or confirmations.
foundError = False
thanked = False
if DEBUG: print "Looking for errors in page " #, b.page
errorStr = self.getError(b.page)
if errorStr:
#import pdb; pdb.set_trace();
if DEBUG: print "Found error: ", errorStr, " done with ", contact_link
foundError = True
if DEBUG: print "Looking for thank you in page: "# , nextpage.lower()
confirmations=[cstr for cstr in confirmationStrings if cstr in nextpage.lower()]
if len(confirmations) > 0:
print 'thanked, done with ', contact_link
thanked = True
# weird check for mulvaney
successUrls = ['https://mulvaneyforms.house.gov/submit-contact.aspx']
if b.url in successUrls:
thanked = True
if thanked and self.is_thankyou_genuine(b.page):
return nextpage
if foundError:
return b.page
if DEBUG: print "Tried ", k, "times, unsuccessfully, to fill form"
return b.page # raise UnsuccessfulAfter5Attempts(b.page)
def usage():
''' print command line usage '''
print "stest senator - example: stest boxer"
print "htest dist - example: htest MA-01"
if __name__ == "__main__":
import sys
if len(sys.argv) == 3:
if sys.argv[1] == 'stest':
sen = sys.argv[2]
writer = WriteYourRep()
i = writer.prepare_i('MA-01')
q = writer.writesenator(sen, i)
file('%s.html' % sen, 'w').write(q)
print "Last page in %s.html"% sen
elif sys.argv[1] == 'htest':
writer = WriteYourRep()
dist = sys.argv[2]
q = writer.writerep(writer.prepare_i(dist))
file('%s.html' % dist, 'w').write(q)
print "Last page in %s.html"% dist
else:
usage()
elif len(sys.argv) == 2 and sys.argv[1] != 'stest' and sys.argv[1] != 'htest':
# try a link
link = sys.argv[1]
writer = WriteYourRep()
i = writer.prepare_i('MA-01')
q = writer.writerep_general(link, i)
print q
if len(sys.argv) == 3:
if sys.argv[1] == 'stest':
writer = WriteYourRep()
i = writer.prepare_i('MA-01')
q = writer.writesenator(sen, i)
file('%s.html' % sen, 'w').write(q)
print "Last page in %s.html"% sen
elif sys.argv[1] == 'htest':
writer = WriteYourRep()
dist = sys.argv[2]
q = writer.writerep(writer.prepare_i(dist))
file('%s.html' % dist, 'w').write(q)
print "Last page in %s.html"% dist
else:
usage()
else:
usage()