<commit_before>#!/usr/bin/python

""" This script will check the OpenSprinkler API every 5 seconds and look for running stations.
    If a station is found running, it'll send a push notification through Instapush. It will only
	send a push notification once per zone, and only at start-up of the zone.
	
	If the script crashes, it can send an email to let you know it's crashed. 
	
	6/26/2015, <NAME>. Licensed under the MIT License.
	6/30/2015, added Pushover as a push notification service.
	
	"""

import urllib2, json, requests
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from time import sleep

# Configure these variables.
ospiApiPasswordHex = "<PASSWORD>"

# Select your push notification service. Options are "pushover" or "instapush"
pushService = "instapush"

# Instapush settings
instapushAppID = "INSTAPUSH_APP_ID"
instapushAppSecret = "INSTAPUSH_APP_SECRET"

# Pushover settings
pushoverUserKey = "PUSHOVER_USER_KEY"
pushoverAppToken = "PUSH_OVER_APP_TOKEN"

# Email setup. This is used if the script crashes.
fromEmail = "YOUR@FROM_EMAIL"
toEmail = ['YOUR@TO_EMAIL']
smtpServer = "localhost"
subject = "OSPi Push Notification Script Failure"
text = "The OSPi push notification script has crashed, or stopped. You should investigate this and restart it.\n\nssh to your OSPi and check /home/pi/ospi_push_notifications.py"


#########################################################
#
# Don't need to edit anything below, unless you want to. 
#
#########################################################
notifyZone = 0
notifySent = "n"

def sendEmail():
	msg = MIMEMultipart('alternative')
	msg['Subject'] = subject
	msg['From'] = fromEmail
	msg['To'] = ", ".join(toEmail)
	part1 = MIMEText(text, 'plain')
	msg.attach(part1)
	s = smtplib.SMTP(smtpServer)
	s.sendmail(fromEmail, toEmail, msg.as_string())
	s.quit()

def getStatus():
	try:
		ospiStationStatus = urllib2.urlopen("http://localhost:8080/js?pw=" + ospiApiPasswordHex).read()
	except:
		sendEmail()
	
	try:
		data = json.loads(ospiStationStatus)
	except:
		sendEmail()
		
	stations = data["sn"]
	#print "Getting sprinkler status. Zones defined: %s. Zone data: %s" % (data["nstations"],data["sn"])
	return stations

def sendPushNotification(zoneID):
	#Send push notifications
	event = "Zone %s is now active" % zoneID<commit_msg>Updated variable name to reduce confusion<commit_after>#!/usr/bin/python

""" This script will check the OpenSprinkler API every 5 seconds and look for running stations.
    If a station is found running, it'll send a push notification through Instapush. It will only
	send a push notification once per zone, and only at start-up of the zone.
	
	If the script crashes, it can send an email to let you know it's crashed. 
	
	6/26/2015, <NAME>. Licensed under the MIT License.
	6/30/2015, added Pushover as a push notification service.
	
	"""

import urllib2, json, requests
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from time import sleep

# Configure these variables.

# You can use this site to convert your plaintext password to hash: http://www.miraclesalad.com/webtools/md5.php
# For example, "hello" converts to 5d41402abc4b2a76b9719d911017c592. You would enter 5d41402abc4b2a76b9719d91101<PASSWORD> below
ospiApiPasswordHash = "<PASSWORD>" 

# Select your push notification service. Options are "pushover" or "instapush"
pushService = "instapush"

# Instapush settings
instapushAppID = "INSTAPUSH_APP_ID"
instapushAppSecret = "INSTAPUSH_APP_SECRET"

# Pushover settings
pushoverUserKey = "PUSHOVER_USER_KEY"
pushoverAppToken = "PUSH_OVER_APP_TOKEN"

# Email setup. This is used if the script crashes.
fromEmail = "YOUR@FROM_EMAIL"
toEmail = ['YOUR@TO_EMAIL']
smtpServer = "localhost"
subject = "OSPi Push Notification Script Failure"
text = "The OSPi push notification script has crashed, or stopped. You should investigate this and restart it.\n\nssh to your OSPi and check /home/pi/ospi_push_notifications.py"


#########################################################
#
# Don't need to edit anything below, unless you want to. 
#
#########################################################
notifyZone = 0
notifySent = "n"

def sendEmail():
	msg = MIMEMultipart('alternative')
	msg['Subject'] = subject
	msg['From'] = fromEmail
	msg['To'] = ", ".join(toEmail)
	part1 = MIMEText(text, 'plain')
	msg.attach(part1)
	s = smtplib.SMTP(smtpServer)
	s.sendmail(fromEmail, toEmail, msg.as_string())
	s.quit()

def getStatus():
	try:
		ospiStationStatus = urllib2.urlopen("http://localhost:8080/js?pw=" + ospiApiPasswordHash).read()
	except:
		sendEmail()
	
	try:
		data = json.loads(ospiStationStatus)
	except:
		sendEmail()
		
	stations = data["sn"]
	#print "Getting sprinkler status. Zones defined: %s. Zone data: %s" % (data["nstations"],data["sn"])
	return stations

def sendPushNotification(zoneID):
	#Send push notifications
	event = "Zone %s is now active" % zoneID
{'fraction_non_alphanumeric': 0.1423123243677238, 'fraction_numerical': 0.018265756724207147, 'mean_word_length': 5.136699507389163, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4763845', 'n_tokens_mistral': 1697, 'n_tokens_neox': 1579, 'n_words': 510}