Add rain info on current day implements #2

This commit is contained in:
2019-09-25 09:18:50 +02:00
parent 910f87a2a1
commit 20f2586565

View File

@ -21,6 +21,12 @@ class WeatherToFreemobile():
locale.setlocale(locale.LC_TIME, self.config['locale']) locale.setlocale(locale.LC_TIME, self.config['locale'])
except locale.Error: except locale.Error:
logging.warning('Error setting locale {}'.format(self.config['locale'])) 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): def send_sms_to_freemobile(self, message):
""" """
@ -43,17 +49,12 @@ class WeatherToFreemobile():
def get_weather(self): def get_weather(self):
""" """
Gets the current weather from OpenWeatherMap Gets the weather forecast from OpenWeatherMap
""" """
city = self.config['openweathermap_city'] city = self.config['openweathermap_city']
apikey = self.config['openweathermap_apikey']
apilanguage = self.config['openweathermap_language']
number_of_days = self.config['number_of_days'] number_of_days = self.config['number_of_days']
logging.info('Opening OpenWeatherMap API') fc = self.owm.daily_forecast(city,limit=number_of_days+1)
owm = pyowm.OWM(apikey,language=apilanguage)
fc = owm.daily_forecast(city,limit=number_of_days+1)
f = fc.get_forecast() f = fc.get_forecast()
return_message=[] return_message=[]
@ -73,23 +74,45 @@ class WeatherToFreemobile():
logging.info('Skipped {} (cf. API Bug)'.format(weather_date)) logging.info('Skipped {} (cf. API Bug)'.format(weather_date))
else: else:
temp = weather.get_temperature(unit='celsius') temp = weather.get_temperature(unit='celsius')
rain = weather.get_rain().get('all',0)
return_message.append( return_message.append(
'{} : {} (min {}ºC, max {}ºC, rain:{}mm)'.format( '{}: {} (min {}ºC, max {}ºC, rain:{}mm)'.format(
weather_date.strftime('%A %d').title(), weather_date.strftime('%A %d').title(),
weather.get_detailed_status(), weather.get_detailed_status(),
round(float(temp['min'])), round(float(temp['min'])),
round(float(temp['max'])), 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)) logging.info("Got the following weather: {}".format(return_message))
return "\n".join(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__": if __name__ == "__main__":
logging.basicConfig( logging.basicConfig(
level=logging.INFO, level=logging.WARNING,
format=' %(asctime)s - %(levelname)s - %(message)s' format=' %(asctime)s - %(levelname)s - %(message)s'
) )
if len(sys.argv) > 1: if len(sys.argv) > 1: