-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcatcher_throwing.py
More file actions
59 lines (45 loc) · 1.75 KB
/
catcher_throwing.py
File metadata and controls
59 lines (45 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import pandas as pd
import requests
from ..constants import DEFAULT_SEASON
from ..enums.statcast_leaderboard import GameType
session = requests.Session()
API_URL = 'https://baseballsavant.mlb.com/leaderboard/services/catcher-throwing'
def catcher_throwing(
catcher_id: str,
game_type: str | GameType = GameType.REGULAR_SEASON,
season: str = str(DEFAULT_SEASON),
) -> pd.DataFrame:
"""
Get catcher throwing data from each stolen base attempt for a specific catcher.
ref: https://baseballsavant.mlb.com/leaderboard/catcher-throwing
Args:
catcher_id (str): The MLBAM ID of the catcher. (Required)
game_type (str): The game type to filter by.
n (str): The number of results to return.
season (str): The season to filter by. The earliest season available is 2016.
Returns:
pd.DataFrame: A DataFrame containing the catcher throwing data.
"""
if not catcher_id:
raise ValueError('catcher_id is required')
if not isinstance(game_type, str) and not isinstance(game_type, GameType):
raise ValueError(f'Invalid type for game_type: {type(game_type)}')
if not GameType.has_value(game_type):
raise ValueError(f'Invalid game type: {game_type}')
if int(season) < 2016:
raise ValueError(
f'Invalid season: {season}, The earliest season available is 2016'
)
params = {
'gameType': game_type,
'season': season,
'n': 0,
}
response = session.get(f'{API_URL}/{catcher_id}', params=params)
if response.status_code == 200:
result = response.json()
return pd.DataFrame(result['data'])
else:
raise Exception(
f'Failed to fetch data: {response.status_code} - {response.text}'
)