import requests import datetime def fetch_data( lookback, span = 24, offset = 3, # data from the api is only upto 3 hours before now url = "https://visual-crossing-weather.p.rapidapi.com/history", headers = { "X-RapidAPI-Key": "12c5552beamshb548b266489323cp1c7cb2jsnf8b18027cebc", "X-RapidAPI-Host": "visual-crossing-weather.p.rapidapi.com" } ): # Calculate the start and end date and time for the last 24 hours end_datetime = datetime.datetime.now() - datetime.timedelta(hours=lookback) start_datetime = end_datetime - datetime.timedelta(hours=(span+offset)) querystring = { "startDateTime": start_datetime.strftime("%Y-%m-%dT%H:%M:%S"), "aggregateHours": "1", "location": "Kolkata", "endDateTime": end_datetime.strftime("%Y-%m-%dT%H:%M:%S"), "unitGroup": "us", "dayStartTime": "00:00:00", "contentType": "csv", "dayEndTime": "23:59:00", "shortColumnNames": "0" } response = requests.get(url, headers=headers, params=querystring) if response.status_code == 200: # Parse the CSV response return response else: print("Error:", response.status_code)