2.3 KiB
mlbstats
mlbstats is a CLI tool for retrieving data from Major League Baseball's Stats API, including efficient retrieval of live game data. Unofficial documentation on the Stats API can be found here (thanks to GitHub user toddrob99 for this resource).
Usage
mlbstats provides sub-commands to query a subset of the Stats API endpoints. As of the time of writing, the sub-commands are:
mlbstats content
(accesses thegame_content
endpoint)mlbstats feed
(accesses thegame
endpoint)mlbstats schedule
(accesses theschedule
endpoint)mlbstats standings
(accesses thestandings
endpoint)mlbstats subscribe
(accesses the game WebSocket endpoint)
Each sub-command outputs the raw JSON response from the API. This can then be
piped into other commands like jq
for further processing. See this
repo for examples
of how the author uses this program for posting live score updates and
highlights to Mastodon.
Note that mlbstats does not provide sub-commands for every endpoint, or flags for every parameter of every endpoint. The functionality provided was prioritized based on the author's own needs. However, the author has made every effort to make the current functionality's code easy to read, and hopes it will be similarly easy for other developers to extend functionality as needed.
subscribe sub-command
mlbstats subscribe
provides an efficient way to obtain live data on active MLB
games. Supply the game's PK (which can be retrieved with mlbstats schedule
) to
receive updates as they happen, without needing to blindly query the game
endpoint.
Example
Use mlbstats schedule
to get the game PK(s) for today's Reds game:
mlbstats schedule -t cin
Optionally, use jq
to filter this output to just the game PK(s):
mlbstats schedule -t cin | jq '.dates[].games[].gamePk'
Then, pass a PK to get live updates:
mlbstats subscribe -g <gamePk>
Disclaimer
This program and its author are not affiliated with MLB. Content supplied by MLB's Stats API is copyright MLB Advanced Media, L.P., and use of any content provided by the API acknowledges agreement to the terms posted here.