[text] H

Viewer

  1. import requests
  2. import json
  3. import os
  4.  
  5. # Authentication URL for the Tesco Portal API
  6. auth_url = "https://toolkit.tesco.com/partner/reports/api/v1/authenticate"
  7.  
  8. # URL to fetch the JSON response containing report details
  9. url = "https://toolkit.tesco.com/partner/reports/api/v1/generated-reports-v2"
  10.  
  11. # Username and password for authentication
  12. username = "your_username"
  13. password = "your_password"
  14.  
  15. # Authentication request data
  16. data = {
  17.     "username": username,
  18.     "password": password
  19. }
  20.  
  21. # Authenticate and get the session token (UUID)
  22. response_auth = requests.post(auth_url, json=data)
  23.  
  24. # Check if authentication was successful
  25. if response_auth.status_code == 200:
  26.     print("Authentication successful!")
  27.  
  28.     # Extract the UUID from the response
  29.     uuid = response_auth.json().get("uuid")
  30.  
  31.     # Set up headers for fetching report URL JSON with UUID
  32.     headers_reports = {
  33.         "Authorization": f"Bearer {uuid}"
  34.     }
  35.  
  36.     # Payload for fetching report details JSON
  37.     payload = {
  38.         "pageNumber": 0,
  39.         "pageSize": 10
  40.     }
  41.  
  42.     # Fetch JSON response containing report details using POST request
  43.     response_reports = requests.post(url, headers=headers_reports, json=payload)
  44.  
  45.     # Check if fetching JSON response was successful
  46.     if response_reports.status_code == 200:
  47.         # Get the latest report from the JSON response
  48.         latest_report = max(response_reports.json()["reports"], key=lambda x: x["createdOn"])
  49.  
  50.         # Extract filename and ID of the latest report
  51.         report_name = latest_report["reportName"]
  52.         report_id = latest_report["id"]
  53.  
  54.         # Download the latest report
  55.         download_url = f"https://toolkit.tesco.com/partner/reports/api/v1/download-report?reportId={report_id}"
  56.         download_response = requests.get(download_url, headers=headers_reports)
  57.  
  58.         # Check if download was successful
  59.         if download_response.status_code == 200:
  60.             # Save the file to DBFS with the same name
  61.             with open(f"/dbfs/{report_name}", "wb") as file:
  62.                 file.write(download_response.content)
  63.             print(f"File '{report_name}' downloaded and saved in DBFS.")
  64.         else:
  65.             print("Failed to download the file.")
  66.     else:
  67.         print("Failed to fetch report details JSON response.")
  68. else:
  69.     print("Authentication failed. Please check your credentials.")

Editor

You can edit this paste and save as new:


File Description
  • H
  • Paste Code
  • 29 Apr-2024
  • 2.38 Kb
You can Share it: