generated from Arquisoft/wiq_0
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from Arquisoft/jota
Conectar generador preguntas con front
- Loading branch information
Showing
6 changed files
with
150 additions
and
104 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,60 @@ | ||
// Importa Express.js | ||
// Importamos el módulo Express para crear un servidor web | ||
const express = require('express'); | ||
// Importamos el módulo Axios para realizar solicitudes HTTP | ||
const axios = require('axios'); | ||
const cors = require('cors'); | ||
|
||
// Crea una instancia de Express | ||
// Creamos una nueva aplicación Express | ||
const app = express(); | ||
const PORT = 3001; | ||
app.use(cors()); | ||
|
||
// Ruta de ejemplo | ||
app.get('/', (req, res) => { | ||
res.send('¡Hola, mundo!'); | ||
// Definimos una ruta GET en '/pregunta' | ||
app.get('/pregunta', async (req, res) => { | ||
// URL del endpoint SPARQL de Wikidata | ||
const url = "https://query.wikidata.org/sparql"; | ||
// Consulta SPARQL para obtener países y sus capitales | ||
const query = ` | ||
SELECT ?country ?countryLabel ?capital ?capitalLabel WHERE { | ||
?country wdt:P31 wd:Q6256; | ||
wdt:P36 ?capital. | ||
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],es". } | ||
} | ||
LIMIT 50 | ||
`; | ||
// Realizamos la solicitud HTTP GET al endpoint SPARQL con la consulta | ||
const response = await axios.get(url, { params: { format: 'json', query } }); | ||
// Extraemos los resultados de la consulta | ||
const bindings = response.data.results.bindings; | ||
// Seleccionamos un índice aleatorio para la respuesta correcta | ||
const correctAnswerIndex = Math.floor(Math.random() * bindings.length); | ||
// Obtenemos la respuesta correcta | ||
const correctAnswer = bindings[correctAnswerIndex]; | ||
// Creamos la pregunta | ||
const question = `¿Cuál es la capital de ${correctAnswer.countryLabel.value}?`; | ||
// Inicializamos las respuestas con la respuesta correcta | ||
const answerGood = correctAnswer.capitalLabel.value; | ||
const answers = [answerGood]; | ||
// Añadimos tres respuestas incorrectas | ||
for (let i = 0; i < 3; i++) { | ||
let randomIndex; | ||
do { | ||
// Seleccionamos un índice aleatorio distinto al de la respuesta correcta | ||
randomIndex = Math.floor(Math.random() * bindings.length); | ||
} while (randomIndex === correctAnswerIndex); | ||
// Añadimos la capital del país seleccionado aleatoriamente a las respuestas | ||
answers.push(bindings[randomIndex].capitalLabel.value); | ||
} | ||
// Mezclamos las respuestas | ||
for (let i = answers.length - 1; i > 0; i--) { | ||
const j = Math.floor(Math.random() * (i + 1)); | ||
// Intercambiamos las respuestas en los índices i y j | ||
[answers[i], answers[j]] = [answers[j], answers[i]]; | ||
} | ||
// Enviamos la pregunta y las respuestas como respuesta a la solicitud HTTP | ||
res.json({ question, answerGood, answers }); | ||
}); | ||
|
||
// Inicia el servidor | ||
app.listen(PORT, () => { | ||
console.log(`Servidor escuchando en el puerto ${PORT}`); | ||
}); | ||
|
||
// Definimos la URL de la API de Wikidata para buscar información sobre un tema específico | ||
const searchUrl = 'https://www.wikidata.org/w/rest.php/wikibase/v0/entities/items/'; | ||
|
||
// Función para obtener información sobre un tema específico | ||
function buscarInformacion(tema) { | ||
// Construimos la URL de búsqueda concatenando el tema a la URL base | ||
const url = searchUrl + tema | ||
console.log(url) | ||
|
||
// Realizamos la solicitud GET a la API de Wikidata | ||
fetch(url) | ||
.then(response => response.json()) | ||
.then(data => { | ||
console.log(data) | ||
}) | ||
.catch(error => console.error('Error al buscar información:', error)); | ||
} | ||
// Iniciamos el servidor en el puerto 3000 | ||
const server = app.listen(2500, () => console.log('El servidor está escuchando en el puerto 2500')); | ||
|
||
// Ejemplo de uso: buscar información sobre el tema "JavaScript" | ||
app.get('/search', (req, res) => { | ||
res.send('¡Hola, mundo!'); | ||
buscarInformacion("Q42"); | ||
}); | ||
module.exports = server |
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters