diff --git a/modules/ical.py b/modules/ical.py index d8693a5..7a4504e 100644 --- a/modules/ical.py +++ b/modules/ical.py @@ -1,4 +1,6 @@ from datetime import datetime, timedelta, timezone +import hashlib +import urllib.error import urllib.request from icalendar import Calendar, Event, vCalAddress, vText @@ -10,13 +12,27 @@ class IcalModule: def __init__(self, config): self.cals = config.cals + self._cached_file = ".ical-%s.cache" + self.cache_time = 15 + def draw_module(self, config, width, height, line_height=19): now = datetime.now(tz=pytz.timezone('Europe/Paris')) toofar = now + timedelta(weeks=1) events = [] for cal in self.cals: - with urllib.request.urlopen(cal) as c: + cache_file = self._cached_file % (hashlib.md5(cal.encode()).hexdigest()) + + try: + with urllib.request.urlopen(cal) as c: + with open(cache_file, 'wb') as fd: + fd.write(c.read()) + except ConnectionResetError: + pass + except urllib.error.URLError: + pass + + with open(cache_file) as c: ecal = Calendar.from_ical(c.read()) for component in ecal.walk(): if component.name == "VEVENT":