From 2a963df62dd96dcbcf6a1ff5ae44455a3e4d7f22 Mon Sep 17 00:00:00 2001 From: flifloo Date: Thu, 26 Dec 2019 12:06:02 +0100 Subject: [PATCH] Add protection on calendar update --- TelegramEDT/EDTcalendar.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/TelegramEDT/EDTcalendar.py b/TelegramEDT/EDTcalendar.py index 32b3e79..78ed2da 100644 --- a/TelegramEDT/EDTcalendar.py +++ b/TelegramEDT/EDTcalendar.py @@ -4,9 +4,11 @@ from os.path import getmtime, isfile import ics import requests from aiogram.utils import markdown +from ics.parse import ParseError, string_to_container from ics.timeline import Timeline URL = "http://adelb.univ-lyon1.fr/jsp/custom/modules/plannings/anonymous_cal.jsp" +EMPTY_CALENDAR = "BEGIN:VCALENDAR\r\nPRODID:ics.py - http://git.io/lLljaA\r\nVERSION:2.0\r\nEND:VCALENDAR" class Calendar(ics.Calendar): @@ -39,7 +41,14 @@ class Calendar(ics.Calendar): name = f"calendars/{resources}-{projectid}.ical" now = self._now().timestamp() if not isfile(name) or now-getmtime(name) < now-360: - open(name, "w").write(requests.get(self._url(url, resources, projectid)).text) + try: + calendar = requests.get(self._url(url, resources, projectid)).text + string_to_container(calendar) + except (ParseError, requests.exceptions.ConnectionError, requests.exceptions.ConnectTimeout): + if not isfile(name): + open(name, "w").write(EMPTY_CALENDAR) + else: + open(name, "w").write(calendar) return open(name, "r").read() def _events(self, time: str, pass_week: bool):