- import requests
- import json
- import os
- # Authentication URL for the Tesco Portal API
- auth_url = "https://toolkit.tesco.com/partner/reports/api/v1/authenticate"
- # URL to fetch the JSON response containing report details
- url = "https://toolkit.tesco.com/partner/reports/api/v1/generated-reports-v2"
- # Username and password for authentication
- username = "your_username"
- password = "your_password"
- # Authentication request data
- data = {
- "username": username,
- "password": password
- }
- # Authenticate and get the session token (UUID)
- response_auth = requests.post(auth_url, json=data)
- # Check if authentication was successful
- if response_auth.status_code == 200:
- print("Authentication successful!")
- # Extract the UUID from the response
- uuid = response_auth.json().get("uuid")
- # Set up headers for fetching report URL JSON with UUID
- headers_reports = {
- "Authorization": f"Bearer {uuid}"
- }
- # Payload for fetching report details JSON
- payload = {
- "pageNumber": 0,
- "pageSize": 10
- }
- # Fetch JSON response containing report details using POST request
- response_reports = requests.post(url, headers=headers_reports, json=payload)
- # Check if fetching JSON response was successful
- if response_reports.status_code == 200:
- # Get the latest report from the JSON response
- latest_report = max(response_reports.json()["reports"], key=lambda x: x["createdOn"])
- # Extract filename and ID of the latest report
- report_name = latest_report["reportName"]
- report_id = latest_report["id"]
- # Download the latest report
- download_url = f"https://toolkit.tesco.com/partner/reports/api/v1/download-report?reportId={report_id}"
- download_response = requests.get(download_url, headers=headers_reports)
- # Check if download was successful
- if download_response.status_code == 200:
- # Save the file to DBFS with the same name
- with open(f"/dbfs/{report_name}", "wb") as file:
- file.write(download_response.content)
- print(f"File '{report_name}' downloaded and saved in DBFS.")
- else:
- print("Failed to download the file.")
- else:
- # Print error message if fetching report details JSON fails
- print("Failed to fetch report details JSON response:", response_reports.text)
- else:
- print("Authentication failed. Please check your credentials.")
[text] A
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Editor
You can edit this paste and save as new: