📦 NEW: rightPrice backend

This commit is contained in:
Divlo 2020-04-27 22:06:37 +02:00
parent 1ae744d3db
commit fbe225fbbb
5 changed files with 1342 additions and 3 deletions

View File

@ -1,4 +1,4 @@
const randomNumberOutput = require('./main/randomNumber'); const { randomNumberOutput } = require('./main/randomNumber');
const convertRomanArabicNumbersOutput = require('./main/convertRomanArabicNumbers'); const convertRomanArabicNumbersOutput = require('./main/convertRomanArabicNumbers');
const convertDistanceOutput = require('./main/convertDistance'); const convertDistanceOutput = require('./main/convertDistance');
const convertTemperatureOutput = require('./main/convertTemperature'); const convertTemperatureOutput = require('./main/convertTemperature');
@ -10,6 +10,7 @@ const heapAlgorithmOutput = require('./main/heapAlgorithm');
const convertEncodingOutput = require('./main/convertEncoding'); const convertEncodingOutput = require('./main/convertEncoding');
const randomQuote = require('./main/randomQuote'); const randomQuote = require('./main/randomQuote');
const linkShortener = require('./main/linkShortener'); const linkShortener = require('./main/linkShortener');
const rightPriceOutput = require('./main/rightPrice');
const functionObject = { const functionObject = {
randomNumber : randomNumberOutput, randomNumber : randomNumberOutput,
@ -24,6 +25,7 @@ const functionObject = {
convertEncoding : convertEncodingOutput, convertEncoding : convertEncodingOutput,
randomQuote : randomQuote, randomQuote : randomQuote,
linkShortener : linkShortener, linkShortener : linkShortener,
rightPrice : rightPriceOutput,
}; };
// Choisi la fonction à exécuter // Choisi la fonction à exécuter

View File

@ -14,7 +14,7 @@ function randomNumber(min, max) {
} }
/* OUTPUTS */ /* OUTPUTS */
module.exports = randomNumberOutput = ({ res, next }, argsObject) => { const randomNumberOutput = ({ res, next }, argsObject) => {
let { min, max } = argsObject; let { min, max } = argsObject;
// S'il n'y a pas les champs obligatoire // S'il n'y a pas les champs obligatoire
@ -31,4 +31,7 @@ module.exports = randomNumberOutput = ({ res, next }, argsObject) => {
const result = randomNumber(min, max); const result = randomNumber(min, max);
return res.status(200).json({ result, resultHTML: `<p>Nombre aléatoire compris entre ${min} inclus et ${max} inclus : <strong>${formatNumberResult(result)}</strong></p>` }); return res.status(200).json({ result, resultHTML: `<p>Nombre aléatoire compris entre ${min} inclus et ${max} inclus : <strong>${formatNumberResult(result)}</strong></p>` });
} }
exports.randomNumber = randomNumber;
exports.randomNumberOutput = randomNumberOutput;

View File

@ -0,0 +1,49 @@
const { randomNumber } = require('./randomNumber');
const subjectList = [
"smartphone",
"pc+gamer",
"pc+portable",
"TV",
"casque",
"clavier",
"souris",
"ecran",
"jeux+vidéos"
];
function getRandomArrayElement(array) {
return array[randomNumber(0, array.length - 1)];
}
async function getAmazonProductList(url) {
const Nightmare = require('nightmare')();
return await Nightmare.goto(url)
.wait('.s-result-item')
.evaluate(() => {
const amazonProductList = document.querySelectorAll('.s-result-item');
const productsList = [];
for (let indexProduct in amazonProductList) {
try {
const elementProduct = amazonProductList[indexProduct];
const productImage = elementProduct.querySelector('.s-image');
productsList.push({
name: productImage["alt"],
image: productImage["src"],
price: elementProduct.querySelector(".a-price-whole").innerHTML
});
} catch (error) {
continue;
}
}
return productsList;
})
.end();
}
module.exports = rightPriceOutput = async ({ res }, _argsObject) => {
const subject = getRandomArrayElement(subjectList);
const productsList = await getAmazonProductList(`https://www.amazon.fr/s?k=${subject}`);
const randomProduct = getRandomArrayElement(productsList);
return res.status(200).json({ subject, ...randomProduct });
}

1284
api/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -32,6 +32,7 @@
"devDependencies": { "devDependencies": {
"dotenv": "^8.2.0", "dotenv": "^8.2.0",
"morgan": "^1.9.1", "morgan": "^1.9.1",
"nightmare": "^3.0.2",
"nodemon": "^2.0.2" "nodemon": "^2.0.2"
} }
} }