Skip to content

Commit 9c38fc2

Browse files
authored
Initial Release - v0.0.5
Initial release includes advanced query support as well as the following built-in functions. See README and `statsapi.__doc__` / `statsapi.<function>.__doc__` for more details and example uses. * `statsapi.get()` - make calls directly to MLB StatsAPI endpoints; supports the most flexibility in request parameters, and returns raw json data * `statsapi.meta()` - retrieve available values from StatsAPI for use in other queries, or look up descriptions for values found in API results * `statsapi.notes()` - retrieve notes for a given endpoint, including a list of required parameters, as well as hints for some endpoints * `statsapi.schedule()` - retrieve a list of games on a given date/range and/or team/opponent * `statsapi.boxscore()` - generate a formatted boxscore for a given game * `statsapi.linescore()` - generate a formatted linescore for a given game * `statsapi.roster()` - generate a list of players on a team's roster * `statsapi.standings()` - generate a formatted list of standings for a given league/date * `statsapi.team_leaders()` - generate a list of a team's leaders for a given stat * `statsapi.league_leaders()` - generate a list of stat leaders for all-time (single season) or a given season * `statsapi.player_stats()` - get a list of a player's career or season stats * `statsapi.last_game()` - get the game id for the given team's most recent game * `statsapi.next_game()` - get the game id for the given team's next game * `statsapi.game_highlights()` - generate a list of highlights with video links for a given game * `statsapi.game_pace()` - get information about pace of game for a given season (back to 1999) * `statsapi.game_scoring_plays()` - get a list of scoring plays for a given game
2 parents 236f1c6 + e12ad1b commit 9c38fc2

File tree

7 files changed

+2637
-1
lines changed

7 files changed

+2637
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/logs
2+
13
# Byte-compiled / optimized / DLL files
24
__pycache__/
35
*.py[cod]

README.md

Lines changed: 141 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,142 @@
11
# 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+
```

setup.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import setuptools
2+
import os
3+
4+
with open("README.md", "r") as fh:
5+
long_description = fh.read()
6+
7+
from statsapi import version
8+
9+
setuptools.setup(
10+
name="MLB-StatsAPI",
11+
version=version.VERSION,
12+
author="Todd Roberts",
13+
author_email="todd@toddrob.com",
14+
description="MLB Stats API Wrapper for Python",
15+
long_description=long_description,
16+
long_description_content_type="text/markdown",
17+
url="https://github.com/toddrob99/MLB-StatsAPI",
18+
packages=setuptools.find_packages(),
19+
install_requires=['requests'],
20+
classifiers=[
21+
"Programming Language :: Python",
22+
"Programming Language :: Python :: 2.7",
23+
"Programming Language :: Python :: 3",
24+
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
25+
"Operating System :: OS Independent",
26+
],
27+
)

0 commit comments

Comments
 (0)