soundbirder/controllers/quizz.js
2024-01-08 23:02:04 +01:00

159 lines
4.8 KiB
JavaScript
Executable File

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');
const { getRegion } = require('./region');
function quizzKey(region, locale, size) {
return `${region}:${locale}#${size}`;
}
async function getQuizzCached(region, locale, size) {
let key = quizzKey(region, locale, size);
let quizz = await cache.pop(key);
if (!quizz) {
quizz = generateQuizz(region, locale, size);
cache.push(key, quizz);
}
return quizz;
}
async function cacheQuizz(region, locale, size) {
let quizz = await generateQuizz(region, locale, size);
let key = quizzKey(region, locale, size);
cache.push(key, quizz);
return quizz;
}
async function generateQuizz(region, locale, size) {
const quizz = {}
try {
let speciesSelection = [];
do {
speciesSelection = await getSpeciesSelection(region, size);
} 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);
let answer;
do {
answer = choice(speciesSelectionLocalized);
quizz.answer = answer;
quizz.audio = await getAudio(answer.sciName);
if (quizz.audio === undefined) {
debug("No audio found for species", answer.sciName);
debug("Trying again...");
}
} 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 getSpeciesSelection(region, number) {
const regionCode = region;
const speciesList = await getSpeciesList(regionCode);
const speciesSelection = choices(speciesList, number);
debug("Species proposals:", speciesSelection)
return speciesSelection;
}
async function getLocalizedNames(speciesCodes, locale) {
let allNames = [];
for (let i = 0; i < speciesCodes.length; i++) {
const code = speciesCodes[i];
let flag = false;
try {
names = await cache.getCached(`${code}-${locale}`);
if (names) {
allNames.push(names);
flag = true;
}
} catch (error) {
console.error(error);
}
if (!flag) {
let names;
try {
names = { speciesCode, sciName, comName } = await getLocalizedName(code, locale);
cache.cacheResponse(`${code}-${locale}`, names);
allNames.push(names);
} catch (error) {
console.error(error);
}
}
}
return allNames;
}
function getLocalizedName(speciesCode, locale) {
return eBird.ref.taxonomy.ebird({
fmt: 'json',
locale: locale,
species: speciesCode
}).then(
response => {
const names = response[0];
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) {
if (cached != []) {
return cached;
} else {
console.log("Empty cached species list, retrieving again");
}
} else {
return eBird.product.spplist.in(regionCode)()
.then(species => {
species = species.filter((sp) => !sp.includes(" x "));
cache.cacheResponse(`spplist-${regionCode}`, species);
return species;
})
.catch(error => {
throw error;
});
}
}
function getAudio(speciesScientificName) {
return XenoCanto.search({
name: speciesScientificName,
quality: 'A'
}).then(response => {
debugResponses(response);
const { recordings } = response;
const randomRecord = choice(recordings);
const audio = randomRecord.file;
return audio;
}).catch(error => {
throw error;
});
}
module.exports = {
generateQuizz,
getQuizzCached,
cacheQuizz
}