From 185917e03703b190ac5a7baf3b9dddbbb8b398a6 Mon Sep 17 00:00:00 2001 From: Christian Franke Date: Tue, 5 Apr 2016 16:38:06 -0300 Subject: Mirror the iCal file --- scripts/get_calendar.py | 27 +++++++++++++++++++++++---- template/pages/termine2/content.html | 6 +++++- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/scripts/get_calendar.py b/scripts/get_calendar.py index 639e1ea..cb3c800 100644 --- a/scripts/get_calendar.py +++ b/scripts/get_calendar.py @@ -15,9 +15,26 @@ import subprocess def fetch_calendar(logger): """Fetches the calendar events and returns a icalendar.Calendar instance. """ - response = urllib2.urlopen(CALENDARIUM_IMPORT_URL) - logger.debug('Fetched calendar from %s' % CALENDARIUM_IMPORT_URL) - return icalendar.Calendar.from_ical(response.read()) + try: + response = urllib2.urlopen(CALENDARIUM_IMPORT_URL) + logger.debug('Fetched calendar from %s' % CALENDARIUM_IMPORT_URL) + except Exception: + sys.excepthook(*sys.exc_info()) + content = None + else: + content = response.read() + + if content is None: + # If we couldn't fetch the content, pull it from the copied file + # if it's not there, just fail fatally as we don't have any data + with open(CALENDARIUM_EXPORT, 'r') as export_file: + content = export_file.read() + else: + with open(CALENDARIUM_EXPORT + '.new', 'w') as export_file: + export_file.write(content) + os.rename(CALENDARIUM_EXPORT + '.new', CALENDARIUM_EXPORT) + + return icalendar.Calendar.from_ical(content) def get_events(calendar, after, before): """Yields all events in calendar as dictionary. @@ -91,8 +108,11 @@ def put_events(calendar, after, before, destination): return True return False +base_path = os.path.dirname(os.path.abspath(__file__)) #CALENDARIUM_IMPORT_URL = 'https://sublab.org:5232/calendars/events' CALENDARIUM_IMPORT_URL = 'https://posteo.de/calendars/ics/ruku1tibpa2b2evfwsxrbvwcy2n60j9g' +CALENDARIUM_EXPORT = os.path.join(base_path, '../public/calendar.ical') + if __name__ == '__main__': logging.basicConfig(stream=sys.stderr, level=logging.ERROR) logger = logging.getLogger('calendar_feed') @@ -100,7 +120,6 @@ if __name__ == '__main__': now = datetime.now() after = now - relativedelta(days=1) before = now + relativedelta(months=+1) - base_path = os.path.dirname(os.path.abspath(__file__)) after_tab = datetime(now.year,now.month,1); if now.month + 2 > 12: diff --git a/template/pages/termine2/content.html b/template/pages/termine2/content.html index adb9f62..3e6c330 100644 --- a/template/pages/termine2/content.html +++ b/template/pages/termine2/content.html @@ -22,4 +22,8 @@ $tabcalendar_events

- +
+
+

Ebenso gibt es einen iCAL Kalender der die Veranstalungen enthaelt.

+
+
-- cgit v1.2.1