From f5c7bf8e9ec51c49e3fc776f2539fe5266444b7c Mon Sep 17 00:00:00 2001 From: JuliusFreudenberger Date: Tue, 1 Dec 2020 08:14:30 +0100 Subject: [PATCH] Add snow emoji for precipitation when it's snowing Also added dict for all emojis --- weather_openweathermap.py | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/weather_openweathermap.py b/weather_openweathermap.py index 6c9afc0..266a4db 100644 --- a/weather_openweathermap.py +++ b/weather_openweathermap.py @@ -8,6 +8,8 @@ from exceptions import ServerCommunicationError, NoArgError, WeatherStationNotFo token: str +EMOJIS = {'feels_like': '🧑', 'rain': '☔', 'snow': '🌨️', 'wind': '💨', 'sun': '☀️'} + def __init__(): with open("openweathermap_token") as openweathermap_token: @@ -39,12 +41,16 @@ class CurrentWeather: self.description = json_data['weather'][0]['description'] def __str__(self): - string = f"*Now*: {self.main} | {self.temperature:.0f}°C 🧑: {self.feels_like:.0f}°C" + string = f"*Now*: {self.main} | {self.temperature:.0f}°C {EMOJIS['feels_like']}: {self.feels_like:.0f}°C " if self.rain != -1: - string += f" ☔: {self.rain:.0f}mm" + if self.main == 'Snow': + string += EMOJIS['snow'] + else: + string += EMOJIS['rain'] + string += f": {self.rain:.0f}mm" - string += f" 💨: {3.6 * self.wind_speed:.1f}km/h" \ - f" ☀️: {self.sunrise.strftime('%H:%M')} - {self.sunset.strftime('%H:%M')}" + string += f" {EMOJIS['wind']}: {3.6 * self.wind_speed:.1f}km/h" \ + f" {EMOJIS['sun']}: {self.sunrise.strftime('%H:%M')} - {self.sunset.strftime('%H:%M')}" return string @@ -72,11 +78,16 @@ class HourlyWeather: self.description = json_data['weather'][0]['description'] def __str__(self): - string = f"*{self.time.strftime('%Hh')}*: {self.main} | {self.temperature:.0f}°C 🧑: {self.feels_like:.0f}°C" + string = f"*{self.time.strftime('%Hh')}*: {self.main} | {self.temperature:.0f}°C" \ + f" {EMOJIS['feels_like']}: {self.feels_like:.0f}°C " if self.probability_of_precipitation != 0: - string += f" ☔: {100 * self.probability_of_precipitation:.0f}% {self.rain}mm" + if self.main == 'Snow': + string += EMOJIS['snow'] + else: + string += EMOJIS['rain'] + string += f": {100 * self.probability_of_precipitation:.0f}% {self.rain}mm" - string += f" 💨: {3.6 * self.wind_speed:.1f}km/h" + string += f" {EMOJIS['wind']}: {3.6 * self.wind_speed:.1f}km/h" return string @@ -105,10 +116,14 @@ class DailyWeather: def __str__(self): string = f"*{self.time.strftime('%d')}*: {self.main} | {self.temp['min']:.0f}°C - {self.temp['day']:.0f}°C" \ - f" - {self.temp['max']:.0f}°C 🧑: {self.feels_like:.0f}°C" + f" - {self.temp['max']:.0f}°C {EMOJIS['feels_like']}: {self.feels_like:.0f}°C " if self.probability_of_precipitation != 0: - string += f" ☔: {100 * self.probability_of_precipitation:.0f}% {self.rain}mm" - string += f" 💨: {3.6 * self.wind_speed:.1f}km/h" + if self.main == 'Snow': + string += EMOJIS['snow'] + else: + string += EMOJIS['rain'] + string += f": {100 * self.probability_of_precipitation:.0f}% {self.rain}mm" + string += f" {EMOJIS['wind']}: {3.6 * self.wind_speed:.1f}km/h" return string @@ -221,6 +236,7 @@ def handle_weather(update, context): if requested_city.name == "none": raise WeatherStationNotFoundError requested_city.query_weather() - update.message.reply_text(requested_city.get_weather_str(), disable_notification=True, parse_mode=ParseMode.MARKDOWN) + update.message.reply_text(requested_city.get_weather_str(), disable_notification=True, + parse_mode=ParseMode.MARKDOWN) requested_city.weather.clear()