Fix pep8 errors
This commit is contained in:
@ -11,6 +11,7 @@ import pyowm
|
|||||||
import requests
|
import requests
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
||||||
class WeatherToFreemobile():
|
class WeatherToFreemobile():
|
||||||
def __init__(self, config_file):
|
def __init__(self, config_file):
|
||||||
logging.info('Load configuration from {}'.format(config_file))
|
logging.info('Load configuration from {}'.format(config_file))
|
||||||
@ -20,13 +21,14 @@ class WeatherToFreemobile():
|
|||||||
try:
|
try:
|
||||||
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')
|
logging.info('Opening OpenWeatherMap API')
|
||||||
|
|
||||||
apikey = self.config['openweathermap_apikey']
|
apikey = self.config['openweathermap_apikey']
|
||||||
apilanguage = self.config['openweathermap_language']
|
apilanguage = self.config['openweathermap_language']
|
||||||
self.owm = pyowm.OWM(apikey,language=apilanguage)
|
self.owm = pyowm.OWM(apikey, language=apilanguage)
|
||||||
|
|
||||||
|
|
||||||
def send_sms_to_freemobile(self, message):
|
def send_sms_to_freemobile(self, message):
|
||||||
"""
|
"""
|
||||||
@ -34,14 +36,14 @@ class WeatherToFreemobile():
|
|||||||
https://mobile.free.fr/moncompte/index.php?page=options
|
https://mobile.free.fr/moncompte/index.php?page=options
|
||||||
"""
|
"""
|
||||||
data = {
|
data = {
|
||||||
'user':self.config['freemobile_user'],
|
'user': self.config['freemobile_user'],
|
||||||
'pass':self.config['freemobile_apikey'],
|
'pass': self.config['freemobile_apikey'],
|
||||||
'msg':bytes(message,'utf-8').decode('iso-8859-1')
|
'msg': bytes(message, 'utf-8').decode('iso-8859-1')
|
||||||
}
|
}
|
||||||
logging.debug(data)
|
logging.debug(data)
|
||||||
|
|
||||||
logging.info('Contacting FreeMobile API')
|
logging.info('Contacting FreeMobile API')
|
||||||
r = requests.post('https://smsapi.free-mobile.fr/sendmsg', json = data)
|
r = requests.post('https://smsapi.free-mobile.fr/sendmsg', json=data)
|
||||||
if r.status_code == 200:
|
if r.status_code == 200:
|
||||||
logging.info('SMS sent')
|
logging.info('SMS sent')
|
||||||
else:
|
else:
|
||||||
@ -54,27 +56,27 @@ class WeatherToFreemobile():
|
|||||||
city = self.config['openweathermap_city']
|
city = self.config['openweathermap_city']
|
||||||
number_of_days = self.config['number_of_days']
|
number_of_days = self.config['number_of_days']
|
||||||
|
|
||||||
fc = self.owm.daily_forecast(city,limit=number_of_days+1)
|
fc = self.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')
|
weather_date = weather.get_reference_time('date')
|
||||||
date_diff = weather_date.date() - datetime.date.today()
|
date_diff = weather_date.date() - datetime.date.today()
|
||||||
|
|
||||||
#Workaround API returning yesterday's weather"
|
# Workaround API returning yesterday's weather"
|
||||||
#https://openweathermap.desk.com/customer/en/portal/questions/
|
# https://openweathermap.desk.com/customer/en/portal/questions/
|
||||||
#17649060-between-hours-of-12-midnight-and-7am-gmt-we-are-receiving
|
# 17649060-between-hours-of-12-midnight-and-7am-gmt-we-are-receiving
|
||||||
#-the-wrong-data-for-most-locations
|
# -the-wrong-data-for-most-locations
|
||||||
if(
|
if(
|
||||||
date_diff < datetime.timedelta(0)
|
date_diff < datetime.timedelta(0)
|
||||||
or
|
or
|
||||||
date_diff >= datetime.timedelta(number_of_days)
|
date_diff >= datetime.timedelta(number_of_days)
|
||||||
):
|
):
|
||||||
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)
|
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(),
|
||||||
@ -84,7 +86,7 @@ class WeatherToFreemobile():
|
|||||||
rain
|
rain
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
if( rain and date_diff == datetime.timedelta(0) ):
|
if(rain and date_diff == datetime.timedelta(0)):
|
||||||
return_message.append(self.get_rain())
|
return_message.append(self.get_rain())
|
||||||
|
|
||||||
logging.info("Got the following weather: {}".format(return_message))
|
logging.info("Got the following weather: {}".format(return_message))
|
||||||
@ -98,14 +100,14 @@ class WeatherToFreemobile():
|
|||||||
fc = self.owm.three_hours_forecast(city)
|
fc = self.owm.three_hours_forecast(city)
|
||||||
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')
|
weather_date = weather.get_reference_time('date')
|
||||||
if( weather_date.date() != datetime.date.today() ):
|
if(weather_date.date() != datetime.date.today()):
|
||||||
break
|
break
|
||||||
return_message.append(" - {:2d}h : {}mm".format(
|
return_message.append(" - {:2d}h : {}mm".format(
|
||||||
weather_date.hour,
|
weather_date.hour,
|
||||||
round(float(weather.get_rain().get('3h',0)),1)
|
round(float(weather.get_rain().get('3h', 0)), 1)
|
||||||
))
|
))
|
||||||
return "\n".join(return_message)
|
return "\n".join(return_message)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user