Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Sport Event Statistics wrapper #14

Merged
merged 1 commit into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "sportradar-api"
version = "0.2.5"
version = "0.2.6"
description = "Lightweight wrapper for Sportradar API"
authors = ["Felipe Allegretti <[email protected]>"]
readme = "README.md"
Expand Down
11 changes: 11 additions & 0 deletions sportradar_api/soccer_extended/soccer_extended.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,14 @@ def get_player_profile(self, player_urn: str) -> dict:
"""

return self._call_endpoint(endpoint=f"players/{player_urn}/profile")

def get_sport_event_summary(self, sport_event_urn: str) -> dict:
"""Get the summary of a given sport event urn, including results.

Args:
sport_event_urn: URN of a given sport event

Returns:
API response
"""
return self._call_endpoint(endpoint=f"sport_events/{sport_event_urn}/summary")
34 changes: 34 additions & 0 deletions sportradar_api/soccer_extended/soccer_extended_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,37 @@ def get_player_profile_roles(self, player_urn: str) -> pd.DataFrame:
player_profile_roles = pd.json_normalize([flatten(role, separator=".") for role in player_profile["roles"]])

return player_profile_roles.assign(player_urn=player_urn)

def get_sport_event_statistics(self, sport_event_urn: str) -> pd.DataFrame:
"""Get the statistics from a sport event

Args:
sport_event_urn: URN of a given sport event

Returns:
Pandas DataFrame
"""
sport_event_summary = self.soccer_extended.get_sport_event_summary(sport_event_urn=sport_event_urn)

sport_event_statistics = (
pd.json_normalize(sport_event_summary)
.pipe(explode_column, "statistics.totals.competitors", ["sport_event.id", "sport_event.start_time"])
.pipe(
explode_column,
"statistics.totals.competitors.players",
[
"sport_event.id",
"sport_event.start_time",
"statistics.totals.competitors.id",
"statistics.totals.competitors.qualifier",
],
)
.pipe(
lambda x: x.set_axis(
[remove_str("_".join(col.split(".")[-2:]), ["sport_event_", "statistics_"]) for col in x.columns],
axis=1,
)
)
)

return sport_event_statistics