-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecordparser.py
More file actions
266 lines (203 loc) · 8.56 KB
/
Copy pathrecordparser.py
File metadata and controls
266 lines (203 loc) · 8.56 KB
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
# !/usr/bin/env python
import logging
from selectolax.lexbor import LexborHTMLParser
from typing import Union, Tuple, List, Dict
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO, format='%(message)s')
def get_at(lst, idx):
return lst[idx] if idx < len(lst) else None
def try_method(method):
try:
return method()
except:
return None
def tidy(text, a=0):
# if not text: return ''
if a == 0:
for i in ('[', ']', ',', '.', ':', '/', ';'):
if text.endswith(i):
text = text[:-1]
text = text.replace("\\'", "'").replace("'", "\\'").strip()
text = text.replace("\\'", "'")
return text
def to_str(items, delim):
return delim.join(i for i in items) if isinstance(items, list) else items
class marcxml_record():
def __init__(self, marcxml):
self.raw = marcxml
self.tree = LexborHTMLParser(marcxml)
def get_field(self, tree=None, tag='', ind1='', ind2='', codes=(), first=True):
if not tree: tree = self.tree
dattrs = [('tag', tag), ('ind1', ind1), ('ind2', ind2)]
datafield = ''.join(f'[{a}="{v}"]' for a, v in dattrs if v)
subfields = f""":is({','.join(f'[code="{c}"]' for c in codes)})""" if codes else None
selector = ' > '.join(i for i in (datafield, subfields) if i)
# selector = '[tag="650"]'
logger.debug('css selector:', selector)
result = tree.css_first(selector) if first else tree.css(selector)
# print(type(result))
return result
def get_title(self, split=False):
title = self.get_field(tag='245', codes=('a', 'b'), first=False)
if split:
titlea = get_at(title, 0)
titleb = get_at(title, 1)
return tidy(titlea.text()) if titlea else '', tidy(titleb.text()) if titleb else ''
return tidy(''.join(i.text() for i in title))
def get_series(self):
series = self.get_field(tag='490', codes=['a']) or self.get_field(tag='440', codes=['a'])
return tidy(series.text(), 0) if series else ''
# none case?
def get_place(self):
place = self.get_field(tag='260', codes=['a']) or self.get_field(tag='264', codes=['a'])
return tidy(place.text()) if place else ''
# none case?
def get_publisher(self):
publisher = self.get_field(tag='260', codes=['b']) or self.get_field(tag='264', codes=['b'])
return tidy(publisher.text())
def get_date(self):
date = self.get_field(tag='260', codes=['c']) or self.get_field(tag='264', codes=['c'])
return tidy(date.text())
# tidy beginning [?
def get_isbn(self) -> list:
isbns = self.get_field(tag='020', codes=['a'], first=False)
return [i.text() for i in isbns]
def get_lcc(self, split=False) -> Union[str, tuple]:
lcc = self.get_field(tag='050', codes=('a', 'b'), first=False)
class_no = get_at(lcc, 0)
if class_no: class_no = class_no.text()
item_no = get_at(lcc, 1)
if item_no: item_no = item_no.text()
if split:
return class_no, item_no
return class_no + ' ' + (item_no or '')
def get_ddc(self) -> str:
ddc = self.get_field(tag='082', codes=('a'))
return ddc.text()
def get_lcsh(self):
datafields = self.get_field(tag='650', first=False) or []
headings = []
for i in datafields:
subdivisions = self.get_field(tree=i, codes=('a', 'v', 'x', 'y', 'z'), first=False)
heading = tidy('--'.join(j.text(strip=True) for j in subdivisions))
headings.append(heading)
return to_str(headings, ' | ')
def get_summary(self):
summary = self.get_field(tag='520', codes=['a'])
return tidy(summary.text(), 1) if summary else ''
def get_author(self):
author = self.get_field(tag="100", codes=['a'])
return tidy(author.text()) if author else ''
def get_contributors(self) -> dict:
datafields = self.get_field(tag='700', first=False) or []
contributors = {}
ctypes = {
'translator': 'translator',
'editor': 'editor',
'ÜbersetzerIn': 'translator',
}
for i in datafields:
contributor = self.get_field(tree=i, codes=('a'))
ctype = self.get_field(tree=i, codes=('e'))
contributor = tidy(contributor.text()) if contributor else ''
ctype = ctypes.get(tidy(ctype.text()), 'contributor') if ctype else ''
contributors[contributor] = ctype
return contributors
def get_language(self):
language = self.get_field(tag="008").text()[35:38]
return language
class openl_record():
def __init__(self, array):
self.array = array['details']['details'] if array.get('details') else array
self.data = array.get('data')
def get_works(self):
return self.array['works'][0]
def get_title(self):
return self.array['title']
def get_publishers(self):
return self.array['publishers'][0]
def get_date(self):
return self.array['publish_date']
def get_isbn(self) -> list:
return (self.array.get('isbn_13') or []) + (self.array.get('isbn_10') or [])
def get_lcc(self):
lcc = self.array.get('lc_classifications') or []
if lcc:
return lcc[0]
return None
def get_ddc(self):
ddc = self.array.get('dewey_decimal_class') or []
if ddc:
return ddc[0]
return None
def get_language(self):
lang = self.array.get('languages') or []
if lang and 'key' in lang[0]:
return lang[0]['key'][-3:]
return None
def get_author(self):
return self.data.get('authors')[0]['name']
class mods_record():
def __init__(self, mods):
self.raw = mods
self.tree = LexborHTMLParser(mods)
def get_title(self, split=False) -> Union[str, Tuple[str, str]]:
selector = "mods\\:titleInfo > :is(mods\\:title, mods\\:subTitle)"
title = self.tree.css(selector)
titlea = get_at(title, 0)
titleb = get_at(title, 1)
norm_a = tidy(titlea.text()) if titlea else ''
norm_b = tidy(titleb.text()) if titleb else ''
if split:
return norm_a, norm_b
return norm_a + '\t' + norm_b
def get_author(self) -> str:
selector = 'mods\\:namePart'
author = self.tree.css_first(selector)
return tidy(author.text()) if author else ''
def get_place(self):
selector = 'mods\\:placeTerm[type="text"]'
place = self.tree.css_first(selector)
return tidy(place.text()) if place else ''
def get_publisher(self):
selector = 'mods\\:publisher'
publisher = self.tree.css_first(selector)
return tidy(publisher.text())
def get_date(self):
selector = 'mods\\:dateIssued'
date = self.tree.css_first(selector)
return tidy(date.text())
# tidy beginning [?
def get_isbn(self) -> List[str]:
selector = 'mods\\:identifier[type="isbn"]'
isbns = self.tree.css(selector)
return [i.text() for i in isbns]
def get_lcc(self) -> str:
selector = 'mods\\:classification[authority="lcc"]'
lcc = self.tree.css_first(selector)
return lcc.text() if lcc else ''
def get_ddc(self) -> str:
selector = 'mods\\:classification[authority="ddc"]'
ddc = self.tree.css_first(selector)
return ddc.text() if ddc else ''
def get_lcsh(self) -> str:
subjectFields = self.tree.css('mods\\:subject[authority="lcsh"]') or []
headings = []
for i in subjectFields:
subdivisions = i.css('*')
# for some reason * returns all children as one in addition to all children
# requiring the slice
heading = tidy('--'.join(j.text(strip=True) for j in subdivisions[1:]))
headings.append(heading)
return to_str(headings, ' | ')
def get_summary(self) -> str:
selector = 'mods\\:abstract'
summary = self.tree.css_first(selector)
return tidy(summary.text(), 1) if summary else ''
def get_contributors(self) -> Dict[str, str]:
selector = 'mods\\:mods > mods\\:name:not([usage]) > mods\\:namePart'
contributors = self.tree.css(selector)
return {i.text() : 'contributor' for i in contributors}
def get_language(self) -> str:
language = self.tree.css_first('mods\\:languageTerm')
return language.text() if language else ''