Skip to content

Instantly share code, notes, and snippets.

@shevabam

shevabam/requirements.txt

Last active Jul 10, 2020
Embed
What would you like to do?
weather-to-pushbullet
pushbullet.py>=0.11.0
urllib3>=1.25.9
#!/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
You can’t perform that action at this time.