[text] J

Viewer

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

Editor

You can edit this paste and save as new:


File Description
  • J
  • Paste Code
  • 29 Apr-2024
  • 1.83 Kb
You can Share it: