Converting an ArcGIS API Content Search output into a dictionary.

223
2
Jump to solution
03-27-2024 06:07 AM
NigelGriffithsAU
New Contributor

Hello, I am having difficulty auditing organisational content, and I wish to gather a full list of content, the type of content and which user owns this content.

I have accessed the list quite easily using the ArcGIS API - https://developers.arcgis.com/python/guide/accessing-and-creating-content/

What I am having difficulty in doing is finding a way to convert the API output into a dictionary in python, to then output into a CSV. 

The output type is coming out as <class 'arcgis.gis.Item'>, with an example below:

[<Item title:"0ff721b591bb4526a0877b4708488db1" type:Feature Layer Collection owner:arcgis_python>,
 <Item title:"UnemploymentRateHotspots245326" type:Feature Layer Collection owner:arcgis_python>,
 <Item title:"6f0e42f4e35540b180f92263da913dba" type:Table Layer owner:arcgis_python>,
 <Item title:"6cf4c07182ef44c3b8d0f64f28a8fd7e" type:Feature Layer Collection owner:arcgis_python>,


Any assistance would be greatly appreciated.

 

0 Kudos
1 Solution

Accepted Solutions
Tom_Laue
New Contributor III

This is what I use:

 

#note: this must be run in Python 3.x
#This script will list all ArcGIS Online content, what it is, who owns it, the dates it was created & modified

import arcgis, time
import csv

gis=GIS(url,username,password)


print ("Organization Name: "+str(gis.properties.name))
print ("Portal Name: "+str(gis.properties.portalName))
#print ("URL: "+str(gis.properties.customBaseUrl))
print ("ArcGIS Online Credits Available: "+str("{:,}".format(gis.properties.availableCredits)))
print("ELA Expiration Date: "+str(time.strftime('%m/%d/%Y %H:%M:%S', time.gmtime(gis.properties.subscriptionInfo.expDate/1000.0))))
print("Max Users per Level: ")
for key,val in gis.properties.subscriptionInfo.maxUsersPerLevel.items():
	    print (key, "=", val)
print ("\n")

#set item_type if you only want to search for ex. "Feature Layer" 
search_result = gis.content.search(query="", item_type="",max_items=10000) #max number is set so it will return more than the default 10 results

outCSVfile=r"L:\Scripts\ArcGISOnline\ArcGISOnline_ContentDetails.csv"
csvfile = open(outCSVfile, 'w',newline='')
writer = csv.writer(csvfile)
writer.writerow(["Title","ItemType","Sharing","Description","Snippet","Tags","Categories","AccessInformation","Protected","Status","OwnedBy","DateCreated","DateLastModified","View Count","Item_ID"])

print("There are: "+str(len(search_result))+" total items in our ArcGIS Online.")

recordNumber = 0
for i in search_result:
    recordNumber+=1
    try:
        description = str(i.description.encode('unicode-escape').decode('utf-8')) #removes emojis
    except:
        description = str(i.description)
 
    #print(i.title+","+","+i.type+","+i.access+","+description+str(i.snippet)+","+str(i.tags)+","+str(i.categories)+","+str(i.accessInformation)+","+str(i.protected)+","+str(i.content_status)+i.owner+","+","+str(time.strftime('%m/%d/%Y %H:%M:%S', time.gmtime(i.created/1000.0))+","+str(time.strftime('%m/%d/%Y %H:%M:%S', time.gmtime(i.modified/1000.0))))+","+str(i.numViews)+","+(i.id))
    #print (recordNumber) # so we know the script is doing something
    writer.writerow([i.title, i.type,i.access,description,str(i.snippet),str(i.tags),str(i.categories),str(i.accessInformation),str(i.protected),str(i.content_status),str(i.owner),str(time.strftime('%m/%d/%Y %H:%M:%S', time.gmtime(i.created/1000.0))),str(time.strftime('%m/%d/%Y %H:%M:%S', time.gmtime(i.modified/1000.0))),i.numViews,i.id])

csvfile.close()

View solution in original post

2 Replies
Tom_Laue
New Contributor III

This is what I use:

 

#note: this must be run in Python 3.x
#This script will list all ArcGIS Online content, what it is, who owns it, the dates it was created & modified

import arcgis, time
import csv

gis=GIS(url,username,password)


print ("Organization Name: "+str(gis.properties.name))
print ("Portal Name: "+str(gis.properties.portalName))
#print ("URL: "+str(gis.properties.customBaseUrl))
print ("ArcGIS Online Credits Available: "+str("{:,}".format(gis.properties.availableCredits)))
print("ELA Expiration Date: "+str(time.strftime('%m/%d/%Y %H:%M:%S', time.gmtime(gis.properties.subscriptionInfo.expDate/1000.0))))
print("Max Users per Level: ")
for key,val in gis.properties.subscriptionInfo.maxUsersPerLevel.items():
	    print (key, "=", val)
print ("\n")

#set item_type if you only want to search for ex. "Feature Layer" 
search_result = gis.content.search(query="", item_type="",max_items=10000) #max number is set so it will return more than the default 10 results

outCSVfile=r"L:\Scripts\ArcGISOnline\ArcGISOnline_ContentDetails.csv"
csvfile = open(outCSVfile, 'w',newline='')
writer = csv.writer(csvfile)
writer.writerow(["Title","ItemType","Sharing","Description","Snippet","Tags","Categories","AccessInformation","Protected","Status","OwnedBy","DateCreated","DateLastModified","View Count","Item_ID"])

print("There are: "+str(len(search_result))+" total items in our ArcGIS Online.")

recordNumber = 0
for i in search_result:
    recordNumber+=1
    try:
        description = str(i.description.encode('unicode-escape').decode('utf-8')) #removes emojis
    except:
        description = str(i.description)
 
    #print(i.title+","+","+i.type+","+i.access+","+description+str(i.snippet)+","+str(i.tags)+","+str(i.categories)+","+str(i.accessInformation)+","+str(i.protected)+","+str(i.content_status)+i.owner+","+","+str(time.strftime('%m/%d/%Y %H:%M:%S', time.gmtime(i.created/1000.0))+","+str(time.strftime('%m/%d/%Y %H:%M:%S', time.gmtime(i.modified/1000.0))))+","+str(i.numViews)+","+(i.id))
    #print (recordNumber) # so we know the script is doing something
    writer.writerow([i.title, i.type,i.access,description,str(i.snippet),str(i.tags),str(i.categories),str(i.accessInformation),str(i.protected),str(i.content_status),str(i.owner),str(time.strftime('%m/%d/%Y %H:%M:%S', time.gmtime(i.created/1000.0))),str(time.strftime('%m/%d/%Y %H:%M:%S', time.gmtime(i.modified/1000.0))),i.numViews,i.id])

csvfile.close()
NigelGriffithsAU
New Contributor

Incredible! Thank you so much for sharing this. Seems so simple now, though my understanding of Python is currently very basic.

This is why I am very glad to use ESRI, since we have access to such an amazing community.

0 Kudos