diff --git a/weather_to_freemobile.py b/weather_to_freemobile.py index d74f822..7b83f4d 100755 --- a/weather_to_freemobile.py +++ b/weather_to_freemobile.py @@ -21,6 +21,12 @@ class WeatherToFreemobile(): locale.setlocale(locale.LC_TIME, self.config['locale']) except locale.Error: logging.warning('Error setting locale {}'.format(self.config['locale'])) + logging.info('Opening OpenWeatherMap API') + + apikey = self.config['openweathermap_apikey'] + apilanguage = self.config['openweathermap_language'] + self.owm = pyowm.OWM(apikey,language=apilanguage) + def send_sms_to_freemobile(self, message): """ @@ -43,17 +49,12 @@ class WeatherToFreemobile(): def get_weather(self): """ - Gets the current weather from OpenWeatherMap + Gets the weather forecast from OpenWeatherMap """ city = self.config['openweathermap_city'] - apikey = self.config['openweathermap_apikey'] - apilanguage = self.config['openweathermap_language'] number_of_days = self.config['number_of_days'] - logging.info('Opening OpenWeatherMap API') - owm = pyowm.OWM(apikey,language=apilanguage) - - fc = owm.daily_forecast(city,limit=number_of_days+1) + fc = self.owm.daily_forecast(city,limit=number_of_days+1) f = fc.get_forecast() return_message=[] @@ -73,23 +74,45 @@ class WeatherToFreemobile(): logging.info('Skipped {} (cf. API Bug)'.format(weather_date)) else: temp = weather.get_temperature(unit='celsius') + rain = weather.get_rain().get('all',0) return_message.append( - '{} : {} (min {}ºC, max {}ºC, rain:{}mm)'.format( + '{}: {} (min {}ºC, max {}ºC, rain:{}mm)'.format( weather_date.strftime('%A %d').title(), weather.get_detailed_status(), round(float(temp['min'])), round(float(temp['max'])), - weather.get_rain().get('all',0) + rain ) ) + if( rain and date_diff == datetime.timedelta(0) ): + return_message.append(self.get_rain()) logging.info("Got the following weather: {}".format(return_message)) return "\n".join(return_message) + + def get_rain(self): + """ + Gets the rain forecast from OpenWeatherMap + """ + city = self.config['openweathermap_city'] + fc = self.owm.three_hours_forecast(city) + f = fc.get_forecast() + + return_message=[] + for weather in f: + weather_date = weather.get_reference_time('date') + if( weather_date.date() != datetime.date.today() ): + break + return_message.append(" - {:2d}h : {}mm".format( + weather_date.hour, + round(float(weather.get_rain()['3h']),1) + )) + return "\n".join(return_message) if __name__ == "__main__": logging.basicConfig( - level=logging.INFO, + level=logging.WARNING, format=' %(asctime)s - %(levelname)s - %(message)s' ) if len(sys.argv) > 1: