2019-09-24 19:36:43 +02:00
parent 9558df0887
commit b20f9b6cbc

View File

@ -2,6 +2,7 @@
""" """
A simple script that sends the daily weather to a FreeMobile phone A simple script that sends the daily weather to a FreeMobile phone
""" """
import datetime
import json import json
import locale import locale
import logging import logging
@ -52,25 +53,42 @@ class WeatherToFreemobile():
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=number_of_days) fc = owm.daily_forecast(city,limit=number_of_days+1)
f = fc.get_forecast() f = fc.get_forecast()
return_message=[] return_message=[]
for weather in f: for weather in f:
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') temp = weather.get_temperature(unit='celsius')
return_message.append( return_message.append(
'{} : {} (min {}ºC, max {}ºC, rain:{}mm)'.format( '{} : {} (min {}ºC, max {}ºC, rain:{}mm)'.format(
weather.get_reference_time('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) 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)) logging.info("Got the following weather: {}".format(return_message))
return "\n".join(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'
)
if len(sys.argv) > 1: if len(sys.argv) > 1:
wtf = WeatherToFreemobile(sys.argv[1]) wtf = WeatherToFreemobile(sys.argv[1])
else: else: