HOW TO
A hosted feature service can be added to a public web app. This provides users with editing capabilities. In some cases, owners may prefer to receive scheduled update notifications when new hosted features are added to the hosted feature service. It is possible to construct a Python script to check for changes to the hosted feature service over a time given interval and send an email to the owner. An automated email can be sent using the smptlib module to access the email SMTP protocol of the desired webmail provider, such as Gmail or Hotmail.
import urllib2, json, urllib, datetime, time, smtplib from datetime import timedelta
username = 'Username' # ArcGIS Online Username password = 'password' # ArcGIS Online Password URL = 'https://services.arcgis.com/[Folder]/arcgis/rest/services/[Feature Data]/FeatureServer/2/query' # Feature Service URL uniqueID = 'OBJECTID' # Desired field to check dateField = 'Date' # Date field to query hoursValue = 1 # Number of hours to check when a feature was added fromEmail = 'Sample@email.com' # Sender's Email toEmail = 'Sample@email.com' # Receiver's Email #smtpServer = 'smtp.gis.com' # SMPT Server Name #portNumber = 25 # SMTP Server port # Create empty list for uniqueIDs oidList = []
try: print('Generating Token') 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'] except: token = ''
params = {'f': 'pjson', 'where': "1=1", 'outfields' : '*' , 'returnGeometry' : 'false', 'token' : token} req = urllib2.Request(URL, urllib.urlencode(params)) response = urllib2.urlopen(req) data = json.load(response)
for feat in data['features']: createDate = feat['attributes'][dateField] createDate = int(str(createDate)[0:-3]) t = datetime.datetime.now() - timedelta(hours=hoursValue) t = time.mktime(t.timetuple()) if createDate > t: oidList.append(feat['attributes'][uniqueID]) print("Edited feature")
FROM = 'sender@hotmail.com' TO = ['receiver@yahoo.com'] SUBJECT = 'New Feature Added' TEXT = "Features with OBJECTIDs " + str(oidList) + " were added."
message = """\ From: %s To: %s Subject: %s %s """ % (FROM, ", ".join(TO), SUBJECT, TEXT) emailPassword = 'Password'
Note: Some email providers, for example, Gmail, require secure access logins from apps. The option to allow less secure app access is required to successfully establish a connection with the smtp server.
if len(oidList) > 0: server = smtplib.SMTP_SSL('smtp.gmail.com') server.connect('smtp.gmail.com') server.ehlo() server.login(FROM, gmailPWD) server.sendmail(FROM, TO, message) print "Successfully sent email" server.close() else: print "No new data found."
The following script shows the final code:
import urllib2, json, urllib, datetime, time, smtplib from datetime import timedelta # Variables username = 'Username' password = 'password' URL = 'https://services.arcgis.com/Wl7Y1m92PbjtJs5n/arcgis/rest/services/tem_data/FeatureServer/2/query' # Feature Service URL uniqueID = 'OBJECTID' dateField = 'EditDate' hoursValue = 1 fromEmail = 'test@gmail.com' toEmail = 'test@esri.com' oidList = [] try: print('Generating Token') 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'] except: token = '' params = {'f': 'pjson', 'where': "1=1", 'outfields' : '*' , 'returnGeometry' : 'false', 'token' : token} req = urllib2.Request(URL, urllib.urlencode(params)) response = urllib2.urlopen(req) data = json.load(response) for feat in data['features']: createDate = feat['attributes'][dateField] createDate = int(str(createDate)[0:-3]) t = datetime.datetime.now() - timedelta(hours=hoursValue) t = time.mktime(t.timetuple()) if createDate > t: oidList.append(feat['attributes'][uniqueID]) print("Edited feature") print(oidList) FROM = fromEmail TO = [toEmail] SUBJECT = 'New Features Added' TEXT = "Features with {0}s {1} were added.".format(uniqueID, oidList) message = """\ From: %s To: %s Subject: %s %s """ % (FROM, ", ".join(TO), SUBJECT, TEXT) gmailPWD = 'password' if len(oidList) > 0: server = smtplib.SMTP_SSL('smtp.gmail.com') server.connect('smtp.gmail.com') server.ehlo() server.login(FROM, gmailPWD) server.sendmail(FROM, TO, message) print "Successfully sent email" server.close() else: print "No new data found."
Get help from ArcGIS experts
Download the Esri Support App