File size: 1,291 Bytes
a3d05c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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)