-
Notifications
You must be signed in to change notification settings - Fork 2
/
weather_api_hist.py
47 lines (36 loc) · 1.24 KB
/
weather_api_hist.py
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
37
38
39
40
41
42
43
44
45
"""
Script to test the Weather API
Contains the function used in the main script to stream HISTORICAL weather data
NB: the API free tier allows to get historical data only for the PREVIOUS 7 days
"""
import requests
import streamlit as st
from datetime import datetime, timedelta
# define the API endpoint
API_URL = "https://weatherapi-com.p.rapidapi.com"
archive = "/history.json"
# headers
headers = {
"X-RapidAPI-Key": "6a425b2a7bmshc1f059e65b98fb7p1cdd78jsn184965114ca2",
"X-RapidAPI-Host": "weatherapi-com.p.rapidapi.com"
}
# fun to make request
@st.cache
def get_historical_weather_data(coords):
# get current time
date = datetime.now().strftime("%Y-%m-%d")
date_from = (datetime.now() - timedelta(days=7)).strftime("%Y-%m-%d")
# create params dict
querystring = {"q": coords, "dt": date_from, "end_dt": date, "lang": "en"}
# make request
response = requests.get(API_URL + archive, headers=headers, params=querystring)
# store response
data = response.json()
return data
# Example usage:
if __name__ == "__main__":
######################################################################################
# test the API
coords = '53.1,-0.13'
data = get_historical_weather_data(coords)
print(data)