From 64013dfbcd779bc676f58f1a2c83d753a5623570 Mon Sep 17 00:00:00 2001 From: "Christoph M. Wintersteiger" Date: Mon, 19 Sep 2022 17:22:58 +0000 Subject: [PATCH] Add time trial seasons --- pyracing/client.py | 92 ++++++++++++++++++++ pyracing/constants.py | 2 + pyracing/response_objects/historical_data.py | 49 +++++++++++ 3 files changed, 143 insertions(+) diff --git a/pyracing/client.py b/pyracing/client.py index 0df69ed..1fc704e 100644 --- a/pyracing/client.py +++ b/pyracing/client.py @@ -798,6 +798,98 @@ async def season_standings( return [] + async def season_tt_standings( + self, + season_id, + race_week=1, + car_class_id=None, + club_id=None, + division=None, + result_num_low=1, + result_num_high=25, + sort=ct.Sort.champ_points.value, + order=ct.Sort.descending.value + ): + """ Returns the championship point standings of a time trial series. + """ + payload = { + 'seasonid': season_id, + 'carclassid': car_class_id, + 'clubid': club_id, + # -1 for all weeks, or the week number to get a specific week. + # 0 indexed in iRacing, so the user passes the week number and we + # subtract one to get the 0 indexed position + 'raceweek': race_week if race_week == -1 else race_week - 1, + 'division': division, + 'start': result_num_low, + 'end': result_num_high, + 'sort': sort, + 'order': order + } + + url = ct.URL_SEASON_TT_STANDINGS + response = await self._build_request(url, payload) + + if response.json(): + mapping = response.json().get('m') + response_data = response.json().get('d') + + if mapping and response_data: + response_items = response_data.get('r') + + if response_items: + renamed_items = [self._rename_numerical_keys(ri, mapping) for ri in response_items] + + return [historical_data.SeasonTTStandings(x) for x in renamed_items if x] + + return [] + + async def season_tt_results( + self, + season_id, + race_week=1, + car_class_id=None, + club_id=None, + division=None, + result_num_low=1, + result_num_high=25, + sort=ct.Sort.champ_points.value, + order=ct.Sort.descending.value + ): + """ Returns the championship point standings of a time trial series. + """ + payload = { + 'seasonid': season_id, + 'carclassid': car_class_id, + 'clubid': club_id, + # -1 for all weeks, or the week number to get a specific week. + # 0 indexed in iRacing, so the user passes the week number and we + # subtract one to get the 0 indexed position + 'raceweek': race_week if race_week == -1 else race_week - 1, + 'division': division, + 'start': result_num_low, + 'end': result_num_high, + 'sort': sort, + 'order': order + } + + url = ct.URL_SEASON_TT_RESULTS + response = await self._build_request(url, payload) + + if response.json(): + mapping = response.json().get('m') + response_data = response.json().get('d') + + if mapping and response_data: + response_items = response_data.get('r') + + if response_items: + renamed_items = [self._rename_numerical_keys(ri, mapping) for ri in response_items] + + return [historical_data.SeasonTTResults(x) for x in renamed_items if x] + + return [] + async def series_race_results(self, season_id, race_week=1): """ Returns the race results for a season_id. race_week can be specified to reduce the size of data returned. diff --git a/pyracing/constants.py b/pyracing/constants.py index 010a801..bfe6179 100644 --- a/pyracing/constants.py +++ b/pyracing/constants.py @@ -19,6 +19,8 @@ URL_CURRENT_SEASONS = (mSite + '/GetSeasons') URL_SEASON_STANDINGS = (mStats + '/GetSeasonStandings') URL_SERIES_RACERESULTS = (mStats + '/GetSeriesRaceResults') +URL_SEASON_TT_STANDINGS = (mStats + '/GetSeasonTTStandings') +URL_SEASON_TT_RESULTS = (mStats + '/GetSeasonTTResults') # RECENT HISTORICAL URL_LASTRACE_STATS = (mStats + '/GetLastRacesStats') diff --git a/pyracing/response_objects/historical_data.py b/pyracing/response_objects/historical_data.py index 53bddeb..f50f5ea 100644 --- a/pyracing/response_objects/historical_data.py +++ b/pyracing/response_objects/historical_data.py @@ -91,6 +91,55 @@ def __init__(self, data): # self.custrow = dict['custrow'] # self.rowcount = dict['rowcount'] +class SeasonTTStandings: + def __init__(self, data): + self.club_id = data['clubid'] + self.club_name = parse_encode(data['clubname']) + self.cust_id = data['custid'] + self.display_name = parse_encode(data['displayname']) + self.division = data['division'] + self.dropped = data['dropped'] + self.helm_color_1 = data['helmcolor1'] + self.helm_color_2 = data['helmcolor2'] + self.helm_color_3 = data['helmcolor3'] + self.helm_pattern = data['helmpattern'] + self.license_level_max = data['maxlicenselevel'] + self.points = data['points'] + self.pos = data['pos'] + self.rank = data['rank'] + self.row = data['rn'] + self.starts = data['starts'] + self.sub_level = parse_encode(data['sublevel']) + self.week = data['week'] + self.wins = data['wins'] + + # Used only on webpage to highlight the member in relaton to others + # self.custrow = dict['custrow'] + # self.rowcount = dict['rowcount'] + +class SeasonTTResults: + def __init__(self, data): + self.club_id = data['clubid'] + self.cust_id = data['custid'] + self.display_name = parse_encode(data['displayname']) + self.division = data['division'] + self.helm_color_1 = data['helmcolor1'] + self.helm_color_2 = data['helmcolor2'] + self.helm_color_3 = data['helmcolor3'] + self.helm_pattern = data['helmpattern'] + self.license_level_max = data['maxlicenselevel'] + self.points = data['points'] + self.pos = data['pos'] + self.rank = data['rank'] + self.row = data['rn'] + self.starts = data['starts'] + self.sub_level = parse_encode(data['sublevel']) + self.best_time_formatted = data['besttimeformatted'] + self.best_time = data['besttime'] + + # Used only on webpage to highlight the member in relaton to others + # self.custrow = dict['custrow'] + # self.rowcount = dict['rowcount'] class DriverStats: def __init__(self, data):