I helped a customer create a script that was run by Windows Task Scheduler every couple hours. The script checked the pressure of valves in their hosted service. If the pressure was over 500 PSI, an e-mail would be sent. The script is posted below:
import urllib, urllib2, json, smtplib
username = 'agol'
password = 'agol'
tokenURL = 'https://www.arcgis.com/sharing/rest/generateToken'
params = {'f': 'pjson', 'username': username, 'password': password, 'referer': 'http://www.arcgis.com'}
req = urllib2.Request(tokenURL, urllib.urlencode(params))
response = urllib2.urlopen(req)
data = json.load(response)
token = data['token']
valveURL = 'http://services2.arcgis.com/DyqsuMihRCOO7S/arcgis/rest/services/DEMO1/FeatureServer/4/query'
params = {'where': 'PRESSURE > 500', 'returnIdsOnly': 'true', 'f': 'pjson', 'token': token}
req = urllib2.Request(valveURL, urllib.urlencode(params))
response = urllib2.urlopen(req)
data = json.load(response)
valveList = data['objectIds']
if len(valveList) > 0:
FROM = 'user@agol.com'
TO = ['user@agol.com']
SUBJECT = 'Valve Inspections'
TEXT = "Valves with OBJECTIDs " + str(valveList) + " need inspection."
message = """\
From: %s
To: %s
Subject: %s
%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
smtpObj = smtplib.SMTP(host='mail.agol.com', port=25)
smtpObj.sendmail(FROM, TO, message)
print "Successfully sent email"
smtpObj.quit()