Advertisement
Guest User

NYT election data analyzer

a guest
Nov 12th, 2020
1,690
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.81 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. """ HOW TO USE SCRIPT:
  4.    Download data from nyt like below:
  5.        https://static01.nyt.com/elections-assets/2020/data/api/2020-11-03/race-page/alabama/president.json
  6.    Change "alabama" to the state you want to download. Use hyphens for states w space in their name like "new-york".
  7.    Place all the .json files in the same directory as this script.
  8.    If you want to run only specific state(s), place the .json files ONLY for those state(s) in the script's directory.
  9.    Run the script. If you are not using an IDE like SPYDER you might have to add a line for plt.show().
  10.    
  11.    Version 2020/11/12-12:00
  12. """
  13.  
  14. import json
  15. import matplotlib.pyplot as plt
  16. from datetime import datetime
  17. import os
  18.  
  19. import locale
  20. locale.setlocale(locale.LC_ALL, '')
  21.  
  22.  
  23. def findfraud():
  24.     stateFiles = [x for x in os.listdir(".") if x.endswith(".json")]
  25.    
  26.     switchTimes = [] #-number of times data got switched
  27.    
  28.     for state in stateFiles:
  29.         with open(state, encoding="utf8") as f:
  30.             x = json.load(f)
  31.  
  32.         series = x["data"]["races"][0]["timeseries"]
  33.  
  34.         trumpTotal = 0
  35.         bidenTotal = 0
  36.    
  37.         trumpLoss = 0
  38.         bidenLoss = 0
  39.    
  40.         trumpWin = 0
  41.         bidenWin = 0
  42.    
  43.         shiftedToBiden = 0
  44.         shiftedToTrump = 0
  45.         shiftedFromBiden = 0
  46.         shiftedFromTrump = 0
  47.    
  48.         shiftsToBiden = 0
  49.         shiftsToTrump = 0
  50.  
  51.         for i in range(1, len(series)):
  52.            
  53.             curr =  series[i]
  54.             prev = series[i-1]
  55.    
  56.             votes = curr["votes"]
  57.             prevVotes = prev["votes"]
  58.    
  59.             trumpShare = curr["vote_shares"]["trumpd"]
  60.             trumpVotes = votes * trumpShare
  61.             prevTrumpShare = prev["vote_shares"]["trumpd"]
  62.             prevTrumpVotes = prevVotes * prevTrumpShare
  63.    
  64.             bidenShare = curr["vote_shares"]["bidenj"]
  65.             bidenVotes = votes * bidenShare
  66.             prevBidenShare = prev["vote_shares"]["bidenj"]
  67.             prevBidenVotes = prevVotes * prevBidenShare
  68.    
  69.             trumpDelta = trumpVotes - prevTrumpVotes
  70.             bidenDelta = bidenVotes - prevBidenVotes
  71.    
  72.             #print("INDEX " + str(i))
  73.    
  74.             if trumpVotes < prevTrumpVotes:
  75.                 trumpLoss += abs(trumpDelta)
  76.                 trumpTotal += trumpDelta
  77.                 #print ("TRUMP LOSS: " + str(trumpDelta))
  78.    
  79.             if bidenVotes < prevBidenVotes:
  80.                 bidenLoss += abs(bidenDelta)
  81.                 bidenTotal += bidenDelta
  82.                 #print ("BIDEN LOSS: " + str(bidenDelta))
  83.    
  84.             if trumpVotes > prevTrumpVotes:
  85.                 trumpWin += abs(trumpDelta)
  86.                 trumpTotal += trumpDelta
  87.                 #print ("TRUMP WIN: " + str(trumpDelta))
  88.    
  89.             if bidenVotes > prevBidenVotes:
  90.                 bidenWin += abs(bidenDelta)
  91.                 bidenTotal += bidenDelta
  92.                 #print ("BIDEN WIN: " + str(bidenDelta))
  93.    
  94.             #if trumpVotes == prevTrumpVotes:
  95.                 #print ("TRUMP NEUTRAL: " + str(trumpDelta))
  96.    
  97.             #if bidenVotes == prevBidenVotes:
  98.                 #print ("BIDEN NEUTRAL: " + str(bidenDelta))
  99.    
  100.    
  101.             if trumpVotes < prevTrumpVotes and bidenVotes > prevBidenVotes:
  102.                 shiftsToBiden+=1
  103.                 shiftedToBiden += bidenDelta
  104.                 shiftedFromTrump += -trumpDelta
  105.                 switchTimes.append(extract_timestamp(curr['timestamp']))
  106.    
  107.             if trumpVotes > prevTrumpVotes and bidenVotes < prevBidenVotes:
  108.                 shiftsToTrump+=1
  109.                 shiftedToTrump += trumpDelta
  110.                 shiftedFromBiden += -bidenDelta
  111.                 switchTimes.append(extract_timestamp(curr['timestamp']))
  112.         """
  113.        #-PRINT STATEMENTS FROM ORIGINAL AUTHOR
  114.        print("CALCULATION COMPLETE")
  115.        print("-----")
  116.        print("Total sum of votes gained/lost for both candidates")
  117.        print("TRUMP TOTAL        " + "{:>12,d}".format(int(trumpTotal)))
  118.        print("BIDEN TOTAL        " + "{:>12,d}".format(int(bidenTotal)))
  119.        print("-----")
  120.        print("No. times 'A' gained votes while 'B' lost votes")
  121.        print("SHIFTS TO TRUMP    " + "{:>12,d}".format(int(shiftsToTrump)))
  122.        print("SHIFTS TO BIDEN    " + "{:>12,d}".format(int(shiftsToBiden)))
  123.        print("-----")
  124.        print("No. votes that has been shifted")
  125.        print("SHIFTED TO TRUMP   " + "{:>12,d}".format(int(shiftedToTrump)))
  126.        print("SHIFTED FROM TRUMP " + "{:>12,d}".format(int(shiftedFromTrump)))
  127.        print("SHIFTED TO BIDEN   " + "{:>12,d}".format(int(shiftedToBiden)))
  128.        print("SHIFTED FROM BIDEN " + "{:>12,d}".format(int(shiftedFromBiden)))
  129.        print("-----")
  130.        print("Total votes gained and lost")
  131.        print("TRUMP WIN          " + "{:>12,d}".format(int(trumpWin)))
  132.        print("TRUMP LOSS         " + "{:>12,d}".format(int(trumpLoss)))
  133.        print("BIDEN WIN          " + "{:>12,d}".format(int(bidenWin)))
  134.        print("BIDEN LOSS         " + "{:>12,d}".format(int(bidenLoss)))
  135.        """
  136.    
  137.     """HERE IS HOW TO SWITCH BETWEEN SINGLE DAY AND MANY DAYS"""
  138.    
  139.     #-Use the following line to get Nov 4th only:    
  140.     # switchTimes = Nov4Only(switchTimes)
  141.    
  142.     #-Update titles and axes labels below for what you are plotting:
  143.     fig, ax = plt.subplots(figsize=(10,6))
  144.     ax.tick_params(axis='x', which='major', labelsize=6.5)
  145.     ax.set_ylabel('Number of times votes \'switched\'')
  146.     ax.set_xlabel('Date')
  147.     ax.set_title('Votes \'Switched\' - All 50 States + D.C. - 11/3/2020->Onward\nAnalysis of data obtained from NYT')
  148.    
  149.     #-Use the following line for entire dataset from 11/3 onward
  150.     ax.hist(switchTimes, bins=183)
  151.    
  152.     #-Use the following 3 lines for 24 hour time period only; e.g.: Nov 4th only
  153.     # ax.set_xticks(range(0, 86400, 3600)) #-sets each tick on the x axis to be 1 hour (the numbers are in seconds)
  154.     # plt.xlim(0, 86400) #-sets limit to full day in case there is no data at beginning or end
  155.     # ax.hist(switchTimes, bins=range(0, 90000, 3600), rwidth=0.9) #-Each bin to correspond to each hour
  156.  
  157.    
  158. def extract_timestamp(timeStr):
  159.     """ Extracts timestamp from datum """
  160.     #-Extract date and time
  161.     return datetime.strptime(timeStr, "%Y-%m-%dT%H:%M:%SZ")
  162.  
  163.     #-Extract time of day only
  164.     #return datetime.strptime(timeStr, "%Y-%m-%dT%H:%M:%SZ").time()
  165.  
  166. def Nov4Only(switchTimes):
  167.     """ use only nov 4th data """
  168.    
  169.     newList = []
  170.    
  171.     for switch in switchTimes:
  172.         if (switch >= datetime(2020, 11, 4, 0, 0) and switch < datetime(2020, 11, 5, 0, 0)):
  173.             newList.append(switch.time()) #-add only if on Nov 4th, and remove date
  174.            
  175.     return newList
  176.    
  177.            
  178. if __name__ == "__main__":
  179.     findfraud()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement