modified: app.py
This commit is contained in:
51
app.py
51
app.py
@@ -8,7 +8,6 @@ app = Flask(__name__)
|
||||
# DWD-Daten URL
|
||||
DWD_URL = "https://opendata.dwd.de/weather/weather_reports/synoptic/germany/json/Z__C_EDZW_latest_bda01%2Csynop_bufr_GER_999999_999999__MW_XXX.json.bz2"
|
||||
|
||||
# Funktion zum Laden und Entpacken der Wetterdaten
|
||||
def load_weather_data():
|
||||
# Hole die komprimierten Daten von der DWD-URL
|
||||
response = requests.get(DWD_URL)
|
||||
@@ -24,37 +23,37 @@ def load_weather_data():
|
||||
|
||||
for message in data["messages"]:
|
||||
if isinstance(message, list):
|
||||
# message[1:] enthält die Stationsdaten
|
||||
for station_data in message[1:]:
|
||||
for subset in message[1:]: # Ignoriere den ersten Header-Teil
|
||||
station_info = {}
|
||||
|
||||
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:
|
||||
if isinstance(subset, list):
|
||||
for item in subset:
|
||||
# Sicherstellen, dass wir ein Dictionary haben
|
||||
if isinstance(item, dict):
|
||||
key = item.get("key")
|
||||
value = item.get("value")
|
||||
# Hier relevante Daten extrahieren und speichern
|
||||
if key == "stationNumber":
|
||||
station_info["stationNumber"] = value
|
||||
elif key == "stationOrSiteName":
|
||||
station_info["stationName"] = value
|
||||
elif key == "airTemperature":
|
||||
station_info["temperature"] = round(value - 273.15, 2) # Umrechnung K -> °C
|
||||
elif key == "relativeHumidity":
|
||||
station_info["humidity"] = value
|
||||
elif key == "pressureReducedToMeanSeaLevel":
|
||||
station_info["pressure"] = value
|
||||
elif key == "windSpeed":
|
||||
station_info["windSpeed"] = value
|
||||
# Füge nur vollständige Einträge hinzu
|
||||
if "stationNumber" in station_info and station_info:
|
||||
weather_data.append(station_info)
|
||||
|
||||
# Sortiere nach stationNumber, falls vorhanden
|
||||
weather_data.sort(key=lambda x: x.get("stationNumber", ""))
|
||||
|
||||
# Sortiere nach stationNumber
|
||||
weather_data.sort(key=lambda x: x["stationNumber"] if "stationNumber" in x else None)
|
||||
|
||||
return weather_data
|
||||
|
||||
|
||||
|
||||
# Flask Route für die Hauptseite
|
||||
@app.route('/')
|
||||
def index():
|
||||
|
||||
Reference in New Issue
Block a user