This repository has been archived on 2024-10-29. You can view files and clone it, but cannot push or open issues or pull requests.
FunctionProject/api/assets/functions/main/rightPrice.js

50 lines
1.7 KiB
JavaScript
Raw Normal View History

2020-04-27 22:06:37 +02:00
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');
const originalPrice = elementProduct.querySelector(".a-price-whole").innerHTML;
2020-04-27 22:06:37 +02:00
productsList.push({
name: productImage["alt"],
image: productImage["src"],
price: Number(originalPrice.replace(",", ".").replace(" ", ""))
2020-04-27 22:06:37 +02:00
});
} 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 });
}