Do you recognize this?
- "Over time our ArcGIS Online organization has grown. We have quite a few users now and a considerable number of groups has been created. Can you please provide me with a quick overview of who is member of which group?"
- "Yes, I can."
- "OK - and please note: not only quick, but also in Excel. Is that possible?"
- "Eh... yeah, that is also possible."
We are going to use the ArcGIS API for Python with groups.search() and users.search() to get an overview of our organization. Next we compile a matrix - users along the y-axis and groups along the x-axis - with a 1 or a 0 to indicate wether the user is a member of the group or not. This matrix is being written to a CSV file.
And then the boss should be able to import this CSV into a colorful and manageable spreadsheet. With or without a little help.
Does the script below work for you? Just 'like' or 'share' if it does. And if you want to, you can just open your Jupyter Notebook to try it line by line.
import csv,os, sys
from arcgis.gis import GIS
from provide_credentials import provide_credentials
print('===================')
print('The script that is running: ' + __file__)
print('First you have to log in to ArcGIS Online')
username, password = provide_credentials()
my_agol = GIS("https://www.arcgis.com", username, password)
print ("Start: "+datetime.datetime.today().strftime('%c'))
my_groups = my_agol.groups.search()
my_users = my_agol.users.search(max_users = 350)
my_group_titles = []
for my_group in my_groups:
my_group_titles.append(my_group.title)
fieldnames = []
fieldnames = ['USERNAME','EMAIL']
for title in my_group_titles:
fieldnames.append(title)
today = datetime.datetime.today().strftime('%Y%m%d')
fname = 'AGOL_Group_Membership'+today+'.csv'
try:
os.remove(fname)
except OSError:
pass
outfile = open(fname, 'a')
writer = csv.DictWriter(outfile, delimiter = ';', lineterminator='\n', fieldnames=fieldnames)
writer.writeheader()
for user in my_users:
membership = []
try:
thisUser = {}
thisUser['USERNAME'] = user.fullName
thisUser['EMAIL'] = user.email
try:
for group in user.groups:
membership.append(group.title)
except:
pass
for title in my_group_titles:
if title in membership:
thisUser[title] = 1
else:
thisUser[title] = 0
except:
print("PLEASE NOTE: no information can be retrieved about user "+user.fullName+".")
pass
writer.writerow(thisUser)
outfile.close()
print ("Ready: "+datetime.datetime.today().strftime('%c'))
print()
print('===================')
print('The CSV file can be found here:')
print(os.path.abspath(fname))
print('===================')
And here is the provide_credentials script which is used above to login to AGOL:
import json, os
from getpass import getpass
def provide_credentials():
file_with_credentials = 'my_credentials.json'
username = ''
password = ''
if os.path.exists(file_with_credentials):
with open(file_with_credentials) as f:
data = json.load(f)
username = data['username']
password = data['password']
if not username or username == 'USERNAME' or not password or password == 'PASSWORD':
username = input('Please enter your username: ')
password = getpass('Please enter your password (this will remain invisible): ')
return username, password
With the input file my_credentials.json:
{
"username":"USERNAME",
"password":"PASSWORD"
}
Happy coding!
Egge-Jan