-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8705d49
commit 0720585
Showing
2 changed files
with
19 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |