-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoai_pmh_dc_to_marc.py
165 lines (147 loc) · 4.79 KB
/
oai_pmh_dc_to_marc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/python3
from sickle import Sickle
from pymarc import Record, Field, MARCWriter
import os
import re
# configurations
save_file = 'c:\\users\\user\\desktop\\books.dat'
# delete old file (if exists)
os.system(f'del {save_file}')
# load OAI-PMH client
# documentation: https://sickle.readthedocs.io/en/latest/
sickle = Sickle('http://content.cdlib.org/oai')
records = sickle.ListRecords(metadataPrefix='oai_dc', set='YOUR_SET_ID_HERE')
# parse harvested records and generate MARC21
for record in records:
# parse dc record
dc = record.metadata
contributors = dc.get("contributor", [])
coverages = dc.get("coverage", [])
creators = dc.get("creator", [])
dates = dc.get("date", [])
descriptions = dc.get("description", [])
formats = dc.get("format", [])
identifiers = dc.get("identifier", [])
languages = dc.get("language", [])
publishers = dc.get("publisher", [])
relations = dc.get("relation", [])
rights = dc.get("rights", [])
sources = dc.get("source", [])
subjects = dc.get("subject", [])
titles = dc.get("title", [])
# other fields
leader = ""
#collections = ['The collection is open for research use.']
# generate MARC record
marc_record = Record(force_utf8=True)
# MARC field 100
if creators:
for creator in creators:
marc_record.add_field(
Field(
tag = '100',
indicators = ['1', ''],
subfields = [
'a', f'{creator}',
]))
# MARC field 245
if titles:
for title in titles:
marc_record.add_field(
Field(
tag = '245',
indicators = ['1', '0'],
subfields = [
'a', f'{title}',
]))
# MARC field 264
if dates:
for date in dates:
marc_record.add_field(
Field(
tag = '264',
indicators = ['', '0'],
subfields = [
'c', f'{date}',
]))
# MARC field 300
if formats:
for format in formats:
format.rstrip('\n')
format = re.sub(r'\s{2}', '', format)
marc_record.add_field(
Field(
tag = '300',
indicators = ['', ''],
subfields = [
'a', f'{format}',
'f', '',
]))
# MARC field 506
if rights:
for right in rights:
marc_record.add_field(
Field(
tag = '506',
indicators = ['', ''],
subfields = [
'a', f'{right}',
]))
# MARC field 520
if descriptions:
for description in descriptions:
marc_record.add_field(
Field(
tag = '520',
indicators = ['3', ''],
subfields = [
'a', f'{description}',
]))
# MARC field 610
if subjects:
for subject in subjects:
marc_record.add_field(
Field(
tag = '610',
indicators = ['1', '0'],
subfields = [
'a', f'{subject}',
]))
# MARC field 651
if coverages:
for coverage in coverages:
marc_record.add_field(
Field(
tag = '651',
indicators = ['', '0'],
subfields = [
'a', f'{coverage}',
]))
# MARC field 700
if creators:
for creator in creators:
marc_record.add_field(
Field(
tag = '700',
indicators = ['1', ''],
subfields = [
'a', f'{creator}',
't', '',
]))
# MARC field 856
if identifiers:
for identifier in identifiers:
marc_record.add_field(
Field(
tag = '856',
indicators = ['4', '2'],
subfields = [
'3', 'Finding aid',
'u', f'{identifier}',
]))
# write to MARC output file
writer = MARCWriter(open(save_file,'ab'))
writer.write(marc_record)
writer.close()
# open up MARC record in default viewer (NOTEPAD most likely)
os.system(save_file)