2019-02-23 19:36:20 +01:00
|
|
|
from WeatherForecast import WeatherForecast
|
|
|
|
from WeatherInterface import WeatherInterface
|
|
|
|
import pyowm
|
|
|
|
from datetime import datetime
|
2019-05-06 16:09:14 +02:00
|
|
|
from settings import units, language
|
2019-05-09 17:41:34 +02:00
|
|
|
from Translator import translate
|
2019-02-23 19:36:20 +01:00
|
|
|
|
|
|
|
class OwmForecasts (WeatherInterface):
|
|
|
|
"""Fetches weather through the Openweathermap-api."""
|
2019-03-09 15:46:42 +01:00
|
|
|
def __init__ (self, location, api_key, paid_api=False):
|
2019-04-18 08:30:08 +02:00
|
|
|
self.subscription = "pro" if paid_api else None
|
|
|
|
self.api_key = api_key
|
2019-02-23 19:36:20 +01:00
|
|
|
self.units = units
|
2019-03-09 15:46:42 +01:00
|
|
|
self.location = location
|
2019-05-06 16:09:14 +02:00
|
|
|
self.api = pyowm.OWM(self.api_key, subscription_type=self.subscription, language=language)
|
2019-02-23 19:36:20 +01:00
|
|
|
|
|
|
|
def is_available (self):
|
2019-02-26 22:08:00 +01:00
|
|
|
try:
|
|
|
|
return self.api.is_API_online()
|
|
|
|
except:
|
|
|
|
return False
|
2019-05-15 06:15:38 +02:00
|
|
|
|
2019-04-18 08:30:08 +02:00
|
|
|
def reload(self):
|
|
|
|
pass
|
2019-02-23 19:36:20 +01:00
|
|
|
|
2019-03-09 15:46:42 +01:00
|
|
|
def get_today_forecast (self, location=None):
|
2019-02-23 19:36:20 +01:00
|
|
|
if self.is_available() is False:
|
|
|
|
return None
|
2019-05-15 06:15:38 +02:00
|
|
|
|
2019-04-14 10:42:14 +02:00
|
|
|
try:
|
|
|
|
location = self.location if location is None else location
|
2019-02-23 19:36:20 +01:00
|
|
|
|
2019-04-14 10:42:14 +02:00
|
|
|
observation = self.api.weather_at_place(location)
|
|
|
|
weather = observation.get_weather()
|
2019-02-23 19:36:20 +01:00
|
|
|
|
2019-04-14 10:42:14 +02:00
|
|
|
return self.__get_forecast_from_weather__(weather, location=location)
|
|
|
|
except:
|
|
|
|
return None
|
2019-02-23 19:36:20 +01:00
|
|
|
|
2019-03-09 15:46:42 +01:00
|
|
|
def get_forecast_in_days (self, offset_by_days, location=None):
|
2019-03-09 15:48:31 +01:00
|
|
|
if offset_by_days is 0:
|
|
|
|
return self.get_today_forecast(location)
|
2019-05-15 06:15:38 +02:00
|
|
|
|
2019-03-09 15:46:42 +01:00
|
|
|
if self.is_available() is False:
|
|
|
|
return None
|
2019-05-15 06:15:38 +02:00
|
|
|
|
2019-03-09 15:46:42 +01:00
|
|
|
location = self.location if location is None else location
|
|
|
|
try:
|
|
|
|
forecast = self.api.daily_forecast(location, limit=offset_by_days)
|
|
|
|
target_weather = forecast.get_forecast().get_weathers()[-1]
|
|
|
|
|
|
|
|
return self.__get_forecast_from_weather__(target_weather, location=location)
|
2019-04-14 10:42:14 +02:00
|
|
|
except: # only allowed for paid membership
|
2019-03-09 15:46:42 +01:00
|
|
|
return None
|
2019-02-23 19:36:20 +01:00
|
|
|
|
|
|
|
def __get_forecast_from_weather__ (self, weather, location):
|
|
|
|
forecast_object = WeatherForecast()
|
|
|
|
forecast_object.units = self.units
|
2019-03-09 17:12:25 +01:00
|
|
|
forecast_object.fetch_datetime = datetime.now()
|
2019-02-23 19:36:20 +01:00
|
|
|
forecast_object.location = location
|
2019-03-09 17:12:25 +01:00
|
|
|
forecast_object.datetime = weather.get_reference_time(timeformat='date')
|
2019-02-23 19:36:20 +01:00
|
|
|
|
|
|
|
forecast_object.icon = weather.get_weather_icon_name()
|
|
|
|
forecast_object.air_humidity = str(weather.get_humidity())
|
|
|
|
forecast_object.clouds = str(weather.get_clouds())
|
2019-05-09 17:41:34 +02:00
|
|
|
forecast_object.short_description = translate(str(weather.get_status()))
|
2019-02-23 19:36:20 +01:00
|
|
|
forecast_object.detailed_description = str(weather.get_detailed_status())
|
|
|
|
forecast_object.air_pressure = str(weather.get_pressure()['press'])
|
2019-05-15 06:15:38 +02:00
|
|
|
forecast_object.wind_deg = str(int(weather.get_wind()['deg']))
|
2019-02-23 19:36:20 +01:00
|
|
|
|
|
|
|
if forecast_object.units == "metric":
|
|
|
|
forecast_object.air_temperature = str(int(weather.get_temperature(unit='celsius')['temp']))
|
2019-05-15 06:15:38 +02:00
|
|
|
forecast_object.wind_speed = str(int(weather.get_wind()['speed'])) #kmh
|
|
|
|
|
|
|
|
if forecast_object.units == "aviation":
|
|
|
|
forecast_object.air_temperature = str(int(weather.get_temperature(unit='celsius')['temp']))
|
|
|
|
forecast_object.wind_speed = str(int(weather.get_wind()['speed'] * 1.94384)) #knots
|
2019-02-23 19:36:20 +01:00
|
|
|
|
|
|
|
if forecast_object.units == "imperial":
|
|
|
|
forecast_object.air_temperature = str(int(weather.get_temperature('fahrenheit')['temp']))
|
2019-05-15 06:15:38 +02:00
|
|
|
forecast_object.wind_speed = str(int(weather.get_wind()['speed'] * 0.621)) #mph
|
2019-02-23 19:36:20 +01:00
|
|
|
|
|
|
|
forecast_object.sunrise = datetime.fromtimestamp(int(weather.get_sunrise_time(timeformat='unix')))
|
|
|
|
forecast_object.sunset = datetime.fromtimestamp(int(weather.get_sunset_time(timeformat='unix')))
|
|
|
|
|
2019-05-15 06:15:38 +02:00
|
|
|
return forecast_object
|