modified: app.py

This commit is contained in:
SimolZimol
2024-09-10 15:21:00 +02:00
parent e10eaa60ba
commit ee90b9a008

49
app.py
View File

@@ -21,37 +21,30 @@ def load_weather_data():
# Extrahieren der relevanten Wetterdaten
weather_data = []
# Annahme: Die Daten enthalten eine Liste von "messages"
for message in data.get("messages", []):
if isinstance(message, list):
# Gehe die Wetterstationen durch, die in der Liste enthalten sind
for station_data in message[1:]:
station_info = {}
if isinstance(station_data, list):
for info in station_data:
# Stelle sicher, dass info ein Dictionary ist
if isinstance(info, dict):
# Extrahiere die notwendigen Wetterinformationen
if info.get("key") == "stationNumber":
station_info["stationNumber"] = info.get("value")
elif info.get("key") == "stationOrSiteName":
station_info["stationName"] = info.get("value")
elif info.get("key") == "airTemperature":
station_info["temperature"] = round(info.get("value") - 273.15, 2) # Umrechnung von K in °C
elif info.get("key") == "windSpeed":
station_info["windSpeed"] = info.get("value")
elif info.get("key") == "pressureReducedToMeanSeaLevel":
station_info["pressure"] = info.get("value")
# Beispielhafte Iteration über die JSON-Daten (anpassen, wenn die Struktur bekannt ist)
for message in data["messages"]:
# Gehe tiefer in die Struktur, je nachdem wie die Daten aufgebaut sind
for station_data in message[1:]:
station_info = {}
if isinstance(station_data, list):
for info in station_data:
if isinstance(info, dict) and "key" in info:
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"]
if "stationNumber" in station_info:
weather_data.append(station_info)
# Füge nur vollständige Stationsinformationen hinzu
if station_info:
weather_data.append(station_info)
# Sortiere die Daten nach stationNumber
weather_data.sort(key=lambda x: x.get("stationNumber"))
return weather_data
# Flask Route für die Hauptseite
@app.route('/')
def index():