Catch parsing error when caching a calendar

This commit is contained in:
Jules 2020-11-27 18:21:17 +01:00
parent 1c4f9f2be2
commit 2ffc0e8d1b
2 changed files with 21 additions and 11 deletions

View file

@ -5,9 +5,11 @@ import threading
import time import time
from hashlib import sha256 from hashlib import sha256
import traceback
import arrow import arrow
import requests import requests
from ics import Calendar from ics import Calendar
from tatsu.exceptions import FailedParse
def cache(entry: dict, scheduler: sched.scheduler = None) -> None: def cache(entry: dict, scheduler: sched.scheduler = None) -> None:
@ -27,18 +29,15 @@ def cache(entry: dict, scheduler: sched.scheduler = None) -> None:
:type scheduler: sched.scheduler :type scheduler: sched.scheduler
""" """
if not os.path.isdir('app/cache'):
os.mkdir('app/cache')
url = entry['url']
path = "app/cache/" + sha256(url.encode()).hexdigest() + ".ics"
try: try:
if not os.path.isdir('app/cache'):
os.mkdir('app/cache')
url = entry['url']
path = "app/cache/" + sha256(url.encode()).hexdigest() + ".ics"
r = requests.get(entry["url"], allow_redirects=True) r = requests.get(entry["url"], allow_redirects=True)
except Exception as e:
print(arrow.now().format("YYYY-MM-DD HH:mm:ss"), "Could not cache", entry)
print(e)
else:
if "encoding" in entry: if "encoding" in entry:
cal = Calendar(imports=r.content.decode(encoding=entry["encoding"])) cal = Calendar(imports=r.content.decode(encoding=entry["encoding"]))
else: else:
@ -47,6 +46,16 @@ def cache(entry: dict, scheduler: sched.scheduler = None) -> None:
cal = horodate(cal, 'Cached at') cal = horodate(cal, 'Cached at')
open(path, 'w').writelines(cal) open(path, 'w').writelines(cal)
print(arrow.now().format("YYYY-MM-DD HH:mm:ss"), "Cached", entry['name']) print(arrow.now().format("YYYY-MM-DD HH:mm:ss"), "Cached", entry['name'])
except FailedParse:
print("Could not parse", entry['name'])
# Save stack trace when an unknown error occurs
except Exception as e:
with open("error " + arrow.now().format("YYYY-MM-DD HH:mm:ss")+".txt", 'w') as file:
file.write(arrow.now().format("YYYY-MM-DD HH:mm:ss") + "\nCould not cache : " + str(entry))
file.write(str(e))
file.write(str(traceback.format_exc()))
finally: finally:
if scheduler is not None: if scheduler is not None:
delay = entry['cache'] if entry['cache'] > 0 else 10 delay = entry['cache'] if entry['cache'] > 0 else 10

View file

@ -2,4 +2,5 @@ requests~=2.22.0
ics~=0.7 ics~=0.7
pathvalidate~=2.3.0 pathvalidate~=2.3.0
flask~=1.1.1 flask~=1.1.1
arrow~=0.14.7 arrow~=0.14.7
tatsu~=4.4.0