From 26bdceb22e564e2b4580fb22b8a705537d4bc68a Mon Sep 17 00:00:00 2001 From: Max G Date: Sat, 9 Mar 2019 15:46:42 +0100 Subject: [PATCH] Removed dead code, changed WeatherInterface and implemented forecasts into Owm weather, but did not test it --- Calendar/E-Paper.py | 4 ++-- Calendar/OwmForecasts.py | 24 +++++++++++++++++++----- Calendar/WeatherHeaderDesign.py | 4 ++-- Calendar/WeatherInterface.py | 4 ++-- 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/Calendar/E-Paper.py b/Calendar/E-Paper.py index 5f41e10..affb7c8 100644 --- a/Calendar/E-Paper.py +++ b/Calendar/E-Paper.py @@ -66,8 +66,8 @@ def main (): raise ImportError("choosen_design must be valid (" + choosen_design + ")") debug.print_line("Fetching weather information from open weather map") - owm = OwmForecasts.OwmForecasts(api_key) - design.add_weather(OwmForecasts.OwmForecasts(api_key)) + owm = OwmForecasts.OwmForecasts(location, api_key) + design.add_weather(owm) debug.print_line('Fetching events from your calendar') events_cal = IcalEvents.IcalEvents(ical_urls) diff --git a/Calendar/OwmForecasts.py b/Calendar/OwmForecasts.py index 71ad866..72e0602 100644 --- a/Calendar/OwmForecasts.py +++ b/Calendar/OwmForecasts.py @@ -6,9 +6,11 @@ from settings import units class OwmForecasts (WeatherInterface): """Fetches weather through the Openweathermap-api.""" - def __init__ (self, api_key): - self.api = pyowm.OWM(api_key) + def __init__ (self, location, api_key, paid_api=False): + subscription = "pro" if paid_api else None + self.api = pyowm.OWM(api_key, subscription_type=subscription) self.units = units + self.location = location def is_available (self): try: @@ -16,17 +18,29 @@ class OwmForecasts (WeatherInterface): except: return False - def get_today_forecast (self, location): + def get_today_forecast (self, location=None): if self.is_available() is False: return None + + location = self.location if location is None else location observation = self.api.weather_at_place(location) weather = observation.get_weather() return self.__get_forecast_from_weather__(weather, location=location) - def get_forecast_in_days (self, offset_by_days, location): - return None + def get_forecast_in_days (self, offset_by_days, location=None): + if self.is_available() is False: + return None + + 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) + except: # only allowed for paied membership + return None def __get_forecast_from_weather__ (self, weather, location): forecast_object = WeatherForecast() diff --git a/Calendar/WeatherHeaderDesign.py b/Calendar/WeatherHeaderDesign.py index 6ac82f0..6e3a10f 100644 --- a/Calendar/WeatherHeaderDesign.py +++ b/Calendar/WeatherHeaderDesign.py @@ -1,7 +1,7 @@ from DesignEntity import DesignEntity from Assets import * from TextDesign import TextDesign -from settings import units, hours, location +from settings import units, hours wiconplace = (0, 0) tempplace = (0.779, 0) @@ -22,7 +22,7 @@ class WeatherHeaderDesign (DesignEntity): self.__render_missing_connection__() return - cur_weather = self.__weather__.get_today_forecast(location) + cur_weather = self.__weather__.get_today_forecast() temperature = cur_weather.air_temperature + " " + self.__get_unit__(("°C", "°F")) windspeed = cur_weather.wind_speed + " " + self.__get_unit__(("km/h", "mph")) diff --git a/Calendar/WeatherInterface.py b/Calendar/WeatherInterface.py index 3140746..bd3fc6e 100644 --- a/Calendar/WeatherInterface.py +++ b/Calendar/WeatherInterface.py @@ -2,8 +2,8 @@ from DataSourceInterface import DataSourceInterface class WeatherInterface (DataSourceInterface): """Interface for fetching and processing weather forecast information.""" - def get_forecast_in_days (self, offset_by_days, location): + def get_forecast_in_days (self, offset_by_days, location=None): raise NotImplementedError("Functions needs to be implemented") - def get_today_forecast (self, location): + def get_today_forecast (self, location=None): raise NotImplementedError("Functions needs to be implemented") \ No newline at end of file