From 795222fbbecf1d8ac36dc8ef85f22c8d4a3730cc Mon Sep 17 00:00:00 2001 From: djib Date: Mon, 26 Aug 2019 00:51:40 +0200 Subject: [PATCH] Share initial script --- .gitignore | 118 +-------------------------------------- Pipfile | 12 ++++ Pipfile.lock | 75 +++++++++++++++++++++++++ README.md | 6 +- config.sample.json | 7 +++ weather_to_freemobile.py | 64 +++++++++++++++++++++ 6 files changed, 165 insertions(+), 117 deletions(-) create mode 100644 Pipfile create mode 100644 Pipfile.lock create mode 100644 config.sample.json create mode 100644 weather_to_freemobile.py diff --git a/.gitignore b/.gitignore index e61bca2..4db54e7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,116 +1,4 @@ -# ---> Python -# Byte-compiled / optimized / DLL files +/config.json +/.project +/.pydevproject __pycache__/ -*.py[cod] -*$py.class - -# C extensions -*.so - -# Distribution / packaging -.Python -build/ -develop-eggs/ -dist/ -downloads/ -eggs/ -.eggs/ -lib/ -lib64/ -parts/ -sdist/ -var/ -wheels/ -*.egg-info/ -.installed.cfg -*.egg -MANIFEST - -# PyInstaller -# Usually these files are written by a python script from a template -# before PyInstaller builds the exe, so as to inject date/other infos into it. -*.manifest -*.spec - -# Installer logs -pip-log.txt -pip-delete-this-directory.txt - -# Unit test / coverage reports -htmlcov/ -.tox/ -.nox/ -.coverage -.coverage.* -.cache -nosetests.xml -coverage.xml -*.cover -.hypothesis/ -.pytest_cache/ - -# Translations -*.mo -*.pot - -# Django stuff: -*.log -local_settings.py -db.sqlite3 - -# Flask stuff: -instance/ -.webassets-cache - -# Scrapy stuff: -.scrapy - -# Sphinx documentation -docs/_build/ - -# PyBuilder -target/ - -# Jupyter Notebook -.ipynb_checkpoints - -# IPython -profile_default/ -ipython_config.py - -# pyenv -.python-version - -# celery beat schedule file -celerybeat-schedule - -# SageMath parsed files -*.sage.py - -# Environments -.env -.venv -env/ -venv/ -ENV/ -env.bak/ -venv.bak/ - -# Spyder project settings -.spyderproject -.spyproject - -# Rope project settings -.ropeproject - -# mkdocs documentation -/site - -# mypy -.mypy_cache/ -.dmypy.json -dmypy.json - -# Pyre type checker -.pyre/ - diff --git a/Pipfile b/Pipfile new file mode 100644 index 0000000..2bb02a9 --- /dev/null +++ b/Pipfile @@ -0,0 +1,12 @@ +[[source]] +url = "https://pypi.python.org/simple" +verify_ssl = true +name = "pypi" + +[packages] +pyowm = "*" + +[dev-packages] + +[requires] +python_version = "3.7" diff --git a/Pipfile.lock b/Pipfile.lock new file mode 100644 index 0000000..0de5f57 --- /dev/null +++ b/Pipfile.lock @@ -0,0 +1,75 @@ +{ + "_meta": { + "hash": { + "sha256": "90ab82af397550e8743df5673adc3a1ad7f261129395971bee308376aaff8fd9" + }, + "pipfile-spec": 6, + "requires": { + "python_version": "3.7" + }, + "sources": [ + { + "name": "pypi", + "url": "https://pypi.python.org/simple", + "verify_ssl": true + } + ] + }, + "default": { + "certifi": { + "hashes": [ + "sha256:046832c04d4e752f37383b628bc601a7ea7211496b4638f6514d0e5b9acc4939", + "sha256:945e3ba63a0b9f577b1395204e13c3a231f9bc0223888be653286534e5873695" + ], + "version": "==2019.6.16" + }, + "chardet": { + "hashes": [ + "sha256:84ab92ed1c4d4f16916e05906b6b75a6c0fb5db821cc65e70cbd64a3e2a5eaae", + "sha256:fc323ffcaeaed0e0a02bf4d117757b98aed530d9ed4531e3e15460124c106691" + ], + "version": "==3.0.4" + }, + "geojson": { + "hashes": [ + "sha256:6e4bb7ace4226a45d9c8c8b1348b3fc43540658359f93c3f7e03efa9f15f658a", + "sha256:ccbd13368dd728f4e4f13ffe6aaf725b6e802c692ba0dde628be475040c534ba" + ], + "version": "==2.5.0" + }, + "idna": { + "hashes": [ + "sha256:c357b3f628cf53ae2c4c05627ecc484553142ca23264e593d327bcde5e9c3407", + "sha256:ea8b7f6188e6fa117537c3df7da9fc686d485087abf6ac197f9c46432f7e4a3c" + ], + "version": "==2.8" + }, + "pyowm": { + "hashes": [ + "sha256:1c51334a9bcd697993d4c68e5437d5f420d33dd7f5d872b2ad93c3c47b7a9649", + "sha256:2ac88777565518b9e9aa3fc172c543ada4cbfc667d83775cd17c0e52ea6ccc86", + "sha256:43d49901493a883335855d8dbd16f8aa0d2e26e0183b6b6fe8a5471904c0e37a", + "sha256:805bd2f42e15770c0d2822b77add7770685cb94cfbeae8633328d0d522332fd7", + "sha256:8fd41a18536f4d6c432bc6d9ea69994efb1ea9b43688cf19523659b6f4d86cf7", + "sha256:b3294065a08417b4b0367fc424a77a0caafa24df8fd842ba9b08f1b5542c092e" + ], + "index": "pypi", + "version": "==2.10.0" + }, + "requests": { + "hashes": [ + "sha256:11e007a8a2aa0323f5a921e9e6a2d7e4e67d9877e85773fba9ba6419025cbeb4", + "sha256:9cf5292fcd0f598c671cfc1e0d7d1a7f13bb8085e9a590f48c010551dc6c4b31" + ], + "version": "==2.22.0" + }, + "urllib3": { + "hashes": [ + "sha256:b246607a25ac80bedac05c6f282e3cdaf3afb65420fd024ac94435cabe6e18d1", + "sha256:dbe59173209418ae49d485b87d1681aefa36252ee85884c31346debd19463232" + ], + "version": "==1.25.3" + } + }, + "develop": {} +} diff --git a/README.md b/README.md index 8a68a94..7213afe 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ -# weather_to_freemobile +# weather\_to\_freemobile -A simple script that sends OpenWeatherMap forecasts to a FreeMobile phone \ No newline at end of file +A simple script that sends [OpenWeatherMap](https://openweathermap.org/) forecasts to a [FreeMobile](http://mobile.free.fr/) phone. + +To run the script you need to fill in `config.sample.json` and to rename it to `config.json`. \ No newline at end of file diff --git a/config.sample.json b/config.sample.json new file mode 100644 index 0000000..7ed77f2 --- /dev/null +++ b/config.sample.json @@ -0,0 +1,7 @@ +{ + "freemobile_user":"", + "freemobile_apikey":"", + "openweathermap_city":"Paris,FR", + "openweathermap_apikey":"", + "openweathermap_language":"fr" +} \ No newline at end of file diff --git a/weather_to_freemobile.py b/weather_to_freemobile.py new file mode 100644 index 0000000..1162fa6 --- /dev/null +++ b/weather_to_freemobile.py @@ -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())