Skip to content

Commit

Permalink
Added the logic to do a simple wikidata query
Browse files Browse the repository at this point in the history
  • Loading branch information
alegarman2002 committed Mar 13, 2024
1 parent 24df8f2 commit 173845e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
async function consulta(query) {
const apiUrl = `https://query.wikidata.org/sparql?query=${encodeURIComponent(query)}&format=json`;
console.log(query)
const respuesta = await fetch(apiUrl, {
headers: {
'User-Agent': 'QuestionCrawler/1.0',
'Accept': 'application/json'
}
});
// console.log(apiUrl)
// console.log(respuesta)
if (!respuesta.ok) {
console.error('Error al realizar la consulta a Wikidata:', respuesta.statusText);
return;
}

const datos = await respuesta.json();

const resultados = [];

for (const resultado of datos.results.bindings) {
const resultadoFormateado = {};
for (const clave in resultado) {
resultadoFormateado[clave] = resultado[clave].value;
}
resultados.push(resultadoFormateado);
}

return resultados;
}

module.exports = {consulta}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const wikidata = require("./wikidataConnexion");

class WikiQueries {

static async obtenerPaisYCapital() {
const query = `
SELECT ?countryLabel ?capitalLabel WHERE {
?country wdt:P31 wd:Q6256.
?country wdt:P36 ?capital.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],es". }
}
`;

const results = await wikidata.consulta(query);
// console.log(results)
return results;
}
}









module.exports = WikiQueries

0 comments on commit 173845e

Please sign in to comment.