Skip to content

Commit

Permalink
use KB API for fetching mdo records
Browse files Browse the repository at this point in the history
  • Loading branch information
bitsgalore committed Aug 15, 2018
1 parent 8705d49 commit 0720585
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 36 deletions.
5 changes: 2 additions & 3 deletions iromlab/cdworker.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
from . import verifyaudio
from . import mdo


def mediumLoaded(driveName):
"""Returns True if medium is loaded (also if blank/unredable), False if not"""

Expand Down Expand Up @@ -296,9 +295,9 @@ def processDisc(carrierData):

# 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:
successMdoWrite = mdo.writeMDORecord(PPN, dirOut)
if not successMdoWrite:
success = False
reject = True
logging.error("Could not write metadata from KB-MDO")
Expand Down
50 changes: 17 additions & 33 deletions iromlab/mdo.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,34 @@
#! /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
"""
"""Fetch MDO record for a PPN and write it to file"""

import os
import io
import requests
import urllib
import logging
import xml.etree.ElementTree as ETree
from .kbapi import sru

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'
sruSearchString = '"PPN=' + str(PPN) + '"'
response = sru.search(sruSearchString, "GGC")

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
if not response:
logging.error("No matching metadata record found in KB-MDO")
success = False
else:
record_data = r.content.decode("utf-8")
recordData = next(response.records).record_data
recordAsString = ETree.tostring(recordData, encoding='UTF-8', method='xml')

try:
with io.open(fileOut, "w", encoding="utf-8") as fOut:
fOut.write(str(record_data))
with io.open(fileOut, "wb") as fOut:
fOut.write(recordAsString)
fOut.close()
wroteMDORecord = True
success = True
except IOError:
wroteMDORecord = False
logging.error("Could not write KB-MDO metadata to file")
success = False

return wroteMDORecord
return success

0 comments on commit 0720585

Please sign in to comment.