Skip to content

Instantly share code, notes, and snippets.

@seaukara
Last active November 13, 2019 22:04
Show Gist options
  • Save seaukara/eba1901b0d46613762c63fbd9bccae62 to your computer and use it in GitHub Desktop.
Save seaukara/eba1901b0d46613762c63fbd9bccae62 to your computer and use it in GitHub Desktop.
#-------------------------------------------------------------------------------
# Name: Return attachment URLs for a feature service
# Purpose: Iterates through features in a feature service and pulls all attachments IDs
#
# Author: Kara Manseau
#
# Created: 16/04/2014
#-------------------------------------------------------------------------------
import requests
def generateToken(username, password):
'''Generate a token using the requests module for the input
username and password'''
url = "https://arcgis.com/sharing/generateToken"
data = {'username': username,
'password': password,
'client' : 'referer',
'referer' : 'https://arcgis.com',
'expiration' : 1209600,
'f': 'json'}
request = requests.post(url, data=data, verify=False)
return request.json()['token']
def queryService(serviceURL, token):
url = serviceURL + "/query?"
data = {'where' : "1=1",
'geometryType' : 'esriGeometryEnvelope',
'spatialRel' : 'esriSpatialRelIntersects',
'units' : 'esriSRUnit_Meter',
'returnGeometry' : 'false',
'returnIdsOnly' : 'true',
'returnCountOnly' : 'false',
'returnExtentOnly' : 'false',
'returnZ' : 'false',
'returM' : 'false',
'f' : 'json',
'token': token}
request = requests.post(url, data=data, verify=False)
return request.json()
def queryAttachments(featureNum, token):
url = serviceURL + "/" + str(featureNum) + "/attachments?"
data = {'f' : 'json',
'token' : token}
request = requests.post(url, data=data, verify=False)
return request.json()
if __name__ == '__main__':
username = raw_input("Please enter your username: ")
password = raw_input("Please enter your password: ")
token = generateToken(username, password)
print(token)
serviceURL = ''
query = queryService(serviceURL, token)
print(query)
sort = sorted(query['objectIds'])
'''You may need to change the name of this field depending on your service'''
for num in sort:
print "Feature number: "+ str(num)
attachments = queryAttachments(num, token)
for attachmentid in attachments['attachmentInfos']:
final = serviceURL + "/" + str(num) + "/attachments/" + str(attachmentid['id'])
print(final + ",")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment