Last active
September 28, 2017 18:00
-
-
Save mxcoder/1ee85cc2b93dbec2c78db218bddac33a to your computer and use it in GitHub Desktop.
Creates list of attachments in arcgis repos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
import requests | |
import sys | |
# Replace these values | |
instance='REPLACEME.arcgis.com/REPLACEME' | |
document='REPLACEME' | |
layer='REPLACEME' #usually 0 | |
baseURL='https://'+instance+'/ArcGIS/rest/services/'+document+'/FeatureServer/'+layer | |
outputfn=document+'.csv' | |
# Fetch ObjectIDs | |
queryObjectsURL=baseURL+'/query?where=1%3D1&objectIds=&time=&geometry=&geometryType=esriGeometryEnvelope&inSR=&spatialRel=esriSpatialRelIntersects&resultType=none&distance=0.0&units=esriSRUnit_Meter&returnGeodetic=false&outFields=&returnGeometry=true&multipatchOption=xyFootprint&maxAllowableOffset=&geometryPrecision=&outSR=&datumTransformation=&returnIdsOnly=true&returnCountOnly=false&returnExtentOnly=false&returnDistinctValues=false&orderByFields=&groupByFieldsForStatistics=&outStatistics=&having=&resultOffset=&resultRecordCount=&returnZ=false&returnM=false&returnExceededLimitFeatures=true&quantizationParameters=&sqlFormat=none&f=pjson&token=' | |
oi_json = requests.get(queryObjectsURL).json() | |
if 'objectIds' not in oi_json: | |
print("Unable to retreieve objectIds") | |
sys.exit(1) | |
all_ids = oi_json['objectIds'] | |
print('Found ' + str(len(all_ids)) + ' objects\n') | |
print(all_ids) | |
# Fetch Attachments | |
output = open(outputfn, 'w') | |
output.write(baseURL+'\n') | |
output.write('Image URL\tImage Filename\n') | |
queryAttachmentURL=baseURL+'/queryAttachments?f=json&objectIds=' | |
# Batches of 100 | |
batchSize=100 | |
while len(all_ids) > 0: | |
# Prepares query for 100 Objects | |
ids = [] | |
for x in range(0, batchSize): | |
if (len(all_ids) > 0): | |
ids.append(str(all_ids.pop())) | |
ids = ','.join(ids) | |
qAURL = queryAttachmentURL+ids | |
print('Fetching attachments from ' + qAURL) | |
# Creates attachment URLs according to JSON data | |
at_json = requests.get(qAURL).json() | |
if 'attachmentGroups' in at_json: | |
print('Found ' + str(len(at_json['attachmentGroups'])) + ' attachment groups') | |
for ag in at_json['attachmentGroups']: | |
pID = str(ag['parentObjectId']) | |
print('Attachment group: ' + pID + ' has ' + str(len(ag['attachmentInfos'])) + ' entries') | |
for ai in ag['attachmentInfos']: | |
aID = str(ai['id']) | |
aName = str(ai['name']) | |
aURL = baseURL+'/'+pID+'/attachments/'+aID | |
output.write(aURL+'\t'+aName+'\n') | |
output.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment