114 lines
3.3 KiB
Python
114 lines
3.3 KiB
Python
import json
|
|
import logging.config
|
|
import os
|
|
from pathlib import Path
|
|
|
|
import pendulum
|
|
import yaml
|
|
from dotenv import load_dotenv
|
|
from models import BookingFilter, Club, User
|
|
|
|
load_dotenv()
|
|
|
|
|
|
def get_club() -> Club:
|
|
"""
|
|
Read the environment variables related to the current club
|
|
and build the Club object
|
|
|
|
:return: the 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")
|
|
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:
|
|
"""
|
|
Read the environment variables related to the current booking filter
|
|
and build the BookingFilter object
|
|
|
|
:return: the club
|
|
"""
|
|
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:
|
|
"""
|
|
Read the environment variables related to the current user
|
|
and build the User object
|
|
|
|
:return: the club
|
|
"""
|
|
login = os.environ.get("LOGIN")
|
|
password = os.environ.get("PASSWORD")
|
|
return User(login=login, password=password)
|
|
|
|
|
|
def get_available_users() -> list[User]:
|
|
"""
|
|
Read the environment variables to get all the available users in order
|
|
to increase the chance of having a user with a free slot for a booking
|
|
|
|
:return: the list of all users that can book a court
|
|
"""
|
|
available_users_credentials = os.environ.get("AVAILABLE_USERS_CREDENTIALS")
|
|
available_users = [
|
|
credential for credential in available_users_credentials.split(",")
|
|
]
|
|
users = []
|
|
for user in available_users:
|
|
login, password = user.split(":")
|
|
users.append(User(login=login, password=password))
|
|
|
|
return users
|
|
|
|
|
|
def get_post_headers(platform_id: str) -> dict:
|
|
"""
|
|
Get the headers for the POST endpoint related to a specific booking platform
|
|
|
|
:param platform_id: the platform to which the headers apply
|
|
:return: the headers as a dictionary
|
|
"""
|
|
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():
|
|
"""
|
|
Read the logging.yaml file to initialize the logging configuration
|
|
"""
|
|
root_dir = os.path.realpath(os.path.dirname(__file__))
|
|
logging_file = root_dir + "/logging.yaml"
|
|
|
|
with open(logging_file, "r") as f:
|
|
logging_config = yaml.safe_load(f.read())
|
|
logging.config.dictConfig(logging_config)
|
|
|
|
|
|
ROOT_PATH = Path(__file__).parent.resolve()
|
|
RESOURCES_DIR = Path(ROOT_PATH, "resources")
|