soundbirder/controllers/quizz.js

185 lines
5.9 KiB
JavaScript
Raw Normal View History

require('dotenv').config();
const debug = require('debug')('soundbirder:api:quizz');
const debugResponses = require('debug')('soundbirder:api:responses');
const cache = require('./cache');
const eBird = require('@unclesamulus/ebird-api')(process.env.EBIRD_API_KEY);
const XenoCanto = require('@unclesamulus/xeno-canto-api');
const { choices, choice } = require('../utils/choices');
function questionKey(region, locale, size) {
return `${region}:${locale}#${size}`;
}
async function getQuestionCached(region, locale, size) {
let key = questionKey(region, locale, size);
let quizz = await cache.pop(key);
if (!quizz) {
quizz = await generateQuestion(region, locale, size);
cache.push(key, quizz);
}
return quizz;
}
async function cacheQuestion(region, locale, size) {
let quizz = await generateQuestion(region, locale, size);
let key = questionKey(region, locale, size);
cache.push(key, quizz);
return quizz;
}
async function generateQuestion(region, locale, nb_propositions) {
const quizz = {}
try {
let speciesSelection = [];
do {
speciesSelection = await getSpeciesSelection(region, nb_propositions);
} while (speciesSelection == []);
debugResponses(`Species selection: ${speciesSelection}`);
const speciesSelectionLocalized = await getLocalizedNames(speciesSelection, locale);
if (!speciesSelectionLocalized) {
throw 'Not localized';
}
debugResponses('Localized species selection:', speciesSelectionLocalized);
quizz.species = speciesSelectionLocalized;
debug("Got species selection", quizz.species);
2022-08-29 10:38:42 +02:00
let answer;
do {
answer = choice(speciesSelectionLocalized);
quizz.answer = answer;
quizz.audio = await getAudio(answer.sciName);
2022-08-29 14:05:34 +02:00
if (quizz.audio === undefined) {
2022-08-29 10:38:42 +02:00
debug("No audio found for species", answer.sciName);
debug("Trying again...");
}
2022-08-29 14:05:34 +02:00
} while (quizz.audio === undefined);
debug("Got answer", answer);
debug("Got audio", quizz.audio);
} catch (error) {
debug("Error raised while generating quizz");
console.error(error);
}
return quizz;
}
async function generateQuizz(region, locale, nb_questions) {
let questions = [];
let answers = [];
let start = Date.now();
let species_in_region_localized = await getSpeciesNamesInRegion(region, locale);
for (let i=0; i < nb_questions; i++) {
try {
let question_species = choice(species_in_region_localized);
let question_audio = await getAudio(question_species.sciName);
questions.push(question_audio);
answers.push(question_species);
} catch (error) {
console.error(error);
}
}
const millis = Date.now() - start;
console.log(`seconds elapsed = ${Math.floor(millis / 1000)}`);
return {questions: questions, answers: answers, propositions: species_in_region_localized};
}
async function getSpeciesNamesInRegion(region, locale) {
const cacheKey = `region-sppnames-${locale}-${region}`;
let cached = await cache.getCached(cacheKey);
if (cached && cached != []) {
return cached;
} else {
const species_in_region = await getSpeciesList(region);
const species_in_region_localized = await getLocalizedNames(species_in_region, locale);
cache.cacheResponse(cacheKey, species_in_region_localized);
return species_in_region_localized;
}
}
async function getSpeciesSelection(region, number) {
const regionCode = region;
const speciesList = await getSpeciesList(regionCode);
const speciesSelection = choices(speciesList, number);
return speciesSelection;
}
async function getLocalizedNames(speciesCodes, locale) {
2024-02-04 07:07:37 +01:00
const localizedNames = [];
for (const speciesCode of speciesCodes) {
const localized = await getLocalizedName(speciesCode, locale);
if (!localized.sciName.includes(" x "))
localizedNames.push(localized);
}
2024-02-04 07:07:37 +01:00
return localizedNames;
}
async function getLocalizedName(speciesCode, locale) {
const cacheKey = `sppnames-${locale}-${speciesCode}`;
const cached = await cache.getCached(cacheKey);
if (cached) {
return cached;
} else {
return eBird.ref.taxonomy.ebird({
fmt: 'json',
locale: locale,
species: speciesCode
}).then(
response => {
const names = response[0];
cache.cacheResponse(cacheKey, names);
debug("Got localized species names");
debugResponses(names);
return names;
}
).catch(error => {
throw error;
});
}
}
async function getSpeciesList(regionCode) {
const cached = await cache.getCached(`spplist-${regionCode}`);
if (cached) {
2024-01-08 23:02:04 +01:00
if (cached != []) {
return cached;
} else {
console.log("Empty cached species list, retrieving again");
}
} else {
return eBird.product.spplist.in(regionCode)()
.then(species => {
cache.cacheResponse(`spplist-${regionCode}`, species);
return species;
})
.catch(error => {
throw error;
});
}
}
function getAudio(speciesScientificName) {
return XenoCanto.search({
name: speciesScientificName,
quality: 'A'
}).then(response => {
const { recordings } = response;
let randomRecord;
let audio;
for (let i=0; i < 3; i++) {
randomRecord = choice(recordings);
if ('file' in randomRecord) {
audio = randomRecord.file;
break;
}
}
return audio;
}).catch(error => {
throw error;
});
}
module.exports = {
generateQuestion,
getQuestionCached,
cacheQuestion,
generateQuizz
}