feat: only book session during window + 1h
This commit is contained in:
@@ -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()}")
|
||||
|
||||
Reference in New Issue
Block a user