Add an option to configure the number of days to display

This commit is contained in:
2019-09-22 08:28:00 +02:00
parent 4dd9bd5809
commit 9558df0887
2 changed files with 13 additions and 9 deletions

View File

@ -4,5 +4,6 @@
"openweathermap_city":"Paris,FR", "openweathermap_city":"Paris,FR",
"openweathermap_apikey":"", "openweathermap_apikey":"",
"openweathermap_language":"fr", "openweathermap_language":"fr",
"locale":"fr_FR.utf8" "locale":"fr_FR.utf8",
"number_of_days":2
} }

View File

@ -47,24 +47,27 @@ class WeatherToFreemobile():
city = self.config['openweathermap_city'] city = self.config['openweathermap_city']
apikey = self.config['openweathermap_apikey'] apikey = self.config['openweathermap_apikey']
apilanguage = self.config['openweathermap_language'] apilanguage = self.config['openweathermap_language']
number_of_days = self.config['number_of_days']
logging.info('Opening OpenWeatherMap API') logging.info('Opening OpenWeatherMap API')
owm = pyowm.OWM(apikey,language=apilanguage) owm = pyowm.OWM(apikey,language=apilanguage)
fc = owm.daily_forecast(city,limit=1) fc = owm.daily_forecast(city,limit=number_of_days)
f = fc.get_forecast() f = fc.get_forecast()
return_message="" return_message=[]
for weather in f: for weather in f:
temp = weather.get_temperature(unit='celsius') temp = weather.get_temperature(unit='celsius')
return_message += '{} : {} (min {}ºC, max {}ºC, rain:{}mm)'.format( return_message.append(
'{} : {} (min {}ºC, max {}ºC, rain:{}mm)'.format(
weather.get_reference_time('date').strftime('%A %d').title(), weather.get_reference_time('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) weather.get_rain().get('all',0)
)
) )
logging.info("Got the following weather: {}".format(return_message)) logging.info("Got the following weather: {}".format(return_message))
return return_message return "\n".join(return_message)
if __name__ == "__main__": if __name__ == "__main__":
logging.basicConfig(level=logging.WARNING, format=' %(asctime)s - %(levelname)s - %(message)s') logging.basicConfig(level=logging.WARNING, format=' %(asctime)s - %(levelname)s - %(message)s')