Skip to content

Instantly share code, notes, and snippets.

@ecaldwell
Created February 12, 2016 19:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ecaldwell/7e8a993946f3aa0e2135 to your computer and use it in GitHub Desktop.
Save ecaldwell/7e8a993946f3aa0e2135 to your computer and use it in GitHub Desktop.
Replace item details for content in ArcGIS Online and Portal for ArcGIS
#!/usr/bin/env python
# Sample Usage
# python bulk_update_details.py -u https://www.arcgis.com -o username -s password -p 'path/to/details.csv'
import argparse
import json
import csv
import requests
def generateToken(username, password, portalUrl):
'''Retrieves a token to be used with API requests.'''
parameters = {'username': username,
'password': password,
'client': 'referer',
'referer': portalUrl,
'expiration': 60, # token life in minutes
'f': 'json'}
url = '{}/sharing/rest/generateToken'.format(portalUrl)
response = requests.post(url, data=parameters)
return response.json()
def getDescription(itemId, portalUrl, token):
'''Retrieves an item's description object.'''
parameters = {'token': token,
'f': 'json'}
url = '{}/sharing/rest/content/items/{}'.format(portalUrl, itemId)
response = requests.get(url, params=parameters)
return response.json()
def updateDescription(username, folder, itemId, description, portalUrl, token):
'''Uploads an ArcCatalog xml file containing metadata to an item.'''
parameters = description
parameters['token'] = token
parameters['f'] = 'json'
url = '{}/sharing/rest/content/users/{}/{}/items/{}/update'.format(portalUrl, username, folder, itemId)
response = requests.post(url, data=parameters)
return response.json()
# Run the script.
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-u', '--portal',
help=('url of the portal (e.g. '
'https://webadaptor.domain.com/arcgis)'))
parser.add_argument('-o', '--username', help='username')
parser.add_argument('-s', '--password', help='password')
parser.add_argument('-c', '--csv', help='path to new details csv')
# Read the command line arguments.
args = parser.parse_args()
portal = args.portal
username = args.username
password = args.password
detailsCsv = args.csv
# Get an authentication token to use with subsequent requests.
print('Authenticating')
token = generateToken(username=username, password=password, portalUrl=portal)['token']
with open(detailsCsv, 'rb') as updateList:
updates = csv.reader(updateList, delimiter=',')
lookup = {}
headers = updates.next()
for i, header in enumerate(headers):
lookup[header] = i
for row in updates:
items = row[lookup['id']].split(',')
description = {'description': row[lookup['description']],
'licenseInfo': row[lookup['licenseInfo']],
'tags': row[lookup['tags']],
'snippet': row[lookup['snippet']]}
for item in items:
itemId = item.strip()
oldDescription = getDescription(itemId, portal, token)
username = oldDescription['owner']
folder = oldDescription['ownerFolder']
update = updateDescription(username, folder, itemId, description, portal, token)
print update
id Dataset Name snippet description tags licenseInfo
cc641234561b407796a7080fce5f87df Political Boundaries Political Boundaries in North America. This dataset represents political boundaries in North America. North America, Political boundaries None (Public Domain Information)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment