Hafas-Depature-Board-DB/Hafas_Main_web.py

51 lines
1.7 KiB
Python
Raw Normal View History

2023-10-12 18:52:17 +02:00
from flask import Flask, render_template, request, make_response, jsonify
import requests
2023-10-09 17:30:29 +02:00
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def index():
2023-10-12 18:52:17 +02:00
station_name = request.cookies.get('station_name', '') # Versuche, den Stationsnamen aus dem Cookie zu lesen
2023-10-09 17:30:29 +02:00
departures = []
if request.method == 'POST':
2023-10-12 18:52:17 +02:00
station_name = request.form.get('station_name', '')
if station_name:
url = f"https://dbf.finalrewind.org/{station_name}.json?version=3"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
departures = data.get("departures", [])
resp = make_response(render_template('index.html', station_name=station_name, departures=departures))
resp.set_cookie('station_name', station_name)
return resp
2023-10-18 15:04:53 +02:00
if station_name:
2023-10-12 18:52:17 +02:00
url = f"https://dbf.finalrewind.org/{station_name}.json?version=3"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
departures = data.get("departures", [])
return render_template('index.html', station_name=station_name, departures=departures)
@app.route('/update_departures', methods=['GET'])
def update_departures():
station_name = request.cookies.get('station_name', '')
departures = []
2023-10-09 17:30:29 +02:00
2023-10-12 18:52:17 +02:00
if station_name:
url = f"https://dbf.finalrewind.org/{station_name}.json?version=3"
response = requests.get(url)
2023-10-09 17:30:29 +02:00
2023-10-12 18:52:17 +02:00
if response.status_code == 200:
data = response.json()
departures = data.get("departures", [])
2023-10-09 17:30:29 +02:00
2023-10-12 18:52:17 +02:00
return jsonify({'departures': departures})
2023-10-09 17:30:29 +02:00
if __name__ == '__main__':
2023-10-12 19:16:44 +02:00
app.run(debug=False, host='0.0.0.0')