Added offline support for services

This commit is contained in:
Maximilian Giller 2019-04-18 08:30:08 +02:00
parent b6691de9e8
commit d2c7428c62
7 changed files with 41 additions and 4 deletions

View file

@ -7,6 +7,12 @@ import calendar
class CalendarInterface (DataSourceInterface): class CalendarInterface (DataSourceInterface):
"""Interface for fetching and processing calendar event information.""" """Interface for fetching and processing calendar event information."""
def __init__ (self): def __init__ (self):
self.events = []
self.reload()
def reload (self):
if self.is_available() == False:
return
self.events = self.__get_events__() self.events = self.__get_events__()
self.events = self.__sort_events__(self.events) self.events = self.__sort_events__(self.events)

View file

@ -1,4 +1,7 @@
class DataSourceInterface (object): class DataSourceInterface (object):
"""Interface for child interfaces that fetch data.""" """Interface for child interfaces that fetch data."""
def is_available (self): def is_available (self):
raise NotImplementedError("Functions needs to be implemented")
def reload (self):
raise NotImplementedError("Functions needs to be implemented") raise NotImplementedError("Functions needs to be implemented")

View file

@ -53,6 +53,8 @@ loop_timer = LoopTimer(update_interval, run_on_hour=True)
"""Main loop starts from here""" """Main loop starts from here"""
def main(): def main():
owm = OwmForecasts.OwmForecasts(location, api_key, paid_api=owm_paid_subscription) 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: while True:
loop_timer.begin_loop() loop_timer.begin_loop()
@ -69,14 +71,15 @@ def main():
raise ImportError("choosen_design must be valid (" + choosen_design + ")") raise ImportError("choosen_design must be valid (" + choosen_design + ")")
debug.print_line("Fetching weather information from open weather map") debug.print_line("Fetching weather information from open weather map")
owm.reload()
design.add_weather(owm) design.add_weather(owm)
debug.print_line('Fetching events from your calendar') 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) design.add_calendar(events_cal)
debug.print_line('Fetching posts from your rss-feeds') debug.print_line('Fetching posts from your rss-feeds')
rss = RssParserPosts.RssParserPosts(rss_feeds) rss.reload()
design.add_rssfeed(rss) design.add_rssfeed(rss)
debug.print_line("\nStarting to render") debug.print_line("\nStarting to render")

View file

@ -13,6 +13,13 @@ class IcalEvents(CalendarInterface):
self.highlighted_urls = highlighted_urls self.highlighted_urls = highlighted_urls
super(IcalEvents, self).__init__() super(IcalEvents, self).__init__()
def is_available(self):
try:
urlopen("https://google.com", timeout=2)
return True
except:
return False
def __get_events__(self): def __get_events__(self):
events = self.__get_events_from_urls__(self.urls) events = self.__get_events_from_urls__(self.urls)

View file

@ -7,16 +7,20 @@ from settings import units
class OwmForecasts (WeatherInterface): class OwmForecasts (WeatherInterface):
"""Fetches weather through the Openweathermap-api.""" """Fetches weather through the Openweathermap-api."""
def __init__ (self, location, api_key, paid_api=False): def __init__ (self, location, api_key, paid_api=False):
subscription = "pro" if paid_api else None self.subscription = "pro" if paid_api else None
self.api = pyowm.OWM(api_key, subscription_type=subscription) self.api_key = api_key
self.units = units self.units = units
self.location = location self.location = location
self.api = pyowm.OWM(self.api_key, subscription_type=self.subscription)
def is_available (self): def is_available (self):
try: try:
return self.api.is_API_online() return self.api.is_API_online()
except: except:
return False return False
def reload(self):
pass
def get_today_forecast (self, location=None): def get_today_forecast (self, location=None):
if self.is_available() is False: if self.is_available() is False:

View file

@ -4,6 +4,12 @@ from datetime import datetime, timezone, timedelta
class RssInterface(DataSourceInterface): class RssInterface(DataSourceInterface):
"""Interface for fetching and processing rss post information.""" """Interface for fetching and processing rss post information."""
def __init__(self): def __init__(self):
self.loaded_posts = []
self.reload()
def reload(self):
if self.is_available() == False:
return
self.loaded_posts = self.__get_posts__() self.loaded_posts = self.__get_posts__()
self.__sort_posts__() self.__sort_posts__()

View file

@ -2,6 +2,7 @@ from RssInterface import RssInterface
from datetime import datetime, timedelta, date from datetime import datetime, timedelta, date
import feedparser import feedparser
import RssPost import RssPost
from urllib.request import urlopen
max_range_days = 14 max_range_days = 14
@ -11,6 +12,13 @@ class RssParserPosts (RssInterface):
self.urls = urls self.urls = urls
super(RssParserPosts, self).__init__() super(RssParserPosts, self).__init__()
def is_available(self):
try:
urlopen("https://google.com", timeout=2)
return True
except:
return False
def __get_posts__(self): def __get_posts__(self):
posts = [] posts = []