From 55455c75b113051bfd68f8949f22141baaad3700 Mon Sep 17 00:00:00 2001 From: Cinabre Date: Tue, 27 Oct 2020 09:22:30 +0100 Subject: [PATCH] Creation de get_weather et gotify pour la reception de la meteo et l'envoie des notifications --- get_weather.py | 34 ++++++++++++++++++++++++++++++++++ gotify.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 get_weather.py create mode 100644 gotify.py diff --git a/get_weather.py b/get_weather.py new file mode 100644 index 0000000..a617c48 --- /dev/null +++ b/get_weather.py @@ -0,0 +1,34 @@ +import requests #pip install requests +from config import Weather +import json #pip install json + + + +def get_data(): + url = "https://api.openweathermap.org/data/2.5/onecall?lat=%s&lon=%s&appid=%s&units=metric" % (Weather['lat'], Weather['lon'], Weather['API_key']) + response = requests.get(url) + # print(url) + data = json.loads(response.text) + return data + + +def weather_tomorrow(): + data = get_data() + weather = data['daily'][1]['weather'][0]['main'] + return weather + +def weather_today(): + data = get_data() + weather = data['daily'][0]['weather'][0]['main'] + return weather + + +def get_temp_today(): + data = get_data() + temp = data['daily'][0]['temp']['day'] + return temp + +def get_temp_tomorrow(): + data = get_data() + temp = data['daily'][1]['temp']['day'] + return temp diff --git a/gotify.py b/gotify.py new file mode 100644 index 0000000..636ea79 --- /dev/null +++ b/gotify.py @@ -0,0 +1,33 @@ +import requests #pip install requests +from config import Gotify, Translation +from get_weather import weather_tomorrow, weather_today, get_temp_today, get_temp_tomorrow + + + +def send_notif(title, message): + resp = requests.post(Gotify['URL'] + '?token=' + Gotify['token_gotify'] , json={ + "message": message, + "priority": 2, + "title": title + }) + + + +def send_notification_for_today(): + weather_today = weather_today() + weather_today = Translation[weather_today] + send_notif("Meteo d'aujourd'hui", "Le temps sera %s, Il y aura une température moyenne de %s °C" % (weather_today, get_temp_today()) ) + + +def send_notification_for_tomorrow(): + weather_tomorrow = weather_tomorrow() + weather_tomorrow = Translation[weather_tomorrow] + send_notif("Meteo de demain", "Le temps sera %s, Il y aura une température moyenne de %s °C" % (weather_tomorrow, get_temp_tomorrow()) ) + + +if __name__ == '__main__': + try : + main() + except : + send_notif("Erreur", "Une erreur s'est produite durant l'execution du script") + main()