def icalendar(name, url): def fetch(): try: response = urllib.request.urlopen(url, timeout=1) return response.read().decode('utf-8') except urllib.error.URLError as e: print("icalendar:{}: {}".format(name, e)) return None data = cache.get("{}.ics".format(name), 60 * 12, fetch) if data is None: # Cache empty and update failed. return icalendar_data(name, data)
def caldav(name, url): def build(): try: cal = Calendar() dav = DAVClient(url) for calendar in dav.principal().calendars(): for event in calendar.events(): evcal = Calendar.from_ical(event.data) for e in evcal.walk(): if isinstance(e, Event): cal.add_component(e) return cal.to_ical().decode('utf-8') except OSError as e: print("caldav:{}: {}".format(name, e)) return None data = cache.get("{}.ics".format(name), 60 * 12, build) if data is None: # Cache empty and update failed. return icalendar_data(name, data)