Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """ HOW TO USE SCRIPT:
- Download data from nyt like below:
- https://static01.nyt.com/elections-assets/2020/data/api/2020-11-03/race-page/alabama/president.json
- Change "alabama" to the state you want to download. Use hyphens for states w space in their name like "new-york".
- Place all the .json files in the same directory as this script.
- If you want to run only specific state(s), place the .json files ONLY for those state(s) in the script's directory.
- Run the script. If you are not using an IDE like SPYDER you might have to add a line for plt.show().
- Version 2020/11/12-12:00
- """
- import json
- import matplotlib.pyplot as plt
- from datetime import datetime
- import os
- import locale
- locale.setlocale(locale.LC_ALL, '')
- def findfraud():
- stateFiles = [x for x in os.listdir(".") if x.endswith(".json")]
- switchTimes = [] #-number of times data got switched
- for state in stateFiles:
- with open(state, encoding="utf8") as f:
- x = json.load(f)
- series = x["data"]["races"][0]["timeseries"]
- trumpTotal = 0
- bidenTotal = 0
- trumpLoss = 0
- bidenLoss = 0
- trumpWin = 0
- bidenWin = 0
- shiftedToBiden = 0
- shiftedToTrump = 0
- shiftedFromBiden = 0
- shiftedFromTrump = 0
- shiftsToBiden = 0
- shiftsToTrump = 0
- for i in range(1, len(series)):
- curr = series[i]
- prev = series[i-1]
- votes = curr["votes"]
- prevVotes = prev["votes"]
- trumpShare = curr["vote_shares"]["trumpd"]
- trumpVotes = votes * trumpShare
- prevTrumpShare = prev["vote_shares"]["trumpd"]
- prevTrumpVotes = prevVotes * prevTrumpShare
- bidenShare = curr["vote_shares"]["bidenj"]
- bidenVotes = votes * bidenShare
- prevBidenShare = prev["vote_shares"]["bidenj"]
- prevBidenVotes = prevVotes * prevBidenShare
- trumpDelta = trumpVotes - prevTrumpVotes
- bidenDelta = bidenVotes - prevBidenVotes
- #print("INDEX " + str(i))
- if trumpVotes < prevTrumpVotes:
- trumpLoss += abs(trumpDelta)
- trumpTotal += trumpDelta
- #print ("TRUMP LOSS: " + str(trumpDelta))
- if bidenVotes < prevBidenVotes:
- bidenLoss += abs(bidenDelta)
- bidenTotal += bidenDelta
- #print ("BIDEN LOSS: " + str(bidenDelta))
- if trumpVotes > prevTrumpVotes:
- trumpWin += abs(trumpDelta)
- trumpTotal += trumpDelta
- #print ("TRUMP WIN: " + str(trumpDelta))
- if bidenVotes > prevBidenVotes:
- bidenWin += abs(bidenDelta)
- bidenTotal += bidenDelta
- #print ("BIDEN WIN: " + str(bidenDelta))
- #if trumpVotes == prevTrumpVotes:
- #print ("TRUMP NEUTRAL: " + str(trumpDelta))
- #if bidenVotes == prevBidenVotes:
- #print ("BIDEN NEUTRAL: " + str(bidenDelta))
- if trumpVotes < prevTrumpVotes and bidenVotes > prevBidenVotes:
- shiftsToBiden+=1
- shiftedToBiden += bidenDelta
- shiftedFromTrump += -trumpDelta
- switchTimes.append(extract_timestamp(curr['timestamp']))
- if trumpVotes > prevTrumpVotes and bidenVotes < prevBidenVotes:
- shiftsToTrump+=1
- shiftedToTrump += trumpDelta
- shiftedFromBiden += -bidenDelta
- switchTimes.append(extract_timestamp(curr['timestamp']))
- """
- #-PRINT STATEMENTS FROM ORIGINAL AUTHOR
- print("CALCULATION COMPLETE")
- print("-----")
- print("Total sum of votes gained/lost for both candidates")
- print("TRUMP TOTAL " + "{:>12,d}".format(int(trumpTotal)))
- print("BIDEN TOTAL " + "{:>12,d}".format(int(bidenTotal)))
- print("-----")
- print("No. times 'A' gained votes while 'B' lost votes")
- print("SHIFTS TO TRUMP " + "{:>12,d}".format(int(shiftsToTrump)))
- print("SHIFTS TO BIDEN " + "{:>12,d}".format(int(shiftsToBiden)))
- print("-----")
- print("No. votes that has been shifted")
- print("SHIFTED TO TRUMP " + "{:>12,d}".format(int(shiftedToTrump)))
- print("SHIFTED FROM TRUMP " + "{:>12,d}".format(int(shiftedFromTrump)))
- print("SHIFTED TO BIDEN " + "{:>12,d}".format(int(shiftedToBiden)))
- print("SHIFTED FROM BIDEN " + "{:>12,d}".format(int(shiftedFromBiden)))
- print("-----")
- print("Total votes gained and lost")
- print("TRUMP WIN " + "{:>12,d}".format(int(trumpWin)))
- print("TRUMP LOSS " + "{:>12,d}".format(int(trumpLoss)))
- print("BIDEN WIN " + "{:>12,d}".format(int(bidenWin)))
- print("BIDEN LOSS " + "{:>12,d}".format(int(bidenLoss)))
- """
- """HERE IS HOW TO SWITCH BETWEEN SINGLE DAY AND MANY DAYS"""
- #-Use the following line to get Nov 4th only:
- # switchTimes = Nov4Only(switchTimes)
- #-Update titles and axes labels below for what you are plotting:
- fig, ax = plt.subplots(figsize=(10,6))
- ax.tick_params(axis='x', which='major', labelsize=6.5)
- ax.set_ylabel('Number of times votes \'switched\'')
- ax.set_xlabel('Date')
- ax.set_title('Votes \'Switched\' - All 50 States + D.C. - 11/3/2020->Onward\nAnalysis of data obtained from NYT')
- #-Use the following line for entire dataset from 11/3 onward
- ax.hist(switchTimes, bins=183)
- #-Use the following 3 lines for 24 hour time period only; e.g.: Nov 4th only
- # ax.set_xticks(range(0, 86400, 3600)) #-sets each tick on the x axis to be 1 hour (the numbers are in seconds)
- # plt.xlim(0, 86400) #-sets limit to full day in case there is no data at beginning or end
- # ax.hist(switchTimes, bins=range(0, 90000, 3600), rwidth=0.9) #-Each bin to correspond to each hour
- def extract_timestamp(timeStr):
- """ Extracts timestamp from datum """
- #-Extract date and time
- return datetime.strptime(timeStr, "%Y-%m-%dT%H:%M:%SZ")
- #-Extract time of day only
- #return datetime.strptime(timeStr, "%Y-%m-%dT%H:%M:%SZ").time()
- def Nov4Only(switchTimes):
- """ use only nov 4th data """
- newList = []
- for switch in switchTimes:
- if (switch >= datetime(2020, 11, 4, 0, 0) and switch < datetime(2020, 11, 5, 0, 0)):
- newList.append(switch.time()) #-add only if on Nov 4th, and remove date
- return newList
- if __name__ == "__main__":
- findfraud()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement