🐛 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. 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 ## 🌐 Installation

View File

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

View File

@ -3,6 +3,7 @@ const config = {
HOST: process.env.HOST, HOST: process.env.HOST,
FRONT_END_HOST: process.env.FRONT_END_HOST, FRONT_END_HOST: process.env.FRONT_END_HOST,
WEATHER_API_KEY: process.env.OpenWeatherMap_API_KEY, WEATHER_API_KEY: process.env.OpenWeatherMap_API_KEY,
SCRAPER_API_KEY: process.env.Scraper_API_KEY,
DATABASE: { DATABASE: {
host: process.env.DB_HOST, host: process.env.DB_HOST,
name: process.env.DB_NAME, 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 = [ const subjectList = [
"smartphone", "smartphone",
@ -18,10 +23,8 @@ function getRandomArrayElement(array) {
async function getAmazonProductList(subject) { async function getAmazonProductList(subject) {
const url = `https://www.amazon.fr/s?k=${subject}`; const url = `https://www.amazon.fr/s?k=${subject}`;
const Nightmare = require('nightmare')(); const { data } = await axios.get(`http://api.scraperapi.com/?api_key=${SCRAPER_API_KEY}&url=${url}`);
return await Nightmare.goto(url) const { document } = (new JSDOM(data)).window;
.wait('.s-result-item')
.evaluate(() => {
const amazonProductList = document.querySelectorAll('.s-result-item'); const amazonProductList = document.querySelectorAll('.s-result-item');
const productsList = []; const productsList = [];
for (let indexProduct in amazonProductList) { for (let indexProduct in amazonProductList) {
@ -39,13 +42,16 @@ async function getAmazonProductList(subject) {
} }
} }
return productsList; return productsList;
})
.end();
} }
module.exports = rightPriceOutput = async ({ res }, _argsObject) => { module.exports = rightPriceOutput = async ({ res, next }, _argsObject) => {
const subject = getRandomArrayElement(subjectList); const subject = getRandomArrayElement(subjectList);
try {
const productsList = await getAmazonProductList(subject); const productsList = await getAmazonProductList(subject);
const randomProduct = getRandomArrayElement(productsList); const randomProduct = getRandomArrayElement(productsList);
return res.status(200).json({ subject, ...randomProduct }); 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-http-to-https": "^1.1.4",
"express-validator": "^6.4.0", "express-validator": "^6.4.0",
"helmet": "^3.21.3", "helmet": "^3.21.3",
"jsdom": "^16.2.2",
"jsonwebtoken": "^8.5.1", "jsonwebtoken": "^8.5.1",
"moment": "^2.24.0", "moment": "^2.24.0",
"ms": "^2.1.2", "ms": "^2.1.2",
@ -32,7 +33,6 @@
"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"
} }
} }