-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (42 loc) · 1.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const dotenv = require("dotenv");
const axios = require("axios");
const cheerio = require("cheerio");
const express = require("express");
const app = express();
const PORT = process.env.PORT;
dotenv.config();
const url = process.env.URL;
axios(url)
.then((response) => {
const html = response.data;
const $ = cheerio.load(html);
const products = [];
$(".package").each((index, element) => {
const title = $(element).find("h3").text();
const description = $(element).find(".package-description").text();
const price = $(element).find(".price-big").text();
const priceSort = parseInt(
$(element)
.find(".price-big")
.text()
.replace(/\u00A3/g, "")
.trim(),
10
);
const discount = $(element).find(".package-price p").text();
products.push({
title,
description,
price,
priceSort,
discount,
});
});
products.sort((a, b) => b.priceSort - a.priceSort);
products.forEach((element) => delete element.priceSort);
console.log(products);
})
.catch((err) => console.log(err));
app.listen(PORT, () => {
console.log(`server running on port ${PORT}`);
});