Rapla-Key-Import + Depreciate calendars/list.json
This commit is contained in:
125
fetchRAPLA.py
125
fetchRAPLA.py
@ -1,14 +1,15 @@
|
||||
import urllib.error
|
||||
from urllib.request import urlretrieve
|
||||
|
||||
from dateutil.parser import *
|
||||
|
||||
import asyncio
|
||||
|
||||
import httpx
|
||||
import icalendar
|
||||
from icalendar import Calendar, Event
|
||||
import json
|
||||
import recurring_ical_events
|
||||
|
||||
from init import scheduler, app
|
||||
from init import scheduler, app, Rapla, db
|
||||
|
||||
|
||||
async def fetchPlan(session, url):
|
||||
@ -21,6 +22,12 @@ def writeToFile(filename, data):
|
||||
f.close()
|
||||
|
||||
|
||||
def writeToDB(kurs, url):
|
||||
new_kurs = Rapla(name=kurs, link=url, file=f"rapla{kurs}.ical")
|
||||
db.session.add(new_kurs)
|
||||
db.session.commit()
|
||||
|
||||
|
||||
def parseURL(url: str):
|
||||
"""
|
||||
Konvertiert URLs ins korrekte Format. \n
|
||||
@ -36,17 +43,19 @@ def parseURL(url: str):
|
||||
url = url[rapla:]
|
||||
http = url.find(":")
|
||||
if url[:http] == "http":
|
||||
url = "https" + url[http:]
|
||||
url = f"https{url[http:]}"
|
||||
elif http == -1:
|
||||
url = "https://" + url
|
||||
url = f"https://{url}"
|
||||
p = url.find("page=")
|
||||
u = url.find("&")
|
||||
if (url[p + 5:u]).lower() == "ical":
|
||||
return url
|
||||
return 1, url
|
||||
elif p != -1:
|
||||
return url[:p + 5] + "ical" + url[u:]
|
||||
return 1, f"{url[:p + 5]}ical{url[u:]}"
|
||||
elif url.find("key") != -1:
|
||||
return 2, url
|
||||
else:
|
||||
return 0
|
||||
return 0, 0
|
||||
|
||||
|
||||
async def getNewRapla(url: str):
|
||||
@ -57,26 +66,26 @@ async def getNewRapla(url: str):
|
||||
:param url:
|
||||
:return str:
|
||||
"""
|
||||
url = parseURL(url)
|
||||
if url == 0:
|
||||
parsed = parseURL(url)
|
||||
if parsed[0] == 0:
|
||||
return 0
|
||||
elif parsed[0] == 1:
|
||||
url = parsed[1]
|
||||
elif parsed[0] == 2:
|
||||
return await buildFromKey(parsed[1])
|
||||
urlfile = url.find("file=")
|
||||
kurs = url[urlfile + 5:].upper()
|
||||
|
||||
try:
|
||||
urlretrieve(url, "calendars/rapla" + kurs + ".ical")
|
||||
async with httpx.AsyncClient() as s:
|
||||
response = await fetchPlan(s, url)
|
||||
writeToFile(f"calendars/rapla{kurs}.ical", response)
|
||||
except urllib.error.URLError:
|
||||
return -1
|
||||
file = open("calendars/list.json", "r+")
|
||||
jsoncal = json.load(file)
|
||||
jsoncal.update({kurs: [f"rapla{kurs}.ical", url]})
|
||||
file.close()
|
||||
file = open("calendars/list.json", "w")
|
||||
json.dump(jsoncal, file, indent=4)
|
||||
return f"rapla{kurs}.ical"
|
||||
if url[-5:] != ".ical":
|
||||
try:
|
||||
async with httpx.AsyncClient() as s:
|
||||
response = await fetchPlan(s, url)
|
||||
writeToFile(f"calendars/rapla{kurs}.ical", response)
|
||||
except urllib.error.URLError:
|
||||
return -1
|
||||
writeToDB(kurs, url)
|
||||
return f"rapla{kurs}.ical"
|
||||
else:
|
||||
return url
|
||||
|
||||
|
||||
def getIcal(kurs: str):
|
||||
@ -85,10 +94,9 @@ def getIcal(kurs: str):
|
||||
:param kurs:
|
||||
:return str:
|
||||
"""
|
||||
file = open("calendars/list.json", "r")
|
||||
jf = json.load(file)
|
||||
rapla = Rapla.query.filter(Rapla.name == kurs).first()
|
||||
try:
|
||||
return jf[kurs][0]
|
||||
return rapla.file
|
||||
except KeyError:
|
||||
return None
|
||||
|
||||
@ -98,16 +106,11 @@ 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)
|
||||
raplas = Rapla.query.all()
|
||||
kursl = [rapla.name for rapla in raplas]
|
||||
filel = [rapla.file for rapla in raplas]
|
||||
urll = [rapla.link for rapla in raplas]
|
||||
return kursl, filel, urll
|
||||
|
||||
|
||||
async def refreshRapla():
|
||||
@ -126,7 +129,51 @@ async def refreshRapla():
|
||||
writeToFile(f"calendars/{filel[cal]}", callist[cal])
|
||||
|
||||
|
||||
@scheduler.task('cron', id="raplaschedule", hour='*', day_of_week='*', minute='*/15', week='*')
|
||||
#@scheduler.task('cron', id="raplaschedule", hour='*', day_of_week='*', minute='*/2', week='*')
|
||||
def raplaschedule():
|
||||
with app.app_context():
|
||||
asyncio.run(refreshRapla())
|
||||
|
||||
|
||||
async def buildFromKey(url):
|
||||
async with httpx.AsyncClient() as s:
|
||||
page = await s.get(url=url)
|
||||
if page.text[:10] == "BEGIN:VCAL":
|
||||
info = page.headers['content-disposition']
|
||||
kursname = info[info.find('filename=')+9:-4].upper()
|
||||
writeToDB(kursname, url)
|
||||
return url
|
||||
else:
|
||||
kursname = page.text[page.text.find("<title>") + 7:page.text.find("</title>")]
|
||||
if len(kursname) > 15:
|
||||
return 0
|
||||
start = "2024-08-01"
|
||||
end = "2024-12-31"
|
||||
payload = {"url": url, "start": start, "end": end}
|
||||
req = await s.post(url="https://dh-api.paulmartin.cloud/rapla", data=payload,
|
||||
headers={'Content-Type': 'application/x-www-form-urlencoded'}, timeout=10)
|
||||
jsonresp = json.loads(req.text)
|
||||
cal = Calendar()
|
||||
cal.add('prodid', '-//Rapla//iCal Plugin//EN')
|
||||
cal.add('version', '2.0')
|
||||
for i in jsonresp:
|
||||
if len(jsonresp[i]) != 0:
|
||||
for jsonday in jsonresp[i]:
|
||||
event = Event()
|
||||
event.add('SUMMARY', jsonday['title'])
|
||||
event.add('LOCATION', jsonday['ressources'])
|
||||
event.add('DTSTART', parse(jsonday['startDate'], dayfirst=True))
|
||||
event.add('DTEND', parse(jsonday['endDate'], dayfirst=True))
|
||||
try:
|
||||
teacher = icalendar.vCalAddress(value=jsonday['persons'])
|
||||
teacher.params["CN"] = jsonday['persons']
|
||||
except KeyError:
|
||||
teacher = icalendar.vCalAddress(value="")
|
||||
teacher.params["CN"] = ""
|
||||
event.add('ATTENDEE', teacher)
|
||||
cal.add_component(event)
|
||||
with open(f"calendars/rapla{kursname}.ical", "wb") as f:
|
||||
f.write(cal.to_ical())
|
||||
f.close()
|
||||
writeToDB(kursname, url)
|
||||
return f"rapla{kursname}.ical"
|
||||
|
||||
Reference in New Issue
Block a user