ABle to send the booking request to several courts at the same time
This commit is contained in:
parent
fcc08f03f1
commit
51af600d28
11 changed files with 288 additions and 99 deletions
|
@ -1,33 +1,33 @@
|
|||
import asyncio
|
||||
import logging
|
||||
|
||||
import pendulum
|
||||
import config
|
||||
from aiohttp import ClientSession
|
||||
|
||||
from resa_padel import config
|
||||
from resa_padel.gestion_sports.gestion_sports_connector import GestionSportsConnector
|
||||
from resa_padel.models import User, BookingFilter
|
||||
from gestion_sports.gestion_sports_connector import GestionSportsConnector
|
||||
from models import BookingFilter, User
|
||||
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def book(url: str, user: User, booking_filter: BookingFilter) -> ClientSession:
|
||||
async def book(url: str, user: User, booking_filter: BookingFilter) -> None:
|
||||
async with ClientSession() as session:
|
||||
platform = GestionSportsConnector(session, url)
|
||||
await platform.connect()
|
||||
await platform.login(user)
|
||||
await platform.book(booking_filter)
|
||||
|
||||
return session
|
||||
|
||||
|
||||
def main() -> None:
|
||||
LOGGER.info("Starting booking padel court")
|
||||
LOGGER.info(
|
||||
f"login={config.USER}, password={config.PASSWORD}, club_id={config.CLUB_ID}"
|
||||
)
|
||||
user = User(login=config.USER, password=config.PASSWORD, club_id=config.CLUB_ID)
|
||||
LOGGER.info(
|
||||
f"court_id={config.COURT_IDS},sport_id={config.SPORT_ID},date={config.DATE_TIME}"
|
||||
)
|
||||
booking_filter = BookingFilter(
|
||||
court_id=config.COURT_ID,
|
||||
sport_id=config.SPORT_ID,
|
||||
date=pendulum.parse(config.DATE_TIME),
|
||||
court_ids=config.COURT_IDS, sport_id=config.SPORT_ID, date=config.DATE_TIME
|
||||
)
|
||||
asyncio.run(book(config.GESTION_SPORTS_URL, user, booking_filter))
|
||||
LOGGER.info("Finished booking padel court")
|
||||
|
|
|
@ -1,7 +1,21 @@
|
|||
import logging.config
|
||||
import os
|
||||
|
||||
import pendulum
|
||||
import yaml
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
|
||||
GESTION_SPORTS_URL = "https://toulousepadelclub.gestion-sports.com"
|
||||
USER = os.environ.get("USER")
|
||||
PASSWORD = os.environ.get("PASSWORD")
|
||||
CLUB_ID = os.environ.get("CLUB_ID")
|
||||
_court_ids = os.environ.get("COURT_IDS") or ""
|
||||
COURT_IDS = [int(court_id) for court_id in _court_ids.split(",")] if _court_ids else []
|
||||
SPORT_ID = int(os.environ.get("SPORT_ID"))
|
||||
DATE_TIME = pendulum.parse(os.environ.get("DATE_TIME"))
|
||||
|
||||
|
||||
def init_log_config():
|
||||
|
@ -11,12 +25,3 @@ def init_log_config():
|
|||
with open(logging_file, "r") as f:
|
||||
logging_config = yaml.safe_load(f.read())
|
||||
logging.config.dictConfig(logging_config)
|
||||
|
||||
|
||||
GESTION_SPORTS_URL = "https://toulousepadelclub.gestion-sports.com"
|
||||
USER = os.environ.get("USER")
|
||||
PASSWORD = os.environ.get("PASSWORD")
|
||||
CLUB_ID = os.environ.get("CLUB_ID")
|
||||
COURT_ID = os.environ.get("COURT_ID")
|
||||
SPORT_ID = os.environ.get("SPORT_ID")
|
||||
DATE_TIME = os.environ.get("DATE_TIME")
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
|
||||
from aiohttp import ClientSession, ClientResponse
|
||||
|
||||
from resa_padel.gestion_sports.gestion_sports_payload_builder import (
|
||||
GestionSportsPayloadBuilder,
|
||||
)
|
||||
from resa_padel.models import User
|
||||
from aiohttp import ClientResponse, ClientSession
|
||||
from gestion_sports.gestion_sports_payload_builder import \
|
||||
GestionSportsPayloadBuilder
|
||||
from models import BookingFilter, User
|
||||
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
HEADERS = {
|
||||
|
@ -30,13 +30,21 @@ class GestionSportsConnector:
|
|||
self.session = session
|
||||
self.payload_builder = GestionSportsPayloadBuilder()
|
||||
|
||||
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||
self.session.close()
|
||||
@property
|
||||
def connection_url(self) -> str:
|
||||
return f"{self.url}/connexion.php?"
|
||||
|
||||
@property
|
||||
def login_url(self) -> str:
|
||||
return f"{self.url}/connexion.php?"
|
||||
|
||||
@property
|
||||
def booking_url(self) -> str:
|
||||
return f"{self.url}/membre/reservation.html?"
|
||||
|
||||
async def connect(self) -> ClientResponse:
|
||||
LOGGER.info("Connecting to GestionSports API")
|
||||
connection_url = self.url + "/connexion.php?"
|
||||
async with self.session.get(connection_url) as response:
|
||||
async with self.session.get(self.connection_url) as response:
|
||||
await response.text()
|
||||
return response
|
||||
|
||||
|
@ -48,29 +56,48 @@ class GestionSportsConnector:
|
|||
.build_login_payload()
|
||||
)
|
||||
|
||||
login_url = f"{self.url}/connexion.php?"
|
||||
|
||||
async with self.session.post(
|
||||
login_url, data=payload, headers=HEADERS
|
||||
self.login_url, data=payload, headers=HEADERS
|
||||
) as response:
|
||||
await response.text()
|
||||
return response
|
||||
|
||||
async def book(self, booking_filter) -> ClientResponse:
|
||||
async def book(self, booking_filter: BookingFilter) -> int | None:
|
||||
bookings = await asyncio.gather(
|
||||
*[
|
||||
self.book_one_court(booking_filter, court_id)
|
||||
for court_id in booking_filter.court_ids
|
||||
],
|
||||
return_exceptions=True,
|
||||
)
|
||||
|
||||
for court, is_booked in bookings:
|
||||
if is_booked:
|
||||
return court
|
||||
return None
|
||||
|
||||
async def book_one_court(
|
||||
self, booking_filter: BookingFilter, court_id: int
|
||||
) -> tuple[int, bool]:
|
||||
date_format = "%d/%m/%Y"
|
||||
time_format = "%H:%M"
|
||||
payload = (
|
||||
self.payload_builder.date(booking_filter.date.date().strftime(date_format))
|
||||
.time(booking_filter.date.time().strftime(time_format))
|
||||
.sport_id(booking_filter.sport_id)
|
||||
.court_id(booking_filter.court_id)
|
||||
.court_id(court_id)
|
||||
.build_booking_payload()
|
||||
)
|
||||
return court_id, await self.is_court_booked(payload)
|
||||
|
||||
booking_url = f"{self.url}/membre/reservation.html?"
|
||||
|
||||
async def is_court_booked(self, payload: str) -> bool:
|
||||
async with self.session.post(
|
||||
booking_url, data=payload, headers=HEADERS
|
||||
self.booking_url, data=payload, headers=HEADERS
|
||||
) as response:
|
||||
await response.text()
|
||||
return response
|
||||
return self.is_response_status_ok(await response.text())
|
||||
|
||||
@staticmethod
|
||||
def is_response_status_ok(response: str) -> bool:
|
||||
formatted_result = response.removeprefix('"').removesuffix('"')
|
||||
result_json = json.loads(formatted_result)
|
||||
return result_json["status"] == "ok"
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
from resa_padel.exceptions import ArgumentMissing
|
||||
from exceptions import ArgumentMissing
|
||||
|
||||
|
||||
class GestionSportsPayloadBuilder:
|
||||
|
@ -11,31 +11,31 @@ class GestionSportsPayloadBuilder:
|
|||
self._sport_id = None
|
||||
self._court_id = None
|
||||
|
||||
def login(self, login):
|
||||
def login(self, login: str):
|
||||
self._login = login
|
||||
return self
|
||||
|
||||
def password(self, password):
|
||||
def password(self, password: str):
|
||||
self._password = password
|
||||
return self
|
||||
|
||||
def club_id(self, club_id):
|
||||
def club_id(self, club_id: str):
|
||||
self._club_id = club_id
|
||||
return self
|
||||
|
||||
def date(self, date):
|
||||
def date(self, date: str):
|
||||
self._date = date
|
||||
return self
|
||||
|
||||
def time(self, time):
|
||||
def time(self, time: str):
|
||||
self._time = time
|
||||
return self
|
||||
|
||||
def sport_id(self, sport_id):
|
||||
def sport_id(self, sport_id: int):
|
||||
self._sport_id = sport_id
|
||||
return self
|
||||
|
||||
def court_id(self, court_id):
|
||||
def court_id(self, court_id: int):
|
||||
self._court_id = court_id
|
||||
return self
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
from typing import List
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
from pydantic_extra_types.pendulum_dt import DateTime
|
||||
|
||||
|
@ -9,6 +11,6 @@ class User(BaseModel):
|
|||
|
||||
|
||||
class BookingFilter(BaseModel):
|
||||
court_id: int = Field()
|
||||
court_ids: List[int] = Field()
|
||||
sport_id: int = Field()
|
||||
date: DateTime = Field()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue