From 5e598f3e4f5fc651b824aecbdaf526ddf5868550 Mon Sep 17 00:00:00 2001 From: JuliusFreudenberger Date: Thu, 24 Sep 2020 20:03:16 +0200 Subject: [PATCH] Added current weather for openweathermap --- weather_openweathermap.py | 46 ++++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/weather_openweathermap.py b/weather_openweathermap.py index 265d0a4..84d2ddf 100644 --- a/weather_openweathermap.py +++ b/weather_openweathermap.py @@ -15,6 +15,39 @@ def __init__(): token = openweathermap_token.read() +class CurrentWeather: + sunrise: datetime + sunset: datetime + temperature: float + feels_like: float + wind_speed: float + rain: float + main: str + description: str + + def __init__(self, json_data: json): + self.sunrise = datetime.fromtimestamp(json_data['sunrise']) + self.sunset = datetime.fromtimestamp(json_data['sunset']) + self.temperature = json_data['temp'] + self.feels_like = json_data['feels_like'] + self.wind_speed = json_data['wind_speed'] + if 'rain' in json_data: + self.rain = json_data['rain']['1h'] + else: + self.rain = -1 + self.main = json_data['weather'][0]['main'] + self.description = json_data['weather'][0]['description'] + + def __str__(self): + string = f"*Now*: {self.main} | {self.temperature:.0f}°C 🧑: {self.feels_like:.0f}°C" + if self.rain != -1: + 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')}" + return string + + class HourlyWeather: time: datetime temperature: float @@ -43,7 +76,7 @@ class HourlyWeather: 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" + string += f" 💨: {3.6 * self.wind_speed:.1f}km/h" return string @@ -80,17 +113,20 @@ class DailyWeather: class TotalWeather: + current_weather: CurrentWeather hourly_weather = [] daily_weather = [] def __init__(self, json_data: json): for single_daily_weather in json_data['daily'][2:]: self.daily_weather.append(DailyWeather(single_daily_weather)) - for single_hourly_weather in json_data['hourly']: + for single_hourly_weather in json_data['hourly'][1:]: self.hourly_weather.append(HourlyWeather(single_hourly_weather)) + self.current_weather = CurrentWeather(json_data['current']) def __str__(self): string = "" + string += (str(self.current_weather) + '\n') short_hourly_weather = self.hourly_weather[:5] + self.hourly_weather[6:20:2] + self.hourly_weather[21::4] day_before = short_hourly_weather[0].time.strftime("%d") for weather in short_hourly_weather: @@ -123,10 +159,8 @@ class City: def query_weather(self): request = requests.get( - "http://api.openweathermap.org/data/2.5/onecall?\ - lang=de&units=metric&exclude=minutely&lat={lat}&lon={lon}&appid={token}".format( - lat=self.lat, - lon=self.lon, token=token)) + f"http://api.openweathermap.org/data/2.5/onecall?\ + lang=de&units=metric&exclude=minutely&lat={self.lat}&lon={self.lon}&appid={token}") if request.status_code != 200: raise ServerCommunicationError self.weather = TotalWeather(request.json())