forked from UTS-eResearch/omeka-datacrate-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreconstitute_omeka_s.py
More file actions
268 lines (205 loc) · 8.14 KB
/
reconstitute_omeka_s.py
File metadata and controls
268 lines (205 loc) · 8.14 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
"""
Quick and dirty script to push Datacrate to Omeka
"""
import json
import shelve
import requests
import sys
import logging
import os
import hashlib
import re
import argparse
import time
import subprocess
import copy
class Properties():
def __init__(self):
self.prop_ids = {}
def get_prop_id(self, nom):
if nom in self.prop_ids:
return self.prop_ids[nom]
else:
name = nom
# Filthy hack but datacrate only supports direct mapping from Key to ID
url = "%sproperties?%s&per_page=1&term=%s" % (host_url, auth, name)
print(url)
res = requests.get(url)
prop_data = res.json()
if prop_data and "o:id" in prop_data[0]:
self.prop_ids[nom] = prop_data[0]["o:id"]
print("Getting prop.", nom, self.prop_ids[nom])
return self.prop_ids[nom]
class JSON_Index():
def __init__(self, json_ld):
self.json_ld = json_ld
self.item_by_id = {}
for item in json_ld["@graph"]:
self.item_by_id[item["o:id"]] = item
class Resource_classes():
def __init__(self):
self.prop_ids = {}
def get_class_id(self,names):
if not isinstance(names, list):
names = [names]
for nom in names:
if nom in self.prop_ids:
return self.prop_ids[nom]
elif nom in context:
name = context[nom]
if "o:id" in name:
name = name["o:id"]
url = "%sresource_classes?%s&per_page=100000&term=%s" % (host_url, auth, name)
r = requests.get(url)
#print(r.content)
prop_data = r.json()
if prop_data and "o:id" in prop_data[0]:
self.prop_ids[nom] = prop_data[0]["o:id"]
return self.prop_ids[nom]
else:
#print("Can't find this class", name )
return None
props = Properties()
classes = Resource_classes()
# TODO - turn these into parameters
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--credential', default=None, help='Omeka API Key')
parser.add_argument('-i', '--identity', default=None, help='Omeka API identify')
parser.add_argument('-u', '--api-url',default=None, help='Omeka API Endpoint URL (hint, ends in /api/)')
parser.add_argument('-s', '--shelf', default="saved_data", help='Where to stash intermediate results')
parser.add_argument('infile', help='Catalog file to load')
args = vars(parser.parse_args())
root_dir, _ = os.path.split(args['infile'])
with open(args["infile"], 'r') as cat:
dc = json.load(cat)
files_dir, _ = os.path.split(args["infile"])
key_identity = args["identity"]
key_credential = args["credential"]
host_url = args["api_url"]
if not host_url.endswith("/"):
host_url += "/"# "http://localhost/api/"
# Don't treat files as primary objects only upload to parent item with hasFile
shelf = shelve.open(args["shelf"]) # Remember what's been uploaded before keyed by datacrate ID
lookup_collection = {} # Keyed by datacrate id, lists omeka IDs for collections
auth = "key_identity=%s&key_credential=%s" % (key_identity, key_credential)
json_header = {'Content-Type': 'application/json'};
media_index = {}
for media_item in dc["media"]:
media_index[media_item["@id"]] = media_item
def global_id(type, id):
return type + str(id)
def upload_media_item(item, parent_item_id):
#path = "makefile" # hee!
if global_id("media", item["@id"]) in shelf:
item["o:id"] = shelf[global_id("media",item["@id"])]["o:id"]
path = os.path.join(files_dir, str(item["o:id"]), item["o:filename"])
print("Uploading", path)
data = {
"o:ingester": "upload",
"file_index": "0",
"o:item": {"o:id": str(parent_item_id)}
}
params = {
'key_identity': args["identity"],
'key_credential': args["credential"]
}
files = [
('data', (None, json.dumps(data), 'application/json')),
('file[0]', (path, open(path, 'rb'), 'image/jpg'))
]
url = "%smedia" % (host_url)
r = requests.post(url, files=files, params=params)
print(r.content)
if "errors" in r.json():
print("errors", r.json())
else:
shelf[global_id("media", item["@id"])] = r.json()
return item["@id"]
# Item sets (collections)
def get_id_from_thing(thing):
if "@id" in thing:
return thing["@id"]
else:
return None
def get_simple_id_from_thing(thing):
if "o:id" in thing:
return thing["o:id"]
else:
return None
def check(value, key, to_remove):
update_thing_ids(value)
id = get_id_from_thing(value)
#print("ID", id)
if id:
glob_id = get_id_from_thing(value)
#print(glob_id)
if glob_id and glob_id in shelf:
#print("Found an id", id, "for key", key)
simple_id = get_simple_id_from_thing(shelf[glob_id])
if simple_id:
value["o:id"] = simple_id
else:
#print("Can't find a simple ID for ", id)
to_remove.add(key)
else:
to_remove.add(key)
#else "id" in value:
# to_remove.add(key) # Get rid of this at least for now TODO: Check
def update_thing_ids(thing):
""" Not sure if this is needed but it removes references to things that don't exist in the saved_data shelf"""
to_remove = set()
for key, value in thing.items():
if isinstance(value, list):
for item in value:
if isinstance(item, dict):
check(item, key, to_remove)
elif isinstance(value, dict):
check(value, key, to_remove)
for remove in to_remove:
#print("Removing", remove)
thing.pop(remove)
def upload_anything(type, first_pass=True):
for item in dc[type]:
item_to_upload = copy.deepcopy(item)
media_to_upload = []
id = get_id_from_thing(item_to_upload)
if id:
keys_to_lose = set()
if "o:media" in item_to_upload:
media_to_upload = item_to_upload["o:media"]
print("Saving media to upload", media_to_upload)
update_thing_ids(item_to_upload)
#if "o:slug" in item_to_upload:
# item_to_upload.pop("o:slug")
if id in shelf and "o:id" in shelf[id]:
if first_pass:
break # this thing is already uploaded
prev_id = shelf[id]["o:id"]
item_to_upload["o:id"] = prev_id
print("Redoing old one: ", id, "to id", prev_id)
url = "%s%s/%s?%s" % (host_url, type, prev_id, auth)
r = requests.put(url, data=json.dumps(item_to_upload), headers=json_header)
else:
print("Making new: ", id)
url = "%s%s?%s" % (host_url, type, auth)
if "o:id" in item_to_upload:
item_to_upload.pop("o:id")
#print(item_to_upload)
r = requests.post(url, data=json.dumps(item_to_upload), headers=json_header)
item_id = None # TODO actually set this :)
new_item = None
new_item = r.json()
if "errors" in new_item:
print("errors", r.json())
#print(json.dumps(item_to_upload, indent=3))
else:
shelf[id] = new_item
item_id = new_item["o:id"]
#print(item_to_upload)
for media_item in media_to_upload:
print("Media upload for", item_id)
upload_media_item(media_index[media_item["@id"]], item_id)
#TODO VOCABS - need to load the whole thing...
for first_pass in [True, False]:
for type in ["sites", "site_pages", "resource_templates", "item_sets", "items"]: # site_pages has problems https://github.com/omeka/omeka-s/issues/1346
upload_anything(type, first_pass)