-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathabes.js
86 lines (84 loc) · 2.36 KB
/
abes.js
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
const PARSER = new DOMParser();
const baseURL = "http://documentation.abes.fr/sudoc/formats/unmb/zones/";
const requester = (url,encoding="utf-8") => {
return new Promise((resolve,reject) => {
const reader = new FileReader();
reader.addEventListener("loadend", () => {
resolve(reader.result)
});
fetch(url)
.then(response => response.blob())
.then(blob => reader.readAsText(blob, encoding))
});
}
let analyzeABES = (document) => {
let mapping = {1:"ind1",2:"ind2",3:"subfields",5:"subfields.value",8:"subfield.description"}
let field = {ind1:{},ind2:{},subfields:{}};
field.value = document.querySelector("h1").innerText.split(": ")[1];
let lines = [...[...document.querySelectorAll("table")][3].querySelectorAll("tr")].slice(2);
lines.map(e => {
let sub = {};
let code = "";
let value = "";
let type = "";
[...e.querySelectorAll("td")].map((cell,i) => {
let map = mapping[i];
let text = cell.innerText.replace(/\s+/g," ");
if (text != " ") {
if (typeof map !== "undefined") {
if (i < 4) {
type = map;
code = text
} else if (i == 5) {
sub[text] = "";
} else {
value = text;
}
}
}
});
if (type == "subfields") {
if (Object.keys(sub).length > 0) {
sub[Object.keys(sub)[Object.keys(sub).length-1]] = value;
} else {
sub["*"] = value;
}
} else {
sub = value;
}
field[type][code] = sub;
});
return field;
};
let normalize = (fieldNumber) => {
fieldNumber = fieldNumber.toString();
while (fieldNumber.length < 3) {
fieldNumber = "0"+fieldNumber;
}
return fieldNumber;
}
let getABES = async (code) => {
code = normalize(code);
let result = await requester(baseURL+code+".htm","iso-8859-1");
result = PARSER.parseFromString(result, "text/html");
try {
result = analyzeABES(result);
} catch(e) {
console.error(e);
}
return {code,result};
};
(async () => {
let fields = {};
await Promise.all([...new Array(1000).fill(0)].map(async (e,i) => {
let abes = await getABES(i);
try {
if (abes.result[Object.keys(abes.result)] !== null) {
fields[abes.code] = abes.result;
}
} catch(e) {
console.error(e);
}
}));
document.querySelector("body").innerHTML = JSON.stringify(fields);
})();