[text] L

Viewer

  1. import requests
  2. import os
  3.  
  4. # URL for the main action
  5. url = 'https://toolkit.tesco.com/partner/reports/'
  6.  
  7. # Define options for each category/subcategory
  8. Country = ['UK', 'ROI']
  9. Report_type = ['Sales and stock', 'Range conformance', 'Promotional funding', 'Auto sales out retro deals', 'Fulfilment fee', 'Performance summary', 'Cost price amendments', 'Purchase order amendments']
  10. Product_subgroup = ['All product subgroups']
  11. Products = ['All products']
  12. View = ['TPNB – Total sales', 'TPNB – Sales x store', 'TPNB – Sales x store format', 'TPNB - Sales x DC','Product subgroup - Total sales','Stores - Total sales','Store Format - Total sales']
  13. Time_period = ['Last full week (Wk 08)']
  14.  
  15. # DBFS path to save the downloaded file
  16. dbfs_path = '/dbfs/mnt/s3/databricks-reports/'
  17.  
  18. # Iterate over combinations of options
  19. for country in Country:
  20.     for report_type in Report_type:
  21.         for product_subgroup in Product_subgroup:
  22.             for product in Products:
  23.                 for view in View:
  24.                     for time_period in Time_period:
  25.                         # Send request to select options
  26.                         options_data = {
  27.                             'Country': country,
  28.                             'Report type': report_type,
  29.                             'Product subgroup': product_subgroup,
  30.                             'Product': product,
  31.                             'View': view,
  32.                             'Time period': time_period
  33.                         }
  34.                         try:
  35.                             # Send POST request to select options
  36.                             response = requests.post(url, data=options_data)
  37.                             response.raise_for_status()  # Raise exception for 4xx or 5xx errors
  38.                             
  39.                             # Assuming the report is generated and downloaded automatically after selecting options
  40.                             # If not, you may need to add additional logic to download the report
  41.                             
  42.                             # Save the downloaded file to DBFS
  43.                             file_name = f"{country}_{report_type}_{product}_{view}_{time_period}.xlsx"
  44.                             with open(os.path.join(dbfs_path, file_name), 'wb') as f:
  45.                                 f.write(response.content)
  46.                             
  47.                             print(f"Report downloaded and saved to DBFS: {file_name}")
  48.                         except requests.exceptions.HTTPError as err:
  49.                             print(f"An error occurred: {err}")

Editor

You can edit this paste and save as new: