Skip to content

Commit 8ad63f2

Browse files
committed
Refactor bitstream export & add a quick version lveraging Solr as much s possible.
1 parent f7e4bc9 commit 8ad63f2

3 files changed

Lines changed: 101 additions & 54 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ The steps to set up a validation run.
192192
3. `./dspace_api_exports.py` to export (CSV) DSpace metadata
193193

194194
* Option for items and bitstreams: `--random_sample_by_percentage` with default 100% and range between 1-100.
195+
* Option for items and bitstreams: `--dso_type=item_quick` and `--dso_type=bitstream_guick` which leverage Solr as much as possible to reduce number of API calls (not as well tested)
195196

196197
```bash
197198
# Set environment variables

src/dspace_api_exports.py

Lines changed: 38 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import sys
2020

2121
from dspace_rest_client.client import DSpaceClient
22+
from dspace_rest_client.models import Bundle
2223

2324
from utils import utilities as utils
2425

@@ -171,7 +172,7 @@ def process_items_quick(dspace_client, output_file):
171172
logging.info("Count: [%d]", item_count_total)
172173

173174

174-
def process_bitstreams(dspace_client, output_file, random_sample_by_percentage):
175+
def process_bitstreams(dspace_client, output_file, random_sample_by_percentage=100):
175176
"""
176177
Process bitstreams: mainly for existence checks and bitstream checksums
177178
"""
@@ -187,65 +188,45 @@ def process_bitstreams(dspace_client, output_file, random_sample_by_percentage):
187188
logging.info("Not in random sample: %s (%s)", item.name, item.uuid)
188189
continue
189190

190-
if "ual.jupiterId" in item.metadata:
191-
ual_jupiterid_item = utils.deconstruct_list_of_dicts_to_a_single_value(
192-
item.metadata["ual.jupiterId"]
193-
)
194-
else:
195-
ual_jupiterid_item = None
196191
bundles = dspace_client.get_bundles(parent=item)
197192
for bundle in bundles:
198193
bitstreams = dspace_client.get_bitstreams(bundle=bundle)
199-
for bitstream in bitstreams:
200-
logging.info("%s (%s)", bitstream.name, item.uuid)
201-
logging.debug("%s", bitstream.to_json_pretty())
202-
logging.debug("%s", bundle.to_json_pretty())
203-
tmp_dict = {
204-
"item.handle": item.handle,
205-
"item.uuid": item.uuid,
206-
"item.name": item.name,
207-
"provenance.ual.jupiterId.item": ual_jupiterid_item,
208-
"bundle.name": bundle.name,
209-
"bitstream.name": bitstream.name,
210-
"bitstream.bundleName": bitstream.bundleName,
211-
"bitstream.checksum.value": bitstream.checkSum["value"],
212-
"bitstream.checksum_algorithm": bitstream.checkSum[
213-
"checkSumAlgorithm"
214-
],
215-
"bitstream.sizeBytes": bitstream.sizeBytes,
216-
"bitstream.sequenceId": bitstream.sequenceId,
217-
"bitstream.id": bitstream.id,
218-
"bitstream.uuid": bitstream.uuid,
219-
}
220-
if "dc.title" in bitstream.metadata:
221-
tmp_dict.update(
222-
{
223-
"bitstream.metadata.dc.title": bitstream.metadata[
224-
"dc.title"
225-
][0]["value"]
226-
}
227-
)
228-
if "dc.source" in bitstream.metadata:
229-
tmp_dict.update(
230-
{
231-
"bitstream.metadata.dc.source.0.value": bitstream.metadata[
232-
"dc.source"
233-
][0]["value"]
234-
}
235-
)
236-
if "dc.description" in bitstream.metadata:
237-
tmp_dict.update(
238-
{
239-
"bitstream.metadata.dc.description": bitstream.metadata[
240-
"dc.description"
241-
][0]["value"],
242-
}
243-
)
244-
245-
utils.output_writer(tmp_dict, "bitstream", writer)
194+
utils.output_bitstream(item, bundle, bitstreams, writer)
195+
246196
logging.info("Count: [%d]", count)
247197

248198

199+
def process_bitstreams_quick(dspace_client, output_file):
200+
"""
201+
Process bitstreams: mainly for existence checks and bitstream checksums
202+
Use the search API in this version
203+
"""
204+
writer = utils.output_init(output_file, "bitstream")
205+
items = dspace_client.search_objects_iter(
206+
query="*:*", dso_type="item", embeds=["bundles"]
207+
)
208+
count = 0
209+
for count, item in enumerate(items, start=1):
210+
logging.info("Item: %s", item.to_json_pretty())
211+
# refresh auth token
212+
213+
if count % 5000 == 0:
214+
dspace_client.refresh_token()
215+
216+
if "bundles" in item.embedded:
217+
# this could be a bug in the discovery search API the need have
218+
# ["_embedded"]["bundles"]
219+
logging.info("Embed: %s", item.embedded["bundles"]["_embedded"]["bundles"])
220+
for bundle in item.embedded["bundles"]["_embedded"]["bundles"]:
221+
logging.info("Bundle: %s", bundle)
222+
if bundle.get("name", None) == "ORIGINAL":
223+
bundle_obj = Bundle(bundle)
224+
bitstreams = dspace_client.get_bitstreams(bundle=bundle_obj)
225+
utils.output_bitstream(item, bundle_obj, bitstreams, writer)
226+
227+
logging.info("Count items (not actual bitstreams): [%d]", count)
228+
229+
249230
def process_users(dspace_client, output_file):
250231
"""
251232
Process users
@@ -334,6 +315,8 @@ def process(dspace_client, output_file, dso_type, random_sample_percentage):
334315
process_items_quick(dspace_client, output_file)
335316
case "bitstreams":
336317
process_bitstreams(dspace_client, output_file, random_sample_percentage)
318+
case "bitstreams_quick":
319+
process_bitstreams_quick(dspace_client, output_file)
337320
case "users":
338321
process_users(dspace_client, output_file)
339322
case "collection_stats":
@@ -356,6 +339,7 @@ def main():
356339
# between the requested page size, actual result size and # of pages
357340
# If 512 items and size is set to 500,
358341
# https://github.com/DSpace/DSpace/issues/8723
342+
# http://198.168.187.81:8080/server/api/discover/search/objects?dsoType=collection&page=0&size=500
359343
dspace_client.ITER_PAGE_SIZE = 100
360344

361345
# Configure logging

src/utils/utilities.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,57 @@ def output_writer(dso, dso_type, writer, output_type="csv", embbed=None):
377377
sys.exit()
378378

379379

380+
def output_bitstream(item, bundle, bitstreams, writer):
381+
"""
382+
Writer a Bistream export to the CSV file
383+
"""
384+
for bitstream in bitstreams:
385+
logging.info("%s (%s)", bitstream.name, item.uuid)
386+
logging.debug("%s", bitstream.to_json_pretty())
387+
logging.debug("%s", bundle.to_json_pretty())
388+
tmp_dict = {
389+
"item.handle": item.handle,
390+
"item.uuid": item.uuid,
391+
"item.name": item.name,
392+
"provenance.ual.jupiterId.item": get_ual_jupiter_item_id(item),
393+
"bundle.name": bundle.name,
394+
"bitstream.name": bitstream.name,
395+
"bitstream.bundleName": bitstream.bundleName,
396+
"bitstream.checksum.value": bitstream.checkSum["value"],
397+
"bitstream.checksum_algorithm": bitstream.checkSum["checkSumAlgorithm"],
398+
"bitstream.sizeBytes": bitstream.sizeBytes,
399+
"bitstream.sequenceId": bitstream.sequenceId,
400+
"bitstream.id": bitstream.id,
401+
"bitstream.uuid": bitstream.uuid,
402+
}
403+
if "dc.title" in bitstream.metadata:
404+
tmp_dict.update(
405+
{
406+
"bitstream.metadata.dc.title": bitstream.metadata["dc.title"][0][
407+
"value"
408+
]
409+
}
410+
)
411+
if "dc.source" in bitstream.metadata:
412+
tmp_dict.update(
413+
{
414+
"bitstream.metadata.dc.source.0.value": bitstream.metadata[
415+
"dc.source"
416+
][0]["value"]
417+
}
418+
)
419+
if "dc.description" in bitstream.metadata:
420+
tmp_dict.update(
421+
{
422+
"bitstream.metadata.dc.description": bitstream.metadata[
423+
"dc.description"
424+
][0]["value"],
425+
}
426+
)
427+
428+
output_writer(tmp_dict, "bitstream", writer)
429+
430+
380431
def get_provenance_ual_jupiter_id(dso, key):
381432
"""
382433
Get the DC provenance UAL Jupiter ID from the collection
@@ -449,6 +500,17 @@ def get_provenance_ual_jupiter_collection_id(dspace_client, item):
449500
return ret
450501

451502

503+
def get_ual_jupiter_item_id(item):
504+
"""
505+
Get the jupiter ID from an Item
506+
"""
507+
if "ual.jupiterId" in item.metadata:
508+
return deconstruct_list_of_dicts_to_a_single_value(
509+
item.metadata["ual.jupiterId"]
510+
)
511+
return None
512+
513+
452514
def get_access_rights(dspace_client, item):
453515
"""
454516
Get the access rights assocated with the Item

0 commit comments

Comments
 (0)