-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathagol_util.py
134 lines (118 loc) · 4.56 KB
/
agol_util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import urllib2
import urllib
import json
import time
class AGOL_util:
"""
Minimal client library for ArcGIS online API
Parameters:
root_url: portal root rest url e.g. http://myorg.arcgis.com/sharing/rest
username: valid arggis online username
password: valid arggis online password
"""
def __init__(self, root_url, username, password):
self.url = root_url
self._check_items = []
self._uid = username
self._pwdd = password
self._validate_user(username, password)
def _validate_user(self, username, password):
'''
Requests token based on username and password
no error catching
'''
keys = {'username':username,
'password':password,
'referer':self.url,
'f':'json'}
data = urllib.urlencode(keys)
req = urllib2.Request('https://www.arcgis.com/sharing/rest/generateToken', data)
resp = json.load(urllib2.urlopen(req))
if 'token' in resp:
self._token = resp['token']
self._expiry = resp['expires']
else:
self._token = ''
self._expiry = 0
return resp
def get_token(self, nd=False):
"""
returns valid token or false on failure
"""
if self._token=='' or self._expiry <= time.time():
if nd:
return False
else:
self._validate_user(self._uid, self._pwd)
return(self.get_token(1))
return self._token
def query(self, endpoint, options):
'''
POST to url endpoint with options as data
autoappends token and json response parameters
concatinates self.url and endpoind assuming matching /'s
return as JSON
'''
options['token'] = self.get_token()
options['f'] = 'json'
data = urllib.urlencode(options)
requrl = "{}{}".format(self.url, endpoint)
req = urllib2.Request(requrl, data)
return json.load(urllib2.urlopen(req))
def add_item_from_url(self, url, options={}):
options['dataUrl'] = url
options['async'] = 'true'
options['overwrite'] = 'true'
return self.query('/content/users/{}/addItem'.format(self._uid), options)
def add_shapefile_from_url(self, url, options={}):
""" URL should point to zipped shapefile """
options['type'] = 'Shapefile'
return self.add_item_from_url(url, options)
def get_item_status(self, itemId):
url = '/content/users/{}/items/{}/status'.format(self._uid, itemId)
return self.query(url, {})
def wait_for_completion(self, itemId, timeout=60):
'''
Check every second for item status to return completed
Return:
true on completion
false on timeout or error
'''
res = self.get_item_status(itemId)
t = 0
while 'status' in res and t < timeout:
if res['status'] == 'completed':
return True
t += 1
time.sleep(1)
res = self.get_item_status(itemId)
return False
def update_item(self, itemId, options):
url = '/content/users/{}/items/{}/update'.format(self._uid, itemId)
return self.query(url, options)
def share_items(self, items, everyone=None, org=None, groups=None):
""" shares items defined by item ids with given groups, org, or everyone """
options = {}
if groups is not None:
options['groups'] = groups
if everyone is not None:
options['everyone'] = everyone
if org is not None:
options['org'] = org
if type(items) == list:
items = ','.join(items)
options['items'] = items
return self.query('/content/users/{}/shareItems'.format(self._uid), options)
def publish_item(self, itemId, options, publishParameters):
options['itemID'] = itemId
options['publishParameters'] = json.dumps(publishParameters)
options['overwrite'] = 'true'
return self.query('/content/users/{}/publish'.format(self._uid), options)
def publish_shapefile(self, itemId, options={}, publishParameters={}):
options['fileType'] = 'shapefile'
if 'name' not in publishParameters:
publishParameters['name'] = itemId
return self.publish_item(itemId, options, publishParameters)
def delete_item(self, itemId):
url = '/content/users/{}/items/{}/delete'.format(self._uid, itemId)
return self.query(url)