Files
WeatherToFreemobile/weather_to_freemobile.py
2019-08-26 00:51:40 +02:00

65 lines
2.3 KiB
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/python3
"""
A simple script that sends the daily weather to a FreeMobile phone
"""
import json
import logging
import pyowm
import requests
class WeatherToFreemobile():
def __init__(self, config_file='config.json'):
logging.info('Load configuration from config.json file')
with open('config.json') as config_file:
self.config = json.load(config_file)
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')
}
logging.debug(data)
logging.info('Contacting FreeMobile API')
r = requests.post('https://smsapi.free-mobile.fr/sendmsg', json = data)
if r.status_code == 200:
logging.info('SMS sent')
else:
logging.warning('SMS *not* sent. Status code %s', r.status_code)
def get_weather(self):
"""
Gets the current weather from OpenWeatherMap
"""
city = self.config['openweathermap_city']
apikey = self.config['openweathermap_apikey']
apilanguage = self.config['openweathermap_apikey']
logging.info('Opening OpenWeatherMap API')
owm = pyowm.OWM(apikey,language=apilanguage)
fc = owm.daily_forecast(city,limit=1)
f = fc.get_forecast()
return_message=""
for weather in f:
temp = weather.get_temperature(unit='celsius')
return_message += '{} : {} (min {}ºC, max {}ºC, rain:{}mm)'.format(
weather.get_reference_time('date').strftime('%x'),
weather.get_detailed_status(),
temp['min'],
temp['max'],
weather.get_rain()
)
logging.info("Got the following weather: {}".format(return_message))
return return_message
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s - %(message)s')
wtf = WeatherToFreemobile()
wtf.send_sms_to_freemobile(wtf.get_weather())