-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarc3f.py
382 lines (300 loc) · 11.3 KB
/
marc3f.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import re
import pymarc
from pymarc import Subfield, Record, Field, MARCWriter
from datetime import datetime
from libgutenberg import GutenbergDatabase
from libgutenberg.DublinCoreMapping import DublinCoreObject
from os.path import join
def stub(dc):
record = pymarc.Record()
now = datetime.now()
# c - Corrected or revised, a - Language material, m - Monograph/Item, 3 - Abbreviated level, u - Unknown
record.leader[5] = 'c'
record.leader[6] = 'a'
record.leader[7] = 'm'
record.leader[17] = '3'
record.leader[18] = 'u'
field001 = pymarc.Field(tag='001', data=str(dc.project_gutenberg_id))
record.add_ordered_field(field001)
field003 = pymarc.Field(tag='003', data='UtSlPG')
record.add_ordered_field(field003)
# m - Computer file/Electronic resource - Coded data elements relating to either a computer file or an electronic resource in form.
field006 = pymarc.Field(tag='006', data='m')
record.add_ordered_field(field006)
# c - Electronic resource, r - Remote, n - Not applicable
field007 = pymarc.Field(tag='007', data='cr n')
record.add_ordered_field(field007)
# 008 in looking at pub date some have a 906 others have a 4 digit year in 260. Have to right an expression to capture that. For position 23 could be o for online or s for electronic. May have to not code for language. Because database is not coded for MARC lang codes only for ISO639-1--use MARCtag041 instead. Position 39 cataloging source d - Other.
new_field_value = now.strftime('%y%m%d') + 's||||||||xx |||||o|||||||||||||| d'
match_found = False
for att in dc.book.attributes:
if (att.fk_attriblist == 906 and att.fk_attriblist is not None) or (att.fk_attriblist == 260 and re.search(r'\b\d{4}\b', str(att.fk_attriblist))):
new_field_value = now.strftime('%y%m%d') + 's' + str(att.text) + '||||||||xx |||||o|||||||||||||| d'
match_found = True
break
if not match_found:
new_field_value = now.strftime('%y%m%d') + '|||||||||xx |||||o|||||||||||||| d'
field008 = pymarc.Field(tag='008', data=new_field_value)
record.add_ordered_field(field008)
for att in dc.book.attributes:
if att.fk_attriblist == 10:
field010 = pymarc.Field(
tag='010',
indicators=[' ', ' '],
subfields=[
Subfield(code='a', value=str(att.text)),
]
)
record.add_ordered_field(field010)
field040 = pymarc.Field(
tag='040',
indicators=[' ', ' '],
subfields=[
Subfield(code='a', value='UtSlPG'),
]
)
record.add_ordered_field(field040)
if len(dc.languages):
field041 = pymarc.Field(
tag='041',
indicators=[' ', '7'],
subfields=[
Subfield(code='a', value=str(lang.id)) for lang in dc.languages
] + [
Subfield(code='2', value='iso639-1')
]
)
record.add_ordered_field(field041)
for att in dc.book.attributes:
if att.fk_attriblist == 240:
field240 = pymarc.Field(
tag='240',
indicators=['1', str(att.nonfiling)],
subfields=[
Subfield(code='a', value=str(att.text)),
]
)
record.add_ordered_field(field240)
for att in dc.book.attributes:
if att.fk_attriblist == 246:
field246 = pymarc.Field(
tag='246',
indicators=['1', ' '],
subfields=[
Subfield(code='a', value=str(att.text)),
]
)
record.add_ordered_field(field246)
for att in dc.book.attributes:
if att.fk_attriblist == 250:
field250 = pymarc.Field(
tag='250',
indicators=[' ', ' '],
subfields=[
Subfield(code='a', value=str(att.text)),
]
)
record.add_ordered_field(field250)
for att in dc.book.attributes:
if att.fk_attriblist == 300:
field300 = pymarc.Field(
tag='300',
indicators=[' ', ' '],
subfields=[
Subfield(code='a', value=str(att.text)),
]
)
record.add_ordered_field(field300)
for att in dc.book.attributes:
if att.fk_attriblist == 440:
field490 = pymarc.Field(
tag='490',
indicators=['1', ' '],
subfields=[
Subfield(code='a', value=str(att.text)),
]
)
record.add_ordered_field(field490)
for att in dc.book.attributes:
if att.fk_attriblist == 440:
field830 = pymarc.Field(
tag='830',
indicators=[' ', '0'],
subfields=[
Subfield(code='a', value=str(att.text)),
]
)
record.add_ordered_field(field830)
# need to replace carriage returns. Tag 500 has multiple lines.
for att in dc.book.attributes:
if att.fk_attriblist == 500:
field500 = pymarc.Field(
tag='500',
indicators=[' ', " "],
subfields=[
Subfield(code='a', value=str(att.text)),
]
)
record.add_ordered_field(field500)
for att in dc.book.attributes:
if att.fk_attriblist == 505:
field505 = pymarc.Field(
tag='505',
indicators=['0', ' '],
subfields=[
Subfield(code='a', value=str(att.text)),
]
)
record.add_ordered_field(field505)
for att in dc.book.attributes:
if att.fk_attriblist == 508:
field508 = pymarc.Field(
tag='508',
indicators=[' ', ' '],
subfields=[
Subfield(code='a', value=str(att.text)),
]
)
record.add_ordered_field(field508)
for att in dc.book.attributes:
if att.fk_attriblist == 904:
field856 = pymarc.Field(
tag='856',
indicators=['4', '0'],
subfields=[
Subfield(code='a', value=f"https://www.gutenberg.org/ebooks/{str(dc.project_gutenberg_id)}"),
]
)
record.add_ordered_field(field856)
for att in dc.book.attributes:
if att.fk_attriblist == 904:
field856 = pymarc.Field(
tag='856',
indicators=['4', ' '],
subfields=[
Subfield(code='a', value=str(att.text)),
]
)
record.add_ordered_field(field856)
# Author name
num_auths = len(dc.authors)
if num_auths:
field100 = pymarc.Field(
tag='100',
indicators=['1', ' '],
subfields=[
Subfield(code='a', value=dc.format_author_date(dc.authors[0])) # Can do better
]
)
record.add_ordered_field(field100)
if num_auths > 1:
for auth in dc.authors[1:]:
field = pymarc.Field(
tag='700',
indicators=['1', ' '],
subfields=[
Subfield(code='a', value=dc.format_author_date(auth)),
Subfield(code='e', value='joint author.'),
]
)
record.add_ordered_field(field)
# Add Subfield to 245 indicating format
for att in dc.book.attributes:
if att.fk_attriblist == 245:
if '\n'in dc.title:
field245 = pymarc.Field(
tag='245',
indicators=['1', str(att.nonfiling)],
subfields=[
Subfield(code='a', value=dc.title_no_subtitle),
Subfield(code='h', value='[electronic resource] :'),
Subfield(code='b', value=re.sub(r'^[^\n]*\n', '', dc.title).replace('\n', ' ')),
]
)
else:
for att in dc.book.attributes:
if att.fk_attriblist == 245:
field245 = pymarc.Field(
tag='245',
indicators=['1', str(att.nonfiling)],
subfields=[
Subfield(code='a', value=dc.title_no_subtitle),
Subfield(code='h', value='[electronic resource]'),
]
)
record.add_ordered_field(field245)
# Publisher, date
if att.fk_attriblist == 260:
field260 = pymarc.Field(
tag='260',
indicators=[' ', ' '],
subfields=[
Subfield(code='a', value=f"{dc.pubinfo.place} :"),
Subfield(code='b', value=f"{dc.pubinfo.publisher},"),
Subfield(code='c', value=str(dc.pubinfo.years).replace('[(\'copyright\', \'', 'c').replace('\'), (\'pubdate\', \'', ', ').replace('\'), (\'copyright\', \'', ', c').replace('\')]', '.')),
]
)
record.add_ordered_field(field260)
add_license(record, dc)
return record
def add_license(record, dc):
if dc.rights:
# Add 540 field (terms governing use)
field540 = pymarc.Field(
tag='540',
indicators=[' ', ' '],
subfields=[
Subfield(code='a', value=dc.rights),
]
)
record.add_ordered_field(field540)
def add_subject(record, dc):
if dc.subjects:
field653 = pymarc.Field(
tag='653',
indicators=[' ', ' '],
subfields=[
Subfield(code='a', data=dc.subjects),
]
)
record.add_ordered_field(field653)
# Generate 100 records
all_records = [] # Create a list to store all records
for i in range(100):
booknums = list(range(1, 101)) # Replace with your actual book numbers
dc = DublinCoreObject()
dc.load_from_database(booknums[i])
record = stub(dc)
all_records.append(record) # Append each record to the list
# Write all records to one file
with open("out/combined_output.txt100f", "w") as text_file:
for record in all_records:
text_file.write(str(record) + "\n") # Separate records with a newline
print("Combined records written to combined_output.txt")
# Generate 100 records
all_records = [] # Create a list to store all records
for i in range(100):
booknums = list(range(68995, 69195)) # Replace with your actual book numbers
dc = DublinCoreObject()
dc.load_from_database(booknums[i])
record = stub(dc)
all_records.append(record) # Append each record to the list
# Write all records to one file
with open("out/combined_output.txt69000f", "w") as text_file:
for record in all_records:
text_file.write(str(record) + "\n") # Separate records with a newline
print("Combined records written to combined_output.txt")
all_records = [] # Create a list to store all records
for i in range(100):
booknums = list(range(68995, 69195)) # Replace with your actual book numbers
dc = DublinCoreObject()
dc.load_from_database(booknums[i])
record = stub(dc)
all_records.append(record) # Append each record to the list
# Write all records to one MARC file
with open("out/combined_output.mrc", "wb") as marc_file:
writer = MARCWriter(marc_file)
for record in all_records:
writer.write(record)
writer.close()
print("Combined records written to combined_output.mrc")