From c2c3c022cce2faec962be9a6b875efba0c569e7a Mon Sep 17 00:00:00 2001 From: Dmitrii Morozov Date: Mon, 29 Jul 2024 18:46:53 +0200 Subject: Error handling for non public stats --- app_types/__init__.py | 49 ++++++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/app_types/__init__.py b/app_types/__init__.py index 12e9adf..a4cf271 100644 --- a/app_types/__init__.py +++ b/app_types/__init__.py @@ -40,22 +40,33 @@ class User: return instance async def fetch_stats(self) -> UserStats: - stats = await self.__fortnite_user.fetch_br_stats() - bp_level: float = await self.__fortnite_user.fetch_battlepass_level(season=__season__) - combined_stats = stats.get_combined_stats() - device_stats = {} - if 'keyboardmouse' in combined_stats: - device_stats = combined_stats['keyboardmouse'] - else: - device_stats = combined_stats['gamepad'] - - stats = UserStats() - - stats.user_id = self.id - stats.user_display_name = self.display_name - stats.level = int(bp_level//1) if bp_level is not None else 0 - stats.matches_played = device_stats['matchesplayed'] - stats.kills = device_stats['kills'] - stats.wins = device_stats['wins'] - - return stats \ No newline at end of file + user_stats = UserStats() + user_stats.user_id = self.id + user_stats.user_display_name = self.display_name + + try: + br_stats = await self.__fortnite_user.fetch_br_stats() + bp_level: float = await self.__fortnite_user.fetch_battlepass_level(season=__season__) + combined_stats = br_stats.get_combined_stats() + user_stats.level = int(bp_level//1) if bp_level is not None else 0 + user_stats.matches_played = self.get_stats_value(combined_stats, 'matchesplayed') + user_stats.kills = self.get_stats_value(combined_stats, 'kills') + user_stats.wins = self.get_stats_value(combined_stats, 'wins') + except: + user_stats.level = 0 + user_stats.matches_played = 0 + user_stats.kills = 0 + user_stats.wins = 0 + return user_stats + + def get_stats_value(self, combined_stats: dict, key: str): + value = 0 + value += self.get_stats_value_null_safe(combined_stats, 'keyboardmouse', key) + value += self.get_stats_value_null_safe(combined_stats, 'gamepad', key) + return value + + def get_stats_value_null_safe(self, combined_stats: dict, device_key: str, key: str): + if device_key in combined_stats: + if key in combined_stats[device_key]: + return combined_stats[device_key][key] + return 0 \ No newline at end of file -- cgit v1.2.3