[text] K

Viewer

  1. import requests
  2. import subprocess
  3.  
  4. # URLs for various actions
  5. select_options_url = 'https://toolkit.tesco.com/partner/reports/select_options'
  6. download_report_url = 'https://toolkit.tesco.com/partner/reports/download_report'
  7.  
  8. # Define options for each category/subcategory
  9. Country = ['UK', 'ROI']
  10. Report_type = ['Sales_and_stock', 'Range_conformance', 'Promotional_funding', 'Auto_sales_out_retro_deals', 'Fulfilment_fee', 'Performance_summary', 'Cost_price_amendments', 'Purchase_order_amendments']
  11. Product_subgroup = ['All_product_subgroups']
  12. Products = ['All_products']
  13. 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']
  14. time_periods = ['Last full week (Wk 08)']
  15.  
  16. # Iterate over combinations of options
  17. for country in Country:
  18.     for report_type in Report_type:
  19.         for product_subgroup in Product_subgroup:
  20.             for product in Products:
  21.                 for view in View:
  22.                     for time_period in time_periods:
  23.                         try:
  24.                             # Send request to select options
  25.                             options_data = {
  26.                                 'Country': country,
  27.                                 'Report_type': report_type,
  28.                                 'product_subgroup': product_subgroup,
  29.                                 'product': product,
  30.                                 'view': view,
  31.                                 'time_period': time_period
  32.                             }
  33.                             response_options = requests.post(select_options_url, data=options_data)
  34.                             response_options.raise_for_status()  # Raise an exception for non-200 status codes
  35.  
  36.                             # Simulate clicking "Download Report" button
  37.                             response_download = requests.get(download_report_url)
  38.                             response_download.raise_for_status()  # Raise an exception for non-200 status codes
  39.  
  40.                             # Check if download request was successful
  41.                             if response_download.status_code == 200:
  42.                                 # Save the downloaded report to a local file
  43.                                 with open('report.xlsx', 'wb') as f:
  44.                                     f.write(response_download.content)
  45.                                 print("Report downloaded successfully.")
  46.  
  47.                                 # Copy the local file to DBFS
  48.                                 subprocess.run(['databricks', 'fs', 'cp', 'report.xlsx', '/mnt/dbfs/report.xlsx'])
  49.                                 print("Report saved to DBFS successfully.")
  50.                             else:
  51.                                 print("Failed to download report. Status code:", response_download.status_code)
  52.  
  53.                         except requests.exceptions.RequestException as e:
  54.                             print(f"An error occurred: {e}")

Editor

You can edit this paste and save as new:


File Description
  • K
  • Paste Code
  • 25 Apr-2024
  • 3 Kb
You can Share it: