refactored code to place the booking specific actions inside a gestion-sports element

This commit is contained in:
Stanislas Jouffroy 2024-02-21 23:24:25 +01:00
parent d0a0072b6d
commit 26670bfe34
5 changed files with 251 additions and 215 deletions

View file

@ -1,53 +1,13 @@
import asyncio
import logging
import time
import config
import pendulum
from aiohttp import ClientSession
from gestion_sports.gestion_sports_connector import GestionSportsConnector
from gestion_sports.gestion_sports_operations import GestionSportsOperations
from models import BookingFilter, Club, User
from pendulum import DateTime
LOGGER = logging.getLogger(__name__)
def wait_until_booking_time(club: Club, booking_filter: BookingFilter):
"""
Wait until the booking is open.
The booking filter contains the date and time of the booking.
The club has the information about when the booking is open for that date.
:param club: the club where to book a court
:param booking_filter: the booking information
"""
LOGGER.info("Waiting booking time")
booking_datetime = build_booking_datetime(booking_filter, club)
now = pendulum.now()
while now < booking_datetime:
time.sleep(1)
now = pendulum.now()
def build_booking_datetime(booking_filter: BookingFilter, club: Club) -> DateTime:
"""
Build the date and time when the booking is open for a given match date.
The booking filter contains the date and time of the booking.
The club has the information about when the booking is open for that date.
:param booking_filter: the booking information
:param club: the club where to book a court
:return: the date and time when the booking is open
"""
date_to_book = booking_filter.date
booking_date = date_to_book.subtract(days=club.booking_open_days_before)
booking_hour = club.booking_opening_time.hour
booking_minute = club.booking_opening_time.minute
return booking_date.at(booking_hour, booking_minute)
async def book(club: Club, user: User, booking_filter: BookingFilter) -> int | None:
"""
Book a court for a user to a club following a booking filter
@ -57,12 +17,8 @@ async def book(club: Club, user: User, booking_filter: BookingFilter) -> int | N
:param booking_filter: the information related to the booking
:return: the id of the booked court, or None if no court was booked
"""
async with ClientSession() as session:
platform = GestionSportsConnector(session, club.url)
await platform.land()
await platform.login(user, club)
wait_until_booking_time(club, booking_filter)
return await platform.book(booking_filter, club)
async with GestionSportsOperations(club) as platform:
return await platform.book(user, booking_filter)
def main() -> int | None:

View file

@ -0,0 +1,70 @@
import logging
import time
import pendulum
from aiohttp import ClientSession
from gestion_sports.gestion_sports_connector import GestionSportsConnector
from models import BookingFilter, Club, User
from pendulum import DateTime
LOGGER = logging.getLogger(__name__)
class GestionSportsOperations:
def __init__(self, club: Club):
self.platform: GestionSportsConnector = None
self.club: Club = club
self.session: ClientSession | None = None
async def __aenter__(self):
self.session = ClientSession()
self.platform = GestionSportsConnector(self.session, self.club.url)
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
await self.session.close()
async def book(self, user: User, booking_filter: BookingFilter) -> int | None:
if self.platform is None or user is None or booking_filter is None:
return None
await self.platform.land()
await self.platform.login(user, self.club)
self.wait_until_booking_time(self.club, booking_filter)
return await self.platform.book(booking_filter, self.club)
@staticmethod
def wait_until_booking_time(club: Club, booking_filter: BookingFilter):
"""
Wait until the booking is open.
The booking filter contains the date and time of the booking.
The club has the information about when the booking is open for that date.
:param club: the club where to book a court
:param booking_filter: the booking information
"""
LOGGER.info("Waiting booking time")
booking_datetime = build_booking_datetime(booking_filter, club)
now = pendulum.now()
while now < booking_datetime:
time.sleep(1)
now = pendulum.now()
def build_booking_datetime(booking_filter: BookingFilter, club: Club) -> DateTime:
"""
Build the date and time when the booking is open for a given match date.
The booking filter contains the date and time of the booking.
The club has the information about when the booking is open for that date.
:param booking_filter: the booking information
:param club: the club where to book a court
:return: the date and time when the booking is open
"""
date_to_book = booking_filter.date
booking_date = date_to_book.subtract(days=club.booking_open_days_before)
booking_hour = club.booking_opening_time.hour
booking_minute = club.booking_opening_time.minute
return booking_date.at(booking_hour, booking_minute)