From 7df7a3816a6e9afbc536afea0ad310ef5b9ce1b1 Mon Sep 17 00:00:00 2001 From: kbe Date: Thu, 17 Jul 2025 16:56:25 +0200 Subject: [PATCH] improved version from Deepseek --- book_crossfit.py | 90 ++++++++++++++++++++++++------------------------ 1 file changed, 45 insertions(+), 45 deletions(-) diff --git a/book_crossfit.py b/book_crossfit.py index 17c04ee..8b64f24 100755 --- a/book_crossfit.py +++ b/book_crossfit.py @@ -32,40 +32,54 @@ class CrossFitBooker: self.auth_token = None self.user_id = None self.session = requests.Session() - self.session.headers.update({ + self.base_headers = { "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:140.0) Gecko/20100101 Firefox/140.0", "Accept": "application/json, text/plain, */*", "Accept-Language": "en-GB,en;q=0.8,fr-FR;q=0.5,fr;q=0.3", "Nubapp-Origin": "user_apps", "Origin": "https://box.resawod.com", "Referer": "https://box.resawod.com/", - }) + } + self.session.headers.update(self.base_headers) + + # Define mandatory parameters for API calls + self.mandatory_params = { + "app_version": APP_VERSION, + "device_type": DEVICE_TYPE, + "id_application": APPLICATION_ID, + "id_category_activity": CATEGORY_ID + } + + def get_auth_headers(self) -> Dict: + """Return headers with authorization if available""" + headers = self.base_headers.copy() + if self.auth_token: + headers["Authorization"] = f"Bearer {self.auth_token}" + return headers def login(self) -> bool: """Authenticate and get the bearer token""" try: # First login endpoint + login_params = { + "app_version": APP_VERSION, + "device_type": DEVICE_TYPE, + "username": USERNAME, + "password": PASSWORD + } + response = self.session.post( "https://sport.nubapp.com/api/v4/users/checkUser.php", headers={"Content-Type": "application/x-www-form-urlencoded"}, - data=urlencode({ - "app_version": APP_VERSION, - "device_type": DEVICE_TYPE, - "username": USERNAME, - "password": PASSWORD - })) + data=urlencode(login_params)) 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", @@ -76,14 +90,10 @@ class CrossFitBooker: "password": PASSWORD })) - if response.ok: login_data = response.json() self.auth_token = login_data.get("token") - # Uncomment to see Bearer token - # print(self.auth_token) - if self.auth_token and self.user_id: print("Successfully logged in") return True @@ -102,34 +112,25 @@ class CrossFitBooker: 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, + # Prepare request with mandatory parameters + request_data = self.mandatory_params.copy() + request_data.update({ "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 - } + "end_timestamp": end_date.strftime("%d-%m-%Y") + }) try: # Debug output print("\n--- Request Details ---") print(f"URL: {url}") - print(f"Headers: {json.dumps(headers, indent=2)}") + print(f"Headers: {json.dumps(self.get_auth_headers(), indent=2)}") print(f"Payload: {request_data}") # Make the request response = self.session.post( url, - headers=headers, + headers=self.get_auth_headers(), data=urlencode(request_data), timeout=10 ) @@ -155,11 +156,8 @@ class CrossFitBooker: 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}") + 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") @@ -182,15 +180,17 @@ class CrossFitBooker: return False try: + # Prepare request with mandatory parameters + request_data = self.mandatory_params.copy() + request_data.update({ + "id_user": self.user_id, + "id_activity": session_id + }) + response = self.session.post( "https://sport.nubapp.com/api/v4/activities/bookActivity.php", - headers={"Authorization": f"Bearer {self.auth_token}"}, - data=urlencode({ - "app_version": APP_VERSION, - "id_application": APPLICATION_ID, - "id_user": self.user_id, - "id_activity": session_id - })) + headers=self.get_auth_headers(), + data=urlencode(request_data)) if response.ok: print(f"Successfully booked session {session_id}") @@ -319,5 +319,5 @@ if __name__ == "__main__": 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_booking_cycle(datetime.now()) # booker.run() \ No newline at end of file