ical: Cache data

This commit is contained in:
nemunaire 2022-08-19 16:59:00 +02:00
parent 89067be592
commit ace077fd68

View File

@ -1,4 +1,6 @@
from datetime import datetime, timedelta, timezone from datetime import datetime, timedelta, timezone
import hashlib
import urllib.error
import urllib.request import urllib.request
from icalendar import Calendar, Event, vCalAddress, vText from icalendar import Calendar, Event, vCalAddress, vText
@ -10,13 +12,27 @@ class IcalModule:
def __init__(self, config): def __init__(self, config):
self.cals = config.cals self.cals = config.cals
self._cached_file = ".ical-%s.cache"
self.cache_time = 15
def draw_module(self, config, width, height, line_height=19): def draw_module(self, config, width, height, line_height=19):
now = datetime.now(tz=pytz.timezone('Europe/Paris')) now = datetime.now(tz=pytz.timezone('Europe/Paris'))
toofar = now + timedelta(weeks=1) toofar = now + timedelta(weeks=1)
events = [] events = []
for cal in self.cals: 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()) ecal = Calendar.from_ical(c.read())
for component in ecal.walk(): for component in ecal.walk():
if component.name == "VEVENT": if component.name == "VEVENT":