Refactoring for reading the config
This commit is contained in:
parent
963ee6b86f
commit
fc11a1e1eb
9 changed files with 264 additions and 166 deletions
|
@ -4,30 +4,36 @@ import logging
|
|||
import config
|
||||
from aiohttp import ClientSession
|
||||
from gestion_sports.gestion_sports_connector import GestionSportsConnector
|
||||
from models import BookingFilter, User
|
||||
from models import BookingFilter, Club, User
|
||||
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def book(url: str, user: User, booking_filter: BookingFilter) -> None:
|
||||
async def book(club: Club, user: User, booking_filter: BookingFilter) -> int | None:
|
||||
async with ClientSession() as session:
|
||||
platform = GestionSportsConnector(session, url)
|
||||
platform = GestionSportsConnector(session, club.url)
|
||||
await platform.connect()
|
||||
await platform.login(user)
|
||||
await platform.book(booking_filter)
|
||||
await platform.login(user, club)
|
||||
return await platform.book(booking_filter, club)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
LOGGER.info("Starting booking padel court")
|
||||
def main() -> int | None:
|
||||
user = config.get_user()
|
||||
booking_filter = config.get_booking_filter()
|
||||
club = config.get_club()
|
||||
|
||||
LOGGER.info(
|
||||
f"login={config.USER}, password={config.PASSWORD}, club_id={config.CLUB_ID}"
|
||||
"Starting booking court of %s for user %s at club %s at %s",
|
||||
booking_filter.sport_id,
|
||||
user.login,
|
||||
club.id,
|
||||
booking_filter.date,
|
||||
)
|
||||
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_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")
|
||||
court_booked = asyncio.run(book(club, user, booking_filter))
|
||||
if court_booked:
|
||||
LOGGER.info(
|
||||
"Court %s booked successfully at %s", court_booked, booking_filter.date
|
||||
)
|
||||
else:
|
||||
LOGGER.info("Booking did not work")
|
||||
return court_booked
|
||||
|
|
|
@ -1,21 +1,50 @@
|
|||
import json
|
||||
import logging.config
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
import pendulum
|
||||
import yaml
|
||||
from dotenv import load_dotenv
|
||||
|
||||
from resa_padel.models import BookingFilter, Club, User
|
||||
|
||||
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 get_club() -> Club:
|
||||
club_url = os.environ.get("CLUB_URL")
|
||||
court_ids_tmp = os.environ.get("COURT_IDS") or ""
|
||||
court_ids = (
|
||||
[int(court_id) for court_id in court_ids_tmp.split(",")]
|
||||
if court_ids_tmp
|
||||
else []
|
||||
)
|
||||
club_id = os.environ.get("CLUB_ID")
|
||||
return Club(id=club_id, url=club_url, courts_ids=court_ids)
|
||||
|
||||
|
||||
def get_booking_filter() -> BookingFilter:
|
||||
sport_id_tmp = os.environ.get("SPORT_ID")
|
||||
sport_id = int(sport_id_tmp) if sport_id_tmp else None
|
||||
date_time_tmp = os.environ.get("DATE_TIME")
|
||||
date_time = pendulum.parse(date_time_tmp) if date_time_tmp else None
|
||||
return BookingFilter(sport_id=sport_id, date=date_time)
|
||||
|
||||
|
||||
def get_user() -> User:
|
||||
login = os.environ.get("LOGIN")
|
||||
password = os.environ.get("PASSWORD")
|
||||
return User(login=login, password=password)
|
||||
|
||||
|
||||
def get_post_headers(platform_id: str) -> dict:
|
||||
root_path = Path(__file__).parent
|
||||
headers_file = Path(root_path, "resources", platform_id, "post-headers.json")
|
||||
with headers_file.open(mode="r", encoding="utf-8") as f:
|
||||
headers = json.load(f)
|
||||
|
||||
return headers
|
||||
|
||||
|
||||
def init_log_config():
|
||||
|
|
|
@ -1,25 +1,20 @@
|
|||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
from urllib.parse import urljoin
|
||||
|
||||
from aiohttp import ClientResponse, ClientSession
|
||||
from gestion_sports.gestion_sports_payload_builder import \
|
||||
GestionSportsPayloadBuilder
|
||||
from models import BookingFilter, User
|
||||
|
||||
import config
|
||||
from gestion_sports.gestion_sports_payload_builder import GestionSportsPayloadBuilder
|
||||
from models import BookingFilter, Club, User
|
||||
|
||||
DATE_FORMAT = "%d/%m/%Y"
|
||||
|
||||
TIME_FORMAT = "%H:%M"
|
||||
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
HEADERS = {
|
||||
"Connection": "keep-alive",
|
||||
"Accept-Language": "en-US,en;q=0.5",
|
||||
"Accept-Encoding": "gzip, deflate, br",
|
||||
"DNT": "1",
|
||||
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
|
||||
"Accept": "application/json, text/javascript, */*; q=0.01",
|
||||
"X-Requested-With": "XMLHttpRequest",
|
||||
"Sec-Fetch-Dest": "empty",
|
||||
"Sec-Fetch-Mode": "cors",
|
||||
"Sec-Fetch-Site": "same-origin",
|
||||
}
|
||||
POST_HEADERS = config.get_post_headers("gestion-sports")
|
||||
|
||||
|
||||
class GestionSportsConnector:
|
||||
|
@ -32,15 +27,15 @@ class GestionSportsConnector:
|
|||
|
||||
@property
|
||||
def connection_url(self) -> str:
|
||||
return f"{self.url}/connexion.php?"
|
||||
return urljoin(self.url, "/connexion.php?")
|
||||
|
||||
@property
|
||||
def login_url(self) -> str:
|
||||
return f"{self.url}/connexion.php?"
|
||||
return urljoin(self.url, "/connexion.php?")
|
||||
|
||||
@property
|
||||
def booking_url(self) -> str:
|
||||
return f"{self.url}/membre/reservation.html?"
|
||||
return urljoin(self.url, "/membre/reservation.html?")
|
||||
|
||||
async def connect(self) -> ClientResponse:
|
||||
LOGGER.info("Connecting to GestionSports API")
|
||||
|
@ -48,29 +43,35 @@ class GestionSportsConnector:
|
|||
await response.text()
|
||||
return response
|
||||
|
||||
async def login(self, user: User) -> ClientResponse:
|
||||
async def login(self, user: User, club: Club) -> ClientResponse:
|
||||
payload = (
|
||||
self.payload_builder.login(user.login)
|
||||
.password(user.password)
|
||||
.club_id(user.club_id)
|
||||
.club_id(club.id)
|
||||
.build_login_payload()
|
||||
)
|
||||
|
||||
async with self.session.post(
|
||||
self.login_url, data=payload, headers=HEADERS
|
||||
self.login_url, data=payload, headers=POST_HEADERS
|
||||
) as response:
|
||||
await response.text()
|
||||
return response
|
||||
|
||||
async def book(self, booking_filter: BookingFilter) -> int | None:
|
||||
async def book(self, booking_filter: BookingFilter, club: Club) -> int | None:
|
||||
# use asyncio to request a booking on every court
|
||||
# the gestion-sports backend is able to book only one court for a user
|
||||
bookings = await asyncio.gather(
|
||||
*[
|
||||
self.book_one_court(booking_filter, court_id)
|
||||
for court_id in booking_filter.court_ids
|
||||
for court_id in club.courts_ids
|
||||
],
|
||||
return_exceptions=True,
|
||||
)
|
||||
|
||||
return await self.get_booked_court(bookings)
|
||||
|
||||
@staticmethod
|
||||
async def get_booked_court(bookings):
|
||||
for court, is_booked in bookings:
|
||||
if is_booked:
|
||||
return court
|
||||
|
@ -79,11 +80,9 @@ class GestionSportsConnector:
|
|||
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))
|
||||
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(court_id)
|
||||
.build_booking_payload()
|
||||
|
@ -92,7 +91,7 @@ class GestionSportsConnector:
|
|||
|
||||
async def is_court_booked(self, payload: str) -> bool:
|
||||
async with self.session.post(
|
||||
self.booking_url, data=payload, headers=HEADERS
|
||||
self.booking_url, data=payload, headers=POST_HEADERS
|
||||
) as response:
|
||||
return self.is_response_status_ok(await response.text())
|
||||
|
||||
|
|
|
@ -1,16 +1,18 @@
|
|||
from typing import List
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
from pydantic_extra_types.pendulum_dt import DateTime
|
||||
|
||||
|
||||
class Club(BaseModel):
|
||||
id: str = Field()
|
||||
url: str = Field()
|
||||
courts_ids: list[int] = Field(default_factory=list)
|
||||
|
||||
|
||||
class BookingFilter(BaseModel):
|
||||
sport_id: int = Field()
|
||||
date: DateTime = Field()
|
||||
|
||||
|
||||
class User(BaseModel):
|
||||
login: str = Field()
|
||||
password: str = Field(repr=False)
|
||||
club_id: str = Field()
|
||||
|
||||
|
||||
class BookingFilter(BaseModel):
|
||||
court_ids: List[int] = Field()
|
||||
sport_id: int = Field()
|
||||
date: DateTime = Field()
|
||||
|
|
12
resa_padel/resources/gestion-sports/post-headers.json
Normal file
12
resa_padel/resources/gestion-sports/post-headers.json
Normal file
|
@ -0,0 +1,12 @@
|
|||
{
|
||||
"Connection": "keep-alive",
|
||||
"Accept-Language": "en-US,en;q=0.5",
|
||||
"Accept-Encoding": "gzip, deflate, br",
|
||||
"DNT": "1",
|
||||
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
|
||||
"Accept": "application/json, text/javascript, */*; q=0.01",
|
||||
"X-Requested-With": "XMLHttpRequest",
|
||||
"Sec-Fetch-Dest": "empty",
|
||||
"Sec-Fetch-Mode": "cors",
|
||||
"Sec-Fetch-Site": "same-origin"
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue