Rapla-Scheduler

This commit is contained in:
2024-04-17 14:36:43 +02:00
parent dd052aeded
commit 0a4cca102b

View File

@ -23,9 +23,10 @@ def writeToFile(filename, data):
def writeToDB(kurs, url):
new_kurs = Rapla(name=kurs, link=url, file=f"rapla{kurs}.ical")
db.session.add(new_kurs)
db.session.commit()
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):
@ -72,7 +73,7 @@ async def getNewRapla(url: str):
elif parsed[0] == 1:
url = parsed[1]
elif parsed[0] == 2:
return await buildFromKey(parsed[1])
return await buildFromKey(parsed[1], onlyUpdate=False)
urlfile = url.find("file=")
kurs = url[urlfile + 5:].upper()
if url[-5:] != ".ical":
@ -123,26 +124,34 @@ async def refreshRapla():
async with httpx.AsyncClient() as s:
for i in range(len(filel)):
print(f"Update Rapla: {filel[i][:-5]}")
jobl += [fetchPlan(s, urll[i])]
if urll[i].find("file") != -1:
jobl += [fetchPlan(s, urll[i])]
else:
jobl += [buildFromKey(urll[i], onlyUpdate=True)]
callist = await asyncio.gather(*jobl, return_exceptions=True)
for cal in range(len(callist)):
writeToFile(f"calendars/{filel[cal]}", callist[cal])
if callist[cal] != 200:
writeToFile(f"calendars/{filel[cal]}", callist[cal])
#@scheduler.task('cron', id="raplaschedule", hour='*', day_of_week='*', minute='*/2', week='*')
def raplaschedule():
@scheduler.task('cron', id="raplaschedule", hour='*', day_of_week='*', minute='*/3', week='*', second='40')
def raplaSchedule():
with app.app_context():
asyncio.run(refreshRapla())
async def buildFromKey(url):
async def buildFromKey(url, onlyUpdate):
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
writeToFile(f"rapla{kursname}.ical", page)
if not onlyUpdate:
writeToDB(kursname, url)
return url
else:
return 200
else:
kursname = page.text[page.text.find("<title>") + 7:page.text.find("</title>")]
if len(kursname) > 15:
@ -175,5 +184,8 @@ async def buildFromKey(url):
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"
if not onlyUpdate:
writeToDB(kursname, url)
return f"rapla{kursname}.ical"
else:
return 200