diff --git a/weather_to_freemobile.py b/weather_to_freemobile.py index d28fa08..8312c7b 100755 --- a/weather_to_freemobile.py +++ b/weather_to_freemobile.py @@ -2,6 +2,7 @@ """ A simple script that sends the daily weather to a FreeMobile phone """ +import datetime import json import locale import logging @@ -52,25 +53,42 @@ class WeatherToFreemobile(): logging.info('Opening OpenWeatherMap API') owm = pyowm.OWM(apikey,language=apilanguage) - fc = owm.daily_forecast(city,limit=number_of_days) + fc = owm.daily_forecast(city,limit=number_of_days+1) f = fc.get_forecast() + return_message=[] for weather in f: - temp = weather.get_temperature(unit='celsius') - return_message.append( - '{} : {} (min {}ºC, max {}ºC, rain:{}mm)'.format( - weather.get_reference_time('date').strftime('%A %d').title(), - weather.get_detailed_status(), - round(float(temp['min'])), - round(float(temp['max'])), - weather.get_rain().get('all',0) + weather_date = weather.get_reference_time('date') + + #Workaround API returning yesterday's weather" + #https://openweathermap.desk.com/customer/en/portal/questions/ + #17649060-between-hours-of-12-midnight-and-7am-gmt-we-are-receiving + #-the-wrong-data-for-most-locations + if( abs(weather_date.date()-datetime.date.today()) + < datetime.timedelta(days=number_of_days) + ): + temp = weather.get_temperature(unit='celsius') + return_message.append( + '{} : {} (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) + ) ) - ) + else: + logging.info('Skipped {} (cf. API Bug)'.format(weather_date)) + logging.info("Got the following weather: {}".format(return_message)) return "\n".join(return_message) + 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' + ) if len(sys.argv) > 1: wtf = WeatherToFreemobile(sys.argv[1]) else: