Wetterabfrage funktoiniert mit deutschen einwörtigen Stadtnamen

This commit is contained in:
2026-01-19 00:03:37 +01:00
parent 4a4bd6f286
commit 7e782e7a45
3 changed files with 89 additions and 1 deletions

Binary file not shown.

11
main.py
View File

@@ -6,6 +6,7 @@ import os
import subprocess import subprocess
import sounddevice as sd import sounddevice as sd
import re import re
import asyncio
#test #test
@@ -102,9 +103,17 @@ def detect_intent(text):
# SKILLS # SKILLS
# ========================= # =========================
from weather_jetzt import get_weather_for_location
def weather_skill(slots): def weather_skill(slots):
location = slots["location"] location = slots["location"]
return f"Das Wetter in {location} ist sonnig bei 20 Grad." result = asyncio.run(get_weather_for_location(location))
if result:
return f"Aktuell sind es in {result['location']} {result['temperatur']} Grad und die Wetterlage sieht {result['wetterlage']} aus."
else:
return f"Keine Wetterdaten verfügbar"
#return f"Das Wetter in {location} ist sonnig bei 20 Grad."
def timer_skill(slots): def timer_skill(slots):
duration = slots["duration"] duration = slots["duration"]

79
weather_jetzt.py Normal file
View File

@@ -0,0 +1,79 @@
# Import the module.
import python_weather
import asyncio
from datetime import date, datetime, timedelta
async def get_weather_for_location(location: str):
now = datetime.now().time()
today = date.today()
# Declare the client. The measuring unit used defaults to the metric system (celcius, km/h, etc.)
async with python_weather.Client(unit=python_weather.METRIC) as client:
# Fetch a weather forecast from a city.
weather = await client.get(location) ##e. g. Brackenheim
# weather ist dein Forecast-Objekt von python_weather
today_forecast = next(
(daily for daily in weather if daily.date == today),
None
)
def closest_hourly(hourlies, now_time):
closest = None
min_diff = None
for hourly in hourlies:
# Differenz zwischen aktueller Uhrzeit und Forecast-Stunde
diff = abs(
datetime.combine(date.today(), hourly.time) -
datetime.combine(date.today(), now_time)
)
if min_diff is None or diff < min_diff:
min_diff = diff
closest = hourly
return closest
current_hourly = closest_hourly(today_forecast.hourly_forecasts, now)
KIND_TO_DE = {
"SUNNY": "sonnig",
"PARTLY_CLOUDY": "teilweise bewölkt",
"CLOUDY": "bewölkt",
"VERY_CLOUDY": "stark bewölkt",
"FOG": "nebelig",
"LIGHT_SHOWERS": "nach leichtem Regen",
"LIGHT_SLEET_SHOWERS": "nach leichtem Schneeregen",
"LIGHT_SLEET": "nach leichtem Schneeregen",
"THUNDERY_SHOWERS": "nach Gewitterregen",
"LIGHT_SNOW": "nach leichtem Schneefall",
"HEAVY_SNOW": "nach starkem Schneefall",
"LIGHT_RAIN": "nach leichtem Regen",
"HEAVY_SHOWERS": "nach starken Schauer",
"HEAVY_RAIN": "nach starkem Regen",
"LIGHT_SNOW_SHOWERS": "nach leichten Schneeschauern",
"HEAVY_SNOW_SHOWERS": "nach starken Schneeschauern",
"THUNDERY_HEAVY_RAIN": "nach starken Gewitterregen",
"THUNDERY_SNOW_SHOWERS": "nach starken Gewitterschnee",
}
wetterlage = KIND_TO_DE.get(current_hourly.kind.name, "unbegständig")
temperatur = current_hourly.temperature
return {
"location": weather.location,
"temperatur": temperatur,
"wetterlage": wetterlage
}
#print(f"Aktuell sind es in {ort} {temperatur} Grad und die Wetterlage sieht {wetterlage} aus.")
#if __name__ == '__main__':
# asyncio.run(main())