def csv_Send_To_House_With_Exclusions(csvfile='demo-dataz.csv', messagefile="noCispaMessage.txt", statfile='csv_Send_To_House.log', dryrun=False, excludedDists=[]): ''' Parse from the csv file ''' writeYourRep = WriteYourRep() reader = csv.DictReader(open(csvfile, 'rb'), delimiter='\t') genderassigner = GenderLookup() (subject, message) = parseMessageFile(messagefile) for row in reader: state='unknown' status = "" try: i = csv_To_Data(row, writeYourRep, genderassigner, subject, message) alldists = writeYourRep.getWyrDistricts(i.zip5) culleddists = [dist for dist in alldists if dist not in excludedDists] distListStr=' '.join(alldists) if dryrun or len(culleddists)==0: status += distListStr + " " + ": Not attempted with "+ i.__str__()+"\n" else: (dist, q) = writeYourRep.write_reps_in_dists(i, culleddists) status += dist + " " + writeYourRep.getStatus(q) +", " except Exception, e: import traceback; traceback.print_exc() status=status + ' failed: ' + e.__str__() file(statfile, 'a').write('%s %s, %s, "%s"\n' % (i.fname, i.lname, i.state, status))
def csv_Send_To_House(csvfile='demo-dataz.csv', messagefile="noCispaMessage.txt", statfile='csv_Send_To_House.log', dryrun=False, onedistrict=None): ''' Parse from the csv file ''' writeYourRep = WriteYourRep() reader = csv.DictReader(open(csvfile, 'rb')) genderassigner = GenderLookup() (subject, message) = parseMessageFile(messagefile) for row in reader: state='unknown' status = "" try: i = csv_To_Data(row, writeYourRep, genderassigner, subject, message) if onedistrict!=None: print "Feature of specifying one district is not supported yet" return if dryrun: distListStr=' '.join(writeYourRep.getWyrDistricts(i.zip5)) status += distListStr + " " + ": Not attempted with "+ i.__str__()+"\n" else: q = writeYourRep.writerep(i) status += writeYourRep.getStatus(q) +", " except Exception, e: import traceback; traceback.print_exc() status=status + ' failed: ' + e.__str__() file(statfile, 'a').write('%s %s, %s, "%s"\n' % (i.fname, i.lname, i.state, status))
def csv_Send_To_Senate(csvfile='demo-dataz.csv', messagefile="noCispaMessage.txt", statfile='csv_Send_To_Senate.log', dryrun=False, onesenator=None): ''' Parse from the csv file Problem forms: www.vitter.senate.gov Problem with link to contact page on senators website www.mccain.senate.gov unknown url type, need to look into this one further levin.senate.gov urllib2.URLError: <urlopen error [Errno 61] Connection refused> www.lieberman.senate.gov Unclear whether it worked or not. franken.senate.gov Unclear whether it worked or not. www.toomey.senate.gov CAPTCHA www.sessions.senate.gov CAPTCHA www.shelby.senate.gov CAPTCHA www.coburn.senate.gov CAPTCHA www.crapo.senate.gov CAPTCHA www.moran.senate.gov CAPTCHA www.roberts.senate.gov CAPTCHA www.paul.senate.gov CAPTCHA ''' import csv from ZipLookup import ZipLookup from GenderLookup import GenderLookup writeYourRep = WriteYourRep() reader = csv.DictReader(open(csvfile, 'rb')) genderassigner = GenderLookup() (subject, message) = parseMessageFile(messagefile) zipLookup = ZipLookup() for row in reader: state='unknown' status = "" try: i = csv_To_Data(row, writeYourRep, genderassigner, subject, message) sens = writeYourRep.getSenators(i.state) for sen in sens: if onesenator != None and onesenator not in sen: status += "%s: not attempted, " % (sen) continue print "Writing to senator %s" % sen senname = web.lstrips(web.lstrips(web.lstrips(sen, 'http://'), 'https://'), 'www.').split('.')[0] captchaforms=['toomey','sessions','shelby','coburn','crapo','roberts','paul'] if senname in captchaforms: status += senname + " has captcha. skipping. " elif dryrun: status += sen + " " + senname + ": Not attempted with "+ i.__str__()+"\n" else: status += senname + ": " q = writeYourRep.writerep_general(sen, i) status += writeYourRep.getStatus(q) +", " except Exception, e: import traceback; traceback.print_exc() status=status + ' failed: ' + e.__str__() file(statfile, 'a').write('%s %s, %s, "%s"\n' % (i.fname, i.lname, i.state, status))
def csv_Send_To_House_With_Exclusions(csvfile='demo-dataz.csv', messagefile="noCispaMessage.txt", statfile='csv_Send_To_House.log', dryrun=False, excludedDists=[]): ''' Parse from the csv file ''' writeYourRep = WriteYourRep() reader = csv.DictReader(open(csvfile, 'rb'), delimiter='\t') genderassigner = GenderLookup() (subject, message) = parseMessageFile(messagefile) for row in reader: state = 'unknown' status = "" try: i = csv_To_Data(row, writeYourRep, genderassigner, subject, message) alldists = writeYourRep.getWyrDistricts(i.zip5) culleddists = [ dist for dist in alldists if dist not in excludedDists ] distListStr = ' '.join(alldists) if dryrun or len(culleddists) == 0: status += distListStr + " " + ": Not attempted with " + i.__str__( ) + "\n" else: (dist, q) = writeYourRep.write_reps_in_dists(i, culleddists) status += dist + " " + writeYourRep.getStatus(q) + ", " except Exception, e: import traceback traceback.print_exc() status = status + ' failed: ' + e.__str__() file(statfile, 'a').write('%s %s, %s, "%s"\n' % (i.fname, i.lname, i.state, status))
def csv_Send_To_House(csvfile='demo-dataz.csv', messagefile="noCispaMessage.txt", statfile='csv_Send_To_House.log', dryrun=False, onedistrict=None, jsoninput=False): ''' Parse from the csv file ''' writeYourRep = WriteYourRep() if not jsoninput: # default is csv reader = csv.DictReader(open(csvfile, 'rb'), delimiter='\t') else: try: reader = json.load(open(csvfile, 'r')) except: reader = load_json(csvfile) genderassigner = GenderLookup() (subject, message) = parseMessageFile(messagefile) for row in reader: i = None state='unknown' status = "" try: i = row_dict_to_data(row, writeYourRep, genderassigner, subject, message) if onedistrict!=None: if DEBUG: print "Feature of specifying one district is not supported yet" return if dryrun: distListStr=' '.join(writeYourRep.getWyrDistricts(i.zip5)) status += distListStr + " " + ": Not attempted with "+ i.__str__()+"\n" else: q = writeYourRep.writerep(i) status += i.dist + ": " status += writeYourRep.getStatus(q) except Exception, e: traceback.print_exc() status=status + ' failed: ' + e.__str__() if i is not None: file(statfile, 'a').write('%s %s, %s, "%s"\n' % (i.fname.encode('utf-8', 'ignore'), i.lname.encode('utf-8', 'ignore'), i.state, status))
def csv_Send_To_House(csvfile='demo-dataz.csv', messagefile="noCispaMessage.txt", statfile='csv_Send_To_House.log', dryrun=False, onedistrict=None): ''' Parse from the csv file ''' writeYourRep = WriteYourRep() reader = csv.DictReader(open(csvfile, 'rb')) genderassigner = GenderLookup() (subject, message) = parseMessageFile(messagefile) for row in reader: state = 'unknown' status = "" try: i = csv_To_Data(row, writeYourRep, genderassigner, subject, message) if onedistrict != None: print "Feature of specifying one district is not supported yet" return if dryrun: distListStr = ' '.join(writeYourRep.getWyrDistricts(i.zip5)) status += distListStr + " " + ": Not attempted with " + i.__str__( ) + "\n" else: q = writeYourRep.writerep(i) status += writeYourRep.getStatus(q) + ", " except Exception, e: import traceback traceback.print_exc() status = status + ' failed: ' + e.__str__() file(statfile, 'a').write('%s %s, %s, "%s"\n' % (i.fname, i.lname, i.state, status))
def csv_Send_To_House(csvfile='demo-dataz.csv', messagefile="noCispaMessage.txt", statfile='csv_Send_To_Senate.log', dryrun=False): ''' Parse from the csv file ''' import csv from ZipLookup import ZipLookup from GenderLookup import GenderLookup writeYourRep = WriteYourRep() reader = csv.reader(open(csvfile, 'rb')) genderassigner = GenderLookup() (subject, message) = parseMessageFile(messagefile) zipLookup = ZipLookup() for row in reader: state='unknown' status = "" try: #First_Name,Email,Street,Zip (first_name, email, addr1, zip5) = row last_name = "" addr2="" zip4 = None (first_name, last_name) = cleanName(first_name, last_name) if zip5.find('-')>0: zip4 = zip5.split('-')[1] zip5 = zip5.split('-')[0] (first_name, last_name) = cleanName(first_name, last_name) zip5=zip5.zfill(5) (city, state) = zipLookup.getCityAndState(zip5) #print "found city and state: %s, %s" % (city, state) i = writeYourRep.prepare_i(state+"_" + "XX") #hack, need dist for prepare_i if email: #print email i.email=email if first_name: # this code below was used when a single name field was given # and not separate first and last name fields #names = name.split() #i.fname = "".join(iter(names[0:len(names)-1])) #i.lname = names[-1] i.fname = first_name i.lname = last_name i.prefix = genderassigner.getPrefix(i.fname) i.id = "%s %s" % (first_name, last_name) if addr1: i.addr1 = addr1 i.addr2 = addr2 if zip5: i.zip5 = zip5 i.zip4 = '0001' if zip4: i.zip4 = zip4 if city: i.city = city if message: i.full_msg = message if subject: i.subject = subject if dryrun: print "getting link for i:", i.addr1, i.addr2 contactLink=writeYourRep.getWyrContactLink(i) status += contactLink + " " + ": Not attempted with "+ i.__str__()+"\n" else: q = writeYourRep.writerep(i) status += writeYourRep.getStatus(q) +", " except Exception, e: import traceback; traceback.print_exc() status=status + ' failed: ' + e.__str__() file(statfile, 'a').write('%s %s, %s, "%s"\n' % (first_name, last_name, state, status))
def csv_Send_To_Senate(csvfile='demo-dataz.csv', messagefile="noCispaMessage.txt", statfile='csv_Send_To_Senate.log', dryrun=False, onesenator=None): ''' Parse from the csv file Problem forms: www.vitter.senate.gov Problem with link to contact page on senators website www.mccain.senate.gov unknown url type, need to look into this one further levin.senate.gov urllib2.URLError: <urlopen error [Errno 61] Connection refused> www.lieberman.senate.gov Unclear whether it worked or not. franken.senate.gov Unclear whether it worked or not. www.toomey.senate.gov CAPTCHA www.sessions.senate.gov CAPTCHA www.shelby.senate.gov CAPTCHA www.coburn.senate.gov CAPTCHA www.crapo.senate.gov CAPTCHA www.moran.senate.gov CAPTCHA www.roberts.senate.gov CAPTCHA www.paul.senate.gov CAPTCHA ''' import csv from ZipLookup import ZipLookup from GenderLookup import GenderLookup writeYourRep = WriteYourRep() reader = csv.DictReader(open(csvfile, 'rb')) genderassigner = GenderLookup() (subject, message) = parseMessageFile(messagefile) zipLookup = ZipLookup() for row in reader: state = 'unknown' status = "" try: i = csv_To_Data(row, writeYourRep, genderassigner, subject, message) sens = writeYourRep.getSenators(i.state) for sen in sens: if onesenator != None and onesenator not in sen: status += "%s: not attempted, " % (sen) continue print "Writing to senator %s" % sen senname = web.lstrips( web.lstrips(web.lstrips(sen, 'http://'), 'https://'), 'www.').split('.')[0] captchaforms = [ 'toomey', 'sessions', 'shelby', 'coburn', 'crapo', 'roberts', 'paul' ] if senname in captchaforms: status += senname + " has captcha. skipping. " elif dryrun: status += sen + " " + senname + ": Not attempted with " + i.__str__( ) + "\n" else: status += senname + ": " q = writeYourRep.writerep_general(sen, i) status += writeYourRep.getStatus(q) + ", " except Exception, e: import traceback traceback.print_exc() status = status + ' failed: ' + e.__str__() file(statfile, 'a').write('%s %s, %s, "%s"\n' % (i.fname, i.lname, i.state, status))
def csv_Send_To_Senate(csvfile='demo-dataz.csv', messagefile="noCispaMessage.txt", statfile='csv_Send_To_Senate.log', dryrun=False): ''' Parse from the csv file Problem forms: www.vitter.senate.gov Problem with link to contact page on senators website www.mccain.senate.gov unknown url type, need to look into this one further levin.senate.gov urllib2.URLError: <urlopen error [Errno 61] Connection refused> www.lieberman.senate.gov Unclear whether it worked or not. franken.senate.gov Unclear whether it worked or not. www.toomey.senate.gov CAPTCHA www.sessions.senate.gov CAPTCHA www.shelby.senate.gov CAPTCHA www.coburn.senate.gov CAPTCHA www.crapo.senate.gov CAPTCHA www.moran.senate.gov CAPTCHA www.roberts.senate.gov CAPTCHA www.paul.senate.gov CAPTCHA ''' import csv from ZipLookup import ZipLookup from GenderLookup import GenderLookup writeYourRep = WriteYourRep() reader = csv.reader(open(csvfile, 'rb')) genderassigner = GenderLookup() (subject, message) = parseMessageFile(messagefile) zipLookup = ZipLookup() for row in reader: state='unknown' status = "" try: #print len(row) #first_name = '' #last_name = '' #(id, first_name, last_name, email, addr1, zip5) = row (first_name,last_name,email,addr1,addr2,city,state,region,zip5,country) = row (first_name, last_name) = cleanName(first_name, last_name) if zip5.find('-')>0: zip4 = zip5.split('-')[1] zip5 = zip5.split('-')[0] (first_name, last_name) = cleanName(first_name, last_name) addr2="" zip5=zip5.zfill(5) (city, state) = zipLookup.getCityAndState(zip5) print "found city and state: %s, %s" % (city, state) i = writeYourRep.prepare_i(state+"_" + "01") #hack, need dist for prepare_i if email: print email i.email=email if first_name: # this code below was used when a single name field was given # and not separate first and last name fields #names = name.split() #i.fname = "".join(iter(names[0:len(names)-1])) #i.lname = names[-1] i.fname = first_name i.lname = last_name i.prefix = genderassigner.getPrefix(i.fname) i.id = "%s %s" % (first_name, last_name) if addr1: i.addr1 = addr1 i.addr2 = addr2 if zip5: i.zip5 = zip5 i.zip4 = '0001' if zip4: i.zip4 = zip4 if city: i.city = city if message: i.full_msg = message if subject: i.subject = subject sens = writeYourRep.getSenators(state) for sen in sens: senname = web.lstrips(web.lstrips(web.lstrips(sen, 'http://'), 'https://'), 'www.').split('.')[0] customizedmessage=message.replace('[[NAME]]', "%s %s" % (first_name, last_name)).replace('[[CITY]]', city).replace('[[STATE]]', state).replace('[[SENATOR]]', senname.title()) i.full_msg = customizedmessage captchaforms=['toomey','sessions','shelby','coburn','crapo','moran','roberts','paul'] if senname in captchaforms: status += senname + " has captcha. skipping. " elif dryrun: status += sen + " " + senname + ": Not attempted with "+ i.__str__()+"\n" else: status += senname + ": " q = writeYourRep.writerep_general(sen, i) status += writeYourRep.getStatus(q) +", " except Exception, e: import traceback; traceback.print_exc() status=status + ' failed: ' + e.__str__() file(statfile, 'a').write('%s %s, %s, "%s"\n' % (first_name, last_name, state, status))