Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """This is just quickly put together; still not perfect (e.g., logic should be put in so that an (n)-digit number subtracted from an (n+1)-digit number resulting in an (n)-digit number rounds the result to have only two significant figure)."""
- import json
- import requests
- import sys
- import math
- round_to_n = lambda x, n: x if x == 0 else round(x, -int(math.floor(math.log10(abs(x)))) + (n - 1))
- statedata = {'Alabama': {'ts':[]},'Alaska': {'ts':[]},'Arizona': {'ts':[]},'Arkansas': {'ts':[]},'California': {'ts':[]},'Colorado': {'ts':[]},'Connecticut': {'ts':[]},'Delaware': {'ts':[]},'Florida': {'ts':[]},'Georgia': {'ts':[]},'Hawaii': {'ts':[]},'Idaho': {'ts':[]},'Illinois': {'ts':[]},'Indiana': {'ts':[]},'Iowa': {'ts':[]},'Kansas': {'ts':[]},'Kentucky': {'ts':[]},'Louisiana': {'ts':[]},'Maine': {'ts':[]},'Maryland': {'ts':[]},'Massachusetts': {'ts':[]},'Michigan': {'ts':[]},'Minnesota': {'ts':[]},'Mississippi': {'ts':[]},'Missouri': {'ts':[]},'Montana': {'ts':[]},'Nebraska': {'ts':[]},'Nevada': {'ts':[]},'New-Hampshire': {'ts':[]},'New-Jersey': {'ts':[]},'New-Mexico': {'ts':[]},'New-York': {'ts':[]},'North-Carolina': {'ts':[]},'North-Dakota': {'ts':[]},'Ohio': {'ts':[]},'Oklahoma': {'ts':[]},'Oregon': {'ts':[]},'Pennsylvania': {'ts':[]},'Rhode-Island': {'ts':[]},'South-Carolina': {'ts':[]},'South-Dakota': {'ts':[]},'Tennessee': {'ts':[]},'Texas': {'ts':[]},'Utah': {'ts':[]},'Vermont': {'ts':[]},'Virginia': {'ts':[]},'Washington': {'ts':[]},'West-Virginia': {'ts':[]},'Wisconsin': {'ts':[]},'Wyoming': {'ts':[]}}
- def main():
- for state in statedata:
- url = "https://static01.nyt.com/elections-assets/2020/data/api/2020-11-03/race-page/" + state.lower() + "/president.json"
- data = requests.get(url, allow_redirects=False)
- with open(state + '.json', 'wb') as f:
- f.write(data.content)
- with open(state + '.json', encoding="utf8") as f:
- statedata[state].update({'json': json.load(f)})
- for i, ts in enumerate(statedata[state]['json']['data']['races'][0]['timeseries']):
- statedata[state]['ts'].append({
- 'votes': ts['votes'],
- 'tshare': ts['vote_shares']['trumpd'],
- 'bshare': ts['vote_shares']['bidenj'],
- 'timestamp': ts['timestamp']
- })
- for i, ts in enumerate(statedata[state]['ts']):
- ts.update({
- 'tvotes_crude': ts['votes']*ts['tshare'],
- 'bvotes_crude': ts['votes']*ts['bshare']
- })
- ts.update({
- 'tvotes': round_to_n(ts['tvotes_crude'],3),
- 'bvotes': round_to_n(ts['bvotes_crude'],3)
- })
- if i == 0:
- ts.update({
- 'tdelta_crude': 0,
- 'bdelta_crude': 0,
- 'tdelta': 0,
- 'bdelta': 0
- })
- else:
- ts.update({
- 'tdelta_crude': ts['tvotes_crude'] - statedata[state]['ts'][i-1]['tvotes_crude'],
- 'bdelta_crude': ts['bvotes_crude'] - statedata[state]['ts'][i-1]['bvotes_crude'],
- 'tdelta': round_to_n(ts['tvotes'] - statedata[state]['ts'][i-1]['tvotes'],3),
- 'bdelta': round_to_n(ts['bvotes'] - statedata[state]['ts'][i-1]['bvotes'],3)
- })
- print(state + "[" + str(i) + "]\ttvotes: " + str(ts['tvotes']) + "\ttdelta: " + str(ts['tdelta']) + "\tbvotes: " + str(ts['bvotes']) + "\tbdelta: " + str(ts['bdelta']))
- if __name__ == "__main__":
- main()
RAW Paste Data