Created
November 6, 2020 08:39
-
-
Save ivirshup/0d545a29f86d8b78dabbacaef7629c94 to your computer and use it in GitHub Desktop.
Accessing nytimes election data for procrastination purposes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import pandas as pd | |
def fetch_state_results(state: str) -> dict: | |
r = requests.get(f"https://static01.nyt.com/elections-assets/2020/data/api/2020-11-03/race-page/{state}/president.json") | |
return r.json()["data"] | |
def _process_records(input: "list[dict]") -> pd.DataFrame: | |
records = [] | |
for el in input: | |
f_el = {} | |
for k, v in el.items(): | |
if isinstance(v, dict): | |
f_el.update({f"{k}.{subk}": subv for subk, subv in v.items()}) | |
else: | |
f_el[k] = v | |
records.append(f_el) | |
return pd.DataFrame.from_records(records) | |
class StateResults(object): | |
def __init__(self, state): | |
self.state = state | |
self._data = fetch_state_results(state) | |
self._created = pd.Timestamp.now() | |
def __repr__(self): | |
return f"< StateResults for '{self.state}', created at {self._created} >" | |
def _get_races(self): | |
races = self._data["races"] | |
assert len(races) == 1, f"Length of 'races' list not 1, was {len(races)}. Bad assumption." | |
return races[0] | |
def get_updated(self): | |
return StateResults(self._state) | |
def county(self): | |
return _process_records(self._get_races()["counties"]) | |
def timeseries(self): | |
df = _process_records(self._get_races()["timeseries"]) | |
df["timestamp"] = df["timestamp"].apply(pd.Timestamp) | |
return df | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment