From 6c29fc08021e716cfcf573d0f03597074abdf99e Mon Sep 17 00:00:00 2001 From: kbe Date: Fri, 8 Aug 2025 22:19:40 +0200 Subject: [PATCH] chore: Renamed datetime to dt --- crossfit_booker.py | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/crossfit_booker.py b/crossfit_booker.py index 00fbf16..1c118c1 100644 --- a/crossfit_booker.py +++ b/crossfit_booker.py @@ -1,6 +1,7 @@ # Native modules import logging, traceback, os, time -from datetime import datetime, timedelta, date +import datetime as dt +from datetime import timedelta, date # Third-party modules import requests, pytz @@ -89,11 +90,11 @@ class CrossFitBooker: time.sleep(wt) return None - def _parse_local(self, ts: str) -> datetime: + def _parse_local(self, ts: str) -> dt.datetime: dt = parse(ts) return dt if dt.tzinfo else pytz.timezone(TIMEZONE).localize(dt) - def _fmt_session(self, s: Dict[str, Any], dt: Optional[datetime] = None) -> str: + def _fmt_session(self, s: Dict[str, Any], dt: Optional[dt] = None) -> str: dt = dt or self._parse_local(s["start_timestamp"]) return f"{s['id_activity_calendar']} {s['name_activity']} at {dt.strftime('%Y-%m-%d %H:%M')}" @@ -180,7 +181,7 @@ class CrossFitBooker: logging.debug("[get_available_sessions] No response (None) from API") return r - def matches_preferred_session(self, session: Dict[str, Any], current_time: datetime) -> bool: + def matches_preferred_session(self, session: Dict[str, Any], current_time: dt.datetime) -> bool: try: st = self._parse_local(session["start_timestamp"]) dow, hhmm = st.weekday(), st.strftime("%H:%M") @@ -192,7 +193,7 @@ class CrossFitBooker: logging.error(f"Failed to check session: {e} - Session: {session}") return False - def is_session_bookable(self, session: Dict[str, Any], current_time: datetime) -> bool: + def is_session_bookable(self, session: Dict[str, Any], current_time: dt.datetime) -> bool: """ Check if a session is bookable based on user_info. """ @@ -205,7 +206,7 @@ class CrossFitBooker: booking_time_str: str = user_info.get("unableToBookUntilTime", "") if booking_date_str and booking_time_str: try: - booking_datetime: datetime = datetime.strptime( + booking_datetime: dt.datetime = dt.datetime.strptime( f"{booking_date_str} {booking_time_str}", "%d-%m-%Y %H:%M" ) @@ -268,7 +269,7 @@ class CrossFitBooker: return False # Script main entry point - async def execute_cycle(self, current_time: datetime) -> None: + async def execute_cycle(self, current_time: dt.datetime) -> None: start_date, end_date = current_time.date(), current_time.date() + timedelta(days=2) sessions_data = self.get_available_sessions(start_date, end_date) if not sessions_data or not sessions_data.get("success", False): @@ -317,21 +318,29 @@ class CrossFitBooker: async def run(self) -> None: tz = pytz.timezone(TIMEZONE) th, tm = map(int, TARGET_RESERVATION_TIME.split(":")) - target_time = datetime.now(tz).replace(hour=th, minute=tm, second=0, microsecond=0) + target_time = dt.datetime.now(tz).replace(hour=th, minute=tm, second=0, microsecond=0) booking_window_end = target_time + timedelta(minutes=BOOKING_WINDOW_END_DELTA_MINUTES) if not self.login(): - logging.error("Authentication failed - exiting program"); return + logging.error("Authentication failed - exiting program") + return try: while True: try: - now = datetime.now(tz) + now = dt.datetime.now(tz) logging.info(f"Current time: {now}") - if target_time <= now <= booking_window_end: - await self.execute_cycle(now); time.sleep(60) + # Check if current time is within the booking window + logging.debug(f"Current time: {now}, Target time: {target_time}, Booking window end: {booking_window_end}") + # Only execute cycle if we're within the booking window + if target_time <= now < booking_window_end: + logging.debug("Inside booking window - executing cycle") + await self.execute_cycle(now) + time.sleep(60) else: + logging.debug("Outside booking window - sleeping for 300 seconds") time.sleep(300) except Exception as e: - logging.error(f"Unexpected error in booking cycle: {e} - Traceback: {traceback.format_exc()}"); time.sleep(60) + logging.error(f"Unexpected error in booking cycle: {e} - Traceback: {traceback.format_exc()}") + time.sleep(60) except KeyboardInterrupt: self.quit()