def do_rankings_for_season(season: int): RANKINGS = seed_path / f"rankings-{season}.csv" rankings = util.load_csv(RANKINGS, headers=True, sort="Wk") weeks = { week.number: week for week in list_resources(models.Week, season=season) } for rank in rankings: team_name = util.strip_record(rank.get('School')) team = lookup_team_by_external_id(source="SportsReference", identifier=team_name) week = weeks.get(int(rank.get("Wk"))) prior_week = weeks.get(week.number - 1, None) prior_ranking = None if prior_week: prior_ranking = models.Ranking.query\ .filter_by(week_id=prior_week.id)\ .filter_by(poll_id=1)\ .filter_by(team_id=team.id)\ .first() prior_rank = prior_ranking.rank if prior_ranking else None ranking = models.Ranking(week_id=week.id, poll_id=1, team_id=team.id, rank=rank.get("Rk"), prior_rank=prior_rank) db.session.add(ranking) db.session.commit() logger.warning(f"added rankings for {season} to database...")
def insert_weeks(): WEEKS = seed_path / "weeks.csv" weeks = util.load_csv(WEEKS, headers=True) for week in weeks: week["start_date"] = parser.parse(week["start_date"]).date() week["end_date"] = parser.parse(week["end_date"]).date() db.session.add(models.Week(**week)) db.session.commit() logger.warning(f"created weeks for rankings...") return 0
def insert_divisions(): DIVS = seed_path / "divisions.csv" conferences = {c.code: c for c in list_resources(models.Conference)} divisions = util.load_csv(DIVS, headers=True) for division in divisions: conf_code = division.pop("conference") conference = conferences.get(conf_code) div = create_resource(models.Division, conference_id=conference.id, name=division.get("name")) logger.debug(f"added {div} to database...") logger.warning(f"added divisions to database...") return 0
def insert_external_team_mappings(f_path, ds_code: str): mappings = util.load_csv(f_path, headers=True) teams = {t.code: t for t in list_resources(models.Team)} sports_reference = meta.DataSource.query.filter_by(code=ds_code).one() for m in mappings: team = teams.get(m.get("hatchet-code")) value = m.get("school") ext_id = m.get("external-id", None) if not team: logger.error(f"unable to get team from {m.get('hatchet-code')}") mapper_model = meta.ExternalTeamIdentifier( team_id=team.id, source_id=sports_reference.id, value=value, external_id=ext_id) db.session.add(mapper_model) db.session.commit() logger.warning(f"created {ds_code} team mappings...") return 0
def insert_stadiums(): STADIUMS = seed_path / "stadiums.csv" surfaces = {s.code: s for s in list_resources(models.Surface)} raw_stadiums = util.load_csv(STADIUMS, headers=True) for s in raw_stadiums: surface = surfaces.get(s.get("surface")) stadium = create_resource(models.Stadium, code=s.get("code"), name=s.get("name"), state=s.get("state"), city=s.get("city"), longitude=s.get("longitude"), latitude=s.get("latitude"), built=s.get("built"), capacity=s.get("capacity"), surface_id=surface.id if surface else None) logger.debug(f"creating {stadium} in db...") logger.warning(f"created stadiums in database...") return 0
def insert_teams(): TEAMS = seed_path / "teams.csv" conferences = {c.code: c for c in list_resources(models.Conference)} divisions = {(d.conference.code, d.name): d for d in list_resources(models.Division)} stadiums = {s.code: s for s in list_resources(models.Stadium)} raw_team_data = util.load_csv(TEAMS, headers=True) for t in raw_team_data: conf_code = t.pop("conference") div_name = t.pop("division") conf = conferences.get(conf_code) division = divisions.get((conf_code, div_name)) stadium = stadiums.get(t.get("stadiumId")) team = create_resource(models.Team, conference_id=conf.id, division_id=division.id, code=t.get("code"), name=t.get("name"), short_name=t.get("shortName"), mascot=t.get("mascot"), stadium_id=stadium.id if stadium else None) logger.warning(f"added teams to database...") return 0
def load_data(path: str) -> List[dict]: raw_data = load_csv(path, headers=True) return raw_data