feat: Better loggin information

This commit is contained in:
kbe
2025-07-18 15:53:58 +02:00
parent 8acde7b62e
commit f67b3ac36f
7 changed files with 42 additions and 15 deletions

3
.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
.env
log/
!log/.gitkeep

View File

@@ -1,9 +1,21 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import requests # Native modules
import json import json
import time import logging
from datetime import datetime, timedelta
import os import os
import sys
import time
# Third-party modules
import requests
from dateutil.parser import parse
import pytz
from dotenv import load_dotenv
from urllib.parse import urlencode
from typing import List, Dict, Optional
from datetime import datetime, timedelta from datetime import datetime, timedelta
# Parse session time (handles timezones if present) # Parse session time (handles timezones if present)
@@ -39,6 +51,17 @@ PREFERRED_SESSIONS = [
# (5, "15:15", "BIG WOD") # Saturday 15:15 BIG WOD # (5, "15:15", "BIG WOD") # Saturday 15:15 BIG WOD
] ]
# Configure logging once at script startup
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler("log/crossfit_booking.log"),
logging.StreamHandler()
]
)
class CrossFitBooker: class CrossFitBooker:
def __init__(self): def __init__(self):
self.auth_token = None self.auth_token = None
@@ -176,9 +199,10 @@ class CrossFitBooker:
def book_session(self, session_id: str) -> bool: def book_session(self, session_id: str) -> bool:
"""Book a specific session with debug logging.""" """Book a specific session with debug logging."""
print(f"\n[INFO] Attempting to book session_id: {session_id}")
logging.info(f"Attempting to book session_id: {session_id}")
if not self.auth_token or not self.user_id: if not self.auth_token or not self.user_id:
print("[ERROR] Not authenticated (missing auth_token or user_id)") logging.error("Not authenticated: missing auth_token or user_id")
return False return False
try: try:
@@ -206,27 +230,27 @@ class CrossFitBooker:
data=urlencode(request_data) data=urlencode(request_data)
) )
print(f"[DEBUG] Response status: {response.status_code}") logging.debug(f"Response status: {response.status_code}")
print(f"[DEBUG] Response text: {response.text}") logging.debug(f"API response: {response.text}")
if response.ok: if response.ok:
try: try:
json_response = response.json() json_response = response.json()
if json_response.get("success", False): if json_response.get("success", False):
print(f"[SUCCESS] Booked session {session_id}") logging.info(f"Successfully booked session {session_id}")
return True return True
else: else:
print(f"[ERROR] API returned success:false: {json_response}") logging.error(f"API returned success:false: {json_response}")
return False return False
except ValueError: except ValueError:
print("[ERROR] Invalid JSON response") logging.error("Invalid JSON response")
return False return False
else: else:
print(f"[ERROR] HTTP {response.status_code}: {response.text}") logging.error(f"HTTP {response.status_code}: {response.text}")
return False return False
except Exception as e: except Exception as e:
print(f"[CRITICAL] Unexpected error: {str(e)}", exc_info=True) logging.critical(f"Unexpected error: {str(e)}", exc_info=True)
return False return False
def is_session_bookable(self, session: Dict, current_time: datetime) -> bool: def is_session_bookable(self, session: Dict, current_time: datetime) -> bool:
@@ -280,7 +304,7 @@ class CrossFitBooker:
return False return False
except Exception as e: except Exception as e:
print(f"Error checking session: {str(e)}") logging.error(f"Failed to check session: {str(e)}")
return False return False
def run_booking_cycle(self, current_time: datetime): def run_booking_cycle(self, current_time: datetime):
@@ -330,7 +354,7 @@ class CrossFitBooker:
# Initial login # Initial login
if not self.login(): if not self.login():
print("Failed to login, exiting") logging.error("Authentication failed - exiting program")
return return
while True: while True:
@@ -362,7 +386,7 @@ if __name__ == "__main__":
session_data = booker.get_available_sessions(start_date, end_date) session_data = booker.get_available_sessions(start_date, end_date)
if not session_data or not session_data.get("success", False): if not session_data or not session_data.get("success", False):
print("Failed to get session data") logging.error("Failed to get session data")
exit(1) exit(1)
activities = session_data.get("data", {}).get("activities_calendar", []) activities = session_data.get("data", {}).get("activities_calendar", [])
@@ -378,7 +402,7 @@ if __name__ == "__main__":
bookable_sessions.append(session) bookable_sessions.append(session)
# print(f"Bookable sessions: {json.dumps(bookable_sessions, indent=2)}") # print(f"Bookable sessions: {json.dumps(bookable_sessions, indent=2)}")
print(f"\nFound {len(bookable_sessions)} sessions to book") print(f"Found {len(bookable_sessions)} sessions to book")
for session in bookable_sessions: for session in bookable_sessions:

0
log/.gitkeep Normal file
View File