Skip to content

SportBot

pysportbot.SportBot

Unified interface for interacting with the booking system.

Creating a bot sends no requests. The centres are fetched on first use, and everything else needs login().

Parameters:

Name Type Description Default
log_level str | None

The level of the logs printed to stderr, e.g. "INFO". None leaves logging to the application.

'INFO'
time_zone str

The time zone of the centre. Slot times are in this zone, and start times without a time zone are taken to be in it.

'Europe/Madrid'

set_log_level

set_log_level(level: str) -> None

Change the level of pysportbot's logs.

Parameters:

Name Type Description Default
level str

The new level, e.g. "DEBUG".

required

Raises:

Type Description
ValueError

If the level name is unknown.

centres

centres(search: str | None = None) -> list[Centre]

List the centres that use Resasports. Needs no login.

The centres are fetched on the first call and then cached.

Parameters:

Name Type Description Default
search str | None

Only return centres whose slug, name or town contains this text, ignoring case.

None

Raises:

Type Description
APIError

If the centres cannot be fetched.

login

login(email: str, password: str, centre: str) -> None

Log in and fetch the activities of the centre.

Parameters:

Name Type Description Default
email str

The account email.

required
password str

The account password.

required
centre str

The slug of the centre, see centres().

required

Raises:

Type Description
CentreNotFoundError

If the account is not registered at the centre.

AuthenticationError

If logging in fails.

APIError

If the activities cannot be fetched.

ensure_logged_in

ensure_logged_in() -> None

Re-login with the stored credentials if the current session is no longer valid.

Long-running programs can call this before time-critical requests, because the session may expire some time after login(). It costs one request when the session is still valid. Otherwise it logs in again with the email, password and centre of the last successful login(), which the bot keeps in memory for this purpose. A re-login keeps the activities fetched by login(), so it sends no activities request.

Raises:

Type Description
NotLoggedInError

If login() has never succeeded.

AuthenticationError

If the re-login fails.

APIError

If fetching the activities fails, when an earlier login failed and a full login is needed.

is_logged_in

is_logged_in() -> bool

Whether the bot is logged in.

activities

activities() -> list[Activity]

List the activities of the centre, as fetched by login().

Raises:

Type Description
NotLoggedInError

If not logged in.

daily_slots

daily_slots(activity: str, day: date | str) -> list[Slot]

List the slots of an activity on one day.

Parameters:

Name Type Description Default
activity str

The activity name, see activities().

required
day date | str

The day, as a date or a 'YYYY-MM-DD' string.

required

Raises:

Type Description
NotLoggedInError

If not logged in.

ValueError

If day is not a valid 'YYYY-MM-DD' date.

ActivityNotFoundError

If the activity does not exist.

APIError

If the slots cannot be fetched.

find_slot

find_slot(activity: str, start: datetime | str) -> Slot

Find the slot of an activity that starts at a given time.

Parameters:

Name Type Description Default
activity str

The activity name, see activities().

required
start datetime | str

The start time: a time zone aware datetime, or a naive datetime or a 'YYYY-MM-DD HH:MM[:SS]' string in the bot's time zone.

required

Raises:

Type Description
NotLoggedInError

If not logged in.

ValueError

If start is a string in neither format.

ActivityNotFoundError

If the activity does not exist.

SlotNotFoundError

If no slot of the activity starts at that time.

APIError

If the slots cannot be fetched.

book

book(activity: str, start: datetime | str) -> Slot

Book the slot of an activity that starts at a given time.

Parameters:

Name Type Description Default
activity str

The activity name, see activities().

required
start datetime | str

The start time, see find_slot().

required

Returns:

Type Description
Slot

The booked slot, as listed before the booking.

Raises:

Type Description
SlotFullError

If the slot has no places left. No booking request is sent.

SlotAlreadyBookedError

If the slot is already booked.

SlotUnavailableError

If the slot is not available.

SlotNotBookableYetError

If the booking window has not opened yet.

BookingError

If the booking fails for another reason.

SlotNotFoundError

If no slot of the activity starts at that time.

(NotLoggedInError, ValueError, ActivityNotFoundError, APIError)

See find_slot().

book_slot

book_slot(slot: Slot) -> Slot

Book a slot found earlier, e.g. by find_slot() or daily_slots(), with a single request.

Unlike book(), this does not look the slot up again, so it suits time-critical bookings where the slot is found ahead of time. It does not check whether the slot is full either, because its counts may be stale by now: the API decides.

Parameters:

Name Type Description Default
slot Slot

The slot to book.

required

Returns:

Type Description
Slot

The given slot.

Raises:

Type Description
NotLoggedInError

If not logged in.

SlotAlreadyBookedError

If the slot is already booked.

SlotUnavailableError

If the slot is not available.

SlotNotBookableYetError

If the booking window has not opened yet.

BookingError

If the booking fails for another reason.

APIError

If the response is not a valid booking response.

cancel

cancel(activity: str, start: datetime | str) -> Slot

Cancel the booking of the slot of an activity that starts at a given time.

Parameters:

Name Type Description Default
activity str

The activity name, see activities().

required
start datetime | str

The start time, see find_slot().

required

Returns:

Type Description
Slot

The cancelled slot, as listed before the cancellation.

Raises:

Type Description
CancellationError

If the cancellation fails, e.g. because the slot was not booked.

SlotNotFoundError

If no slot of the activity starts at that time.

(NotLoggedInError, ValueError, ActivityNotFoundError, APIError)

See find_slot().