import urllib.error from urllib.request import urlretrieve import icalendar import json import recurring_ical_events from init import scheduler def parseURL(url: 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): 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): file = open("calendars/list.json", "r") jf = json.load(file) try: return jf[kurs][0] except KeyError: return None def getRaplas(): 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=30) def refreshRapla(): filel = getRaplas()[1] urll = getRaplas()[2] for i in range(len(filel)): urlretrieve(urll[i], "calendars/"+filel[i]) print("Update die Kalender...")