def export_by_state(self, states): bucket = get_bucket(self.production) for fips in states: state = Division.objects.get(level=self.STATE_LEVEL, code=fips) print('>> Exporting: {}'.format(state.code)) state_data = self.aggregate_counties(state) self.export_state_files(bucket, state, state_data)
def bake(self, key, data, content_type, production=False): bucket = get_bucket(production) bucket.put_object(Key=key, ACL=defaults.ACL, Body=data, CacheControl=defaults.CACHE_HEADER, ContentType=content_type)
def handle(self, *args, **options): print('> Publishing statics') self.bucket = get_bucket(options['production']) print('> >> Upgrading dependencies') subprocess.run(['yarn', 'upgrade'], cwd='theshow/staticapp/') print('> >> Building statics') subprocess.run(['gulp', 'build'], cwd='theshow/staticapp/') hash = options['hash'] base_key = 'elections/cdn/{0}'.format(options['election']) for file in glob('theshow/static/theshow/js/*'): filename, ext = os.path.splitext(file.split('/')[-1]) key = os.path.join(base_key, 'js', '{}-{}{}'.format(filename, hash, ext)) self.upload(file, key, 'text/javascript') for file in glob('theshow/static/theshow/css/*'): filename, ext = os.path.splitext(file.split('/')[-1]) key = os.path.join(base_key, 'css', '{}-{}{}'.format(filename, hash, ext)) self.upload(file, key, 'text/css') for file in glob('theshow/static/theshow/images/*'): filename = file.split('/')[-1] key = os.path.join(base_key, 'images', filename) self.upload(file, key, 'image/jpeg')
def handle(self, *args, **options): va = Division.objects.get(code='51') counties = va.children.all() data = [] state_json = '{0}/Statewide.json'.format(self.base_url) r = requests.get(state_json) response = r.json() candidates = self.parse_data(response) for candidate in candidates: data.append(candidate) for county in counties: print(county.name, county.code) if county.code == '51097': url = '{0}/Locality/{1}/Index.json'.format( self.base_url, 'KING%20&%20QUEEN%20COUNTY') else: url = '{0}/Locality/{1}/Index.json'.format( self.base_url, county.name.upper().replace(' ', '%20')) r = requests.get(url) if r.status_code == 200: response = r.json() candidates = self.parse_data(response) for candidate in candidates: data.append(candidate) with open('sos.json', 'w') as f: json.dump(data, f) bucket = get_bucket() bucket.put_object(Key='elections/2017/virginia/governor/results.json', ACL=defaults.ACL, Body=json.dumps(data), CacheControl=defaults.CACHE_HEADER, ContentType='application/json') date = check_output('date') date_obj = {"date": date} bucket.put_object( Key='elections/2017/virginia/governor/last-updated.json', ACL=defaults.ACL, Body=json.dumps(date_obj), CacheControl=defaults.CACHE_HEADER, ContentType='application/json')
def handle(self, *args, **options): print('Exporting geographies') states = options['states'] bucket = get_bucket(production=True) STATE_LEVEL = DivisionLevel.objects.get(name=DIVISION_LEVELS['state']) COUNTY_LEVEL = DivisionLevel.objects.get( name=DIVISION_LEVELS['county']) for state in tqdm(states): division = Division.objects.get(level=STATE_LEVEL, code=state) # TODO Add a concept of years geography = division.geographies.get( subdivision_level=COUNTY_LEVEL) key = os.path.join(OUTPUT_PATH, 'state', division.code, 'counties.json') bucket.put_object(Key=key, ACL=defaults.ACL, Body=json.dumps(geography.topojson), CacheControl=defaults.CACHE_HEADER, ContentType='application/json')
def handle(self, *args, **options): bucket = get_bucket(True) census = Census(os.getenv('CENSUS_API_KEY')) response = requests.get(url) f = StringIO(response.content.decode('utf-8')) reader = csv.DictReader(f, delimiter=',') records = [r for r in reader] states = options['states'] for state in states: postal = us.states.lookup(state).abbr counties = census.sf1.get('NAME', geo={ 'for': 'county:*', 'in': 'state:{}'.format(state) }) state_results = [ { **r, **{"fips": lookup_fips(r['county'], counties, state)} } for r in records if r['state'] == postal and r['county'] != 'Total' ] total_votes = self.summarize_total_votes(state_results) gop_results = [ { "party": 'GOP', "fips": r['fips'], "votes": int(r['votes']), "total_votes": total_votes[r['fips']], } for r in list(filter( lambda row: row['party'] in GOP_PARTY_NAMES, state_records ))] dem_results = [ { "party": 'Dem', "fips": r['fips'], "votes": int(r['votes']), "total_votes": total_votes[r['fips']], } for r in list(filter( lambda row: row['party'] in DEM_PARTY_NAMES, state_records ))] all_results = dem_results + gop_results key = os.path.join( OUTPUT_PATH, slugify(us.states.lookup(state).name), 'data.json' ) bucket.put_object( Key=key, ACL=defaults.ACL, Body=json.dumps(all_results), CacheControl=defaults.CACHE_HEADER, ContentType='application/json' )