Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Accessing nytimes election data for procrastination purposes
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