-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_inventory.py
207 lines (173 loc) · 6.3 KB
/
build_inventory.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
"""
Create an inventory of content on an ArcGIS Portal
"""
import os
from collections import defaultdict
from arcgis.gis import GIS
from arcgis.mapping import WebMap
from config import Config
VERSION = '1.0'
path,exe = os.path.split(__file__)
myname = exe + ' ' + VERSION
exclude_esri = '-owner:esri -owner:esri_apps'
def get_apps(items):
dtype = {} # A dictionary of all item types
applications = [
'Application',
'Code Attachment',
'Dashboard',
'Desktop Application Template',
'Desktop Application',
'Web Experience',
'Web Mapping Application',
]
for item in items:
if item.type in applications:
# if not ('zhunt' in item.owner or "@CLATSOP" in item.owner):
if not item.type in dtype:
dtype[item.type] = {}
dtype[item.type][item.id] = item
return dtype
def generate_html(dtype):
for itype,items in dtype.items():
print('<h2>%s</h2>' % itype)
count = 0
print("<table>")
for id in items.keys():
count += 1
item = items[id]
url = item.url
if url:
if url[0:2] == '//':
url = "https:" + url
url = '<a href="%s" target="_app">URL</a> ' % url
else:
url = 'no url'
homepage = '<a href="%s" target="_details">%s</a>' % (item.homepage, str(item.title))
print('<tr><td>{0:3n}</td><td>{1}</td><td>{2}</td><td>{3}</td></tr>'.format(count, homepage, item.owner, item.access))
# keywords = str(', '.join(item.typeKeywords))
# print('keywords:', keywords)
print('</table>')
print()
return
def inventory_maps(gis, query=''):
q = query + ' ' + exclude_esri
list_of_maps = gis.content.search(q, item_type='web map', max_items=-1)
print("Maps found %d" % len(list_of_maps))
# Build a dictionary with each layer as the index
# and a list of the maps that the layer participates in
layer_dict = defaultdict(list)
for item in list_of_maps:
# Look up the layers.
wm = WebMap(item)
mapId = wm.item.id
for l in wm.layers:
try:
layer_dict[l.itemId].append(mapId)
pass
except Exception as e:
layer_dict[l.id].append(mapId)
pass
# Each item is indexed by a layer id and contains a list of the maps containing that id.
print(layer_dict)
# Now make another dictoinary that is indexed by type.
dtype = defaultdict(dict)
for item in list_of_maps:
dtype[item.type][item.id] = item
print(dtype)
def inventory_services(gis) -> None:
interesting_services = list()
interesting_types = ['Map Service', 'Feature Service']
urls = list()
myservers = gis.admin.servers.list()
for f in myservers[0].services.folders:
services = myservers[0].services.list(folder=f)
print("Checking folder=\"%s\"; %d services." % (f, len(services)))
for s in services:
properties = s.iteminformation.properties
try:
if properties['type'] in interesting_types:
interesting_services.append(s)
else:
print(properties['title'], ':', properties['type'])
except KeyError:
if 'GPServer' in s.url:
continue
if 'GeometryServer' in s.url:
continue
if 'VectorTileServer' in s.url:
continue
if 'ImageServer' in s.url:
continue
urls.append(s.url)
# These did not have proprties,
# look like mostly Hosted
#print(urls)
for s in interesting_services:
properties = s.iteminformation.properties
if properties['type'] == 'Map Service':
print(s.url)
continue
else:
print(properties)
if __name__ == "__main__":
# Weird stuff happens if these are not defined.
assert(Config.PORTAL_URL)
assert(Config.SERVER_URL)
# See arcgis.gis.ContentManager
# For query definition, refer to http://bitly.com/1fJ8q31
#q = "title:Clatsop County Template"
#q = "owner:bwilson@CLATSOP"
gis = GIS(profile=os.environ.get('USERNAME'))
#inventory_maps(gis)
types = [
'AppBuilder Extension',
'Application',
'Desktop Application','Desktop Application Template',
'Site Application',
'Document Link',
'Administrative Report',
'Form', 'Site Page',
'CSV', 'Microsoft Excel', 'Microsoft Word', 'PDF', 'KML',
'Map Area',
'WMS', 'WMTS',
'Code Attachment', 'Code Sample',
'Geoprocessing Service',
'Dashboard', 'StoryMap', 'StoryMap Theme',
'Web Experience', 'Web Mapping Application',
'Image',
'Geometry Service',
'Feature Service',
'Shapefile',
'Layer Package', 'Tile Package',
'SQLite Geodatabase',
'Vector Tile Service', 'Vector Tile Package',
'Web Scene', 'Service Definition', 'Map Service',
'Web Map', ]
cm = gis.content
q = 'title:EGDB_surveys -owner:esri -owner:esri_apps -owner:esri_nav'
items = cm.search(q, item_type='Feature Service', max_items=-1)
print("Feature Services", len(items))
for item in items:
print(item.title, item.type)
try:
for l in item.layers:
print(l)
continue
except Exception as e:
pass
items = cm.search(q, item_type='Map Service', max_items=-1)
print("Map Services", len(items))
for item in items:
print(item.title)
for layer in item.layers:
print(layer, layer.source)
inventory_services(gis)
print("That's all!")
# q = "NOT owner:esri_apps"
# items = gis.content.search(q, outside_org=False, max_items=5000)
# print("Items found %d" % len(items))
# dtype = get_apps(items)
# print(dtype)
# generate_html(dtype)
# That's all!