feat: Add a tolerance window of 5 minutes for reservation

This commit is contained in:
kbe
2025-07-21 15:19:38 +02:00
parent 18cf2a0a18
commit 3a378c03a6
2 changed files with 224 additions and 29 deletions

View File

@@ -513,39 +513,46 @@ class CrossFitBooker:
logging.error(f"Failed to book {session_type} session at {session_time} - Session: {session}")
def run(self) -> None:
"""
Main execution loop.
"""
# Set up timezone
tz: pytz.timezone = pytz.timezone(TIMEZONE)
"""
Main execution loop.
"""
# Set up timezone
tz: pytz.timezone = pytz.timezone(TIMEZONE)
# Initial login
if not self.login():
logging.error("Authentication failed - exiting program")
return
# Initial login
if not self.login():
logging.error("Authentication failed - exiting program")
return
try:
while True:
try:
current_time: datetime = datetime.now(tz)
logging.info(f"Current time: {current_time}")
try:
while True:
try:
current_time: datetime = datetime.now(tz)
logging.info(f"Current time: {current_time}")
# Always run booking cycle to check for preferred sessions and notify
self.run_booking_cycle(current_time)
# Always run booking cycle to check for preferred sessions and notify
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())
else:
# Check again in 5 minutes
time.sleep(300)
except Exception as e:
logging.error(f"Unexpected error in booking cycle: {str(e)} - Traceback: {traceback.format_exc()}")
time.sleep(60) # Wait before retrying after error
except KeyboardInterrupt:
self.quit()
# Run booking cycle at the target time for actual booking
if current_time.strftime("%H:%M") == TARGET_RESERVATION_TIME:
# Wait until the next booking window
wait_until = current_time + timedelta(minutes=60)
time.sleep((wait_until - current_time).total_seconds())
else:
# Check again in 5 minutes
time.sleep(300)
except Exception as e:
logging.error(f"Unexpected error in booking cycle: {str(e)} - Traceback: {traceback.format_exc()}")
time.sleep(60) # Wait before retrying after error
except KeyboardInterrupt:
self.quit()
def quit(self) -> None:
"""