I don't remember
This commit is contained in:
118
book_crossfit.py
118
book_crossfit.py
@@ -117,6 +117,51 @@ class CrossFitBooker:
|
||||
"end_timestamp": end_date.strftime("%d-%m-%Y")
|
||||
})
|
||||
|
||||
# Debugging logs
|
||||
# print(f"Request Data: {request_data}")
|
||||
# print(f"Headers: {self.get_auth_headers()}")
|
||||
|
||||
try:
|
||||
# Make the request
|
||||
response = self.session.post(
|
||||
url,
|
||||
headers=self.get_auth_headers(),
|
||||
data=urlencode(request_data),
|
||||
timeout=10
|
||||
)
|
||||
|
||||
# Debug raw response
|
||||
# print(f"Response Status Code: {response.status_code}")
|
||||
# print(f"Response Content: {response.text}")
|
||||
|
||||
# Handle response
|
||||
if response.status_code == 200:
|
||||
try:
|
||||
json_response = response.json()
|
||||
return json_response
|
||||
except ValueError:
|
||||
print("Failed to decode JSON response")
|
||||
return None
|
||||
elif response.status_code == 400:
|
||||
print("400 Bad Request - likely missing or invalid parameters")
|
||||
print("Verify these parameters:")
|
||||
for param, value in request_data.items():
|
||||
print(f"- {param}: {value}")
|
||||
return None
|
||||
elif response.status_code == 401:
|
||||
print("401 Unauthorized - token may be expired or invalid")
|
||||
return None
|
||||
else:
|
||||
print(f"Unexpected status code: {response.status_code}")
|
||||
return None
|
||||
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"Request failed: {str(e)}")
|
||||
return None
|
||||
except Exception as e:
|
||||
print(f"Unexpected error: {str(e)}")
|
||||
return None
|
||||
|
||||
try:
|
||||
# Debug output
|
||||
# print("\n--- Request Details ---")
|
||||
@@ -196,24 +241,33 @@ class CrossFitBooker:
|
||||
return False
|
||||
|
||||
def is_session_bookable(self, session: Dict, current_time: datetime) -> bool:
|
||||
"""Check if a session is bookable based on user_info"""
|
||||
"""Check if a session is bookable based on user_info, ignoring error codes."""
|
||||
user_info = session.get("user_info", {})
|
||||
|
||||
# First check if can_join is true
|
||||
|
||||
# First check if can_join is true (primary condition)
|
||||
if user_info.get("can_join", False):
|
||||
return True
|
||||
|
||||
# Otherwise check booking availability time
|
||||
|
||||
# If can_join is False, check if there's a booking window
|
||||
booking_date_str = user_info.get("unableToBookUntilDate", "")
|
||||
booking_time_str = user_info.get("unableToBookUntilTime", "")
|
||||
|
||||
|
||||
if booking_date_str and booking_time_str:
|
||||
try:
|
||||
booking_datetime = datetime.strptime(f"{booking_date_str} {booking_time_str}", "%d-%m-%Y %H:%M")
|
||||
booking_datetime = datetime.strptime(
|
||||
f"{booking_date_str} {booking_time_str}",
|
||||
"%d-%m-%Y %H:%M"
|
||||
)
|
||||
booking_datetime = pytz.timezone(TIMEZONE).localize(booking_datetime)
|
||||
return current_time >= booking_datetime
|
||||
|
||||
if current_time >= booking_datetime:
|
||||
return True # Booking window is open
|
||||
else:
|
||||
return False # Still waiting for booking to open
|
||||
except ValueError:
|
||||
pass
|
||||
pass # Ignore invalid date formats
|
||||
|
||||
# Default case: not bookable
|
||||
return False
|
||||
|
||||
def matches_preferred_session(self, session: Dict, current_time: datetime) -> bool:
|
||||
@@ -309,8 +363,44 @@ class CrossFitBooker:
|
||||
|
||||
if __name__ == "__main__":
|
||||
booker = CrossFitBooker()
|
||||
booker.login()
|
||||
sessions = booker.get_available_sessions(datetime.strptime("21-07-2025", "%d-%m-%Y"), datetime.strptime("27-07-2025", "%d-%m-%Y"))
|
||||
# print(sessions)
|
||||
# booker.run_booking_cycle(datetime.now())
|
||||
# booker.run()
|
||||
if not booker.login():
|
||||
print("Failed to login")
|
||||
exit(1)
|
||||
|
||||
# Set timezone for current_time
|
||||
tz = pytz.timezone(TIMEZONE)
|
||||
current_time = datetime.now(tz)
|
||||
|
||||
# Get sessions for the next 7 days
|
||||
start_date = datetime.now()
|
||||
end_date = start_date + timedelta(days=3)
|
||||
|
||||
session_data = booker.get_available_sessions(start_date, end_date)
|
||||
if not session_data or not session_data.get("success", False):
|
||||
print("Failed to get session data")
|
||||
exit(1)
|
||||
|
||||
activities = session_data.get("data", {}).get("activities_calendar", [])
|
||||
|
||||
sessions_to_book = []
|
||||
for session in activities:
|
||||
# Assuming the string is stored in a variable named session_time_str
|
||||
session_time_str = "2025-07-19 20:01:08.858174+02:00"
|
||||
session_time = datetime.strptime(session_time_str, "%Y-%m-%d %H:%M:%S.%f%z")
|
||||
|
||||
# if booker.is_session_bookable(session, current_time):
|
||||
if booker.is_session_bookable(session, session_time):
|
||||
if booker.matches_preferred_session(session, current_time):
|
||||
sessions_to_book.append(("Preferred", session))
|
||||
elif current_time.strftime("%H:%M") == TARGET_RESERVATION_TIME:
|
||||
sessions_to_book.append(("Available", session))
|
||||
|
||||
print(f"\nFound {len(sessions_to_book)} sessions to book")
|
||||
|
||||
for session_type, session in sessions_to_book:
|
||||
session_time = datetime.strptime(session["start_datetime"], "%Y-%m-%d %H:%M:%S")
|
||||
print(f"Attempting to book {session_type} session: {session['name_activity']} at {session_time}")
|
||||
if booker.book_session(session["id_activity_calendar"]):
|
||||
print("Booking successful!")
|
||||
else:
|
||||
print("Booking failed")
|
||||
Reference in New Issue
Block a user