From 8705d494f5ad106d3acbe95aec0b6438206da03d Mon Sep 17 00:00:00 2001 From: Johan van der Knijff Date: Tue, 14 Aug 2018 18:33:34 +0200 Subject: [PATCH] write MDO record to output dir --- iromlab/cdworker.py | 11 ++++++++++ iromlab/mdo.py | 50 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 iromlab/mdo.py diff --git a/iromlab/cdworker.py b/iromlab/cdworker.py index 3b6960e..7c83288 100644 --- a/iromlab/cdworker.py +++ b/iromlab/cdworker.py @@ -23,6 +23,7 @@ from . import isobuster from . import dbpoweramp from . import verifyaudio +from . import mdo def mediumLoaded(driveName): @@ -107,6 +108,7 @@ def processDisc(carrierData): """Process one disc / job""" jobID = carrierData['jobID'] + PPN = carrierData['PPN'] logging.info(''.join(['### Job identifier: ', jobID])) logging.info(''.join(['PPN: ', carrierData['PPN']])) @@ -292,6 +294,15 @@ def processDisc(carrierData): logging.info(''.join(['isolyzerSuccess: ', str(isolyzerSuccess)])) logging.info(''.join(['imageTruncated: ', str(imageTruncated)])) + # Fetch metadata from KBMDO and store as file + logging.info('*** Writing metadata from KB-MDO to file ***') + wroteMDORecord = mdo.writeMDORecord(PPN, dirOut) + + if not wroteMDORecord: + success = False + reject = True + logging.error("Could not write metadata from KB-MDO") + # Generate checksum file logging.info('*** Computing checksums ***') successChecksum = checksumDirectory(dirOut) diff --git a/iromlab/mdo.py b/iromlab/mdo.py new file mode 100644 index 0000000..cbae3ed --- /dev/null +++ b/iromlab/mdo.py @@ -0,0 +1,50 @@ +#! /usr/bin/env python +""" +Quick and dirty module for writing entire MDO record for a PPN to file +using SRU query. Code based on kbapi.sru.py +""" + +import os +import io +import requests +import urllib + +def writeMDORecord(PPN, writeDirectory): + """Write MDO record for a PPN to file""" + + fileOut = os.path.join(writeDirectory, "meta-kbmdo.xml") + sruSearchString = '"PPN=' + PPN + '"' + + SRU_BASEURL = 'http://jsru.kb.nl/sru/sru' + SRU_BASEURL += '?version=1.2&maximumRecords=%i' + SRU_BASEURL += '&operation=searchRetrieve' + SRU_BASEURL += '&startRecord=%i' + SRU_BASEURL += '&recordSchema=%s' + SRU_BASEURL += '&x-collection=%s&query=%s' + + maximumrecords = 1 + startrecord = 1 + recordschema = 'dcx' + collection = 'GGC' + + query = urllib.parse.quote_plus(sruSearchString) + + url = SRU_BASEURL % (maximumrecords, startrecord, + recordschema, collection, query) + r = requests.get(url) + + if r.status_code != 200: + # Status code: must be 200, otherwise an error occurred + wroteMDORecord = False + else: + record_data = r.content.decode("utf-8") + + try: + with io.open(fileOut, "w", encoding="utf-8") as fOut: + fOut.write(str(record_data)) + fOut.close() + wroteMDORecord = True + except IOError: + wroteMDORecord = False + + return wroteMDORecord