HOW TO
Einer öffentlichen Web-App kann ein gehosteter Feature-Service hinzugefügt werden. Dadurch erhalten Benutzer die Möglichkeit zur Bearbeitung. Manche Besitzer möchten Benachrichtigungen zu geplanten Aktualisierungen erhalten, wenn dem gehosteten Feature-Service gehostete Features hinzugefügt werden. Es ist möglich, ein Python-Skript zu erstellen, mit dem geprüft wird, ob in einem bestimmten Zeitraum am gehosteten Feature-Service Änderungen vorgenommen wurden, und mit dem an den Besitzer eine E-Mail gesendet wird. Eine E-Mail kann automatisch versendet werden, wobei das smptlib-Modul verwendet wird, um auf das E-Mail-Protokoll SMTP eines gewünschten Webmail-Anbieters wie Gmail oder Hotmail zuzugreifen.
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'
Hinweis: Einige E-Mail-Anbieter wie Gmail verlangen eine Anmeldung über einen sicheren Zugriff von Apps. Um eine Verbindung mit dem SMTP-Server herzustellen, ist jedoch ein weniger sicherer App-Zugriff erforderlich.
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."
Das folgende Skript enthält den endgültigen 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."
Unterstützung durch ArcGIS-Experten anfordern
Esri Support App herunterladen