feat(sessions): Retrieve all available courses from API
This commit is contained in:
117
book_crossfit.py
117
book_crossfit.py
@@ -1,4 +1,4 @@
|
||||
#!/bin/bash
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import requests
|
||||
import json
|
||||
@@ -54,11 +54,18 @@ class CrossFitBooker:
|
||||
"username": USERNAME,
|
||||
"password": PASSWORD
|
||||
}))
|
||||
|
||||
|
||||
if not response.ok:
|
||||
print(f"First login step failed: {response.status_code} - {response.text}")
|
||||
return False
|
||||
|
||||
login_data = response.json()
|
||||
# self.auth_token = login_data["data"]["token"]
|
||||
self.user_id = str(login_data["data"]["user"]["id_user"])
|
||||
|
||||
# Uncomment to see user_id
|
||||
# print(self.user_id)
|
||||
|
||||
# Second login endpoint
|
||||
response = self.session.post(
|
||||
"https://sport.nubapp.com/api/v4/login",
|
||||
@@ -72,9 +79,11 @@ class CrossFitBooker:
|
||||
|
||||
if response.ok:
|
||||
login_data = response.json()
|
||||
# print(login_data)
|
||||
self.auth_token = login_data.get("token")
|
||||
self.user_id = str(login_data.get("id_user"))
|
||||
|
||||
# Uncomment to see Bearer token
|
||||
# print(self.auth_token)
|
||||
|
||||
if self.auth_token and self.user_id:
|
||||
print("Successfully logged in")
|
||||
return True
|
||||
@@ -86,40 +95,86 @@ class CrossFitBooker:
|
||||
return False
|
||||
|
||||
def get_available_sessions(self, start_date: datetime, end_date: datetime) -> Optional[Dict]:
|
||||
"""Fetch available sessions from the API"""
|
||||
"""Fetch available sessions from the API with comprehensive error handling"""
|
||||
if not self.auth_token or not self.user_id:
|
||||
print("Not authenticated")
|
||||
print("Authentication required - missing token or user ID")
|
||||
return None
|
||||
|
||||
url = "https://sport.nubapp.com/api/v4/activities/getActivitiesCalendar.php"
|
||||
|
||||
# Prepare request components
|
||||
headers = {
|
||||
"Authorization": f"Bearer {self.auth_token}",
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:140.0) Gecko/20100101 Firefox/140.0",
|
||||
"Nubapp-Origin": "user_apps"
|
||||
}
|
||||
|
||||
request_data = {
|
||||
"app_version": APP_VERSION,
|
||||
"id_application": APPLICATION_ID,
|
||||
"id_user": self.user_id,
|
||||
"start_timestamp": start_date.strftime("%d-%m-%Y"),
|
||||
"end_timestamp": end_date.strftime("%d-%m-%Y"),
|
||||
"id_category_activity": CATEGORY_ID
|
||||
}
|
||||
|
||||
try:
|
||||
headers = {"Authorization": f"Bearer {self.auth_token}"}
|
||||
|
||||
request_data = urlencode({
|
||||
"app_version": APP_VERSION,
|
||||
"id_application": APPLICATION_ID,
|
||||
"id_user": self.user_id,
|
||||
"start_timestamp": start_date.strftime("%d-%m-%Y"),
|
||||
"end_timestamp": end_date.strftime("%d-%m-%Y"),
|
||||
"id_category_activity": CATEGORY_ID
|
||||
})
|
||||
|
||||
# Debug output
|
||||
print("\n--- Request Details ---")
|
||||
print(f"URL: {url}")
|
||||
print(f"Headers: {json.dumps(headers, indent=2)}")
|
||||
print(f"Payload: {request_data}")
|
||||
|
||||
# Make the request
|
||||
response = self.session.post(
|
||||
"https://sport.nubapp.com/api/v4/activities/getActivitiesCalendar.php",
|
||||
url,
|
||||
headers=headers,
|
||||
data=request_data
|
||||
)
|
||||
data=urlencode(request_data),
|
||||
timeout=10
|
||||
)
|
||||
|
||||
|
||||
print(headers)
|
||||
print(request_data)
|
||||
print(response.json())
|
||||
|
||||
return response.json() if response.ok else None
|
||||
# Debug raw response
|
||||
print("\n--- Response ---")
|
||||
print(f"Status Code: {response.status_code}")
|
||||
print(f"Headers: {response.headers}")
|
||||
print(f"Content: {response.text}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error getting sessions: {str(e)}")
|
||||
# Handle response
|
||||
if response.status_code == 200:
|
||||
try:
|
||||
json_response = response.json()
|
||||
if json_response.get("success", False):
|
||||
return json_response
|
||||
else:
|
||||
print(f"API reported failure: {json_response.get('message')}")
|
||||
return None
|
||||
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:")
|
||||
print(f"- app_version: {APP_VERSION}")
|
||||
print(f"- id_application: {APPLICATION_ID}")
|
||||
print(f"- id_user: {self.user_id}")
|
||||
print(f"- Date format (DD-MM-YYYY): {start_date.strftime('%d-%m-%Y')} to {end_date.strftime('%d-%m-%Y')}")
|
||||
print(f"- id_category_activity: {CATEGORY_ID}")
|
||||
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
|
||||
|
||||
def book_session(self, session_id: str) -> bool:
|
||||
"""Book a specific session"""
|
||||
if not self.auth_token or not self.user_id:
|
||||
@@ -263,6 +318,6 @@ 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())
|
||||
# print(sessions)
|
||||
booker.run_booking_cycle(datetime.now())
|
||||
# booker.run()
|
||||
Reference in New Issue
Block a user