Skip to content

Instantly share code, notes, and snippets.

@Moving-Electrons
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Moving-Electrons/c6f977eefab97815e2b4 to your computer and use it in GitHub Desktop.
Save Moving-Electrons/c6f977eefab97815e2b4 to your computer and use it in GitHub Desktop.
This script uses the DarkSky API and the Pushover App to get instant notifications in your phone on precipitation (i.e. rain or snow) in a predefined location. For more information, visit www.movingelectrons.net
#!/usr/lib/python2.7
import json
import urllib
import httplib #used by pushover
# contants:
APIkey = 'XXXXXXXXXXXXX' #Enter yours here
lat = XXXXXX #Enter location's latitude here
lon = XXXXXX # Enter location's longitude here
def pushover(msg):
conn = httplib.HTTPSConnection("api.pushover.net:443")
conn.request("POST", "/1/messages.json",
urllib.urlencode({
"token": "XXXXXXXXXXXXX",
"user": "XXXXXXXXXXXXX",
"message": msg,
}), { "Content-type": "application/x-www-form-urlencoded" })
conn.getresponse()
# Main Program
# ------------
dsURL = 'https://api.forecast.io/forecast/%s/%s,%s' % (APIkey, lat, lon)
try:
print 'Conneting to Dark Sky ....'
jsonString = urllib.urlopen(dsURL).read()
weather = json.loads(jsonString)
except (IOError, ValueError):
print 'connection failure to %s' % dsURL
exit()
print 'current conditions:\n'
print weather['currently']
dataNextDay = weather['hourly']['data'][24] #Gets conditions 24 hrs from now.
print dataNextDay['precipProbability']
pIntensity = ''
pProb = '0'
if dataNextDay['precipProbability'] >= 0.55: #modify this to be different from 55%
pProb = dataNextDay['precipProbability'] * 100
if dataNextDay['precipIntensity'] >= 0 and dataNextDay['precipIntensity'] < 0.002: pIntensity = 'Extremely Light'
if dataNextDay['precipIntensity'] >= 0.002 and dataNextDay['precipIntensity'] < 0.017: pIntensity = 'Very Light'
if dataNextDay['precipIntensity'] >= 0.017 and dataNextDay['precipIntensity'] < 0.1: pIntensity = 'Light'
if dataNextDay['precipIntensity'] >= 0.1 and dataNextDay['precipIntensity'] < 0.4: pIntensity = 'Moderate'
if dataNextDay['precipIntensity'] >= 0.4: pIntensity = 'Heavy'
body = "Precipitation alert at Home\n(24 hrs from now)\n\nProbability: %s%% \nIntensity: %s" % (pProb, pIntensity)
print body
pushover(body)
else:
print 'Precipitation probability below threshold'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment