mirror of
https://gitlab.com/Alamantus/Readlebee.git
synced 2025-05-09 03:31:16 +02:00
Add endpoint for getting covers
This commit is contained in:
parent
da1fa7c77a
commit
c80e5e63ff
2 changed files with 75 additions and 0 deletions
|
@ -141,6 +141,73 @@ class SearchController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getInventaireCovers(inventaireURI) {
|
||||||
|
if (!inventaireURI) {
|
||||||
|
return Promise.resolve([]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Note: property `wdt:P629` is a given entity (uri)'s list of editions (ISBNs).
|
||||||
|
const editionsRequest = fetch(`https://inventaire.io/api/entities?action=reverse-claims&uri=${encodeURIComponent(inventaireURI)}&property=wdt:P629`)
|
||||||
|
editionsRequest.catch(exception => {
|
||||||
|
console.error(exception);
|
||||||
|
return {
|
||||||
|
error: exception,
|
||||||
|
message: `An error occurred when trying to reach the Inventaire API for URI ${inventaireURI}.`,
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
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([]);
|
||||||
|
}
|
||||||
|
|
||||||
|
const isbnsRequest = fetch(`https://inventaire.io/api/entities?action=by-uris&uris=${encodeURIComponent(editionURIs)}`);
|
||||||
|
isbnsRequest.catch(exception => {
|
||||||
|
console.error(exception);
|
||||||
|
return {
|
||||||
|
error: exception,
|
||||||
|
message: `An error occurred when trying to reach the Inventaire API for URI ${inventaireURI}.`,
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
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 [];
|
||||||
|
}
|
||||||
|
|
||||||
|
return Object.keys(responseJSON.entities).filter(key => {
|
||||||
|
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,
|
||||||
|
cover: `https://inventaire.io/img/entities/${entity.claims['invp:P2'][0]}`,
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Query a MediaWiki api.php instance with the given options
|
* Query a MediaWiki api.php instance with the given options
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -8,6 +8,14 @@ async function routes(fastify, options) {
|
||||||
|
|
||||||
return await search.searchInventaire();
|
return await search.searchInventaire();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
fastify.get('/api/search/cover', async (request, reply) => {
|
||||||
|
const inventaireURI = typeof request.query.uri !== 'undefined' ? request.query.uri.trim() : false;
|
||||||
|
const language = typeof request.query.lang !== 'undefined' ? request.query.lang.trim().split('-')[0] : undefined; // Get base language in cases like 'en-US'
|
||||||
|
const search = new SearchController(null, language);
|
||||||
|
|
||||||
|
return await search.getInventaireCovers(inventaireURI);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = routes
|
module.exports = routes
|
Loading…
Add table
Reference in a new issue