Added a service that can get all current tournaments list
This commit is contained in:
parent
e6023e0687
commit
3d0bd47079
26 changed files with 4305 additions and 204 deletions
|
@ -1,10 +1,12 @@
|
|||
import json
|
||||
import logging
|
||||
import time
|
||||
|
||||
import pendulum
|
||||
from aiohttp import ClientSession
|
||||
from connectors import GestionSportsConnector
|
||||
from models import BookingFilter, BookingOpening, Club, Court, User
|
||||
from bs4 import BeautifulSoup
|
||||
from gestion_sport_connector import GestionSportsConnector
|
||||
from models import BookingFilter, BookingOpening, Club, Court, Tournament, User
|
||||
from pendulum import DateTime
|
||||
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
|
@ -125,3 +127,62 @@ class GestionSportsServices:
|
|||
booking_minute = opening_time.minute
|
||||
|
||||
return booking_date.at(booking_hour, booking_minute)
|
||||
|
||||
@staticmethod
|
||||
async def get_all_tournaments(user: User, club: Club) -> list[Tournament]:
|
||||
connector = GestionSportsConnector(club)
|
||||
async with ClientSession() as session:
|
||||
await connector.land(session)
|
||||
await connector.login(session, user)
|
||||
|
||||
session_html = await connector.send_tournaments_sessions_request(session)
|
||||
tournaments_id = GestionSportsServices.retrieve_tournament_session(
|
||||
await session_html.text()
|
||||
)
|
||||
|
||||
tournaments = await connector.send_tournaments_request(
|
||||
session, tournaments_id
|
||||
)
|
||||
return GestionSportsServices.retrieve_tournaments(await tournaments.text())
|
||||
|
||||
@staticmethod
|
||||
def retrieve_tournament_session(sessions: str) -> str:
|
||||
session_object = json.loads(sessions).get("Inscription tournois:school-outline")
|
||||
return list(session_object.keys())[0]
|
||||
|
||||
@staticmethod
|
||||
def retrieve_tournaments(html: str) -> list[Tournament]:
|
||||
soup = BeautifulSoup(html, "html.parser")
|
||||
tournaments = []
|
||||
|
||||
cards = soup.find_all("div", {"class": "card-body"})
|
||||
for card in cards:
|
||||
title = card.find("h5")
|
||||
price = title.find("span").get_text().strip()
|
||||
name = title.get_text().strip().removesuffix(price).strip()
|
||||
elements = card.find("div", {"class": "row"}).find_all("li")
|
||||
date = elements[0].get_text().strip()
|
||||
start_time, end_time = (
|
||||
elements[2].get_text().strip().replace("h", ":").split(" - ")
|
||||
)
|
||||
start_datetime = pendulum.from_format(
|
||||
f"{date} {start_time}", "DD/MM/YYYY HH:mm"
|
||||
)
|
||||
end_datetime = pendulum.from_format(
|
||||
f"{date} {end_time}", "DD/MM/YYYY HH:mm"
|
||||
)
|
||||
gender = elements[1].get_text().strip()
|
||||
places_left = (
|
||||
card.find("span", {"class": "nb_place_libre"}).get_text().strip()
|
||||
)
|
||||
tournament = Tournament(
|
||||
name=name,
|
||||
price=price,
|
||||
start_date=start_datetime,
|
||||
end_date=end_datetime,
|
||||
gender=gender,
|
||||
places_left=places_left,
|
||||
)
|
||||
tournaments.append(tournament)
|
||||
|
||||
return tournaments
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue