Skip wrong days in API result, fixes #3
This is actually a bug in the OWM API, see: 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
This commit is contained in:
@ -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:
|
||||||
temp = weather.get_temperature(unit='celsius')
|
weather_date = weather.get_reference_time('date')
|
||||||
return_message.append(
|
|
||||||
'{} : {} (min {}ºC, max {}ºC, rain:{}mm)'.format(
|
#Workaround API returning yesterday's weather"
|
||||||
weather.get_reference_time('date').strftime('%A %d').title(),
|
#https://openweathermap.desk.com/customer/en/portal/questions/
|
||||||
weather.get_detailed_status(),
|
#17649060-between-hours-of-12-midnight-and-7am-gmt-we-are-receiving
|
||||||
round(float(temp['min'])),
|
#-the-wrong-data-for-most-locations
|
||||||
round(float(temp['max'])),
|
if( abs(weather_date.date()-datetime.date.today())
|
||||||
weather.get_rain().get('all',0)
|
< 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))
|
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:
|
||||||
|
Reference in New Issue
Block a user