Refactoring

This commit is contained in:
2024-06-02 19:03:00 +02:00
parent 57b5b4ffab
commit 8c11166397
14 changed files with 401 additions and 322 deletions

View File

@ -9,29 +9,45 @@ import icalendar
from icalendar import Calendar, Event
import json
from init import scheduler, flask_app, Rapla, db
from init import scheduler, flaskApp, Rapla, db
async def fetchPlan(session, url):
"""
Hilfsfunktion, liefert die Response auf einen GET-Request zur angegebenen URL
:param session:
:param url:
:return Response:
"""
return await session.get(url=url)
def writeToFile(filename, data):
with open(filename, 'w+') as f:
f.write(data.text)
f.close()
"""
Schreibt die Daten in die angegebene Datei. Erstellt die Datei, falls sie noch nicht existiert.
:param filename:
:param data:
"""
with open(filename, 'w+') as file:
file.write(data.text)
file.close()
def writeToDB(kurs, url):
def writeKursToDB(kurs, url):
"""
Schreibt Kurs und URL in die Datenbank
:param kurs:
:param url:
"""
if Rapla.query.filter_by(name=kurs).first() is None:
new_kurs = Rapla(name=kurs, link=url, file=f"rapla{kurs}.ical")
db.session.add(new_kurs)
db.session.commit()
def parseURL(url: str):
def parseRaplaURL(url: str):
"""
Konvertiert URLs ins korrekte Format. \n
Konvertiert Rapla-URLs ins korrekte Format. \n
Konvertiert werden: http, www.; page=calendar \n
In: https; page=ical
:param url:
@ -47,12 +63,12 @@ def parseURL(url: str):
url = f"https{url[http:]}"
elif http == -1:
url = f"https://{url}"
p = url.find("page=")
u = url.find("&")
if (url[p + 5:u]).lower() == "ical":
pageInURL = url.find("page=")
andInURL = url.find("&")
if (url[pageInURL + 5:andInURL]).lower() == "ical":
return 1, url
elif p != -1:
return 1, f"{url[:p + 5]}ical{url[u:]}"
elif pageInURL != -1:
return 1, f"{url[:pageInURL + 5]}ical{url[andInURL:]}"
elif url.find("key") != -1:
return 2, url
else:
@ -67,15 +83,15 @@ async def getNewRapla(url: str):
:param url:
:return str:
"""
parsed = parseURL(url)
parsed = parseRaplaURL(url)
if parsed[0] == 0:
return 0
elif parsed[0] == 1:
url = parsed[1]
elif parsed[0] == 2:
return await buildFromKey(parsed[1], onlyUpdate=False)
urlfile = url.find("file=")
kurs = url[urlfile + 5:].upper()
return await buildICALfromKey(parsed[1], onlyUpdate=False)
fileInURL = url.find("file=")
kurs = url[fileInURL + 5:].upper()
if url[-5:] != ".ical":
try:
async with httpx.AsyncClient() as s:
@ -83,7 +99,7 @@ async def getNewRapla(url: str):
writeToFile(f"calendars/rapla{kurs}.ical", response)
except urllib.error.URLError:
return -1
writeToDB(kurs, url)
writeKursToDB(kurs, url)
return f"rapla{kurs}.ical"
else:
return url
@ -104,43 +120,52 @@ def getIcal(kurs: str):
def getRaplas():
"""
Liefert alle auf dem Server gespeicherten Raplas.
Liefert alle in der Datenbank gespeicherten Raplas.
:return (Kursliste, Dateiliste, URL-Liste):
"""
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
kursList = [rapla.name for rapla in raplas]
fileList = [rapla.file for rapla in raplas]
urlList = [rapla.link for rapla in raplas]
return kursList, fileList, urlList
async def refreshRapla():
"""
Aktualisiert alle 5 Minuten alle gespeicherten Raplas.
Aktualisiert alle gespeicherten Raplas.
"""
filel = getRaplas()[1]
urll = getRaplas()[2]
jobl = []
fileList = getRaplas()[1]
urlList = getRaplas()[2]
jobList = []
async with httpx.AsyncClient() as s:
for i in range(len(filel)):
print(f"Update Rapla: {filel[i][:-5]}")
if urll[i].find("file") != -1:
jobl += [fetchPlan(s, urll[i])]
for i in range(len(fileList)):
print(f"Update Rapla: {fileList[i][:-5]}")
if urlList[i].find("file") != -1:
jobList += [fetchPlan(s, urlList[i])]
else:
jobl += [buildFromKey(urll[i], onlyUpdate=True)]
callist = await asyncio.gather(*jobl, return_exceptions=True)
for cal in range(len(callist)):
if callist[cal] != 200:
writeToFile(f"calendars/{filel[cal]}", callist[cal])
jobList += [buildICALfromKey(urlList[i], onlyUpdate=True)]
calendarList = await asyncio.gather(*jobList, return_exceptions=True)
for calendar in range(len(calendarList)):
if calendarList[calendar] != 200:
writeToFile(f"calendars/{fileList[calendar]}", calendarList[calendar])
@scheduler.task('cron', id="raplaschedule", hour='*', day_of_week='*', minute='*/3', week='*', second='40')
@scheduler.task('cron', id="raplaSchedule", hour='*', day_of_week='*', minute='*/3', week='*', second='40')
def raplaSchedule():
with flask_app.app_context():
"""
Nutzt alle 3 Minuten refreshRapla() um die Stundenpläne zu aktualisieren.
"""
with flaskApp.app_context():
asyncio.run(refreshRapla())
async def buildFromKey(url, onlyUpdate):
async def buildICALfromKey(url, onlyUpdate):
"""
Baut eine .ical-Datei aus der mitgegebenen URL, die ein Key-Parameter enthalten muss.
:param url:
:param onlyUpdate:
:return Dateinamen:
"""
async with httpx.AsyncClient() as s:
page = await s.get(url=url)
if page.text[:10] == "BEGIN:VCAL":
@ -148,7 +173,7 @@ async def buildFromKey(url, onlyUpdate):
kursname = info[info.find('filename=')+9:-4].upper()
writeToFile(f"rapla{kursname}.ical", page)
if not onlyUpdate:
writeToDB(kursname, url)
writeKursToDB(kursname, url)
return url
else:
return 200
@ -185,7 +210,7 @@ async def buildFromKey(url, onlyUpdate):
f.write(cal.to_ical())
f.close()
if not onlyUpdate:
writeToDB(kursname, url)
writeKursToDB(kursname, url)
return f"rapla{kursname}.ical"
else:
return 200