From ef6506959215361a35ed1a45727270ef1270f13a Mon Sep 17 00:00:00 2001 From: kbe Date: Mon, 21 Jul 2025 23:48:14 +0200 Subject: [PATCH] feat: only book session during window + 1h --- crossfit_booker.py | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/crossfit_booker.py b/crossfit_booker.py index 74adb63..286fe36 100644 --- a/crossfit_booker.py +++ b/crossfit_booker.py @@ -525,6 +525,11 @@ class CrossFitBooker: # Set up timezone tz: pytz.timezone = pytz.timezone(TIMEZONE) + # Parse TARGET_RESERVATION_TIME to get the target hour and minute + target_hour, target_minute = map(int, TARGET_RESERVATION_TIME.split(":")) + target_time = datetime.now(tz).replace(hour=target_hour, minute=target_minute, second=0, microsecond=0) + booking_window_end = target_time + timedelta(hours=1) + # Initial login if not self.login(): logging.error("Authentication failed - exiting program") @@ -536,22 +541,14 @@ class CrossFitBooker: current_time: datetime = datetime.now(tz) logging.info(f"Current time: {current_time}") - # Always run booking cycle to check for preferred sessions and notify - await self.run_booking_cycle(current_time) - - # Run booking cycle at the target time for actual booking - target_time_str = current_time.strftime("%H:%M") - target_time = datetime.strptime(target_time_str, "%H:%M").replace(year=current_time.year, month=current_time.month, day=current_time.day, tzinfo=tz) - tolerance_window = timedelta(minutes=5) - - # Check if current time is within the tolerance window after the target time - if (target_time <= current_time <= (target_time + tolerance_window)): - # Calculate the next booking window - next_booking_window = current_time + timedelta(minutes=20) - # Wait until the next booking window - time.sleep((next_booking_window - current_time).total_seconds()) + # Only book sessions if current time is within the booking window + if target_time <= current_time <= booking_window_end: + # Run booking cycle to check for preferred sessions and book + await self.run_booking_cycle(current_time) + # Wait for a short time before next check + time.sleep(60) else: - # Check again in 5 minutes + # Check again in 5 minutes if outside booking window time.sleep(300) except Exception as e: logging.error(f"Unexpected error in booking cycle: {str(e)} - Traceback: {traceback.format_exc()}")