Files
DualHub/fetchRAPLA.py
2023-11-29 11:10:01 +01:00

59 lines
1.4 KiB
Python

import urllib.error
from urllib.request import urlretrieve
import icalendar
import json
import recurring_ical_events
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"})
file.close()
file = open("calendars/list.json", "w")
json.dump(jsoncal, file, indent=4)
return "rapla"+kurs+".ical"
def getRaplas():
file = open("calendars/list.json", "r")
jsonf = json.load(file)
kursl = []
filel = []
for i in jsonf:
kursl += [i]
filel += [jsonf[i]]
return sorted(kursl), sorted(filel)