diff --git a/get_rankings.py b/get_rankings.py index d6f8440..c493f4d 100644 --- a/get_rankings.py +++ b/get_rankings.py @@ -2,6 +2,7 @@ from pathlib import Path from typing import Optional import tbaapiv3client from tbaapiv3client.rest import ApiException +import statbotics from pprint import pprint from datetime import datetime as dt @@ -19,25 +20,49 @@ config = tbaapiv3client.Configuration( config.verify_ssl = False +district = 'fim' -def get_rankings(api_client: tbaapiv3client.ApiClient, seasons: list[str]) -> dict[dict]: + +def get_rankings(api_client: tbaapiv3client.ApiClient, seasons: list[int]) -> dict[dict]: # Create an instance of the API class api_instance = tbaapiv3client.DistrictApi(api_client) rankings = {} for season in seasons: + year = int(season[:4]) try: results = api_instance.get_district_rankings(season) except ApiException as e: print("Exception when calling DistrictApi->get_district_rankings: %s\n" % e) else: + + epa_list:dict[int] = {} + + try: + sb = statbotics.Statbotics() + sb.session.verify = False + sb_results = sb.get_team_years(year=year,district=district, limit=1000, fields=['team', 'epa_end']) + except Exception as e: + print("Exception when calling Statbotics DistrictApi->get_district_rankings: %s\n" % e) + else: + for sb_result in sb_results: + epa_list[sb_result['team']] = sb_result['epa_end'] + for result in results: team = result.team_key + team_num = int(team[3:]) if team not in rankings: rankings[team] = {} + + epa = '' + + if team_num in epa_list: + epa = epa_list[team_num] + + points_event1 = None points_event1_na = None points_event2 = None @@ -56,12 +81,15 @@ def get_rankings(api_client: tbaapiv3client.ApiClient, seasons: list[str]) -> di rankings[team][season] = { "rank": result.rank, "point_total": result.point_total, + "epa_end": epa, "points_event1": points_event1, "points_event2": points_event2, "points_event1_na": points_event1_na, - "points_event2_na": points_event2_na + "points_event2_na": points_event2_na, + } + return rankings @@ -111,6 +139,7 @@ def write_power_rank(path: str | Path, sheet_name: str, rankings: list[dict], se rank_sum = 0 point_total_sum = 0 + epa_sum = 0 point_district_sum = 0 point_district_na_sum = 0 @@ -119,12 +148,14 @@ def write_power_rank(path: str | Path, sheet_name: str, rankings: list[dict], se for season in seasons: rank = '' point_total = '' + epa = '' point_district = '' point_district_na = '' if season in ranks: rank = ranks[season]['rank'] point_total = ranks[season]['point_total'] + epa = ranks[season]['epa_end'] point_district = 0 @@ -147,26 +178,33 @@ def write_power_rank(path: str | Path, sheet_name: str, rankings: list[dict], se point_district_sum += point_district point_district_na_sum += point_district_na + if epa != '': + epa_sum += epa + entry_count += 1 result[f'{season} Rank'] = rank result[f'{season} Point Total'] = point_total + result[f'{season} EPA'] = epa result[f'{season} Points District'] = point_district result[f'{season} Points District No Awards'] = point_district_na rank_avg = '' point_total_avg = '' + epa_avg = '' point_district_avg = '' point_district_na_avg = '' if entry_count > 0: rank_avg = rank_sum / entry_count point_total_avg = point_total_sum / entry_count + epa_avg = epa_sum / entry_count point_district_avg = point_district_sum / entry_count point_district_na_avg = point_district_na_sum / entry_count result[f'Avg Rank'] = rank_avg result[f'Avg Point Total'] = point_total_avg + result[f'Avg EPA'] = epa_avg result[f'Avg Points District'] = point_district_avg result[f'Avg Points District No Awards'] = point_district_na_avg @@ -175,7 +213,7 @@ def write_power_rank(path: str | Path, sheet_name: str, rankings: list[dict], se headers = ['team'] prefix_list = seasons.copy() prefix_list.append('Avg') - suffix_list = ['Rank', 'Point Total', + suffix_list = ['Rank', 'Point Total', 'EPA', 'Points District', 'Points District No Awards'] for suffix in suffix_list: @@ -185,6 +223,85 @@ def write_power_rank(path: str | Path, sheet_name: str, rankings: list[dict], se write_result(path, results_list, sheet_name, headers=headers) +def write_event_summary(path: str | Path, seasons: list[str], event_teams: dict[dict], rankings: dict[dict]) -> None: + event_summary: list[dict] = [] + + for key, event in event_teams.items(): + rank_sum = 0 + point_total_sum = 0 + epa_sum = 0 + point_district_sum = 0 + point_district_na_sum = 0 + + entry_count = 0 + + event_rankings: dict[dict] = {} + + for team in event['teams']: + if team in rankings: + event_rankings[team] = rankings[team] + else: + event_rankings[team] = {} + + if team in rankings: + for season in seasons: + if season in rankings[team]: + team_season = rankings[team][season] + + rank_sum += team_season['rank'] + point_total_sum += team_season['point_total'] + + if team_season['epa_end'] != '': + epa_sum += team_season['epa_end'] + + if team_season['points_event1'] is not None: + point_district_sum += team_season['points_event1'] + + if team_season['points_event2'] is not None: + point_district_sum += team_season['points_event2'] + + if team_season['points_event1_na'] is not None: + point_district_na_sum += team_season['points_event1_na'] + + if team_season['points_event2_na'] is not None: + point_district_na_sum += team_season['points_event2_na'] + + entry_count += 1 + + write_power_rank(path, event['key'], event_rankings, seasons) + + # Generate Event Summary + rank_avg = '' + point_total_avg = '' + epa_avg = '' + point_district_avg = '' + point_district_na_avg = '' + + if entry_count > 0: + rank_avg = rank_sum / entry_count + point_total_avg = point_total_sum / entry_count + epa_avg = epa_sum / entry_count + point_district_avg = point_district_sum / entry_count + point_district_na_avg = point_district_na_sum / entry_count + + event_summary.append({ + 'key': key, + 'name': event['name'], + 'start date': event['start date'], + 'week': event['week'], + 'average rank': rank_avg, + 'average point total': point_total_avg, + 'average EPA': epa_avg, + 'average point district': point_district_avg, + 'average point district no awards': point_district_na_avg, + 'entries': entry_count, + 'team count': len(event['teams']) + }) + + # Write Event Summary + write_result(path, event_summary, "Summary", 0) + + def write_rank(path: str | Path, rankings: list[dict], seasons: list[str]) -> None: # Create annual rank CSV results_list: list[dict] = [] @@ -271,7 +388,7 @@ def write_result(path: str | Path, results_list: list[dict], sheet_name: Optiona if sheet_name is None or sheet_name in wb.sheetnames: wb.remove(wb[sheet_name]) - + ws = wb.create_sheet(sheet_name, sheet_index) else: wb = Workbook() @@ -304,94 +421,29 @@ def write_result(path: str | Path, results_list: list[dict], sheet_name: Optiona def main(): - + # Initialize system run_time = dt.now() run_time_str = run_time.strftime('%Y%m%d_%H%M') - skip_years = [2020,2021] + skip_years = [2020, 2021] - seasons = [f'{year}fim' for year in range(2022, 2025) if year not in skip_years] + seasons = [f'{year}{district}' for year in range( + 2022, 2025) if year not in skip_years] root_dir = Path('results') - - event_details_path = root_dir / f'{run_time_str} - Event Details.xlsx' - + # Get Statistics with tbaapiv3client.ApiClient(config) as api_client: + print('Getting Team Rankings') rankings = get_rankings(api_client, seasons) + + print('Getting event team lists') event_teams = get_event_teams(api_client, '2025fim') - event_summary: list[dict] = [] - - for key, event in event_teams.items(): - rank_sum = 0 - point_total_sum = 0 - point_district_sum = 0 - point_district_na_sum = 0 - - entry_count = 0 - - event_rankings: dict[dict] = {} - - for team in event['teams']: - if team in rankings: - event_rankings[team] = rankings[team] - else: - event_rankings[team] = {} - - - if team in rankings: - for season in seasons: - if season in rankings[team]: - team_season = rankings[team][season] - - rank_sum += team_season['rank'] - point_total_sum += team_season['point_total'] - - if team_season['points_event1'] is not None: - point_district_sum += team_season['points_event1'] - - if team_season['points_event2'] is not None: - point_district_sum += team_season['points_event2'] - - if team_season['points_event1_na'] is not None: - point_district_na_sum += team_season['points_event1_na'] - - if team_season['points_event2_na'] is not None: - point_district_na_sum += team_season['points_event2_na'] - - entry_count += 1 - - write_power_rank(event_details_path, event['key'], event_rankings, seasons) - - # Generate Event Summary - rank_avg = '' - point_total_avg = '' - point_district_avg = '' - point_district_na_avg = '' - - if entry_count > 0: - rank_avg = rank_sum / entry_count - point_total_avg = point_total_sum / entry_count - point_district_avg = point_district_sum / entry_count - point_district_na_avg = point_district_na_sum / entry_count - - event_summary.append({ - 'key': key, - 'name': event['name'], - 'start date': event['start date'], - 'week': event['week'], - 'average rank': rank_avg, - 'average point total': point_total_avg, - 'average point district': point_district_avg, - 'average point district no awards': point_district_na_avg, - 'entries': entry_count, - 'team count': len(event['teams']) - }) - # Write Event Summary print('Writing Event Summary') - write_result(event_details_path, event_summary, "Summary", 0) + write_event_summary( + root_dir / f'{run_time_str} - Event Details.xlsx', seasons, event_teams, rankings) # Write Power Rankings print('Writing Power Ranking')