complete rapla async
This commit is contained in:
@ -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())
|
||||
|
||||
Reference in New Issue
Block a user