-
Notifications
You must be signed in to change notification settings - Fork 1
/
babelio.js
192 lines (191 loc) · 6.08 KB
/
babelio.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
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
const fetch = require("node-fetch");
const cheerio = require("cheerio");
const natural = require("natural");
const FormData = require("form-data");
const Utils = require("./utils.js");
const enrichedFunctions = {
couverture: (e) => Babelio.prependURL(e(".livre_con img").slice(0,1).attr("src")),
extra_resume: async (e) => {
let resume = e(".livre_resume");
let more = resume.find("a").slice(-1);
if (more.length == 0) {
return Utils.winDecoder(Utils.normalize(resume.text()));
} else {
let params = more.attr("onclick");
params = params.split("(")[1].split(")")[0].split(",");
let form = new FormData();
form.append("type",params[1]);
form.append("id_obj",params[2]);
await Utils.wait();
let results = await fetch("https://www.babelio.com/aj_voir_plus_a.php", {
method: "POST",
body: form
});
results = Utils.normalize(await results.textConverted());
return (results.length > resume.length) ? results : resume;
}
},
enrichedAuthors: (e) => {
let output = [];
e(".livre_auteurs").map((i,node) => {
node = cheerio(node,e);
output.push({
name: Utils.normalize(node.text()),
role: "main_author",
babelioLink: Babelio.prependURL(node.attr("href"))
});
});
e(".livre_collabs").map((i,node) => {
node = cheerio(node,e);
let name = node.text();
let role = node.find("span").text();
name = Utils.normalize(name.slice(0,name.length-role.length));
role = Utils.normalize(role.split("(")[1].split(")")[0]);
output.push({
name,
role,
babelioLink: Babelio.prependURL(node.attr("href"))
});
});
return output;
},
pages: (e) => {
e = e(".livre_refs").html().split("<br>");
return Utils.normalize(e
.find(ligne => ligne.indexOf("page") > 0)
.split("page")[0]);
},
isbn: (e) => {
e = e(".livre_refs").html().split("<br>");
return Utils.normalize(e
.find(ligne => ["EAN","ISBN"].some(code => ligne.indexOf(code) >= 0))
.split(":")[1]);
},
editeurName: (e) => Utils.normalize(e(".livre_refs a").text()),
editeurLink: (e) => Babelio.prependURL(e(".livre_refs a").attr("href")),
extra_tags: (e) => {
let tags = [];
e(".side_l_content .tags a").map((i,node) => {
node = cheerio(node,e);
tags.push(Utils.normalize(node.text()));
});
return tags;
}
};
const Babelio = {
prependURL: (string) => {
return (string.slice(0,4) == "http")
?string
:"https://www.babelio.com"+string;
},
requester: async (auteurs,titre,isbn="") => {
await Utils.wait();
let results;
if (isbn.length > 0) {
results = await Babelio.search(isbn);
if (results.length == 0) {
return Babelio.requester(auteurs,titre);
}
} else {
results = await Babelio.search(titre+" "+auteurs.join(" "));
let count = 0;
while ((results.length == 0)
&& (auteurs.length > 1)
&& (count < auteurs.length)) {
console.warn("no result found... retrying with author '"
+auteurs[count]+"' only");
await Utils.wait();
results = await Babelio.search(titre+" "+auteurs[count]);
count++;
}
if ((results.length == 0) && (titre.indexOf("&") >= 0)) {
return Babelio.requester(auteurs,titre.replace(/&/g,""));
}
}
return results;
},
search: async (string,retry=0) => {
let url = "https://www.babelio.com/resrecherche.php?Recherche="
+escape(string.replace(/œ/g,"oe").replace(/Œ/g,"OE"));
let rows = [];
let results;
try {
results = await fetch(url);
} catch {
if (retry <= 3) {
await Utils.wait();
return Babelio.search(string,retry++);
} else {
return [];
}
}
results = await results.textConverted();
results = cheerio.load(results);
results = results(".side_l table").find("tr").map((i,e) => {
rows.push(cheerio.load(e));
});
rows = rows.slice(1).map(e => {
let jsonRow = {};
jsonRow.title = Utils.normalize(e(".titre_livre a").slice(0,1).text());
jsonRow.link = Babelio.prependURL(
Utils.normalize(e(".titre_livre a").slice(0,1).attr("href")));
return jsonRow;
});
return rows;
},
getBook: async (livre) => {
console.info("Requesting Babelio for book "
+livre.titre+" by "+livre.auteurs.join(" & "));
let search = await Babelio.requester(livre.auteurs,livre.titre,(livre.isbn || ""))
if (search.length == 0) {
throw new Error("No record found");
} else {
if (typeof livre.isbn !== "undefined") {
foundBook = search[0];
} else {
foundBook = Utils.findMoreSimilar(livre.titre,search,"title")[0];
delete foundBook.score;
}
return foundBook;
}
},
enrich: async (livre) => {
let record;
try {
record = await Babelio.getBook(livre);
} catch (err) {
console.error(err+"\n");
return { featuresFound: 0 };
}
let enrichedData = { featuresFound:0 };
console.log("Analyzing record "+record.link);
enrichedData.babelioLink = record.link;
record = await fetch(record.link);
record = await record.textConverted();
record = cheerio.load(record);
await Promise.all(Object.keys(enrichedFunctions).map(async key => {
let value;
try {
value = await enrichedFunctions[key](record);
} catch {
value = "";
}
if (value.length > 0) {
enrichedData.featuresFound +=
(typeof value == "string") ? 1 : value.length;
enrichedData[key] = value;
}
}));
if (typeof enrichedData.editeurName !== "undefined") {
enrichedData.editeur = [ { name: enrichedData.editeurName } ];
delete enrichedData.editeurName;
if (typeof enrichedData.editeurLink !== "undefined") {
enrichedData.editeur[0].babelioLink = enrichedData.editeurLink;
delete enrichedData.editeurLink;
}
}
console.info(enrichedData.featuresFound+" enriched features found\n");
return enrichedData;
}
};
module.exports = Babelio;