Share initial script
This commit is contained in:
64
weather_to_freemobile.py
Normal file
64
weather_to_freemobile.py
Normal file
@ -0,0 +1,64 @@
|
||||
#!/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())
|
Reference in New Issue
Block a user