diff options
Diffstat (limited to 'scripts/get_calendar.py')
-rw-r--r-- | scripts/get_calendar.py | 27 |
1 files changed, 23 insertions, 4 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: |