2019-09-12 19:35:43 +02:00
|
|
|
const fetch = require('node-fetch');
|
|
|
|
|
2020-02-04 23:31:58 +01:00
|
|
|
class Inventaire {
|
|
|
|
constructor(bookURI, language) {
|
|
|
|
this.url = 'https://inventaire.io';
|
2019-09-12 19:35:43 +02:00
|
|
|
this.uri = bookURI;
|
|
|
|
this.lang = language;
|
|
|
|
}
|
|
|
|
|
2020-02-04 23:31:58 +01:00
|
|
|
handleQuickEntity(entityObject) {
|
2019-09-16 20:07:45 +02:00
|
|
|
return {
|
|
|
|
name: (
|
|
|
|
typeof entityObject.label !== 'undefined'
|
|
|
|
? entityObject.label
|
|
|
|
: null
|
|
|
|
),
|
|
|
|
description: (
|
|
|
|
typeof entityObject.description !== 'undefined'
|
|
|
|
? entityObject.description
|
|
|
|
: null
|
|
|
|
),
|
|
|
|
link: (
|
|
|
|
typeof entityObject.uri !== 'undefined'
|
2020-02-04 23:31:58 +01:00
|
|
|
? `${this.url}/entity/${entityObject.uri}`
|
2019-09-16 20:07:45 +02:00
|
|
|
: null
|
|
|
|
),
|
|
|
|
uri: (
|
|
|
|
typeof entityObject.uri !== 'undefined'
|
|
|
|
? entityObject.uri
|
|
|
|
: null
|
|
|
|
),
|
|
|
|
covers: (
|
|
|
|
typeof entityObject.image !== 'undefined'
|
|
|
|
? entityObject.image.map(imageId => {
|
|
|
|
return {
|
2019-09-26 20:22:58 +02:00
|
|
|
uri: imageId.toString(),
|
2020-02-04 23:31:58 +01:00
|
|
|
url: `${this.url}/img/entities/${imageId}`,
|
2019-09-16 20:07:45 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
: []
|
|
|
|
),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-02-04 23:31:58 +01:00
|
|
|
handleEntity(entityObject) {
|
2019-09-12 19:35:43 +02:00
|
|
|
const hasLabels = typeof entityObject.labels !== 'undefined';
|
|
|
|
const hasDescriptions = typeof entityObject.descriptions !== 'undefined';
|
|
|
|
|
|
|
|
return {
|
|
|
|
name: (
|
|
|
|
hasLabels && typeof entityObject.labels[this.lang] !== 'undefined'
|
|
|
|
? entityObject.labels[this.lang]
|
|
|
|
: (
|
|
|
|
hasLabels && Object.keys(entityObject.labels).length > 0
|
|
|
|
? entityObject.labels[Object.keys(entityObject.labels)[0]]
|
|
|
|
: null
|
|
|
|
)
|
|
|
|
),
|
|
|
|
description: (
|
|
|
|
hasDescriptions && typeof entityObject.descriptions[this.lang] !== 'undefined'
|
|
|
|
? entityObject.descriptions[this.lang]
|
|
|
|
: (
|
|
|
|
hasDescriptions && Object.keys(entityObject.descriptions).length > 0
|
|
|
|
? entityObject.descriptions[Object.keys(entityObject.descriptions)[0]]
|
|
|
|
: null
|
|
|
|
)
|
|
|
|
),
|
|
|
|
link: (
|
|
|
|
typeof entityObject.uri !== 'undefined'
|
2020-02-04 23:31:58 +01:00
|
|
|
? `${this.url}/entity/${entityObject.uri}`
|
2019-09-12 19:35:43 +02:00
|
|
|
: null
|
|
|
|
),
|
|
|
|
uri: (
|
|
|
|
typeof entityObject.uri !== 'undefined'
|
|
|
|
? entityObject.uri
|
|
|
|
: null
|
|
|
|
),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-02-04 23:31:58 +01:00
|
|
|
async getBookData(uri) {
|
|
|
|
if (uri) {
|
|
|
|
const request = fetch(`${this.url}/api/entities?action=by-uris&uris=${encodeURIComponent(uri)}`)
|
2019-09-12 19:35:43 +02:00
|
|
|
request.catch(exception => {
|
|
|
|
console.error(exception);
|
|
|
|
return {
|
|
|
|
error: exception,
|
|
|
|
message: 'An error occurred when trying to reach the Inventaire API.',
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const json = request.then(response => response.json());
|
|
|
|
json.catch(exception => {
|
|
|
|
console.error(exception);
|
|
|
|
return {
|
|
|
|
error: exception,
|
|
|
|
message: 'An error occurred when trying read the response from Inventaire as JSON.',
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const bookData = await json;
|
|
|
|
|
2020-02-04 23:31:58 +01:00
|
|
|
if (typeof bookData.entities !== 'undefined' && typeof bookData.entities[uri] !== 'undefined') {
|
|
|
|
const bookData = this.handleEntity(bookData.entities[uri], this.lang);
|
|
|
|
bookData['covers'] = await this.getCovers();
|
2019-09-12 19:35:43 +02:00
|
|
|
|
|
|
|
return bookData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
name: 'No URI provided',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-02-04 23:31:58 +01:00
|
|
|
async getCovers(uri) {
|
|
|
|
if (!uri) {
|
2019-09-12 19:35:43 +02:00
|
|
|
return Promise.resolve([]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: property `wdt:P629` is a given entity (uri)'s list of editions (ISBNs).
|
2020-02-04 23:31:58 +01:00
|
|
|
const editionsRequest = fetch(`${this.url}/api/entities?action=reverse-claims&uri=${encodeURIComponent(uri)}&property=wdt:P629`)
|
2019-09-12 19:35:43 +02:00
|
|
|
editionsRequest.catch(exception => {
|
|
|
|
console.error(exception);
|
|
|
|
return {
|
|
|
|
error: exception,
|
2020-02-04 23:31:58 +01:00
|
|
|
message: `An error occurred when trying to reach the Inventaire API for URI ${uri}.`,
|
2019-09-12 19:35:43 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const editionsJson = editionsRequest.then(response => response.json());
|
|
|
|
editionsJson.catch(exception => {
|
|
|
|
console.error(exception);
|
|
|
|
return {
|
|
|
|
error: exception,
|
|
|
|
message: 'An error occurred when trying read the response from Inventaire as JSON.',
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const editions = await editionsJson;
|
|
|
|
const editionURIs = typeof editions.uris !== 'undefined' ? editions.uris.join('|') : false;
|
|
|
|
|
|
|
|
if (editionURIs === false) {
|
|
|
|
return Promise.resolve([]);
|
|
|
|
}
|
|
|
|
|
2020-02-04 23:31:58 +01:00
|
|
|
const isbnsRequest = fetch(`${this.url}/api/entities?action=by-uris&uris=${encodeURIComponent(editionURIs)}`);
|
2019-09-12 19:35:43 +02:00
|
|
|
isbnsRequest.catch(exception => {
|
|
|
|
console.error(exception);
|
|
|
|
return {
|
|
|
|
error: exception,
|
2020-02-04 23:31:58 +01:00
|
|
|
message: `An error occurred when trying to reach the Inventaire API for URI ${uri}.`,
|
2019-09-12 19:35:43 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const isbnsJson = isbnsRequest.then(response => response.json());
|
|
|
|
isbnsJson.catch(exception => {
|
|
|
|
console.error(exception);
|
|
|
|
return {
|
|
|
|
error: exception,
|
|
|
|
message: 'An error occurred when trying read the response from Inventaire as JSON.',
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return isbnsJson.then(responseJSON => {
|
|
|
|
if (typeof responseJSON.entities === 'undefined') {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2019-09-13 01:04:50 +02:00
|
|
|
const covers = Object.keys(responseJSON.entities).filter(key => {
|
2019-09-12 19:35:43 +02:00
|
|
|
const entity = responseJSON.entities[key];
|
|
|
|
return entity.originalLang === this.lang && typeof entity.claims !== undefined && typeof entity.claims['invp:P2'] !== undefined;
|
|
|
|
}).map(key => {
|
|
|
|
const entity = responseJSON.entities[key];
|
|
|
|
return {
|
|
|
|
uri: entity.uri,
|
2020-02-04 23:31:58 +01:00
|
|
|
url: typeof entity.claims['invp:P2'] !== 'undefined' ? `${this.url}/img/entities/${entity.claims['invp:P2'][0]}` : null,
|
2019-09-13 01:04:50 +02:00
|
|
|
publishDate: typeof entity.claims['wdt:P577'] !== 'undefined' ? entity.claims['wdt:P577'][0] : null,
|
2019-09-12 19:35:43 +02:00
|
|
|
}
|
|
|
|
});
|
2019-09-13 01:04:50 +02:00
|
|
|
|
|
|
|
covers.sort((a, b) => {
|
|
|
|
if (a.publishDate === b.publishDate) return 0;
|
|
|
|
if (!a.publishDate && !!b.publishDate) return 1;
|
|
|
|
if (!!a.publishDate && !b.publishDate) return 1;
|
|
|
|
return a.publishDate < b.publishDate ? -1 : 1;
|
|
|
|
});
|
|
|
|
|
|
|
|
return covers;
|
2019-09-12 19:35:43 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-04 23:31:58 +01:00
|
|
|
module.exports = Inventaire;
|