Rapla-Scheduler
This commit is contained in:
@ -23,9 +23,10 @@ def writeToFile(filename, data):
|
|||||||
|
|
||||||
|
|
||||||
def writeToDB(kurs, url):
|
def writeToDB(kurs, url):
|
||||||
new_kurs = Rapla(name=kurs, link=url, file=f"rapla{kurs}.ical")
|
if Rapla.query.filter_by(name=kurs).first() is None:
|
||||||
db.session.add(new_kurs)
|
new_kurs = Rapla(name=kurs, link=url, file=f"rapla{kurs}.ical")
|
||||||
db.session.commit()
|
db.session.add(new_kurs)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
|
||||||
def parseURL(url: str):
|
def parseURL(url: str):
|
||||||
@ -72,7 +73,7 @@ async def getNewRapla(url: str):
|
|||||||
elif parsed[0] == 1:
|
elif parsed[0] == 1:
|
||||||
url = parsed[1]
|
url = parsed[1]
|
||||||
elif parsed[0] == 2:
|
elif parsed[0] == 2:
|
||||||
return await buildFromKey(parsed[1])
|
return await buildFromKey(parsed[1], onlyUpdate=False)
|
||||||
urlfile = url.find("file=")
|
urlfile = url.find("file=")
|
||||||
kurs = url[urlfile + 5:].upper()
|
kurs = url[urlfile + 5:].upper()
|
||||||
if url[-5:] != ".ical":
|
if url[-5:] != ".ical":
|
||||||
@ -123,26 +124,34 @@ async def refreshRapla():
|
|||||||
async with httpx.AsyncClient() as s:
|
async with httpx.AsyncClient() as s:
|
||||||
for i in range(len(filel)):
|
for i in range(len(filel)):
|
||||||
print(f"Update Rapla: {filel[i][:-5]}")
|
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)
|
callist = await asyncio.gather(*jobl, return_exceptions=True)
|
||||||
for cal in range(len(callist)):
|
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='*')
|
@scheduler.task('cron', id="raplaschedule", hour='*', day_of_week='*', minute='*/3', week='*', second='40')
|
||||||
def raplaschedule():
|
def raplaSchedule():
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
asyncio.run(refreshRapla())
|
asyncio.run(refreshRapla())
|
||||||
|
|
||||||
|
|
||||||
async def buildFromKey(url):
|
async def buildFromKey(url, onlyUpdate):
|
||||||
async with httpx.AsyncClient() as s:
|
async with httpx.AsyncClient() as s:
|
||||||
page = await s.get(url=url)
|
page = await s.get(url=url)
|
||||||
if page.text[:10] == "BEGIN:VCAL":
|
if page.text[:10] == "BEGIN:VCAL":
|
||||||
info = page.headers['content-disposition']
|
info = page.headers['content-disposition']
|
||||||
kursname = info[info.find('filename=')+9:-4].upper()
|
kursname = info[info.find('filename=')+9:-4].upper()
|
||||||
writeToDB(kursname, url)
|
writeToFile(f"rapla{kursname}.ical", page)
|
||||||
return url
|
if not onlyUpdate:
|
||||||
|
writeToDB(kursname, url)
|
||||||
|
return url
|
||||||
|
else:
|
||||||
|
return 200
|
||||||
else:
|
else:
|
||||||
kursname = page.text[page.text.find("<title>") + 7:page.text.find("</title>")]
|
kursname = page.text[page.text.find("<title>") + 7:page.text.find("</title>")]
|
||||||
if len(kursname) > 15:
|
if len(kursname) > 15:
|
||||||
@ -175,5 +184,8 @@ async def buildFromKey(url):
|
|||||||
with open(f"calendars/rapla{kursname}.ical", "wb") as f:
|
with open(f"calendars/rapla{kursname}.ical", "wb") as f:
|
||||||
f.write(cal.to_ical())
|
f.write(cal.to_ical())
|
||||||
f.close()
|
f.close()
|
||||||
writeToDB(kursname, url)
|
if not onlyUpdate:
|
||||||
return f"rapla{kursname}.ical"
|
writeToDB(kursname, url)
|
||||||
|
return f"rapla{kursname}.ical"
|
||||||
|
else:
|
||||||
|
return 200
|
||||||
|
|||||||
Reference in New Issue
Block a user