Added offline support for services
This commit is contained in:
parent
b6691de9e8
commit
d2c7428c62
7 changed files with 41 additions and 4 deletions
|
@ -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)
|
||||
|
||||
|
|
|
@ -2,3 +2,6 @@ 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")
|
|
@ -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")
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -7,10 +7,11 @@ 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:
|
||||
|
@ -18,6 +19,9 @@ class OwmForecasts (WeatherInterface):
|
|||
except:
|
||||
return False
|
||||
|
||||
def reload(self):
|
||||
pass
|
||||
|
||||
def get_today_forecast (self, location=None):
|
||||
if self.is_available() is False:
|
||||
return None
|
||||
|
|
|
@ -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__()
|
||||
|
||||
|
|
|
@ -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 = []
|
||||
|
||||
|
|
Loading…
Reference in a new issue