diff --git a/Calendar/CalendarInterface.py b/Calendar/CalendarInterface.py index 64e0c44..cc9fef2 100644 --- a/Calendar/CalendarInterface.py +++ b/Calendar/CalendarInterface.py @@ -7,6 +7,12 @@ import calendar class CalendarInterface (DataSourceInterface): """Interface for fetching and processing calendar event information.""" def __init__ (self): + self.events = [] + self.reload() + + def reload (self): + if self.is_available() == False: + return self.events = self.__get_events__() self.events = self.__sort_events__(self.events) diff --git a/Calendar/DataSourceInterface.py b/Calendar/DataSourceInterface.py index c7d7ecd..4c5004c 100644 --- a/Calendar/DataSourceInterface.py +++ b/Calendar/DataSourceInterface.py @@ -1,4 +1,7 @@ class DataSourceInterface (object): """Interface for child interfaces that fetch data.""" def is_available (self): + raise NotImplementedError("Functions needs to be implemented") + + def reload (self): raise NotImplementedError("Functions needs to be implemented") \ No newline at end of file diff --git a/Calendar/E-Paper.py b/Calendar/E-Paper.py index 1f04cc6..a99a55a 100644 --- a/Calendar/E-Paper.py +++ b/Calendar/E-Paper.py @@ -53,6 +53,8 @@ loop_timer = LoopTimer(update_interval, run_on_hour=True) """Main loop starts from here""" def main(): owm = OwmForecasts.OwmForecasts(location, api_key, paid_api=owm_paid_subscription) + events_cal = IcalEvents.IcalEvents(ical_urls, highlighted_ical_urls) + rss = RssParserPosts.RssParserPosts(rss_feeds) while True: loop_timer.begin_loop() @@ -69,14 +71,15 @@ def main(): raise ImportError("choosen_design must be valid (" + choosen_design + ")") debug.print_line("Fetching weather information from open weather map") + owm.reload() design.add_weather(owm) debug.print_line('Fetching events from your calendar') - events_cal = IcalEvents.IcalEvents(ical_urls, highlighted_ical_urls) + events_cal.reload() design.add_calendar(events_cal) debug.print_line('Fetching posts from your rss-feeds') - rss = RssParserPosts.RssParserPosts(rss_feeds) + rss.reload() design.add_rssfeed(rss) debug.print_line("\nStarting to render") diff --git a/Calendar/IcalEvents.py b/Calendar/IcalEvents.py index 5d0dc5e..22d6ccd 100644 --- a/Calendar/IcalEvents.py +++ b/Calendar/IcalEvents.py @@ -13,6 +13,13 @@ class IcalEvents(CalendarInterface): self.highlighted_urls = highlighted_urls super(IcalEvents, self).__init__() + def is_available(self): + try: + urlopen("https://google.com", timeout=2) + return True + except: + return False + def __get_events__(self): events = self.__get_events_from_urls__(self.urls) diff --git a/Calendar/OwmForecasts.py b/Calendar/OwmForecasts.py index 2014bf2..fc68143 100644 --- a/Calendar/OwmForecasts.py +++ b/Calendar/OwmForecasts.py @@ -7,16 +7,20 @@ from settings import units class OwmForecasts (WeatherInterface): """Fetches weather through the Openweathermap-api.""" 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.subscription = "pro" if paid_api else None + self.api_key = api_key self.units = units self.location = location + self.api = pyowm.OWM(self.api_key, subscription_type=self.subscription) def is_available (self): try: return self.api.is_API_online() except: return False + + def reload(self): + pass def get_today_forecast (self, location=None): if self.is_available() is False: diff --git a/Calendar/RssInterface.py b/Calendar/RssInterface.py index 031d092..d65ea60 100644 --- a/Calendar/RssInterface.py +++ b/Calendar/RssInterface.py @@ -4,6 +4,12 @@ from datetime import datetime, timezone, timedelta class RssInterface(DataSourceInterface): """Interface for fetching and processing rss post information.""" def __init__(self): + self.loaded_posts = [] + self.reload() + + def reload(self): + if self.is_available() == False: + return self.loaded_posts = self.__get_posts__() self.__sort_posts__() diff --git a/Calendar/RssParserPosts.py b/Calendar/RssParserPosts.py index d0bdd01..3b0dfda 100644 --- a/Calendar/RssParserPosts.py +++ b/Calendar/RssParserPosts.py @@ -2,6 +2,7 @@ from RssInterface import RssInterface from datetime import datetime, timedelta, date import feedparser import RssPost +from urllib.request import urlopen max_range_days = 14 @@ -11,6 +12,13 @@ class RssParserPosts (RssInterface): self.urls = urls super(RssParserPosts, self).__init__() + def is_available(self): + try: + urlopen("https://google.com", timeout=2) + return True + except: + return False + def __get_posts__(self): posts = []