🐛 FIX: Changement de logique pour rightPrice

This commit is contained in:
Divlo 2020-05-15 20:49:59 +02:00
parent 008f518f96
commit 02078ecc7a
6 changed files with 399 additions and 1011 deletions

View File

@ -30,7 +30,7 @@ Le projet est disponible sur [function.divlo.fr](https://function.divlo.fr/) (ac
Le partage est essentiel afin de progresser, l'**Open Source** permet l'entraide et le **partage de connaissance** entre développeurs.
Si vous voulez **contribuer**, avant toute chose écrivait une **"[issue](https://github.com/Divlo/FunctionProject/issues)" sur GitHub** à propos des changements que vous voulez apporter et on pourra en **discuter avec grand plaisir**. 😉
Si vous voulez **contribuer**, avant toute chose écrivez une **"[issue](https://github.com/Divlo/FunctionProject/issues)" sur GitHub** à propos des changements que vous voulez apporter et on pourra en **discuter avec grand plaisir**. 😉
## 🌐 Installation

View File

@ -1,6 +1,7 @@
HOST = "http://localhost:8080"
FRONT_END_HOST = "http://localhost:3000"
OpenWeatherMap_API_KEY = ""
Scraper_API_KEY = ""
DB_HOST = ""
DB_NAME = ""
DB_USER = ""

View File

@ -3,6 +3,7 @@ const config = {
HOST: process.env.HOST,
FRONT_END_HOST: process.env.FRONT_END_HOST,
WEATHER_API_KEY: process.env.OpenWeatherMap_API_KEY,
SCRAPER_API_KEY: process.env.Scraper_API_KEY,
DATABASE: {
host: process.env.DB_HOST,
name: process.env.DB_NAME,

View File

@ -1,4 +1,9 @@
const { randomNumber } = require('./randomNumber');
const { randomNumber } = require('./randomNumber');
const errorHandling = require('../../utils/errorHandling');
const { serverError } = require('../../config/errors');
const { SCRAPER_API_KEY } = require('../../config/config');
const axios = require('axios');
const { JSDOM } = require("jsdom");
const subjectList = [
"smartphone",
@ -17,35 +22,36 @@ function getRandomArrayElement(array) {
}
async function getAmazonProductList(subject) {
const url = `https://www.amazon.fr/s?k=${subject}`;
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;
productsList.push({
name: productImage["alt"],
image: productImage["src"],
price: Number(originalPrice.replace(",", ".").replace(" ", ""))
});
} catch (_error) {
continue;
}
}
return productsList;
})
.end();
const url = `https://www.amazon.fr/s?k=${subject}`;
const { data } = await axios.get(`http://api.scraperapi.com/?api_key=${SCRAPER_API_KEY}&url=${url}`);
const { document } = (new JSDOM(data)).window;
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;
productsList.push({
name: productImage["alt"],
image: productImage["src"],
price: Number(originalPrice.replace(",", ".").replace(" ", ""))
});
} catch (_error) {
continue;
}
}
return productsList;
}
module.exports = rightPriceOutput = async ({ res }, _argsObject) => {
const subject = getRandomArrayElement(subjectList);
const productsList = await getAmazonProductList(subject);
const randomProduct = getRandomArrayElement(productsList);
return res.status(200).json({ subject, ...randomProduct });
module.exports = rightPriceOutput = async ({ res, next }, _argsObject) => {
const subject = getRandomArrayElement(subjectList);
try {
const productsList = await getAmazonProductList(subject);
const randomProduct = getRandomArrayElement(productsList);
return res.status(200).json({ subject, ...randomProduct });
} catch (error) {
console.error(error);
return errorHandling(next, serverError);
}
}

1338
api/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -19,6 +19,7 @@
"express-http-to-https": "^1.1.4",
"express-validator": "^6.4.0",
"helmet": "^3.21.3",
"jsdom": "^16.2.2",
"jsonwebtoken": "^8.5.1",
"moment": "^2.24.0",
"ms": "^2.1.2",
@ -32,7 +33,6 @@
"devDependencies": {
"dotenv": "^8.2.0",
"morgan": "^1.9.1",
"nightmare": "^3.0.2",
"nodemon": "^2.0.2"
}
}