-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
54 lines (50 loc) · 1.6 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
48
49
50
51
52
53
54
const express = require("express");
const axios = require("axios");
const cheerio = require("cheerio");
const PORT = process.env.PORT || 3000;
const app = express();
app.get("/", async (_, res) => {
const promos = await axios
.get("https://www.traveloka.com/id-id/promotion")
.then(async (res) => {
let result = [];
const $ = await cheerio.load(res.data);
$(".component-overflow.list-promo").each(() => {
$(".promo-thumb").each((i, elem) => {
const promoDetailLink = $(elem)
.find("a.buttonLinkPromotion")
.attr("href");
const promoImg = $(elem).find(".promo-thumb-img-el").attr("src");
const promoDesc = $(elem).find(".promo-thumb-desc").text();
let promoPeriod = $(elem)
.find(".promo-thumb-duration > p:nth-child(1)")
.html();
promoPeriod =
typeof promoPeriod === "string"
? promoPeriod.replace("–", "-")
: "";
const promoStayPeriod = $(elem)
.find(".promo-thumb-duration > p:nth-child(2)")
.text();
result.push({
promo_detail_link: promoDetailLink,
promo_img: promoImg,
promo_desc: promoDesc,
promo_period: promoPeriod,
promo_stay_period: promoStayPeriod,
});
});
});
return result;
})
.catch((err) => {
return {
status: "ERROR",
data: err,
};
});
res.send(promos);
});
app.listen(PORT, () => {
console.log(`Your application is running on http://localhost:${PORT}`);
});