Skip to content

Instantly share code, notes, and snippets.

@cole-wilson
Created May 20, 2024 03:57
Show Gist options
  • Save cole-wilson/d4b753e3e9399f7370b94eddc68ac32f to your computer and use it in GitHub Desktop.
Save cole-wilson/d4b753e3e9399f7370b94eddc68ac32f to your computer and use it in GitHub Desktop.
AP Statistics Final Project: Is FRC pay-to-win?
from itertools import count
import sys
import re
import random
import requests as req_orig
import requests_cache
import statbotics
from code import interact
import geocoder
requests = requests_cache.CachedSession('demo_cache')
apiKey = open(".apikey").read().strip()
sb = statbotics.Statbotics()
def getTBA(path): return requests.get("https://www.thebluealliance.com/api/v3"+path, headers={"X-TBA-Auth-Key":apiKey}).json()
def printLine(*args):
out = ""
for arg in args:
out += str(arg).ljust(30) + "\t"
print(out.strip())
import csv
from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="apstats_cole_wilson")
incomes = {}
with open('income.csv', newline='') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',', quotechar='"')
prevcounty = ""
for row in spamreader:
state, county, income = row
if prevcounty != "" and income != "":
if state not in incomes:
incomes[state] = {}
incomes[state.strip()][county.strip()] = float(income)
# print(county, state)
prevcounty = county
# print(geolocator.geocode(f"{county}, {state}").latitude, county, state)
# city_dict = {} # 0 1 2 3 4 5
# with open('cities.csv', newline='') as csvfile: #"city","city_ascii","state_id","state_name","county_fips","county_name","lat","lng","population","density","source","military","incorporated","timezone","ranking","zips","id"
# spamreader = csv.reader(csvfile, delimiter=',', quotechar='"')
# for row in spamreader:
# city_dict[(row[0], row[3])] = row[5]
page = 0
teams = []
while True:
data = getTBA(f"/teams/2024/{page}/simple")
if len(data) == 0:
break
teams += data
page += 1
# print(teams)
printLine("Team Number", "Income", "EPA", "County", "State", "Coords", "Name")
teams = list(filter(lambda i:i["country"]=="USA", teams))
print(len(teams))
teams = random.sample(teams, k=281)
i = 0
for team in teams: # state_prov name city team_number
sys.stderr.write(f"{i}/281\r")
i+=1
city = team["city"]
# print(team)
state = team["state_prov"]
location = geolocator.geocode(f"{city}, {state}")
coords = (location.latitude, location.longitude)
location = location.address
county = re.findall(r"(\w[\w\s]*) County", location, re.MULTILINE)
if len(county) == 0:
sys.stderr.write(f"county for {location} >> ")
sys.stderr.flush()
county = input()
else:
county = county[0].replace("Saint", "St.")
try:
income = incomes[state.strip()][county.strip()]
except KeyError:
sys.stderr.write(f"nothing found for county {county}, {state}. income?? >> ")
sys.stderr.flush()
income = float(input())
# interact(local=locals())
epa = sb.get_team_year(team["team_number"], year=2024)["epa_end"]
printLine(team["team_number"], income, epa, county, state, coords, team["nickname"])
# print(city, state, location.address.split(",")[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment