From fa9d73a9a9e902fa6a54723c5035dc39c612d1f0 Mon Sep 17 00:00:00 2001 From: kbe Date: Tue, 12 Aug 2025 02:02:20 +0200 Subject: [PATCH] feat: enhance booking process with improved error handling and logging Added logging to display message when outside booking window in booker.py Added error handling for asyncio.run in crossfit_booker.py Added logging for errors during booking process --- src/booker.py | 2 ++ src/crossfit_booker.py | 5 ++- test/test_booking_window.py | 66 +++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 test/test_booking_window.py diff --git a/src/booker.py b/src/booker.py index 832e7c6..ea1667a 100644 --- a/src/booker.py +++ b/src/booker.py @@ -213,6 +213,8 @@ class Booker: # Wait for a short time before next check time.sleep(60) else: + # Display message when outside booking window + logging.info(f"Not booking now - current time {current_time} is outside the booking window ({target_time} to {booking_window_end})") # Check again in 5 minutes if outside booking window time.sleep(300) except Exception as e: diff --git a/src/crossfit_booker.py b/src/crossfit_booker.py index 9a3bc0d..9cb0a44 100644 --- a/src/crossfit_booker.py +++ b/src/crossfit_booker.py @@ -84,7 +84,10 @@ class CrossFitBooker: This method initiates the booking process by running the Booker's main execution loop. """ import asyncio - asyncio.run(self.booker.run()) + try: + asyncio.run(self.booker.run()) + except Exception as e: + logging.error(f"Error in booking process: {str(e)}") def get_auth_headers(self) -> Dict[str, str]: """ diff --git a/test/test_booking_window.py b/test/test_booking_window.py new file mode 100644 index 0000000..8469047 --- /dev/null +++ b/test/test_booking_window.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python3 +""" +Test script to verify booking window functionality. +""" +import os +import sys +import logging +from datetime import datetime, timedelta +import pytz + +# Add the parent directory to the path +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) + +from src.crossfit_booker import CrossFitBooker +from src.booker import Booker +from src.auth import AuthHandler + +# Set up logging +logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') + +# Mock the login method to avoid actual authentication +def mock_login(self) -> bool: + self.auth_token = "mock_token" + self.user_id = "12345" + return True + +# Test the booking window functionality +def test_booking_window(): + """Test the booking window functionality.""" + # Create a booker instance + booker = CrossFitBooker() + + # Replace the login method with our mock + original_login = AuthHandler.login + AuthHandler.login = mock_login + + # Set up timezone and target time + tz = pytz.timezone("Europe/Paris") + current_time = datetime.now(tz) + + # Get the target time from the environment variable or use default + target_time_str = os.environ.get("TARGET_RESERVATION_TIME", "20:01") + target_hour, target_minute = map(int, target_time_str.split(":")) + target_time = current_time.replace(hour=target_hour, minute=target_minute, second=0, microsecond=0) + + # Calculate booking window end + booking_window_end = target_time + timedelta(minutes=10) + + # Display current time and booking window + logging.info(f"Current time: {current_time}") + logging.info(f"Target booking time: {target_time}") + logging.info(f"Booking window end: {booking_window_end}") + + # Check if we're in the booking window + if target_time <= current_time <= booking_window_end: + logging.info("We are within the booking window!") + else: + logging.info("We are outside the booking window.") + time_diff = (target_time - current_time).total_seconds() + logging.info(f"Next booking window starts in: {time_diff//60} minutes and {time_diff%60:.0f} seconds") + + # Restore the original login method + AuthHandler.login = original_login + +if __name__ == "__main__": + test_booking_window() \ No newline at end of file