def update(output_dir='data'): """ Update data from AP. """ client = AP(SECRETS['AP_FTP_USER'], SECRETS['AP_FTP_PASSWORD']) ticket = client.get_topofticket('2014-11-04') write_update(ticket, '%s/update.json' % output_dir) write_calls(ticket, '%s/calls.json' % output_dir)
def init(output_dir='data'): """ Initialize data from AP. """ client = AP(SECRETS['AP_FTP_USER'], SECRETS['AP_FTP_PASSWORD']) ticket = client.get_topofticket('2014-11-04') races = [] candidates = [] for race in ticket.races: races.append(process_race(race)) for candidate in race.candidates: candidates.append(process_candidate(candidate, race)) with open('%s/init_races.json' % output_dir, 'w') as f: json.dump(races, f, indent=4) with open('%s/init_candidates.json' % output_dir, 'w') as f: json.dump(candidates, f, indent=4)
def get_results(): """saves election results as json locally and on s3""" from elections import AP startday = datetime.date.today() client = AP(os.getenv('AP_USER'), os.getenv('AP_PASS')) fla = client.get_state('FL') # prez = fla.filter_races(office_name='President')[0] # prez_winner = None # sen = fla.filter_races(office_name='U.S. Senate')[0] # sen_winner = None # statewide = prez.get_reporting_unit('Florida1') # statewide_sen = sen.get_reporting_unit('Florida1') # obama_state_total, romney_state_total, nelson_state_total, mack_state_total = (0, 0, 0, 0) gov = fla.filter_races(office_name='Governor')[0] for each in gov.candidates: if each.party=='GOP': GOP = each if each.party=='Dem': DEM = each if each.party=='Lib': THIRD = each winner = None statewide = gov.get_reporting_unit('Florida1') gop_state_total, dem_state_total, third_state_total = 0, 0, 0 for each in statewide.results: if each.candidate.is_winner: winner=each.candidate.name if each.candidate.party=="GOP": gop_state_total=each.vote_total gop_state_comma=intcomma(each.vote_total) gop_state_pct=roundone(each.vote_total_percent) if each.candidate.party=="Dem": dem_state_total=each.vote_total dem_state_comma=intcomma(each.vote_total) dem_state_pct=roundone(each.vote_total_percent) if each.candidate.party=="Lib": third_state_total=each.vote_total third_state_comma=intcomma(each.vote_total) third_state_pct=roundone(each.vote_total_percent) state_gov_leading = "no leader" state_gov_victory_votes, state_gov_victory_comma = None, None if gop_state_total > dem_state_total and gop_state_total > third_state_total: state_gov_leading = GOP.last_name state_gov_victory_votes = gop_state_total - dem_state_total state_gov_victory_comma = intcomma(state_gov_victory_votes) elif dem_state_total > gop_state_total and dem_state_total > third_state_total: state_gov_leading = DEM.last_name state_gov_victory_votes = dem_state_total - gop_state_total state_gov_victory_comma = intcomma(state_gov_victory_votes) elif third_state_total > gop_state_total and third_state_total > dem_state_total: state_gov_leading = THIRD.last_name state_gov_victory_votes = third_state_total - dem_state_total state_gov_victory_comma = intcomma(state_gov_victory_votes) timestamp = datetime.datetime.now() # dbstamp = Timestamp(stamp=timestamp, page="county results") # dbstamp.save() # tstamp_pk = dbstamp.pk tstamp = print_date_style(timestamp) tstamp_slug = timestamp.strftime("%I%M%p") mapdict = {'winner': winner, 'victory_votes': state_gov_victory_votes, 'victory_comma': state_gov_victory_comma, 'tstamp': tstamp, '12': { 'name': "Florida", 'registered': statewide.num_reg_voters, 'stateVote': statewide.votes_cast, 'ptotal': statewide.precincts_total, 'pcount': statewide.precincts_reporting, 'ppct': roundone(statewide.precincts_reporting_percent), 'leading_gov': state_gov_leading, 'dem': dem_state_total, 'dem_comma': dem_state_comma, 'dem_pct': dem_state_pct, 'gop': gop_state_total, 'gop_comma': gop_state_comma, 'gop_pct': gop_state_pct, 'third': third_state_total, 'third_comma': third_state_comma, 'third_pct': third_state_pct, } } for each in gov.counties: if each.fips=='12086': FIPS = '12025' else: FIPS = each.fips mapdict[FIPS]={'name': each.name, 'pcount': each.precincts_reporting, 'ptotal': each.precincts_total, 'ppct': roundone(each.precincts_reporting_percent), 'vcast': each.votes_cast, 'registered': each.num_reg_voters, } for cand in each.results: if cand.candidate.party == 'GOP': mapdict[FIPS]['gop']=cand.vote_total mapdict[FIPS]['gop_comma']=intcomma(cand.vote_total) mapdict[FIPS]['gop_pct']=roundone(cand.vote_total_percent) elif cand.candidate.party == 'Dem': mapdict[FIPS]['dem']=cand.vote_total mapdict[FIPS]['dem_comma']=intcomma(cand.vote_total) mapdict[FIPS]['dem_pct']=roundone(cand.vote_total_percent) elif cand.candidate.party == 'Lib': mapdict[FIPS]['third']=cand.vote_total mapdict[FIPS]['third_comma']=intcomma(cand.vote_total) mapdict[FIPS]['third_pct']=roundone(cand.vote_total_percent) if mapdict[FIPS]['pcount']==0: mapdict[FIPS]['leading_gov']='no_results' elif mapdict[FIPS]['gop']>mapdict[FIPS]['dem']: mapdict[FIPS]['leading_gov']=GOP.last_name mapdict[FIPS]['victory_votes']=mapdict[FIPS]['gop']-mapdict[FIPS]['dem'] mapdict[FIPS]['victory_votes_comma']=intcomma(mapdict[FIPS]['victory_votes']) elif mapdict[FIPS]['dem']>mapdict[FIPS]['gop']: mapdict[FIPS]['leading_gov']=DEM.last_name mapdict[FIPS]['victory_votes']=mapdict[FIPS]['dem']-mapdict[FIPS]['gop'] mapdict[FIPS]['victory_votes_comma']=intcomma(mapdict[FIPS]['victory_votes']) else: mapdict[FIPS]['leading_gov']='tie' with open('/opt/django-projects/elections/data/voter_reg.json', 'r') as f: regd = json.loads(f.read()) for key in regd: mapdict[key]['registered']=regd[key]['reg'] map_out="var R%s = %s;" % (YEAR, json.dumps(mapdict)) data_out=json.dumps(mapdict) datafile = "/opt/django-projects/elections/data/countydata.json" with open(datafile, 'w') as f: f.write(data_out) datafile2 = "/opt/django-projects/elections/data/backups/countydata-%s.json" % tstamp_slug with open(datafile2, 'w') as f: f.write(data_out) rootkey="elections/%s/results/" % YEAR roots3="http://tbprojects.s3.amazonaws.com/%s" % rootkey jslug = "R%s.js" % YEAR time.sleep(2) # json bakery from boto.s3.key import Key s3=boto.connect_s3(os.getenv('S3_KEY'), os.getenv('S3_SECRET')) bucket = s3.create_bucket('tbprojects') k=Key(bucket) k.key="%sjs/R%s.js" % (rootkey, YEAR) k.content_type='application/javascript' k.set_contents_from_string(map_out) k.set_acl('public-read') URL1 = "%sjs/R%s.js" % (roots3, YEAR) print "baked R%s to:\n%s" % (YEAR, URL1) k.key="%sjs/backups/R%s_%s.js" % (rootkey, YEAR, tstamp_slug) k.content_type='application/javascript' k.set_contents_from_string(map_out) k.set_acl('public-read') print "baked R%s backup %s" % (YEAR, tstamp_slug)
def test_badlogin(self): client = AP("foo", "bar") self.assertRaises(BadCredentialsError, client.get_state, os.environ['TEST_STATE'])
def setUp(self): self.client = AP( os.environ['AP_USERNAME'], os.environ['AP_PASSWORD'] )
# Get config info username = cfg.get('google', 'username') password = cfg.get('google', 'password') sheet_key = cfg.get('google', 'spreadsheet') # Login to Google c = Client(auth=(username, password)) c.login() # Connect to AP and get the state file ap_username = cfg.get('ap', 'username') ap_password = cfg.get('ap', 'password') ap_state_code = cfg.get('ap', 'state') client = AP(ap_username, ap_password) state = client.get_state(ap_state_code) # Get our election results spreadsheet s = c.open_by_key(sheet_key) # Loop through all worksheets in the spreadsheet, parse them # and add them to a results list results = [] for sheet in s.worksheets(): results.append(parse(sheet, state)) # Write it all out to JSON json_result = json.dumps(results) json_out = open('data.json', 'w') json_out.write(json_result)
def test_badlogin(self): client = AP("foo", "bar") self.assertRaises(BadCredentialsError, client.get_state, "IA")
def setUp(self): self.client = AP(AP_USERNAME, AP_PASSWORD)