FitTrackee/fittrackee/workouts/utils/weather.py

41 lines
1.0 KiB
Python
Raw Normal View History

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