ical: Cache data
This commit is contained in:
parent
89067be592
commit
ace077fd68
@ -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":
|
||||
|
Loading…
Reference in New Issue
Block a user