-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
355 lines (314 loc) · 12.3 KB
/
server.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
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
const fetch = require('node-fetch');
const express = require('express');
const { fetchCriticsFromGallica, fetchSheetsFromGallica, fetchSoundFromGallica } = require('./sru');
const { fetchFromDeezer } = require('./deezer');
const app = express();
app.use("/", express.static('vue/'));
const CACHE = new Map();
const roles = {
r70: 'auteur du texte',
r80: 'auteur de l\'argument',
r160: 'chorégraphe',
r220: 'compositeur',
};
const databnfPrefixes = {
'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
'rdfs': 'http://www.w3.org/2000/01/rdf-schema#',
'skos': 'http://www.w3.org/2004/02/skos/core#',
'foaf': 'http://xmlns.com/foaf/0.1/',
'xfoaf': 'http://www.foafrealm.org/xfoaf/0.1/',
'dcmitype': 'http://purl.org/dc/dcmitype/',
'ore': 'http://www.openarchives.org/ore/terms/',
'ark': 'http://ark.bnf.fr/ark:/12148/',
'dbpedia': 'http://dbpedia.org/',
'dbpediaowl': 'http://dbpedia.org/ontology/',
'dbprop': 'http://dbpedia.org/property/',
'rdagroup2elements': 'http://rdvocab.info/ElementsGr2/',
'frbr': 'http://rdvocab.info/uri/schema/FRBRentitiesRDA/',
'rdarole': 'http://rdvocab.info/roles/',
'rdagroup1elements': 'http://rdvocab.info/Elements/',
'rdarelationships': 'http://rdvocab.info/RDARelationshipsWEMI/',
'og': 'http://ogp.me/ns#',
'bnf-onto': 'http://data.bnf.fr/ontology/bnf-onto/',
'dcterms': 'http://purl.org/dc/terms/',
'owl': 'http://www.w3.org/2002/07/owl#',
'time': 'http://www.w3.org/TR/owl-time/',
'marcrel': 'http://id.loc.gov/vocabulary/relators/',
'bnfroles': 'http://data.bnf.fr/vocabulary/roles/',
'mo': 'http://purl.org/ontology/mo/',
'geo': 'http://www.w3.org/2003/01/geo/wgs84_pos#',
'ign': 'http://data.ign.fr/ontology/topo.owl/',
'insee': 'http://rdf.insee.fr/geo/',
'gn': 'http://www.geonames.org/ontology/ontology_v3.1.rdf/',
'dcdoc': 'http://dublincore.org/documents/',
'bio': 'http://vocab.org/bio/0.1/',
'isni': 'http://isni.org/ontology#',
'bibo': 'http://purl.org/ontology/bibo/',
'schema': 'http://schema.org/',
}
/**
* > ns_prop('http://www.w3.org/2004/02/skos/core#prefLabel',
DatabnfDatabase.namespaces)
'skos:prefLabel'
*
* @param {*} uri
* @param {*} namespaces
*/
function nsprop(uri, namespaces) {
for (const ns of Object.keys(namespaces)) {
const prefix = namespaces[ns];
if (uri.startsWith(prefix)) {
return `${ns}:${uri.slice(prefix.length)}`;
}
}
return uri;
}
function simplifyData(data, namespaces) {
const props = {};
for (let { attr, value } of data) {
if (attr === undefined) {
continue;
}
attr = nsprop(attr, namespaces);
if (props[attr] !== undefined) {
const propval = props[attr];
if (propval instanceof Array) {
propval.push(value);
} else {
props[attr] = [propval, value];
}
} else {
props[attr] = value;
}
}
return props;
}
function exposeData(data) {
const normalizeMap = {
'skos:note': 'notes',
'skos:prefLabel': 'prefLabel',
'skos:altLabel': 'altLabels',
'bnf-onto:lastYear': 'year',
'rdarelationships:expressionOfWok': 'expressions',
'dcterms:description': 'description',
'mo:genre': 'genre',
'contributors': 'contributors',
'foaf:depiction': 'depictions',
'dbpedia:abstract': 'abstract',
'http://data.bnf.fr/vocabulary/roles/r70': 'r70',
'http://data.bnf.fr/vocabulary/roles/r80': 'r80',
'http://data.bnf.fr/vocabulary/roles/r160': 'r160',
'http://data.bnf.fr/vocabulary/roles/r220': 'r220',
'eans': 'eans',
};
const exposed = {};
for (const key of Object.keys(normalizeMap)) {
if (data[key] !== undefined) {
exposed[normalizeMap[key]] = data[key];
}
}
return exposed;
}
function parseJsonResults(rset) {
const results = [];
for (const row of rset.results.bindings) {
const result = {};
for (const varname of rset.head.vars) {
if (row[varname] === undefined) {
result[varname] = null;
} else {
result[varname] = row[varname].value;
}
}
results.push(result);
}
return results;
}
async function sparqlexec(endpoint, query) {
const format = 'application/sparql-results+json';
const result = await fetch(`${endpoint}?query=${encodeURIComponent(query)}&format=${encodeURIComponent(format)}&timeout=0`);
return parseJsonResults(await result.json());
}
async function fetchFromDbpedia(uri) {
const dbpediaInfos = await sparqlexec('http://fr.dbpedia.org/sparql', `
SELECT ?abstract WHERE {
<${uri}> <http://dbpedia.org/ontology/abstract> ?abstract.
FILTER (lang(?abstract) = 'fr')
}`);
return dbpediaInfos.length === 1 ? dbpediaInfos[0] : null;
}
async function bnfFetchAuthority(noticeid) {
const match = /ark:\/12148\/cb([0-9]{8})/.exec(noticeid);
if (match !== null) {
noticeid = match[1];
}
if (CACHE.has(noticeid)) {
return CACHE.get(noticeid);
}
let rawData = await sparqlexec('http://data.bnf.fr/sparql', `
SELECT ?attr ?value WHERE {
?work bnf-onto:FRBNF "${noticeid}"^^xsd:integer.
?work ?attr ?value.
}`);
rawData = rawData.concat(await sparqlexec('http://data.bnf.fr/sparql', `
SELECT ?attr ?value WHERE {
?concept bnf-onto:FRBNF "${noticeid}"^^xsd:integer.
?concept foaf:focus ?work.
?work ?attr ?value.
}`));
rawData = simplifyData(rawData, databnfPrefixes);
if (rawData['owl:sameAs'] !== undefined && rawData['owl:sameAs'] instanceof Array) {
for (const uri of rawData['owl:sameAs']) {
if (uri.startsWith('http://fr.dbpedia.org')) {
rawData['dbpedia:abstract'] = await fetchFromDbpedia(uri);
}
}
}
rawData = exposeData(rawData);
CACHE.set(noticeid, rawData);
return rawData;
}
async function fetchEANs(workid) {
const eanData = await sparqlexec('http://data.bnf.fr/sparql', `
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdarelationships: <http://rdvocab.info/RDARelationshipsWEMI/>
PREFIX bnfroles: <http://data.bnf.fr/vocabulary/roles/>
select distinct ?ean where {
?concept bnf-onto:FRBNF "${workid}"^^xsd:integer ;
foaf:focus ?work.
?manifestation bnf-onto:ean ?ean ;
rdarelationships:workManifested ?work.
}`);
return eanData.map(row => row.ean);
}
async function bnfFetchWork(workid) {
let workData = await bnfFetchAuthority(workid);
workData.eans = await fetchEANs(workid);
let contributorRoles = await sparqlexec('http://data.bnf.fr/sparql', `
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdarelationships: <http://rdvocab.info/RDARelationshipsWEMI/>
PREFIX bnfroles: <http://data.bnf.fr/vocabulary/roles/>
select distinct ?authorConcept ?rel where {
?concept bnf-onto:FRBNF "${workid}"^^xsd:integer ;
foaf:focus ?work.
?work rdarelationships:expressionOfWork ?expr.
?expr owl:sameAs ?newExpr.
?newExpr ?rel ?author.
?oldAuthor owl:sameAs ?author.
?authorConcept foaf:focus ?oldAuthor.
{
?work dcterms:contributor ?oldAuthor
}
UNION
{
?work dcterms:creator ?oldAuthor
}
FILTER REGEX(?rel, '^http://data.bnf.fr/vocabulary/roles/(${Object.keys(roles).join("|")})')
} `);
const done = new Set();
const contributors = {};
for (let { authorConcept, rel } of contributorRoles) {
rel = roles[rel.slice(rel.lastIndexOf('/') + 1)];
if (done.has(authorConcept)) {
continue;
}
done.add(authorConcept);
const authordata = await bnfFetchAuthority(authorConcept);
if (contributors[rel] === undefined) {
contributors[rel] = [authordata];
} else {
contributors[rel].push(authordata);
}
}
workData.contributors = contributors;
return workData;
}
function catcherror(fn) {
return (req, res, next) => {
Promise.resolve(fn(req, res, next)).catch(next);
}
}
app.get('/critics/:title/:author/:year', catcherror(async(req, res) => {
res.json(await fetchCriticsFromGallica(req.params.title, req.params.author, req.params.year));
}));
app.get('/sheets/:title/:author', catcherror(async(req, res) => {
res.json(await fetchSheetsFromGallica(req.params.title, req.params.author));
}));
app.get('/sound/:title/:author', catcherror(async(req, res) => {
res.json(await fetchSoundFromGallica(req.params.title, req.params.author));
}));
app.get('/deezer/:eans', catcherror(async(req, res) => {
res.json(await fetchFromDeezer(req.params.eans.split(',')));
}));
app.get('/work/:workid', catcherror(async(req, res) => {
res.json(await bnfFetchWork(req.params.workid));
}));
app.get('/concert/:concert*', catcherror(async(req, res) => {
var concert = req.path.slice(9);
var expressions = await sparqlexec('http://data.doremus.org/sparql', `select ?expression ?title ?comment ?oeuvrebnf ?databnf where {
<${concert}> <http://erlangen-crm.org/current/P165_incorporates> ?expression .
?expression rdfs:label ?title .
optional {
?expression rdfs:comment ?comment .
} .
optional {
?expression owl:sameAs ?oeuvrebnf .
?oeuvrebnf owl:sameAs ?databnf.
}
} group by ?expression `);
res.json(expressions);
}));
app.get('/other_concert/:expresssion*', catcherror(async(req, res) => {
var expression = req.path.slice(15);
var concerts = await sparqlexec('http://data.doremus.org/sparql', `select ?expression ?title ?execution ?titreconcert ?date ?creationex ?comment ?lieu group_concat(concat(?name,' / ',?fctlabel,' / ',?interpreter);separator='|') as ?interpretre where {
<http://data.doremus.org/expression/f278628e-40cc-3c64-8ed3-42ab00e01d1f> owl:sameAs ?oeuvrebnf .
?expression owl:sameAs ?oeuvrebnf;
rdfs:label ?title .
?execution <http://data.doremus.org/ontology#U54_is_performed_expression_of> ?expression .
?concert <http://erlangen-crm.org/efrbroo/R66_included_performed_version_of> ?expression ;
rdfs:label ?titreconcert .
optional {
?concert <http://erlangen-crm.org/current/P4_has_time-span> ?timesp .
?timesp rdfs:label ?date .
}
optional {
?concert <http://erlangen-crm.org/current/P7_took_place_at> ?place .
?place rdfs:label ?lieu .
} .
?creationex <http://erlangen-crm.org/efrbroo/R17_created> ?execution .
optional {
?creationex rdfs:comment ?comment .
} .
optional {
?creationex <http://erlangen-crm.org/current/P9_consists_of> ?activity .
?activity <http://erlangen-crm.org/current/P14_carried_out_by> ?interpreter .
?activity <http://data.doremus.org/ontology#U31_had_function_of_type> | <http://data.doremus.org/ontology#U1_used_medium_of_performance> ?fonction .
?interpreter rdfs:label ?name .
?fonction rdfs:label | skos:prefLabel ?fctlabel .
FILTER (lang(?fctlabel)='fr') .
FILTER (!REGEX(?fctlabel, 'Femme'))
}
FILTER (?expression != <${expression}>)
} group by ?expression ?title ?execution ?titreconcert ?date ?creationex ?comment ?lieu`);
res.json(concerts);
}));
app.get('/ean', catcherror(async(req, res) => {
const results = await sparqlexec('http://data.bnf.fr/sparql', `
PREFIX bnf-onto: <http://data.bnf.fr/ontology/bnf-onto/>
PREFIX rdarelationships: <http://rdvocab.info/RDARelationshipsWEMI/>
SELECT ?bnfid WHERE {
?x bnf-onto:ean "${req.query.ean}" ;
rdarelationships:workManifested ?work.
?concept foaf:focus ?work ;
bnf-onto:FRBNF ?bnfid.
}`);
if (results.length) {
res.redirect(`/?work_id=${results[0].bnfid}`);
} else {
res.status(404).send('Œuvre non trouvée');
}
}));
// eslint-disable-next-line no-console
app.listen(3000, () => console.log('TousAuConcert listening on port 3000!'));