Fix pep8 errors

This commit is contained in:
2019-10-19 16:36:50 +02:00
parent 7da8720566
commit 2d75e110b6

View File

@ -11,6 +11,7 @@ import pyowm
import requests
import sys
class WeatherToFreemobile():
def __init__(self, config_file):
logging.info('Load configuration from {}'.format(config_file))
@ -20,28 +21,29 @@ class WeatherToFreemobile():
try:
locale.setlocale(locale.LC_TIME, self.config['locale'])
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')
apikey = self.config['openweathermap_apikey']
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):
"""
Sends a SMS using the FreeMobile API
https://mobile.free.fr/moncompte/index.php?page=options
"""
data = {
'user':self.config['freemobile_user'],
'pass':self.config['freemobile_apikey'],
'msg':bytes(message,'utf-8').decode('iso-8859-1')
'user': self.config['freemobile_user'],
'pass': self.config['freemobile_apikey'],
'msg': bytes(message, 'utf-8').decode('iso-8859-1')
}
logging.debug(data)
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:
logging.info('SMS sent')
else:
@ -53,28 +55,28 @@ class WeatherToFreemobile():
"""
city = self.config['openweathermap_city']
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()
return_message=[]
return_message = []
for weather in f:
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"
#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
# 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(
date_diff < datetime.timedelta(0)
or
or
date_diff >= datetime.timedelta(number_of_days)
):
logging.info('Skipped {} (cf. API Bug)'.format(weather_date))
else:
temp = weather.get_temperature(unit='celsius')
rain = weather.get_rain().get('all',0)
rain = weather.get_rain().get('all', 0)
return_message.append(
'{}: {} (min {}ºC, max {}ºC, rain:{}mm)'.format(
weather_date.strftime('%A %d').title(),
@ -84,9 +86,9 @@ class WeatherToFreemobile():
rain
)
)
if( rain and date_diff == datetime.timedelta(0) ):
if(rain and date_diff == datetime.timedelta(0)):
return_message.append(self.get_rain())
logging.info("Got the following weather: {}".format(return_message))
return "\n".join(return_message)
@ -98,18 +100,18 @@ class WeatherToFreemobile():
fc = self.owm.three_hours_forecast(city)
f = fc.get_forecast()
return_message=[]
return_message = []
for weather in f:
weather_date = weather.get_reference_time('date')
if( weather_date.date() != datetime.date.today() ):
if(weather_date.date() != datetime.date.today()):
break
return_message.append(" - {:2d}h : {}mm".format(
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)
if __name__ == "__main__":
logging.basicConfig(
level=logging.WARNING,