diff --git a/src/actions.py b/src/actions.py index a5ea779..6d348c9 100644 --- a/src/actions.py +++ b/src/actions.py @@ -1,4 +1,5 @@ from common import * +from weather_codes import weather_codes async def spotify_action(text: str): client_id = os.getenv('SPOTIFY_CLIENT_ID') @@ -78,11 +79,10 @@ async def open_weather_action(text: str): response = await session.get(f"https://api.open-meteo.com/v1/forecast?latitude={coords.get('lat')}&longitude={coords.get('lon')}¤t_weather=true&temperature_unit=fahrenheit") if response.status == 200: json_response = await response.json() - weather = json_response.get('current_weather').get('weathercode') + weather_code = json_response.get('current_weather').get('weathercode') temp = json_response.get('current_weather').get('temperature') - return f"It is currently {round(float(temp))} degrees with weather code {weather} in {city}." - else: - raise Exception(f"Received a {response.status} status code from both OpenWeather and Open-Meteo. {response.content.decode()}") + weather_description = weather_codes[str(weather_code)]['day']['description'] if datetime.now().hour < 18 else weather_codes[str(weather_code)]['night']['description'] + return f"It is currently {round(float(temp))} degrees and {weather_description.lower()} in {city}." # Weather forecast else: @@ -118,17 +118,20 @@ async def open_weather_action(text: str): json_response = await response.json() forecast = [] for day in json_response.get('daily'): + day_weather_code = day.get('weathercode') + day_weather_description = weather_codes[str(day_weather_code)]['day']['description'] if datetime.now().hour < 18 else weather_codes[str(day_weather_code)]['night']['description'] forecast.append({ 'temp_max': day.get('temperature_2m_max'), 'temp_min': day.get('temperature_2m_min'), + 'weather_description': day_weather_description, 'date': day.get('time') }) tomorrow_forecast = list(filter(lambda x: x.get('date') == tomorrow.strftime('%Y-%m-%d'), forecast))[0] speech_responses = [] - speech_responses.append(f"Tomorrow, it will be between {tomorrow_forecast.get('temp_min')}\u00B0F and {tomorrow_forecast.get('temp_max')}\u00B0F in {city}.") + speech_responses.append(f"Tomorrow, it will be between {tomorrow_forecast.get('temp_min')}\u00B0F and {tomorrow_forecast.get('temp_max')}\u00B0F and {tomorrow_forecast.get('weather_description').lower()} in {city}.") for day in forecast: if day.get('date') != tomorrow.strftime('%Y-%m-%d'): - speech_responses.append(f"On {day.get('date')}, it will be between {day.get('temp_min')}\u00B0F and {day.get('temp_max')}\u00B0F in {city}.") + speech_responses.append(f"On {day.get('date')}, it will be between {day.get('temp_min')}\u00B0F and {day.get('temp_max')}\u00B0F and {day.get('weather_description').lower()} in {city}.") return ' '.join(speech_responses) else: @@ -153,12 +156,10 @@ async def open_weather_action(text: str): response = await session.get(f"https://api.open-meteo.com/v1/forecast?latitude={coords.get('lat')}&longitude={coords.get('lon')}¤t_weather=true&temperature_unit=fahrenheit") if response.status == 200: json_response = await response.json() - weather = json_response.get('current_weather').get('weathercode') + weather_code = json_response.get('current_weather').get('weathercode') temp = json_response.get('current_weather').get('temperature') - return f"It is currently {round(float(temp))} degrees with weather code {weather} in your location." - else: - content = await response.content.read() - raise Exception(f"Received a {response.status} status code from both OpenWeather and Open-Meteo. {content.decode()}") + weather_description = weather_codes[str(weather_code)]['day']['description'] if datetime.now().hour < 18 else weather_codes[str(weather_code)]['night']['description'] + return f"It is currently {round(float(temp))} degrees and {weather_description.lower()} in {city}." raise Exception("No Open Weather API key found. Please enter your API key for Open Weather in the web interface or try reconnecting the service.") diff --git a/src/weather_codes.py b/src/weather_codes.py new file mode 100644 index 0000000..01d3f35 --- /dev/null +++ b/src/weather_codes.py @@ -0,0 +1,282 @@ +weather_codes = { + "0":{ + "day":{ + "description":"Sunny", + "image":"http://openweathermap.org/img/wn/01d@2x.png" + }, + "night":{ + "description":"Clear", + "image":"http://openweathermap.org/img/wn/01n@2x.png" + } + }, + "1":{ + "day":{ + "description":"Mainly Sunny", + "image":"http://openweathermap.org/img/wn/01d@2x.png" + }, + "night":{ + "description":"Mainly Clear", + "image":"http://openweathermap.org/img/wn/01n@2x.png" + } + }, + "2":{ + "day":{ + "description":"Partly Cloudy", + "image":"http://openweathermap.org/img/wn/02d@2x.png" + }, + "night":{ + "description":"Partly Cloudy", + "image":"http://openweathermap.org/img/wn/02n@2x.png" + } + }, + "3":{ + "day":{ + "description":"Cloudy", + "image":"http://openweathermap.org/img/wn/03d@2x.png" + }, + "night":{ + "description":"Cloudy", + "image":"http://openweathermap.org/img/wn/03n@2x.png" + } + }, + "45":{ + "day":{ + "description":"Foggy", + "image":"http://openweathermap.org/img/wn/50d@2x.png" + }, + "night":{ + "description":"Foggy", + "image":"http://openweathermap.org/img/wn/50n@2x.png" + } + }, + "48":{ + "day":{ + "description":"Rime Fog", + "image":"http://openweathermap.org/img/wn/50d@2x.png" + }, + "night":{ + "description":"Rime Fog", + "image":"http://openweathermap.org/img/wn/50n@2x.png" + } + }, + "51":{ + "day":{ + "description":"Light Drizzle", + "image":"http://openweathermap.org/img/wn/09d@2x.png" + }, + "night":{ + "description":"Light Drizzle", + "image":"http://openweathermap.org/img/wn/09n@2x.png" + } + }, + "53":{ + "day":{ + "description":"Drizzle", + "image":"http://openweathermap.org/img/wn/09d@2x.png" + }, + "night":{ + "description":"Drizzle", + "image":"http://openweathermap.org/img/wn/09n@2x.png" + } + }, + "55":{ + "day":{ + "description":"Heavy Drizzle", + "image":"http://openweathermap.org/img/wn/09d@2x.png" + }, + "night":{ + "description":"Heavy Drizzle", + "image":"http://openweathermap.org/img/wn/09n@2x.png" + } + }, + "56":{ + "day":{ + "description":"Light Freezing Drizzle", + "image":"http://openweathermap.org/img/wn/09d@2x.png" + }, + "night":{ + "description":"Light Freezing Drizzle", + "image":"http://openweathermap.org/img/wn/09n@2x.png" + } + }, + "57":{ + "day":{ + "description":"Freezing Drizzle", + "image":"http://openweathermap.org/img/wn/09d@2x.png" + }, + "night":{ + "description":"Freezing Drizzle", + "image":"http://openweathermap.org/img/wn/09n@2x.png" + } + }, + "61":{ + "day":{ + "description":"Light Rain", + "image":"http://openweathermap.org/img/wn/10d@2x.png" + }, + "night":{ + "description":"Light Rain", + "image":"http://openweathermap.org/img/wn/10n@2x.png" + } + }, + "63":{ + "day":{ + "description":"Rain", + "image":"http://openweathermap.org/img/wn/10d@2x.png" + }, + "night":{ + "description":"Rain", + "image":"http://openweathermap.org/img/wn/10n@2x.png" + } + }, + "65":{ + "day":{ + "description":"Heavy Rain", + "image":"http://openweathermap.org/img/wn/10d@2x.png" + }, + "night":{ + "description":"Heavy Rain", + "image":"http://openweathermap.org/img/wn/10n@2x.png" + } + }, + "66":{ + "day":{ + "description":"Light Freezing Rain", + "image":"http://openweathermap.org/img/wn/10d@2x.png" + }, + "night":{ + "description":"Light Freezing Rain", + "image":"http://openweathermap.org/img/wn/10n@2x.png" + } + }, + "67":{ + "day":{ + "description":"Freezing Rain", + "image":"http://openweathermap.org/img/wn/10d@2x.png" + }, + "night":{ + "description":"Freezing Rain", + "image":"http://openweathermap.org/img/wn/10n@2x.png" + } + }, + "71":{ + "day":{ + "description":"Light Snow", + "image":"http://openweathermap.org/img/wn/13d@2x.png" + }, + "night":{ + "description":"Light Snow", + "image":"http://openweathermap.org/img/wn/13n@2x.png" + } + }, + "73":{ + "day":{ + "description":"Snow", + "image":"http://openweathermap.org/img/wn/13d@2x.png" + }, + "night":{ + "description":"Snow", + "image":"http://openweathermap.org/img/wn/13n@2x.png" + } + }, + "75":{ + "day":{ + "description":"Heavy Snow", + "image":"http://openweathermap.org/img/wn/13d@2x.png" + }, + "night":{ + "description":"Heavy Snow", + "image":"http://openweathermap.org/img/wn/13n@2x.png" + } + }, + "77":{ + "day":{ + "description":"Snow Grains", + "image":"http://openweathermap.org/img/wn/13d@2x.png" + }, + "night":{ + "description":"Snow Grains", + "image":"http://openweathermap.org/img/wn/13n@2x.png" + } + }, + "80":{ + "day":{ + "description":"Light Showers", + "image":"http://openweathermap.org/img/wn/09d@2x.png" + }, + "night":{ + "description":"Light Showers", + "image":"http://openweathermap.org/img/wn/09n@2x.png" + } + }, + "81":{ + "day":{ + "description":"Showers", + "image":"http://openweathermap.org/img/wn/09d@2x.png" + }, + "night":{ + "description":"Showers", + "image":"http://openweathermap.org/img/wn/09n@2x.png" + } + }, + "82":{ + "day":{ + "description":"Heavy Showers", + "image":"http://openweathermap.org/img/wn/09d@2x.png" + }, + "night":{ + "description":"Heavy Showers", + "image":"http://openweathermap.org/img/wn/09n@2x.png" + } + }, + "85":{ + "day":{ + "description":"Light Snow Showers", + "image":"http://openweathermap.org/img/wn/13d@2x.png" + }, + "night":{ + "description":"Light Snow Showers", + "image":"http://openweathermap.org/img/wn/13n@2x.png" + } + }, + "86":{ + "day":{ + "description":"Snow Showers", + "image":"http://openweathermap.org/img/wn/13d@2x.png" + }, + "night":{ + "description":"Snow Showers", + "image":"http://openweathermap.org/img/wn/13n@2x.png" + } + }, + "95":{ + "day":{ + "description":"Thunderstorm", + "image":"http://openweathermap.org/img/wn/11d@2x.png" + }, + "night":{ + "description":"Thunderstorm", + "image":"http://openweathermap.org/img/wn/11n@2x.png" + } + }, + "96":{ + "day":{ + "description":"Light Thunderstorms With Hail", + "image":"http://openweathermap.org/img/wn/11d@2x.png" + }, + "night":{ + "description":"Light Thunderstorms With Hail", + "image":"http://openweathermap.org/img/wn/11n@2x.png" + } + }, + "99":{ + "day":{ + "description":"Thunderstorm With Hail", + "image":"http://openweathermap.org/img/wn/11d@2x.png" + }, + "night":{ + "description":"Thunderstorm With Hail", + "image":"http://openweathermap.org/img/wn/11n@2x.png" + } + } +} \ No newline at end of file