diff --git a/book_crossfit.py b/book_crossfit.py index 2ba345b..b93b376 100755 --- a/book_crossfit.py +++ b/book_crossfit.py @@ -15,7 +15,7 @@ from dateutil.parser import parse import pytz from dotenv import load_dotenv from urllib.parse import urlencode -from typing import List, Dict, Optional +from typing import List, Dict, Optional, Any load_dotenv() @@ -474,23 +474,34 @@ class CrossFitBooker: logging.error("Authentication failed - exiting program") return - while True: - try: - current_time: datetime = datetime.now(tz) - logging.info(f"Current time: {current_time}") - - # Run booking cycle at the target time or if it's a test, with optimized checking - if current_time.strftime("%H:%M") == TARGET_RESERVATION_TIME: - self.run_booking_cycle(current_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 + try: + while True: + try: + current_time: datetime = datetime.now(tz) + logging.info(f"Current time: {current_time}") + + # Run booking cycle at the target time or if it's a test, with optimized checking + if current_time.strftime("%H:%M") == TARGET_RESERVATION_TIME: + self.run_booking_cycle(current_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: + """ + Clean up resources and exit the script. + """ + logging.info("Script interrupted by user. Quitting...") + # Add any cleanup code here + exit(0) if __name__ == "__main__": @@ -501,3 +512,5 @@ if __name__ == "__main__": # Start continuous booking loop booker.run() + logging.info("Script completed") +