import urllib.error from urllib.request import urlretrieve import icalendar import json import recurring_ical_events from init import scheduler def parseURL(url: str): """ Konvertiert URLs ins korrekte Format. \n Konvertiert werden: http, www.; page=calendar \n In: https; page=ical :param url: :return str: """ rapla = url.find("rapla.") if rapla == -1: return 0 elif len(url[:rapla]) == 4 or len(url[:rapla]) > 6: url = url[rapla:] http = url.find(":") if url[:http] == "http": url = "https" + url[http:] elif http == -1: url = "https://" + url p = url.find("page=") u = url.find("&") if (url[p + 5:u]).lower() == "ical": return url elif p != -1: return url[:p + 5] + "ical" + url[u:] else: return 0 def getNewRapla(url: str): """ Speichert den iCal eines Raplas auf dem Server. \n Gibt Namen der Datei zurück. \n TODO: Standort zu Dateibezeichner hinzufügen, um Konflikte zu vermeiden. :param url: :return str: """ url = parseURL(url) if url == 0: return 0 urlfile = url.find("file=") kurs = url[urlfile + 5:].upper() try: urlretrieve(url, "calendars/rapla" + kurs + ".ical") except urllib.error.URLError: return -1 file = open("calendars/list.json", "r+") jsoncal = json.load(file) jsoncal.update({kurs: ["rapla" + kurs + ".ical", url]}) file.close() file = open("calendars/list.json", "w") json.dump(jsoncal, file, indent=4) return "rapla" + kurs + ".ical" def getIcal(kurs: str): """ Liefert den Namen der Datei des mitgegebenen Kurses. :param kurs: :return str: """ file = open("calendars/list.json", "r") jf = json.load(file) try: return jf[kurs][0] except KeyError: return None def getRaplas(): """ Liefert alle auf dem Server gespeicherten Raplas. :return (Kursliste, Dateiliste, URL-Liste): """ file = open("calendars/list.json", "r") jsonf = json.load(file) kursl = [] filel = [] urll = [] for i in jsonf: kursl += [i] filel += [jsonf[i][0]] urll += [jsonf[i][1]] return sorted(kursl), sorted(filel), sorted(urll) @scheduler.task("interval", id="refreshRapla", minutes=5) def refreshRapla(): """ Aktualisiert alle 5 Minuten alle gespeicherten Raplas. """ filel = getRaplas()[1] urll = getRaplas()[2] for i in range(len(filel)): print("Update Rapla: " + filel[i][:-5]) urlretrieve(urll[i], "calendars/" + filel[i])