2018-06-13 17:18:12 +02:00
|
|
|
import os
|
2021-01-02 19:28:03 +01:00
|
|
|
from typing import Dict, Optional
|
2018-06-13 17:18:12 +02:00
|
|
|
|
|
|
|
import forecastio
|
|
|
|
import pytz
|
2020-09-16 15:41:02 +02:00
|
|
|
from fittrackee import appLog
|
2021-01-02 19:28:03 +01:00
|
|
|
from gpxpy.gpx import GPXRoutePoint
|
2018-06-13 17:18:12 +02:00
|
|
|
|
2020-09-19 13:56:14 +02:00
|
|
|
API_KEY = os.getenv('WEATHER_API_KEY')
|
2018-06-13 17:18:12 +02:00
|
|
|
|
|
|
|
|
2021-01-02 19:28:03 +01:00
|
|
|
def get_weather(point: GPXRoutePoint) -> Optional[Dict]:
|
2018-06-13 17:18:12 +02:00
|
|
|
if not API_KEY or API_KEY == '':
|
|
|
|
return None
|
|
|
|
try:
|
|
|
|
point_time = pytz.utc.localize(point.time)
|
|
|
|
forecast = forecastio.load_forecast(
|
|
|
|
API_KEY,
|
|
|
|
point.latitude,
|
|
|
|
point.longitude,
|
|
|
|
time=point_time,
|
2019-08-28 13:25:39 +02:00
|
|
|
units='si',
|
2018-06-13 17:18:12 +02:00
|
|
|
)
|
|
|
|
weather = forecast.currently()
|
|
|
|
return {
|
|
|
|
'summary': weather.summary,
|
|
|
|
'icon': weather.icon,
|
|
|
|
'temperature': weather.temperature,
|
|
|
|
'humidity': weather.humidity,
|
|
|
|
'wind': weather.windSpeed,
|
|
|
|
}
|
|
|
|
except Exception as e:
|
|
|
|
appLog.error(e)
|
|
|
|
return None
|