Skip to content

Commit

Permalink
Add team match stats (#65)
Browse files Browse the repository at this point in the history
* add team match stats

* ignore build files
  • Loading branch information
anenglishgoat authored Sep 6, 2024
1 parent f619112 commit 341155e
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
__pycache__/
build/
dist/
*.egg-info/
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2084,6 +2084,7 @@ For customers we also provide aggregated statistics at the player-match, player-
```
player_match = sb.player_match_stats(3772072)
player_season = sb.player_season_stats(competition_id=9, season_id=42)
team_match = sb.team_match_stats(3772072)
team_season = sb.team_season_stats(competition_id=9, season_id=42)
player_match
Expand Down Expand Up @@ -2658,6 +2659,8 @@ sb.player_match_stats(3772072, fmt="dict")
sb.player_season_stats(competition_id=9, season_id=42, fmt="dict")
sb.team_match_stats(3772072, fmt="dict")
sb.team_season_stats(competition_id=9, season_id=42, fmt="dict")
```
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setup(
name="statsbombpy",
version="1.13.1",
version="1.14.0",
description="easily stream StatsBomb data into Python",
long_description=README,
long_description_content_type="text/markdown",
Expand Down
5 changes: 5 additions & 0 deletions statsbombpy/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ def player_season_stats(competition_id: int, season_id: int, creds: dict) -> lis
return get_resource(url, creds)


def team_match_stats(match_id: int, creds: dict) -> list:
url = f"{HOSTNAME}/api/{VERSIONS['team-match-stats']}/matches/{match_id}/team-stats"
return get_resource(url, creds)


def team_season_stats(competition_id: int, season_id: int, creds: dict) -> list:
url = f"{HOSTNAME}/api/{VERSIONS['team-season-stats']}/competitions/{competition_id}/seasons/{season_id}/team-stats"
return get_resource(url, creds)
1 change: 1 addition & 0 deletions statsbombpy/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@
"player-match-stats": "v4",
"player-season-stats": "v4",
"team-season-stats": "v2",
"team-match-stats": "v1",
}
16 changes: 16 additions & 0 deletions statsbombpy/sb.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,22 @@ def player_season_stats(
return player_season_stats


def team_match_stats(
match_id: int,
fmt: str = "dataframe",
creds: dict = DEFAULT_CREDS,
) -> Union[pd.DataFrame, dict]:
if api_client.has_auth(creds) is True:
team_match_stats = api_client.team_match_stats(match_id, creds=creds)
else:
raise Exception(
"There is currently no open data for aggregated stats, please provide credentials"
)
if fmt == "dataframe":
team_match_stats = pd.json_normalize(team_match_stats)
return team_match_stats


def team_season_stats(
competition_id: int,
season_id: int,
Expand Down
15 changes: 13 additions & 2 deletions tests/statsbombpy_test/test_sb.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_matches(self):
self.assertIsInstance(matches, pd.DataFrame)

matches = sb.matches(competition_id=11, season_id=1)
self.assertEquals(
self.assertEqual(
matches.query("match_id == 9695")["away_managers"].iloc[0],
"Ernesto Valverde Tejedor",
)
Expand All @@ -57,7 +57,7 @@ def test_lineups(self):
self.assertIsInstance(lineups, dict)

lineups = sb.lineups(match_id=301244)
self.assertEquals(
self.assertEqual(
lineups["Stoke City"]["country"].iloc[0],
"England",
)
Expand Down Expand Up @@ -206,6 +206,17 @@ def test_player_season_stats(self):
with self.assertRaises(Exception):
sb.player_season_stats(competition_id=2, season_id=44, creds={})

def test_team_match_stats(self):
team_match_stats = sb.team_match_stats(match_id=3764302)
self.assertIsInstance(team_match_stats, pd.DataFrame)

team_match_stats = sb.team_match_stats(match_id=3764302, fmt="json")
self.assertIsInstance(team_match_stats, list)

with self.assertRaises(Exception):
sb.team_match_stats(match_id=7562, creds={})


def test_team_season_stats(self):
team_season_stats = sb.team_season_stats(competition_id=43, season_id=3)
self.assertIsInstance(team_season_stats, pd.DataFrame)
Expand Down

0 comments on commit 341155e

Please sign in to comment.