stop unnecessary request to steam API
All checks were successful
Build and Push Docker Image / build (push) Successful in 16s

This commit is contained in:
Corban-Lee Jones 2024-12-12 16:08:21 +00:00
parent da217c1242
commit edf901b647
2 changed files with 38 additions and 18 deletions

View File

@ -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:

View File

@ -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: