-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweatherapi.py
76 lines (61 loc) · 2.74 KB
/
weatherapi.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import openmeteo_requests
import requests_cache
import pandas as pd
from retry_requests import retry
class OpenMeteoApi:
def __init__(self) -> None:
self.get_openweather_response()
def setup_api_client(self) -> openmeteo_requests.Client:
cache_session = requests_cache.CachedSession('.cache', expire_after = 3600)
retry_session = retry(cache_session, retries = 5, backoff_factor = 0.2)
self.openmeteo = openmeteo_requests.Client(session = retry_session)
def get_openweather_response(self):
self.setup_api_client()
url = "https://api.open-meteo.com/v1/forecast"
params = {
"latitude": 47.1149,
"longitude": 8.3901,
"current": ["temperature_2m", "relative_humidity_2m", "rain", "surface_pressure", "wind_speed_10m", "wind_direction_10m"],
"daily": ["temperature_2m_max", "temperature_2m_min", "daylight_duration", "uv_index_max"],
"timezone": "auto",
"past_days": 5
}
self.response = self.openmeteo.weather_api(url, params=params)[0]
self.current = self.response.Current()
self.daily = self.response.Daily()
def daily_forecast(self) -> pd.DataFrame:
daily_temperature_2m_max = self.daily.Variables(0).ValuesAsNumpy()
daily_temperature_2m_min = self.daily.Variables(1).ValuesAsNumpy()
daily_daylight_duration = self.daily.Variables(2).ValuesAsNumpy()
daily_uv_index_max = self.daily.Variables(3).ValuesAsNumpy()
daily_data = {"date": pd.date_range(
start = pd.to_datetime(self.daily.Time(), unit = "s", utc = True),
end = pd.to_datetime(self.daily.TimeEnd(), unit = "s", utc = True),
freq = pd.Timedelta(seconds = self.daily.Interval()),
inclusive = "left"
)}
daily_data["temperature_2m_max"] = daily_temperature_2m_max
daily_data["temperature_2m_min"] = daily_temperature_2m_min
daily_data["daylight_duration"] = daily_daylight_duration
daily_data["uv_index_max"] = daily_uv_index_max
return pd.DataFrame(data = daily_data)
def refresh(self) -> None:
self.get_openweather_response()
@property
def temperature(self) -> float:
return self.current.Variables(0).Value()
@property
def humidity(self) -> float:
return self.current.Variables(1).Value()
@property
def rain(self) -> float:
return self.current.Variables(2).Value()
@property
def pressure(self) -> float:
return self.current.Variables(3).Value()
@property
def pressure(self) -> float:
return self.current.Variables(4).Value()
@property
def pressure(self) -> float:
return self.current.Variables(5).Value()