feat: Add a SPARQL query to retrieve images from Wikimedia commons

This commit is contained in:
Samuel Ortion 2024-02-13 21:21:43 +01:00
parent 9d2822709e
commit b0d267c6b3
1 changed files with 28 additions and 0 deletions

28
controllers/wikidata.js Normal file
View File

@ -0,0 +1,28 @@
// Query wikidata with axios
const axios = require("axios");
function query(sparqlQuery) {
const endpoint = 'https://query.wikidata.org/sparql';
const fullUrl = endpoint + '?query=' + encodeURIComponent( sparqlQuery );
const headers = { 'Accept': 'application/sparql-results+json' };
return axios.get(fullUrl, { headers }).then( body => body.data );
}
function encodeSparqlQuery(species) {
let sparqlTemplate = `#defaultView:ImageGrid
SELECT ?item ?itemLabel ?pic
WHERE
{
?item wdt:P225 "${species}" .
?item wdt:P18 ?pic
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
}`
return sparqlTemplate;
}
function getPictures(species) {
return query(encodeSparqlQuery(species));
}
getPictures("Picus viridis").then((data) => console.log(data.results.bindings));