Skip to content

Commit

Permalink
Merge pull request #174 from DMcP89/173-stats-command-fails-in-nhl-le…
Browse files Browse the repository at this point in the history
…agues

173 stats command fails in nhl leagues
  • Loading branch information
DMcP89 authored Oct 18, 2024
2 parents e1ad9bd + 34ccc74 commit 9d10428
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 13 deletions.
20 changes: 11 additions & 9 deletions harambot/cogs/yahoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ async def trade(self, interaction: discord.Interaction):
player_set0.append(player["name"])
api_details = (
self.get_player_text(
self.yahoo_api.get_player_details(player["name"], guild_id=interaction.guild_id)
self.yahoo_api.get_player_details(
player["name"], guild_id=interaction.guild_id
)
)
+ "\n"
)
Expand All @@ -155,7 +157,9 @@ async def trade(self, interaction: discord.Interaction):
player_set1_details = ""
for player in latest_trade["tradee_players"]:
player_set1.append(player["name"])
player_details = self.yahoo_api.get_player_details(player["name"], guild_id=interaction.guild_id)
player_details = self.yahoo_api.get_player_details(
player["name"], guild_id=interaction.guild_id
)
if player_details is None:
await interaction.followup.send(self.error_message)
return
Expand Down Expand Up @@ -267,13 +271,11 @@ def get_player_embed(self, player):
value=player["stats"]["total_points"],
inline=False,
)
del player["stats"]["player_id"]
del player["stats"]["name"]
del player["stats"]["position_type"]
for key, value in player["stats"].items():
if key == "total_points":
continue
embed.add_field(name=key, value=value)
if len(player["stats"].items()) < 20:
for key, value in player["stats"].items():
if key == "total_points":
continue
embed.add_field(name=key, value=value)
return embed

def get_player_text(self, player):
Expand Down
10 changes: 8 additions & 2 deletions harambot/yahoo_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,22 @@ def get_player_details(self, player_name, guild_id, week=None):
player = self.league().player_details(player_name)[0]
player["owner"] = self.get_player_owner(player["player_id"])
if week:
player["stats"] = self.league().player_stats(
stats = self.league().player_stats(
[player["player_id"]],
req_type="week",
week=week,
)[0]
else:
player["stats"] = self.league().player_stats(
stats = self.league().player_stats(
[player["player_id"]],
req_type="season",
)[0]
del stats["player_id"]
del stats["name"]
del stats["position_type"]
if len(stats) > 20:
stats = {k: v for k, v in stats.items() if v != 0.0}
player["stats"] = stats
return player
except Exception:
logger.exception(
Expand Down
9 changes: 8 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,14 @@ def mock_player_details():

@pytest.fixture
def mock_player_stats():
return [{"mock": "stats"}]
return [
{
"mock": "stats",
"player_id": "39077",
"name": "Josh Allen",
"position_type": "o",
}
]


@pytest.fixture
Expand Down
27 changes: 26 additions & 1 deletion tests/test-player-details.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,32 @@
"coverage_type": "season",
"season": "2020"
},
"stats": [{
"stats": [
{
"stat": {
"stat_id": "0",
"value": "396"
}
},
{
"stat": {
"stat_id": "1",
"value": "572"
}
},
{
"stat": {
"stat_id": "2",
"value": "4544"
}
},
{
"stat": {
"stat_id": "3",
"value": "37"
}
},
{
"stat": {
"stat_id": "4",
"value": "2871"
Expand Down

0 comments on commit 9d10428

Please sign in to comment.