From edf901b6479bf80e66c37c513ee1b8736342b1f6 Mon Sep 17 00:00:00 2001 From: Corban-Lee Jones Date: Thu, 12 Dec 2024 16:08:21 +0000 Subject: [PATCH] stop unnecessary request to steam API --- cogs/players.py | 10 ++++++++-- utils/models.py | 46 ++++++++++++++++++++++++++++++---------------- 2 files changed, 38 insertions(+), 18 deletions(-) diff --git a/cogs/players.py b/cogs/players.py index d2d8698..106412e 100644 --- a/cogs/players.py +++ b/cogs/players.py @@ -134,7 +134,10 @@ class PlayersCog(commands.Cog): coord_y=re_match.group("y"), coord_z=re_match.group("z") ) - await player.update_steam_summary(re_match.group("steam_id"), self.bot.steam_api_key) + await player.update_outdated_steam_summary( + re_match.group("steam_id"), + self.bot.steam_api_key + ) await player.save() # This connection method is called when the player respawns @@ -171,7 +174,10 @@ class PlayersCog(commands.Cog): coord_y=re_match.group("y"), coord_z=re_match.group("z") ) - await player.update_steam_summary(re_match.group("steam_id"), self.bot.steam_api_key) + await player.update_outdated_steam_summary( + re_match.group("steam_id"), + self.bot.steam_api_key + ) await player.save() if player.is_dead or not alert: diff --git a/utils/models.py b/utils/models.py index e29c5b1..8fa7908 100644 --- a/utils/models.py +++ b/utils/models.py @@ -103,7 +103,7 @@ class Player(Model): # are so bad and the annotations don't work like Django's models. Deal with it! for session in sessions: log.info( - "session start: %s\nsession end: %s\nsession playtime: %s", + "playtime info:\nsession start: %s\nsession end: %s\nsession playtime: %s", session.connected_at, session.disconnected_at, session.playtime @@ -182,14 +182,15 @@ class Player(Model): async def get_steam_summary(self) -> SteamProfileSummary | None: """ + Returns the linked steam profile summary or `NoneType` if it doesn't exist. """ return await SteamProfileSummary.get_or_none(player=self) - async def update_steam_summary(self, steam_id: str | int, steam_api_key: str) -> SteamProfileSummary: + @staticmethod + async def fetch_steam_summary_data(steam_id: str | int, steam_api_key: str | int) -> dict: """ + Fetches and returns the raw data of a steam profile summary for the given steam user ID. """ - log.info("Updating Steam summary for player: %s", self.username) - if not steam_api_key: raise ValueError("No Steam API key provided, can't get profile summary.") @@ -202,24 +203,37 @@ class Player(Model): if not profiles: raise ValueError("No profiles found in response") - profile = profiles[0] + return profiles[0] - summary, created = await SteamProfileSummary.get_or_create( + async def update_outdated_steam_summary( + self, + steam_id: str | int, + steam_api_key: str + ) -> SteamProfileSummary: + """ + Updates the linked steam profile summary if missing or outdated. + Returns the resulting summary, regardless of whether it's updated or not. + """ + log.debug("Checking steam summary for player: %s", self.username) + + summary = await self.get_steam_summary() + + # If the summary exists and isn't outdated then return it, no work to be done! + if summary and summary.last_update + timedelta(days=1) > datetime.today(): + return summary + + # Update if summary is NoneType or older than 1 day + log.info("Steam summary missing or outdated, updating: %s", self.username) + data = await self.fetch_steam_summary_data(steam_id, steam_api_key) + summary, created = await SteamProfileSummary.update_or_create( steam_id=steam_id, defaults={ "player": self, - "profile_name": profile.get("personaname"), - "url": profile.get("profileurl"), - "avatar_url": profile.get("avatarfull") + "profile_name": data.get("personaname"), + "url": data.get("profileurl"), + "avatar_url": data.get("avatarfull") } ) - - if not created: - summary.profile_name = profile.get("personaname") - summary.url = profile.get("profileurl") - summary.avatar_url = profile.get("avatarfull") - await summary.save() - return summary async def get_embed(self) -> Embed: