Return CURRENT event if asked for montagsküche

This commit is contained in:
clerie 2020-02-11 00:04:52 +01:00
parent 4718cc8d7e
commit 1901d76e4d

34
app.py
View File

@ -5,6 +5,7 @@ import os
from flask import Flask,redirect from flask import Flask,redirect
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
import requests import requests
from datetime import date
app = Flask(__name__) app = Flask(__name__)
@ -113,15 +114,40 @@ def bi_club():
# Montagsküche # Montagsküche
@app.route('/montag/') @app.route('/montag/')
def montagskueche(): def montagskueche():
# Get the startpage of bi club
try: try:
posts = requests.get("https://www.bi-club.de/tags/montagskueche") current_event = requests.get("https://www.bi-club.de/")
soup = BeautifulSoup(posts.text, 'html.parser') current_soup = BeautifulSoup(current_event.text, 'html.parser')
link = soup.find(id='main').article.header.h2.a.get('href') # Get an array of all events
return redirect("https://www.bi-club.de" + link, code=302) events = current_soup.find(id='main').div.div.div.div.div.div.div.div.find_all('div', recursive=False)
except: except:
return redirect("https://www.bi-club.de/tags/montagskueche", code=302) return redirect("https://www.bi-club.de/tags/montagskueche", code=302)
# Pseudo event
next_montagskueche = date.max
next_montagskueche_url = ""
for event in events:
# Get interesting information
content = event.div.div.div.div.find_all('div', recursive=False)[1]
# Get title
title = content.h2.a.text
# Get date
date_string = content.div.div.span.text.split(' - ')[1].split('.')
date_object = date(int(date_string[2]), int(date_string[1]), int(date_string[0]))
# Get url
url = content.h2.a.get('href')
# Select the event
if "Montagsküche" in title and date_object >= date.today() and date_object < next_montagskueche:
next_montagskueche = date_object
next_montagskueche_url = url
# Answer
if next_montagskueche_url != "":
return redirect(next_montagskueche_url, code=302)
else:
return redirect("https://www.bi-club.de/tags/montagskueche", code=302)
######################################################################################################################## ########################################################################################################################
# Stura # Stura