[text] G

Viewer

  1. import requests
  2.  
  3. # Function to authenticate and obtain session token
  4. def authenticate(username, password):
  5.     auth_url = "https://example.com/authenticate"
  6.     data = {
  7.         "username": username,
  8.         "password": password
  9.     }
  10.     response = requests.post(auth_url, json=data)
  11.     if response.status_code == 200:
  12.         return response.json().get("session_token")
  13.     else:
  14.         return None
  15.  
  16. # Function to download the file
  17. def download_file(url, filename, headers):
  18.     with open(filename, 'wb') as f:
  19.         response = requests.get(url, headers=headers)
  20.         f.write(response.content)
  21.  
  22. # URL to the endpoint that provides details of the file
  23. details_url = "https://toolkit.tesco.com/partner/reports/api/v1/generated-reports-v2?pageNumber=0&pageSize=18"
  24.  
  25. # Assuming you have a username and password for authentication
  26. username = "your_username"
  27. password = "your_password"
  28.  
  29. # Authenticate and obtain session token
  30. session_token = authenticate(username, password)
  31.  
  32. # Check if authentication was successful
  33. if session_token:
  34.     # Include session token in headers for subsequent requests
  35.     headers = {
  36.         "Authorization": f"Bearer {session_token}",
  37.         "Content-Type": "application/json",
  38.         "Accept": "application/json"
  39.     }
  40.  
  41.     # Making a request to get the report details
  42.     response = requests.get(details_url, headers=headers)
  43.  
  44.     # Check if the response status code is successful (2xx)
  45.     if response.status_code == 200:
  46.         # Assuming the response contains the 'reports' key
  47.         if 'reports' in response.json():
  48.             # Extracting the URLs of the files from the response JSON
  49.             report_details = response.json()
  50.  
  51.             # Find the report with the latest createdOn timestamp
  52.             latest_report = max(report_details['reports'], key=lambda x: x['createdOn'])
  53.  
  54.             # Extract the URL and filename of the CSV file with the latest createdOn timestamp
  55.             csv_url = latest_report['reportName']
  56.             csv_filename = latest_report['reportData']['reportName']
  57.  
  58.             # Download the CSV file
  59.             download_file(csv_url, f"/dbfs/path/to/save/{csv_filename}", headers)
  60.  
  61.             print("Latest file saved to DBFS successfully!")
  62.         else:
  63.             print("Error: No 'reports' key found in the response.")
  64.     else:
  65.         print(f"Request failed with status code: {response.status_code}")
  66. else:
  67.     print("Authentication failed. Please check your credentials.")

Editor

You can edit this paste and save as new:


File Description
  • G
  • Paste Code
  • 29 Apr-2024
  • 2.47 Kb
You can Share it: