modified: app.py

This commit is contained in:
SimolZimol
2024-09-10 13:52:30 +02:00
parent d5ef95350d
commit c0c5d40830

44
app.py
View File

@@ -23,36 +23,38 @@ def load_weather_data():
weather_data = []
for message in data["messages"]:
# Jede Nachricht kann mehrere Wetterstationen enthalten
if isinstance(message, list):
for station_data in message:
# message[1:] enthält die Stationsdaten
for station_data in message[1:]:
station_info = {}
# Überprüfe, ob station_data eine Liste ist
if isinstance(station_data, list):
for info in station_data:
# Stelle sicher, dass info ein Dictionary ist
if isinstance(info, dict) and "key" in info:
if info["key"] == "stationNumber":
station_info["stationNumber"] = info.get("value", None)
elif info["key"] == "stationOrSiteName":
station_info["stationName"] = info.get("value", None)
elif info["key"] == "airTemperature":
station_info["temperature"] = round(info.get("value", 0) - 273.15, 2) # Umwandlung von K in °C
elif info["key"] == "windSpeed":
station_info["windSpeed"] = info.get("value", None)
elif info["key"] == "pressureReducedToMeanSeaLevel":
station_info["pressure"] = info.get("value", None)
# Füge nur Daten hinzu, wenn sie vollständig sind
if "stationNumber" in station_info and station_info["stationNumber"] is not None:
if isinstance(station_data, list):
# Gehe durch alle Elemente in station_data
for info in station_data:
if isinstance(info, dict) and "key" in info:
# Extrahiere die relevanten Informationen
if info["key"] == "stationNumber":
station_info["stationNumber"] = info["value"]
elif info["key"] == "stationOrSiteName":
station_info["stationName"] = info["value"]
elif info["key"] == "airTemperature":
station_info["temperature"] = round(info["value"] - 273.15, 2) # Umwandlung von K in °C
elif info["key"] == "windSpeed":
station_info["windSpeed"] = info["value"]
elif info["key"] == "pressureReducedToMeanSeaLevel":
station_info["pressure"] = info["value"]
# Füge nur Daten hinzu, wenn stationNumber existiert
if "stationNumber" in station_info:
weather_data.append(station_info)
# Sortiere nach stationNumber
weather_data.sort(key=lambda x: x.get("stationNumber", 0))
# Sortiere nach stationNumber, falls vorhanden
weather_data.sort(key=lambda x: x.get("stationNumber", ""))
return weather_data
# Flask Route für die Hauptseite
@app.route('/')
def index():