complete mensa async

This commit is contained in:
2024-04-09 08:50:05 +02:00
parent 490ee7f02f
commit 0d31d84d48
2 changed files with 41 additions and 42 deletions

View File

@ -1,4 +1,3 @@
import requests
import urllib.parse
import time
from bs4 import BeautifulSoup
@ -59,7 +58,7 @@ async def getKurs(token: int, cookie: str):
headers=headers)
html = BeautifulSoup(response.text, 'lxml')
link = html.body.find('a', attrs={'id': "Popup_details0001"})['href']
response = requests.request("GET", url + link[21:], headers=headers, data={})
response = await s.get(url=f"{url}{link[21:]}", headers=headers)
html = BeautifulSoup(response.text, 'lxml')
content = html.body.find('td', attrs={'class': 'level02'}).text
start = content.find(" ") + 4

View File

@ -1,8 +1,8 @@
import json
from init import db, Meals#, scheduler
import datetime
import requests
import time
import httpx
nomeal = 'Essen nicht (mehr) verfügbar'
@ -26,7 +26,7 @@ def getMeals(day: datetime):
return getMealsFromAPI(day, dbentry=True)
def getMealsFromAPI(day: str, dbentry: bool = False):
async def getMealsFromAPI(day: str, dbentry: bool = False):
"""
Fragt die Mensa-API nach den Mahlzeiten eines Tages ab. \n
Wenn dbentry: Schreibt die Ergebnisse in die Datenbank. \n
@ -35,46 +35,46 @@ def getMealsFromAPI(day: str, dbentry: bool = False):
:param dbentry:
:return [Name1, Name2, ...]:
"""
url = "https://dh-api.paulmartin.cloud/plans/" + day + "?canteens=erzberger"
response = requests.request("GET", url)
response = response.content
jres = json.loads(response.decode("utf-8"))
essen = []
try:
num = len(jres["data"][0]["lines"])
for i in range(num):
try:
jsmeal = jres["data"][0]["lines"][i]["meals"]
cont = True
except IndexError:
essen = []
cont = False
if cont:
for e in range(len(jsmeal)):
ji = jsmeal[e]
name = ji["name"]
if pricetofloat(ji["price"]) >= 1.1:
vegan = ji["classifiers"].count("VG") == 1
schwein = ji["classifiers"].count("S") == 1
if vegan:
veget = True
else:
veget = ji["classifiers"].count("VEG") == 1
if veget:
if name.count("Reibekäse") > 0:
vegan = True
async with httpx.AsyncClient() as s:
response = await s.get(url=f"https://dh-api.paulmartin.cloud/plans/{day}?canteens=erzberger")
response = response.content
jres = json.loads(response.decode("utf-8"))
essen = []
try:
num = len(jres["data"][0]["lines"])
for i in range(num):
try:
jsmeal = jres["data"][0]["lines"][i]["meals"]
cont = True
except IndexError:
essen = []
cont = False
if cont:
for e in range(len(jsmeal)):
ji = jsmeal[e]
name = ji["name"]
if pricetofloat(ji["price"]) >= 1.1:
vegan = ji["classifiers"].count("VG") == 1
schwein = ji["classifiers"].count("S") == 1
if vegan:
veget = True
else:
veget = ji["classifiers"].count("VEG") == 1
if veget:
if name.count("Reibekäse") > 0:
vegan = True
essen += [name]
if dbentry:
mid = int(time.time() * 1000) % 100000
neu = Meals(date=day, name=name, id=mid, vegan=vegan, vegetarian=veget, schwein=schwein)
db.session.add(neu)
db.session.commit()
if not essen:
essen += [name]
if dbentry:
mid = int(time.time() * 1000) % 100000
neu = Meals(date=day, name=name, id=mid, vegan=vegan, vegetarian=veget, schwein=schwein)
db.session.add(neu)
db.session.commit()
if not essen:
essen = [nomeal]
except KeyError:
essen = [nomeal]
except KeyError:
essen = [nomeal]
return essen
return essen
def pricetofloat(price: str):