|
1 | 1 | # MLB-StatsAPI
|
2 |
| -Python 3 wrapper for MLB Stats API |
| 2 | + |
| 3 | +Python wrapper for MLB Stats API |
| 4 | + |
| 5 | +Created by Todd Roberts |
| 6 | + |
| 7 | +https://github.com/toddrob99/MLB-StatsAPI |
| 8 | + |
| 9 | +## Installation |
| 10 | +MLB-StatsAPI is listed on the [Python Package Index](https://pypi.org/project/MLB-StatsAPI/), |
| 11 | +and the preferred installation method is pip for all platforms. |
| 12 | +If you install manually, be sure to also install requests. |
| 13 | + |
| 14 | +```pip install MLB-StatsAPI``` |
| 15 | + |
| 16 | +## Available Functions |
| 17 | + |
| 18 | +* `statsapi.get()` - make calls directly to MLB StatsAPI endpoints; |
| 19 | + supports the most flexibility in request parameters, and returns raw json data |
| 20 | + |
| 21 | +* `statsapi.meta()` - retrieve available values from StatsAPI for use in other queries, |
| 22 | + or look up descriptions for values found in API results |
| 23 | + |
| 24 | +* `statsapi.notes()` - retrieve notes for a given endpoint, |
| 25 | + including a list of required parameters, as well as hints for some endpoints |
| 26 | + |
| 27 | +* `statsapi.schedule()` - retrieve a list of games on a given date/range and/or team/opponent |
| 28 | + |
| 29 | +* `statsapi.boxscore()` - generate a formatted boxscore for a given game |
| 30 | + |
| 31 | +* `statsapi.linescore()` - generate a formatted linescore for a given game |
| 32 | + |
| 33 | +* `statsapi.roster()` - generate a list of players on a team's roster |
| 34 | + |
| 35 | +* `statsapi.standings()` - generate a formatted list of standings for a given league/date |
| 36 | + |
| 37 | +* `statsapi.team_leaders()` - generate a list of a team's leaders for a given stat |
| 38 | + |
| 39 | +* `statsapi.league_leaders()` - generate a list of stat leaders for all-time (single season) or a given season |
| 40 | + |
| 41 | +* `statsapi.player_stats()` - get a list of a player's career or season stats |
| 42 | + |
| 43 | +* `statsapi.last_game()` - get the game id for the given team's most recent game |
| 44 | + |
| 45 | +* `statsapi.next_game()` - get the game id for the given team's next game |
| 46 | + |
| 47 | +* `statsapi.game_highlights()` - generate a list of highlights with video links for a given game |
| 48 | + |
| 49 | +* `statsapi.game_pace()` - get information about pace of game for a given season (back to 1999) |
| 50 | + |
| 51 | +* `statsapi.game_scoring_plays()` - get a list of scoring plays for a given game |
| 52 | + |
| 53 | +## Example Use |
| 54 | + |
| 55 | +### Print the number of games won by the Oakland Athletics in 2018 |
| 56 | + |
| 57 | +Use `statsapi.schedule()` to retrieve all A's games for 2018, |
| 58 | +and use `sum()` to count records in the resultset where the A's were the winning_team. |
| 59 | + |
| 60 | +``` |
| 61 | +print('The A\'s won %s games in 2018.' % sum(1 for x in statsapi.schedule(team=133,start_date='01/01/2018',end_date='12/31/2018') if x.get('winning_team','')=='Oakland Athletics')) |
| 62 | +``` |
| 63 | + |
| 64 | +### Print the linescore for all games the Phillies won in July 2008 |
| 65 | + |
| 66 | +Use `statsapi.schedule()` to retrieve all games for July 2018, |
| 67 | +run the resulting dict through a list comprehension |
| 68 | +to iterate over the records where the Phillies are the winning team, |
| 69 | +and feed the `game_id` into `statsapi_linescore()`. |
| 70 | + |
| 71 | +``` |
| 72 | +for x in [y for y in statsapi.schedule(team=143,start_date='07/01/2008',end_date='07/31/2008') if y.get('winning_team','')=='Philadelphia Phillies']: |
| 73 | + print('%s\nWinner: %s, Loser: %s\n%s\n\n' % (x['game_date'], x['winning_team'], x['losing_team'], statsapi.linescore(x['game_id']))) |
| 74 | +``` |
| 75 | + |
| 76 | +### Print the Phillies 40-man Roster on opening day of the 2018 season |
| 77 | + |
| 78 | +Use `statsapi.get('season')` to retrieve the dates for the 2018 season, |
| 79 | +feed the opening day date into `statsapi.roster()`. |
| 80 | + |
| 81 | +``` |
| 82 | +print('Phillies 40-man roster on opening day of the 2018 season:\n%s' % statsapi.roster(143,'40Man',date=statsapi.get('season',{'seasonId':2018,'sportId':1})['seasons'][0]['regularSeasonStartDate'])) |
| 83 | +``` |
| 84 | + |
| 85 | +### Print the boxscore and linescore from the A's most recent game (which may be in progress) |
| 86 | + |
| 87 | +Use `statsapi.last_game()` to retrieve the most recent A's game |
| 88 | +and feed the gamePk into `statsapi.boxscore()` and `statsapi.linescore()`. |
| 89 | + |
| 90 | +``` |
| 91 | +most_recent_game_id = statsapi.last_game(133) |
| 92 | +print(statsapi.boxscore(most_recent_game_id)) |
| 93 | +print(statsapi.linescore(most_recent_game_id)) |
| 94 | +``` |
| 95 | + |
| 96 | +### Find the team with the longest name |
| 97 | + |
| 98 | +Use `statsapi.get('teams')` to retrieve all active team names, |
| 99 | +then feed into max() to find the longest value and its length |
| 100 | + |
| 101 | +``` |
| 102 | +longest_team_name = max([x['name'] for x in statsapi.get('teams',{'sportIds':1,'activeStatus':'Yes','fields':'teams,name'})['teams']],key=len) |
| 103 | +print('The team with the longest name is %s, at %s characters.' % (longest_team_name, len(longest_team_name))) |
| 104 | +``` |
| 105 | + |
| 106 | +### Print the standings from July 4, 2018 |
| 107 | + |
| 108 | +Use `statsapi.standings()` with the `date` parameters |
| 109 | + |
| 110 | +``` |
| 111 | +print(statsapi.standings(date='07/04/2018')) |
| 112 | +``` |
| 113 | + |
| 114 | +### Print the top 5 team leaders in walks for the 2008 Phillies |
| 115 | + |
| 116 | +Use `statsapi.team_leaders()` |
| 117 | + |
| 118 | +``` |
| 119 | +print(statsapi.team_leaders(143,'walks',limit=5,season=2008)) |
| 120 | +``` |
| 121 | + |
| 122 | +### Print the top 10 all time single season leaders in doubles |
| 123 | + |
| 124 | +use `statsapi.league_leaders()` |
| 125 | + |
| 126 | +``` |
| 127 | +print(statsapi.league_leaders('doubles',statGroup='hitting',limit=10)) |
| 128 | +``` |
| 129 | + |
| 130 | +### Print Chase Utley's career hitting stats |
| 131 | + |
| 132 | +use `statsapi.get()` to call the sports_players endpoint for the 2008 World Series, |
| 133 | +lookup Chase Utley's person id from the results, and pass it into `statsapi.player_stats()` |
| 134 | +using `type='hitting'` and `group='career'` |
| 135 | + |
| 136 | +print( statsapi.player_stats(next(x['id'] for x in statsapi.get('sports_players',{'season':2008,'gameType':'W'})['people'] if x['fullName']=='Chase Utley'), 'hitting', 'career') ) |
| 137 | + |
| 138 | +### Print a list of scoring plays from the 4/28/2019 Marlins @ Phillies game |
| 139 | + |
| 140 | +``` |
| 141 | +print( statsapi.game_scoring_plays(567074) ) |
| 142 | +``` |
0 commit comments