- import requests
- # Function to authenticate and obtain session token
- def authenticate(username, password):
- auth_url = "https://example.com/authenticate"
- data = {
- "username": username,
- "password": password
- }
- response = requests.post(auth_url, json=data)
- if response.status_code == 200:
- return response.json().get("session_token")
- else:
- return None
- # Function to download the file
- def download_file(url, filename, headers):
- with open(filename, 'wb') as f:
- response = requests.get(url, headers=headers)
- f.write(response.content)
- # URL to the endpoint that provides details of the file
- details_url = "https://toolkit.tesco.com/partner/reports/api/v1/generated-reports-v2?pageNumber=0&pageSize=18"
- # Assuming you have a username and password for authentication
- username = "your_username"
- password = "your_password"
- # Authenticate and obtain session token
- session_token = authenticate(username, password)
- # Check if authentication was successful
- if session_token:
- # Include session token in headers for subsequent requests
- headers = {
- "Authorization": f"Bearer {session_token}",
- "Content-Type": "application/json",
- "Accept": "application/json"
- }
- # Making a request to get the report details
- response = requests.get(details_url, headers=headers)
- # Check if the response status code is successful (2xx)
- if response.status_code == 200:
- # Assuming the response contains the 'reports' key
- if 'reports' in response.json():
- # Extracting the URLs of the files from the response JSON
- report_details = response.json()
- # Find the report with the latest createdOn timestamp
- latest_report = max(report_details['reports'], key=lambda x: x['createdOn'])
- # Extract the URL and filename of the CSV file with the latest createdOn timestamp
- csv_url = latest_report['reportName']
- csv_filename = latest_report['reportData']['reportName']
- # Download the CSV file
- download_file(csv_url, f"/dbfs/path/to/save/{csv_filename}", headers)
- print("Latest file saved to DBFS successfully!")
- else:
- print("Error: No 'reports' key found in the response.")
- else:
- print(f"Request failed with status code: {response.status_code}")
- else:
- print("Authentication failed. Please check your credentials.")
[text] G
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:
File Description
- G
- Paste Code
- 29 Apr-2024
- 2.47 Kb
You can Share it:
Latest Code Pastes