Skip to content

Instantly share code, notes, and snippets.

@amarinelli
Created March 25, 2015 16:31
Show Gist options
  • Select an option

  • Save amarinelli/6a59b9a6cc9ca7a62bf5 to your computer and use it in GitHub Desktop.

Select an option

Save amarinelli/6a59b9a6cc9ca7a62bf5 to your computer and use it in GitHub Desktop.
Get a formatted JSON representation of an item's data hosted in ArcGIS Online
import json
import tortilla
class AGOL:
""" A class for administering an ArcGIS Online account"""
def __init__(self, in_username, in_password, expiration=60):
self.agol = tortilla.wrap('https://www.arcgis.com')
self.username = in_username
self.password = in_password
self.expiration = expiration
self.token = self.gen_token()
def gen_token(self):
""" Returns a token given a username and password """
param = dict(username=self.username, password=self.password, expiration=self.expiration, client='referer',
referer='https://www.arcgis.com', f='json')
token = self.agol.sharing.rest.generateToken.post(params=param)
if hasattr(token, 'error'):
print token.error.message
print token.error.details
else:
return token
def item_data(self, item_id):
""" Returns the data for a given AGOL item """
param = dict(token=self.token.token, f='json')
data = self.agol.sharing.rest.content.items(item_id).data.get(params=param)
if hasattr(data, 'error'):
print data.error.message
print data.error.details
else:
return data
if __name__ == "__main__":
username = 'username'
password = 'password'
agol = AGOL(username, password)
print "\nITEM DATA"
print "========="
item = raw_input('Item id: ')
data = agol.item_data(item)
print json.dumps(obj=data, indent=4)
print "\nfinished"
@amarinelli

Copy link
Copy Markdown
Author

Requires the tortilla package
pip install tortilla

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment