-
Notifications
You must be signed in to change notification settings - Fork 9
/
bg_extract_data.mjs
350 lines (310 loc) · 12.2 KB
/
bg_extract_data.mjs
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
/*
MIT License
Copyright (c) 2020 Robert M Pavey
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
function cleanText(inputText) {
let text = inputText;
if (text) {
text = text.trim();
text = text.replace(/\s+/g, " ");
text = text.replace(/\s([,;.])/g, "$1");
}
return text;
}
function convertDateString(text) {
const monthArray = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
// Does it look like a date - usually yyyy-mm-dd?
if (/^(\d{4}|-)(?:-?)([0-1]?[0-9]|-?)(?:-?)([0-3]?[0-9]|-)?$/.test(text)) {
const dateSplit = /^(\d{4}|-)(?:-?)([0-1]?[0-9]|-?)(?:-?)([0-3]?[0-9]|-)?$/.exec(text);
let dateString = "";
if (dateSplit[3]) {
dateString = dateSplit[3] + " ";
}
if (dateSplit[2]) {
dateString += monthArray[+dateSplit[2] - 1] + " ";
}
if (dateSplit[1] && dateSplit[1] !== "-") {
dateString += dateSplit[1];
}
return dateString.trim();
}
return text;
}
function setFromNodeWithAttribute(result, dataNode, attributeText, fieldName) {
const node = dataNode.querySelector(attributeText);
if (node && node.textContent) {
result[fieldName] = cleanText(node.textContent);
}
}
function setFromJsonWithKey(result, dataJson, itemKey, fieldName) {
const value = dataJson[itemKey];
if (value && value !== "Not Available") {
result[fieldName] = value;
}
}
function getElementByTextContent(document, element, text) {
const elements = document.querySelectorAll(element);
for (let i = 0; i < elements.length; i += 1) {
if (elements[i].innerText && elements[i].innerText === text) {
return elements[i];
}
}
}
function appendWithComma(text, appendText) {
if (appendText && appendText.length > 0) {
if (text && text.length > 0) {
return text + ", " + appendText;
} else {
return appendText;
}
}
return text;
}
function buildFullAddress(cemetery) {
let address = "";
address = appendWithComma(address, cemetery.addressStreet);
address = appendWithComma(address, cemetery.addressLocality);
address = appendWithComma(address, cemetery.addressDistrict);
address = appendWithComma(address, cemetery.addressRegion);
address = appendWithComma(address, cemetery.addressCountry);
return address;
}
function extractFromJson(document, result, dataScript) {
const jsonText = dataScript.innerHTML;
if (!jsonText || !jsonText.startsWith("{")) {
console.log("bg extract not JSON text");
return result;
}
const dataJson = JSON.parse(jsonText);
// Either the main person on memorial or some inscribed
let personData = null;
if (dataJson["@type"] && dataJson["@type"] === "Person") {
personData = dataJson;
} else if (dataJson.mainEntity && dataJson.mainEntity["@type"] && dataJson.mainEntity["@type"] === "Person") {
personData = dataJson.mainEntity;
} else {
console.log("bg extract not a person record");
return result;
}
setFromJsonWithKey(result, personData, "name", "fullName");
setFromJsonWithKey(result, personData, "familyName", "lastName");
setFromJsonWithKey(result, personData, "givenName", "givenName");
setFromJsonWithKey(result, personData, "birthDate", "birthDate");
if (result.birthDate) {
result.birthDate = convertDateString(result.birthDate);
}
setFromJsonWithKey(result, personData, "deathDate", "deathDate");
if (result.deathDate) {
result.deathDate = convertDateString(result.deathDate);
}
if (personData["relatedTo"]) {
const relations = personData["relatedTo"];
result.relations = [];
relations.forEach((relation) => {
if (relation["@type"] && relation["@type"] === "Person") {
let relative = {};
setFromJsonWithKey(relative, relation, "name", "name");
setFromJsonWithKey(relative, relation, "familyName", "lastName");
setFromJsonWithKey(relative, relation, "givenName", "givenName");
setFromJsonWithKey(relative, relation, "birthDate", "birthDate");
setFromJsonWithKey(relative, relation, "deathDate", "deathDate");
result.relations.push(relative);
}
});
}
}
function extractData(document, url) {
var result = {};
if (url) {
result.url = url;
}
result.success = false;
result.hasImage = false;
// On old style record page (pre Oct 2022)
const vitalInformationNode = document.querySelector("div#VitalInformation");
// new style record page (Oct 2022)
const recordPageHeaderNode = document.querySelector("[class^='RecordPage_header__']");
const dataScript = document.querySelector("script[type='application/ld+json']");
// extract from script tag JSON in head
if (dataScript) {
extractFromJson(document, result, dataScript);
}
if (vitalInformationNode) {
const infoNode = document.querySelector("#VitalInformation");
// Cemetery Pre Oct 2022 pages
let cemeteryLinkNode = infoNode.querySelector('[data-vars-link-name="VitalInformationCemetery"]');
if (cemeteryLinkNode) {
const cemeteryVitalNode = cemeteryLinkNode.parentNode;
if (cemeteryVitalNode) {
let cemeteryAddress = {};
setFromNodeWithAttribute(result, cemeteryVitalNode, 'h2[itemprop="name"]', "cemeteryName");
setFromNodeWithAttribute(cemeteryAddress, cemeteryVitalNode, 'div[itemprop="streetAddress"]', "addressStreet");
setFromNodeWithAttribute(
cemeteryAddress,
cemeteryVitalNode,
'span[itemprop="addressLocality"]',
"addressLocality"
);
setFromNodeWithAttribute(
cemeteryAddress,
cemeteryVitalNode,
'span[itemprop="addressDistrict"]',
"addressDistrict"
);
setFromNodeWithAttribute(cemeteryAddress, cemeteryVitalNode, 'span[itemprop="addressRegion"]', "addressRegion");
setFromNodeWithAttribute(
cemeteryAddress,
cemeteryVitalNode,
'div[itemprop="addressCountry"]',
"addressCountry"
);
result.cemeteryFullAddress = buildFullAddress(cemeteryAddress);
}
}
// transcriber pre Oct 2022 pages
const transcriberHeading = infoNode.querySelector("[alt='Transcriber']");
if (transcriberHeading) {
const transcriberNode = transcriberHeading.nextElementSibling;
const transcriberNameNode = transcriberNode.querySelector("h2");
const transcriberDateNode = transcriberNode.querySelector("div");
if (transcriberNameNode) {
result.transcriber = cleanText(transcriberNameNode.textContent);
if (transcriberDateNode) {
result.transcriber += ", " + cleanText(transcriberDateNode.textContent);
}
}
}
// photographer pre Oct 2022 pages
const photographerHeading = infoNode.querySelector("[alt='Photographer']");
if (photographerHeading) {
const photographerNode = photographerHeading.nextElementSibling;
const photographerNameNode = photographerNode.querySelector("h2");
const photographerDateNode = photographerNode.querySelector("div");
if (photographerNameNode) {
result.photographer = cleanText(photographerNameNode.textContent);
if (photographerDateNode) {
result.photographer += ", " + cleanText(photographerDateNode.textContent);
}
}
}
// Epitaph pre Oct 2022 pages
const epitaphHeading = infoNode.querySelector("[alt='Epitaph']");
if (epitaphHeading) {
const epitaphNode = epitaphHeading.nextElementSibling;
if (epitaphNode) {
const epitaphDiv = epitaphNode.querySelector("div");
if (epitaphDiv && epitaphDiv.innerText) {
// using innerText as epitaph can contain markup which textContent ignores
result.epitaph = cleanText(epitaphDiv.innerText.replace(/\n/g, " "));
}
}
}
// Has image
const imageLink = document.querySelector("div.record-image-wrapper img[src]");
if (imageLink) {
result.hasImage = true;
}
} else {
// post Oct 2022 pages
// Cemetery
// Logged On
const cemeteryNameNode = document.querySelector("div[class^='Stack_stack']>div>a>h2");
if (cemeteryNameNode && cemeteryNameNode.textContent) {
result.cemeteryName = cleanText(cemeteryNameNode.textContent);
const cemeteryAddressNodes = cemeteryNameNode.parentNode.parentNode.querySelectorAll("div");
let cemeteryAddress = {};
for (let i = 0; i < cemeteryAddressNodes.length; i += 1) {
cemeteryAddress = appendWithComma(cemeteryAddress, cleanText(cemeteryAddressNodes[i].textContent));
}
result.cemeteryFullAddress = cemeteryAddress;
}
const dataTableNode = document.querySelector("table");
if (dataTableNode) {
const dataRowNodes = dataTable.querySelectorAll("tr");
if (dataRowNodes.length > 0) {
const cemeteryRowNode = dataRowNodes[dataRowNodes.length - 1];
let cemetery = cleanText(cemeteryRowNode.querySelector("td").textContent);
let indexOfComma = cemetery.indexOf(",");
result.cemeteryName = cemetery.slice(0, indexOfComma - 1);
result.cemeteryFullAddress = cemetery.slice(indexOfComma + 2);
}
}
// Get transcriber from Oct 2022 pages
const transcriberCaptionNode = getElementByTextContent(document, "span", "Transcriber");
if (transcriberCaptionNode) {
const transcriberNode = transcriberCaptionNode.parentNode.parentNode;
let transcriberNameNode = transcriberNode.querySelector("h5");
if (transcriberNameNode && transcriberNameNode.textContent) {
result.transcriber = cleanText(transcriberNameNode.textContent);
let transcriberDateNode = transcriberNode.querySelector("div > span");
if (transcriberDateNode && transcriberDateNode.textContent) {
result.transcriber += ", " + cleanText(transcriberDateNode.textContent);
}
}
}
// Get photographer from Oct 2022 pages
const photographerCaptionNode = getElementByTextContent(document, "span", "Photographer");
if (photographerCaptionNode) {
const photographerNode = photographerCaptionNode.parentNode.parentNode;
let photographerNameNode = photographerNode.querySelector("h5");
if (photographerNameNode && photographerNameNode.textContent) {
result.photographer = cleanText(photographerNameNode.textContent);
let photographerDateNode = photographerNode.querySelector("div > span");
if (photographerDateNode && photographerDateNode.textContent) {
result.photographer += ", " + cleanText(photographerDateNode.textContent);
}
}
}
// Get epitaph from Oct 2022 pages
const epitaphHeading = getElementByTextContent(document, "h4", "Epitaph");
if (epitaphHeading) {
const epitaphNode = epitaphHeading.nextElementSibling;
if (epitaphNode) {
if (epitaphNode && epitaphNode.innerText) {
// using innerText as epitaph can contain markup which textContent ignores
result.epitaph = cleanText(epitaphNode.innerText.replace(/\n/g, " "));
}
}
}
//Has Image
const imageLink = document.querySelector("div[class^='RecordPage_ImageWrapper__'] a");
if (imageLink) {
result.hasImage = true;
}
}
if (result.fullName && result.cemeteryName) {
result.success = true;
}
//console.log("Extracted - ");
//console.log(result);
return result;
}
export { extractData };