Clean up ISBN fields while we're processing
authorDan Scott <dscott@laurentian.ca>
Fri, 1 Sep 2017 20:09:51 +0000 (16:09 -0400)
committerDan Scott <dscott@laurentian.ca>
Fri, 1 Sep 2017 20:09:51 +0000 (16:09 -0400)
Might as well put junk into 020q if we have it.

Signed-off-by: Dan Scott <dscott@laurentian.ca>
tools/ebooks/prep_ebook_records.py

index 063c2a2..9a9dc5c 100755 (executable)
@@ -506,6 +506,10 @@ def process_fields(record, options):
         # except for 924 fields that we create
         elif field.tag[0] == '9' and field.tag != '924':
             pass
+        # ISBN cleanup
+        elif field.tag == '020':
+            new_isbn = create_clean_isbn(field)
+            new_record.add_ordered_field(new_isbn)
         # Strip out 300 fields that only contain placeholders
         elif field.tag == '300' and field['a'] == 'p. cm.':
             pass
@@ -945,6 +949,26 @@ def add_marc_source(record, options):
     )
     record.add_ordered_field(marc_source)
 
+def create_clean_isbn(field):
+    """Move 020a junk to 020q"""
+    
+    if not field.get_subfields('a') or ' ' not in field['a']:
+        return field
+
+    isbn = pymarc.Field(
+        tag = '020',
+        indicators=[field.indicator1, field.indicator2]
+    )
+    for sf in field:
+        if sf[0] == 'a' and ' ' in sf[1]:
+            junk = sf[1].strip()
+            junk = junk[junk.find(' '):].strip()
+            isbn.add_subfield('a', clean_isbn(sf[1]))
+            isbn.add_subfield('q', junk)
+        else:
+            isbn.add_subfield(sf[0], sf[1])
+    return isbn
+
 def process_urls(field, options, publisher):
     """Creates 856 fields required by Conifer"""