Python Ballchasing
An easy-to-use and comprehensive Python wrapper for the ballchasing.com API.
Installation
You can install the library from PyPI using pip:
pip install python-ballchasing
Authentication
Before you can use the library, you need an API authentication key from ballchasing.com.
- Log in to ballchasing.com.
- Navigate to the Upload tab.
- Create an API key and copy it.
Keep your API key secure and do not share it publicly.
API
For detailed information, please refer to the docs inside the code. For the most part it follows the API spec closely, but there are some differences.
The API is exposed via the BallchasingApi class.
Making the client:
from ballchasing import BallchasingApi
api = BallchasingApi("Your token here")
or equivalently
import ballchasing
api = ballchasing.Api("Your token here")
By default this will also ping the API to make sure it's working.
Some simple examples:
# Get lots of SSL replays
from ballchasing import Rank # there's also Playlist, Season, Map, and more
replays = api.get_replays(
min_rank=Rank.SUPERSONIC_LEGEND,
count=10_000 # The API limits you to 200 replays per request but the library handles this for you
)
for replay in replays: # (replays is an iterable so you don't need to wait for all the replays to be collected)
... # Do something with the replays
# Get a specific replay with more detail than the iterator (including stats!)
replay = api.get_replay("2627e02a-aa46-4e13-b66b-b76a32069a07")
# Get groups by the "RLCS Referee" account
groups = api.get_groups(creator="76561199225615730")
for group in groups:
# Download the group
api.download_group(
group_id=group["id"],
path="/path/to/destination/"
recursive=True, # To download all the replays and retain the group structure with subfolders
)
# Get replays from the group
replays = get_group_replays(
group_id=group["id"],
deep=True, # To get detailed replay info
)
for replay in replays:
api.download_replay(replay_id=replay["id"], path="/path/to/destination/") # You could also download like this
Additionally, there's the option to put responses (replays, groups) into typed objects for better type hinting and validation.
It also attempts to fill in missing variables not returned by ballchasing (e.g. if a team has 0 goals they won't have a "goal" entry in the response)
The classes are generated from the API's OpenAPI document into
ballchasing/typed/models.py, with each field's description, and can be used as a reference for what the API returns.
# When creating the API, you can specify a default setting for typing (defaults to False)
api = ballchasing.Api("Your token here", typed=True)
# or you can specify it explicitly
replay = api.get_replay("2627e02a-aa46-4e13-b66b-b76a32069a07", typed=True)
print(replay.blue.players[0].name) # Example of easy attribute access
group = api.get_group("g2-vs-bds-hwbf2eyolb", typed=True)
print(group.players[0].name)
Fields the API adds after a release don't break the typed objects: they are kept in the object's _extra dict
(and included by to_dict()) until the classes get a field for them.
The client is checked against a snapshot of the OpenAPI document in
tests/data/openapi.json. To see what changed in the API since, update the snapshot and regenerate the typed models:
python scripts/check_api_spec.py # exits with 1 if something changed
python scripts/check_api_spec.py --update # then run the tests to see what else the client is missing
python scripts/generate_models.py # regenerate models.py alone (e.g. after changing the generator)
Methods of the typed classes are written by hand in ballchasing/typed/methods.py.