complete rapla async

This commit is contained in:
2024-04-10 13:10:56 +02:00
parent ccb088e36d
commit 447800ad73
5 changed files with 49 additions and 20 deletions

View File

@ -1,6 +1,2 @@
{
"TINF22B3": [
"raplaTINF22B3.ical",
"https://rapla.dhbw-karlsruhe.de/rapla?page=ical&user=vollmer&file=tinf22b3"
]
}

View File

@ -1,5 +1,8 @@
import json
from init import db, Meals, scheduler
import asyncio
from init import db, Meals, scheduler, app
import datetime
import time
import httpx
@ -110,7 +113,6 @@ def formatDay(day: datetime):
return day
@scheduler.task('cron', id="refreshMeals", hour='8-11', day_of_week='*', minute='*/15', week='*', second='30')
async def refreshMeals():
"""
Aktualisiert immer vormittags alle Mahlzeiten in der Datenbank. \n
@ -126,6 +128,7 @@ async def refreshMeals():
for i in range(len(dates)):
dates[i] = formatDay(dates[i])
for i in dates:
apinames = await getMealsFromAPI(i)
apinames = await getMealsFromAPI(i)
dbmeals = Meals.query.filter_by(date=i).all()
dbnames = []
@ -136,3 +139,9 @@ async def refreshMeals():
db.session.delete(Meals.query.filter_by(date=i, name=n).first())
db.session.commit()
await getMealsFromAPI(i, True)
@scheduler.task('cron', id="mensaschedule", hour='8-11', day_of_week='*', minute='*/15', week='*', second='5')
def mensaschedule():
with app.app_context():
asyncio.run(refreshMeals())

View File

@ -1,13 +1,24 @@
import urllib.error
from urllib.request import urlretrieve
import asyncio
import httpx
import icalendar
import json
import recurring_ical_events
from init import scheduler
from init import scheduler, app
#from init import scheduler
async def fetchPlan(session, url):
return await session.get(url=url)
def writeToFile(filename, data):
with open(filename, 'w+') as f:
f.write(data.text)
f.close()
def parseURL(url: str):
@ -38,7 +49,7 @@ def parseURL(url: str):
return 0
def getNewRapla(url: str):
async def getNewRapla(url: str):
"""
Speichert den iCal eines Raplas auf dem Server. \n
Gibt Namen der Datei zurück. \n
@ -54,16 +65,18 @@ def getNewRapla(url: str):
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: ["rapla" + kurs + ".ical", url]})
jsoncal.update({kurs: [f"rapla{kurs}.ical", url]})
file.close()
file = open("calendars/list.json", "w")
json.dump(jsoncal, file, indent=4)
return "rapla" + kurs + ".ical"
return f"rapla{kurs}.ical"
def getIcal(kurs: str):
@ -97,13 +110,23 @@ def getRaplas():
return sorted(kursl), sorted(filel), sorted(urll)
@scheduler.task("interval", id="refreshRapla", minutes=5)
def refreshRapla():
async 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])
jobl = []
async with httpx.AsyncClient() as s:
for i in range(len(filel)):
print(f"Update Rapla: {filel[i][:-5]}")
jobl += [fetchPlan(s, urll[i])]
callist = await asyncio.gather(*jobl, return_exceptions=True)
for cal in range(len(callist)):
writeToFile(f"calendars/{filel[cal]}", callist[cal])
@scheduler.task('cron', id="raplaschedule", hour='*', day_of_week='*', minute='*/15', week='*')
def raplaschedule():
with app.app_context():
asyncio.run(refreshRapla())

View File

@ -1,3 +1,4 @@
import apscheduler
from flask import Flask
from flask_login import LoginManager, UserMixin
from flask_sqlalchemy import SQLAlchemy
@ -80,7 +81,7 @@ class Meals(db.Model):
schwein = db.Column(db.Boolean)
scheduler = APScheduler()
scheduler = APScheduler ()
app = create()
def_src = ["*.paulmartin.cloud", '\'self\'']
Talisman(app, content_security_policy={"default-src": def_src, "script-src": def_src # + ["'unsafe-inline'"]

View File

@ -223,7 +223,7 @@ def chooseRaplas():
@app.route("/set-up/rapla", methods=["POST"])
@login_required
def getRapla():
async def getRapla():
"""
Verarbeitet die Eingabe von chooseRaplas().
:return HTML:
@ -236,7 +236,7 @@ def getRapla():
loadUser(current_user.id).kurs = file[5:-5]
db.session.commit()
elif url != "None":
file = getNewRapla(url)
file = await getNewRapla(url)
if type(file) is not int:
User.query.filter_by(id=current_user.id).first().kurs = file[5:-5]
db.session.commit()