-
Notifications
You must be signed in to change notification settings - Fork 2
/
COACS_json_to_marc.py
executable file
·331 lines (263 loc) · 9.11 KB
/
COACS_json_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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#!/usr/bin/env python3
import argparse
import os
# map from IANA 2-character language codes to ISO639-2 3-character language codes, as used in MARC
lang_map = {
"af": "afr",
"an": "arg",
"ar": "ara",
"az": "aze",
"be": "bel",
"bg": "bul",
"br": "bre",
"bs": "bos",
"ca": "cat",
"cs": "cze",
"cy": "wel",
"da": "dan",
"de": "ger",
"el": "gre",
"en": "eng",
"eo": "epo",
"es": "spa",
"et": "est",
"eu": "baq",
"fa": "per",
"fi": "fin",
"fo": "fao",
"fr": "fre",
"gl": "glg",
"he": "heb",
"hr": "hrv",
"hu": "hun",
"id": "ind",
"it": "ita",
"ja": "jpn",
"jv": "jav",
"ka": "geo",
"ko": "kor",
"ku": "kur",
"ky": "kir",
"la": "lat",
"lb": "ltz",
"lo": "lao",
"lt": "lit",
"lv": "lav",
"mg": "mlg",
"mk": "mac",
"mr": "mar",
"mt": "mlt",
"nl": "dut",
"no": "nno",
"oc": "oci",
"pl": "pol",
"pt": "por",
"ro": "rum",
"ru": "rus",
"sk": "slo",
"sl": "slv",
"sr": "srp",
"sv": "swe",
"sw": "swa",
"tl": "tgl",
"tr": "tur",
"uk": "ukr",
"ur": "urd",
"vi": "vie",
"wa": "wln",
"zh": "chi"
}
# In[6]:
# extract relevant bibliographic information from the AWOL index json dump
# and produce marc files for use in library catalogues
import glob
import json
import re
from pymarc import Record, Field
from pymarc import MARCReader
import time
from datetime import timedelta
start_time = time.time()
# marc fields for each journal record
def json_to_marc(infilename, outfilename):
print('Processing: ' + infilename) #progress message
data = json.load(open(infilename, "r"))
record = Record(force_utf8=True) #create MARC record, enforce Unicode
# add fields 006, 007 and 008 with minimal physical information to every marc file
record.add_field(
Field(
tag = '006',
data = "m"))
record.add_field(
Field(
tag = '007',
data = "cr"))
# the iana language code from the json file is taken, checked against the list of language codes,
# substituted with its iso639-2 equivalent and put in position 21-24 of the field 008 content
field008val = " o 0eng d" # DEFAULT ENG
try:
if 'languages' in data and data['languages'][0] is not None:
field008val = field008val[0:21] + lang_map.get(data['languages'][0], " ") + field008val[24:]
except IndexError:
field008val = field008val[0:21] + " " + field008val[24:]
record.add_field(
Field(
tag = '008',
data = field008val))
# extract issn, in json 'generic' and/or 'electronic', and put into separate subfields of 022
if "identifiers" in data and "issn" in data["identifiers"]:
field_issn = Field(
tag='022',
indicators=['0', '#']
)
if "generic" in data["identifiers"]["issn"]:
field_issn.add_subfield('a', data["identifiers"]["issn"]["generic"][0])
if "electronic" in data["identifiers"]["issn"]:
field_issn.add_subfield('l', data["identifiers"]["issn"]["electronic"][0])
record.add_field(field_issn)
# title of the series or journal
if data["is_part_of"] is not None and data["is_part_of"]['title_full']:
record.add_field(
Field(
tag = '245',
indicators=['0', '0'],
subfields=["a", data["is_part_of"]["title_full"][:9000]]))
if data["title"]:
record.add_field(
Field(
tag='246',
indicators=['0', '0'],
subfields=["a", data["title"][:9000]])
)
if data["year"]:
record.add_field(
Field(
tag="260",
indicators=["#", "#"],
subfields=["c", data["year"]]))
# add field 506 to all records, as not present in all json files
record.add_field(
Field(
tag='506',
indicators=['0', '#'],
subfields=["a", "Open access"])
)
# some json files contain a very long description; the maximum length of data in a variable field
#in MARC21 is 9,999 bytes, so here only a certain amount of content is put into the 520 field
if data["description"]:
record.add_field(
Field(
tag='520',
indicators=['2', '#'],
subfields=["a", data["description"][:9000]])
)
# keep together the journal url, host and domain as different subfields of field 856
# check if either exists, before initializing a new field instance
if data['url'] or (data['is_part_of'] is not None and data['is_part_of']['url']):
field = Field(
tag='856',
indicators=['0', '0']
)
if data['domain']:
field.add_subfield('a', data['domain'])
if data['is_part_of'] is not None and data['is_part_of']['url']:
field.add_subfield('d', data['is_part_of']['url'])
if data['url']:
field.add_subfield('u', data['url'])
record.add_field(field)
if data["volume"]:
record.add_field(
Field(
tag='866',
indicators=['0', '0'],
subfields=["a", data["volume"]])
)
#output marc file with same filename in Output directory
out = open(outfilename, 'wb')
out.write(record.as_marc())
out.close()
# execute function for creating separate records for subordinate resources
if data['subordinate_resources'] is not None:
subordinate_records = create_subordinate_records(record, data['subordinate_resources'])
counter = 0
# add counter and "-sub" to filenames of subordinate records
for subordinate_record in subordinate_records:
out = open(outfilename.replace(".marc", "-sub"+str(counter)+".marc"), 'wb')
out.write(subordinate_record.as_marc())
out.close()
counter = counter + 1
def create_subordinate_records(parent_record, subordinate_data_list):
'''If a journal record includes a list of individual issues or volumes, this function creates separate marc files for each of those issues or volumes. The journal title and url are taken from the parent record (the journal record) and kept in the subordinate records.'''
result_list = []
for subordinate_resource in subordinate_data_list:
sub_record = Record(force_utf8=True)
# add fields 006, 007 and 008 with minimal physical information to every marc file
if 'title_full' in subordinate_resource:
sub_record.add_field(
Field(
tag = '006',
data = "m"))
sub_record.add_field(
Field(
tag = '007',
data = "cr"))
# the value of field 008 is taken from the parent record and put into the subordinate one
field008val = " o 0eng d" # DEFAULT ENG
if 'languages' in parent_record and parent_record['languages'] is not None:
field008val = field008val[0:21] + lang_map.get(parent_record['languages'][0], " ") + field008val[24:]
sub_record.add_field(
Field(
tag = '008',
data = field008val))
sub_record.add_field(
Field(
tag='245',
indicators=['0', '0'],
subfields=['a', subordinate_resource['title_full'][:9000]]
)
)
sub_record.add_field(
Field(
tag='506',
indicators=['0', '#'],
subfields=["a", "Open access"])
)
if parent_record['246']['a']:
sub_record.add_field(
Field(
tag='490',
indicators=['0', '0'],
subfields=['a', parent_record['246']['a']])
)
# put together the issue/volume url, the journal url and the domain in field 856;
# domain and journal url taken from the parent record, issue/volume url taken from the subordinate record
if 'url' in subordinate_resource:
current_field = Field(
tag='856',
indicators=['0', '0']
)
current_field.add_subfield('u', subordinate_resource['url'])
if parent_record['856']['a']:
current_field.add_subfield('a', parent_record['856']['a'])
if parent_record['856']['u']:
current_field.add_subfield('d', parent_record['856']['u'])
sub_record.add_field(
current_field
)
result_list.append(sub_record)
return result_list
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='json to marc')
parser.add_argument('input_dir', type=str,
help='put path to input directory')
parser.add_argument('out_dir',
help='put path to output direct0ry')
args = parser.parse_args()
for parent, dir_names, file_names in os.walk(args.input_dir):
for fn in file_names:
if fn.endswith(".json" ):
infilepath = os.path.join(parent, fn)
marc_fn = fn.replace(".json", ".marc")
outfilepath = os.path.join(args.out_dir, marc_fn)
print(infilepath, outfilepath)
json_to_marc(infilepath, outfilepath)