28 lines
849 B
JavaScript
28 lines
849 B
JavaScript
// 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)); |