complete rapla async
This commit is contained in:
@ -1,6 +1,2 @@
|
|||||||
{
|
{
|
||||||
"TINF22B3": [
|
|
||||||
"raplaTINF22B3.ical",
|
|
||||||
"https://rapla.dhbw-karlsruhe.de/rapla?page=ical&user=vollmer&file=tinf22b3"
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
@ -1,5 +1,8 @@
|
|||||||
import json
|
import json
|
||||||
from init import db, Meals, scheduler
|
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
from init import db, Meals, scheduler, app
|
||||||
import datetime
|
import datetime
|
||||||
import time
|
import time
|
||||||
import httpx
|
import httpx
|
||||||
@ -110,7 +113,6 @@ def formatDay(day: datetime):
|
|||||||
return day
|
return day
|
||||||
|
|
||||||
|
|
||||||
@scheduler.task('cron', id="refreshMeals", hour='8-11', day_of_week='*', minute='*/15', week='*', second='30')
|
|
||||||
async def refreshMeals():
|
async def refreshMeals():
|
||||||
"""
|
"""
|
||||||
Aktualisiert immer vormittags alle Mahlzeiten in der Datenbank. \n
|
Aktualisiert immer vormittags alle Mahlzeiten in der Datenbank. \n
|
||||||
@ -126,6 +128,7 @@ async def refreshMeals():
|
|||||||
for i in range(len(dates)):
|
for i in range(len(dates)):
|
||||||
dates[i] = formatDay(dates[i])
|
dates[i] = formatDay(dates[i])
|
||||||
for i in dates:
|
for i in dates:
|
||||||
|
apinames = await getMealsFromAPI(i)
|
||||||
apinames = await getMealsFromAPI(i)
|
apinames = await getMealsFromAPI(i)
|
||||||
dbmeals = Meals.query.filter_by(date=i).all()
|
dbmeals = Meals.query.filter_by(date=i).all()
|
||||||
dbnames = []
|
dbnames = []
|
||||||
@ -136,3 +139,9 @@ async def refreshMeals():
|
|||||||
db.session.delete(Meals.query.filter_by(date=i, name=n).first())
|
db.session.delete(Meals.query.filter_by(date=i, name=n).first())
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
await getMealsFromAPI(i, True)
|
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())
|
||||||
|
|||||||
@ -1,13 +1,24 @@
|
|||||||
import urllib.error
|
import urllib.error
|
||||||
from urllib.request import urlretrieve
|
from urllib.request import urlretrieve
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
import httpx
|
||||||
import icalendar
|
import icalendar
|
||||||
import json
|
import json
|
||||||
import recurring_ical_events
|
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):
|
def parseURL(url: str):
|
||||||
@ -38,7 +49,7 @@ def parseURL(url: str):
|
|||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
def getNewRapla(url: str):
|
async def getNewRapla(url: str):
|
||||||
"""
|
"""
|
||||||
Speichert den iCal eines Raplas auf dem Server. \n
|
Speichert den iCal eines Raplas auf dem Server. \n
|
||||||
Gibt Namen der Datei zurück. \n
|
Gibt Namen der Datei zurück. \n
|
||||||
@ -54,16 +65,18 @@ def getNewRapla(url: str):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
urlretrieve(url, "calendars/rapla" + kurs + ".ical")
|
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:
|
except urllib.error.URLError:
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
file = open("calendars/list.json", "r+")
|
file = open("calendars/list.json", "r+")
|
||||||
jsoncal = json.load(file)
|
jsoncal = json.load(file)
|
||||||
jsoncal.update({kurs: ["rapla" + kurs + ".ical", url]})
|
jsoncal.update({kurs: [f"rapla{kurs}.ical", url]})
|
||||||
file.close()
|
file.close()
|
||||||
file = open("calendars/list.json", "w")
|
file = open("calendars/list.json", "w")
|
||||||
json.dump(jsoncal, file, indent=4)
|
json.dump(jsoncal, file, indent=4)
|
||||||
return "rapla" + kurs + ".ical"
|
return f"rapla{kurs}.ical"
|
||||||
|
|
||||||
|
|
||||||
def getIcal(kurs: str):
|
def getIcal(kurs: str):
|
||||||
@ -97,13 +110,23 @@ def getRaplas():
|
|||||||
return sorted(kursl), sorted(filel), sorted(urll)
|
return sorted(kursl), sorted(filel), sorted(urll)
|
||||||
|
|
||||||
|
|
||||||
@scheduler.task("interval", id="refreshRapla", minutes=5)
|
async def refreshRapla():
|
||||||
def refreshRapla():
|
|
||||||
"""
|
"""
|
||||||
Aktualisiert alle 5 Minuten alle gespeicherten Raplas.
|
Aktualisiert alle 5 Minuten alle gespeicherten Raplas.
|
||||||
"""
|
"""
|
||||||
filel = getRaplas()[1]
|
filel = getRaplas()[1]
|
||||||
urll = getRaplas()[2]
|
urll = getRaplas()[2]
|
||||||
|
jobl = []
|
||||||
|
async with httpx.AsyncClient() as s:
|
||||||
for i in range(len(filel)):
|
for i in range(len(filel)):
|
||||||
print("Update Rapla: " + filel[i][:-5])
|
print(f"Update Rapla: {filel[i][:-5]}")
|
||||||
urlretrieve(urll[i], "calendars/" + filel[i])
|
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())
|
||||||
|
|||||||
1
init.py
1
init.py
@ -1,3 +1,4 @@
|
|||||||
|
import apscheduler
|
||||||
from flask import Flask
|
from flask import Flask
|
||||||
from flask_login import LoginManager, UserMixin
|
from flask_login import LoginManager, UserMixin
|
||||||
from flask_sqlalchemy import SQLAlchemy
|
from flask_sqlalchemy import SQLAlchemy
|
||||||
|
|||||||
@ -223,7 +223,7 @@ def chooseRaplas():
|
|||||||
|
|
||||||
@app.route("/set-up/rapla", methods=["POST"])
|
@app.route("/set-up/rapla", methods=["POST"])
|
||||||
@login_required
|
@login_required
|
||||||
def getRapla():
|
async def getRapla():
|
||||||
"""
|
"""
|
||||||
Verarbeitet die Eingabe von chooseRaplas().
|
Verarbeitet die Eingabe von chooseRaplas().
|
||||||
:return HTML:
|
:return HTML:
|
||||||
@ -236,7 +236,7 @@ def getRapla():
|
|||||||
loadUser(current_user.id).kurs = file[5:-5]
|
loadUser(current_user.id).kurs = file[5:-5]
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
elif url != "None":
|
elif url != "None":
|
||||||
file = getNewRapla(url)
|
file = await getNewRapla(url)
|
||||||
if type(file) is not int:
|
if type(file) is not int:
|
||||||
User.query.filter_by(id=current_user.id).first().kurs = file[5:-5]
|
User.query.filter_by(id=current_user.id).first().kurs = file[5:-5]
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|||||||
Reference in New Issue
Block a user