Last active
July 10, 2020 08:27
-
-
Save shevabam/44cc6293e414781e6802a7937bd16609 to your computer and use it in GitHub Desktop.
weather-to-pushbullet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pushbullet.py>=0.11.0 | |
urllib3>=1.25.9 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# -*- coding: utf-8 -*- | |
from pushbullet import Pushbullet | |
import json, urllib | |
PB_API_KEY = "" | |
OWM_API_KEY = "" | |
CITY = "" | |
NOTIF_TITLE = "Météo du jour" | |
DEVICE_ID = ""; # Send to specific device (run print(pb.devices) to get key) | |
OWM_API_BASE_URL = "http://api.openweathermap.org/data/2.5/" | |
def getWeather(): | |
content = "" | |
weather_gps = json.loads(urllib.request.urlopen(OWM_API_BASE_URL+"weather?APPID="+OWM_API_KEY+"&q="+urllib.parse.quote_plus(CITY.encode("utf8"))+"&mode=json&units=metric").read()) | |
if 'lon' in weather_gps['coord']: | |
gps_lon = str(weather_gps['coord']['lon']) | |
gps_lat = str(weather_gps['coord']['lat']) | |
weather = json.loads(urllib.request.urlopen(OWM_API_BASE_URL+"onecall?APPID="+OWM_API_KEY+"&lat="+gps_lat+"&lon="+gps_lon+"&units=metric&lang=fr&exclude=hourly").read()) | |
if 'current' in weather: | |
current_temp = str(weather['current']['temp']) | |
current_temp_feels = str(weather['current']['feels_like']) | |
weather_desc = str(weather['current']['weather'][0]['description']) | |
day_max_temp = str(weather['daily'][0]['temp']['max']) | |
content = "Il fait actuellement "+current_temp+"°C ("+current_temp_feels+"°C ressenti) à "+CITY+"." | |
content += "\n" | |
content += "Le temps est "+weather_desc+"." | |
content += "\n" | |
content += "La maximale sera de "+day_max_temp+"°C." | |
return content; | |
def SendWeatherWithPushbullet(): | |
pb = Pushbullet(PB_API_KEY) | |
weather = getWeather() | |
if DEVICE_ID != "": | |
device = pb.devices[DEVICE_ID] | |
push = pb.push_note(NOTIF_TITLE, weather, device=device) | |
else: | |
push = pb.push_note(NOTIF_TITLE, weather) | |
SendWeatherWithPushbullet() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment