modified: app.py

This commit is contained in:
SimolZimol
2024-09-10 12:11:08 +02:00
parent 08db5a8a8f
commit e00389682b

43
app.py
View File

@@ -1,7 +1,7 @@
import requests
import bz2
import json
from flask import Flask, jsonify, render_template
from flask import Flask, jsonify
app = Flask(__name__)
@@ -18,52 +18,15 @@ def download_and_extract(url):
else:
return None
# Funktion zum Extrahieren der relevanten Wetterdaten
def extract_weather_data(messages):
weather_info = []
# Iteriere durch jede Nachricht (jeder Block enthält die Daten einer Wetterstation)
for message in messages:
station_info = {}
# Iteriere durch die Schlüssel-Wert-Paare in jeder Nachricht
for item in message:
if isinstance(item, list):
for subitem in item:
if isinstance(subitem, dict):
# Extrahiere die Stationsinformationen
if subitem.get('key') == "stationOrSiteName":
station_info['station'] = subitem.get('value')
elif subitem.get('key') == "airTemperature":
station_info['temperature'] = subitem.get('value') - 273.15 # Umrechnung von Kelvin in Celsius
elif subitem.get('key') == "pressureReducedToMeanSeaLevel":
station_info['pressure'] = subitem.get('value') / 100 # Umrechnung in hPa
elif subitem.get('key') == "windSpeed":
station_info['wind_speed'] = subitem.get('value')
elif subitem.get('key') == "cloudCoverTotal":
station_info['cloud_cover'] = subitem.get('value')
# Wenn wir Informationen für diese Station gefunden haben, fügen wir sie zur Liste hinzu
if station_info:
weather_info.append(station_info)
return weather_info
# API-Route zum Bereitstellen der Wetterdaten
# Flask-Route, die die Wetterdaten bereitstellt
@app.route('/weather')
def weather():
data = download_and_extract(url)
if data:
weather_data = json.loads(data)
extracted_data = extract_weather_data(weather_data['messages'])
return jsonify(extracted_data)
return jsonify(weather_data)
else:
return jsonify({"error": "Fehler beim Abrufen der Wetterdaten"}), 500
# Route für die Website
@app.route('/')
def index():
return render_template('index.html')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)