40 lines
1.6 KiB
Python

import openmeteo_requests
import requests_cache
from retry_requests import retry
class OpenMeteoClient:
def __init__(self, timezone="GMT+0"):
self.timezone = timezone
# Set up the Open-Meteo API client with cache and retry on error
self.cache_session = requests_cache.CachedSession('.cache', expire_after=3600)
self.retry_session = retry(self.cache_session, retries=5, backoff_factor=0.2)
self.openmeteo = openmeteo_requests.Client(session=self.retry_session)
def fetch_weather_data(self, latitude, longitude, start_date, end_date):
url = "https://api.open-meteo.com/v1/forecast"
params = {
"latitude": latitude,
"longitude": longitude,
"daily": ["wind_speed_10m_max", "wind_direction_10m_dominant"],
"timezone": self.timezone,
"start_date": start_date,
"end_date": end_date
}
responses = self.openmeteo.weather_api(url, params=params)
return responses
def process_response(self, response):
daily = response.Daily()
daily_wind_speed_10m = daily.Variables(0).ValuesAsNumpy()
daily_wind_direction_10m = daily.Variables(1).ValuesAsNumpy()
return daily_wind_speed_10m.tolist(), daily_wind_direction_10m.tolist()
def get_weather_info(self, start_date, end_date, latitude=54.35119762746125, longitude=48.389356992149345):
responses = self.fetch_weather_data(latitude, longitude, start_date, end_date)
response = responses[0]
return self.process_response(response)