Booking can be made as early as possible depending on the club booking opening
This commit is contained in:
parent
fc11a1e1eb
commit
8562e101b4
5 changed files with 171 additions and 13 deletions
|
@ -1,19 +1,43 @@
|
|||
import asyncio
|
||||
import logging
|
||||
import time
|
||||
|
||||
import pendulum
|
||||
from aiohttp import ClientSession
|
||||
from pendulum import DateTime
|
||||
|
||||
import config
|
||||
from aiohttp import ClientSession
|
||||
from gestion_sports.gestion_sports_connector import GestionSportsConnector
|
||||
from models import BookingFilter, Club, User
|
||||
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def wait_until_booking_time(club: Club, booking_filter: BookingFilter):
|
||||
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:
|
||||
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:
|
||||
async with ClientSession() as session:
|
||||
platform = GestionSportsConnector(session, club.url)
|
||||
await platform.connect()
|
||||
await platform.login(user, club)
|
||||
wait_until_booking_time(club, booking_filter)
|
||||
return await platform.book(booking_filter, club)
|
||||
|
||||
|
||||
|
|
|
@ -21,7 +21,16 @@ def get_club() -> Club:
|
|||
else []
|
||||
)
|
||||
club_id = os.environ.get("CLUB_ID")
|
||||
return Club(id=club_id, url=club_url, courts_ids=court_ids)
|
||||
booking_open_days_before = int(os.environ.get("BOOKING_OPEN_DAYS_BEFORE", "7"))
|
||||
booking_opening_time_str = os.environ.get("BOOKING_OPENING_TIME", "00:00")
|
||||
booking_opening_time = pendulum.parse(booking_opening_time_str)
|
||||
return Club(
|
||||
id=club_id,
|
||||
url=club_url,
|
||||
courts_ids=court_ids,
|
||||
booking_open_days_before=booking_open_days_before,
|
||||
booking_opening_time=booking_opening_time.time(),
|
||||
)
|
||||
|
||||
|
||||
def get_booking_filter() -> BookingFilter:
|
||||
|
|
|
@ -1,11 +1,15 @@
|
|||
from pydantic import BaseModel, Field
|
||||
from pendulum import Time
|
||||
from pydantic import BaseModel, Field, ConfigDict
|
||||
from pydantic_extra_types.pendulum_dt import DateTime
|
||||
|
||||
|
||||
class Club(BaseModel):
|
||||
model_config = ConfigDict(arbitrary_types_allowed=True)
|
||||
id: str = Field()
|
||||
url: str = Field()
|
||||
courts_ids: list[int] = Field(default_factory=list)
|
||||
booking_open_days_before: int = Field(default=7)
|
||||
booking_opening_time: Time = Field(default=Time(hour=0, minute=0))
|
||||
|
||||
|
||||
class BookingFilter(BaseModel):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue