-
Notifications
You must be signed in to change notification settings - Fork 5
/
dotdpacktpub.js
51 lines (39 loc) · 1.49 KB
/
dotdpacktpub.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
// github.com/junwatu
const url = "https://www.packtpub.com/packt/offers/free-learning";
const cheerio = require("cheerio");
const request = require("request");
let jsonData = { image: "", title: "", description: "", timeLeft: "" };
function DOTDPacktPub() {
return new Promise((resolve, reject) => {
request(url, (error, response, html) => {
if (!error) {
resolve(queryData(html));
} else {
reject(error);
}
})
})
}
function queryData(html) {
let $ = cheerio.load(html);
let dotdImage = $(".dotd-main-book-image").children().children().last().attr("data-original");
let dotdTitle = $(".dotd-title").children().text();
let dotdTimeLeft = $(".packt-js-countdown").text();
let dotdDescription = $(".dotd-main-book-summary").children().last().prev().text();
jsonData.image = dotdImage;
jsonData.title = dotdTitle.trim();
jsonData.description = dotdDescription.trim();
jsonData.timeLeft = dotdTimeLeft;
if (jsonData.image && jsonData.title && jsonData.description === null ||
jsonData.image && jsonData.title && jsonData.description === "" ||
jsonData.image && jsonData.title && jsonData.description === undefined) {
return undefined;
} else {
return jsonData;
}
}
function processHTML(arg) {
let htmlData = queryData(arg);
return htmlData;
}
module.exports = { dotdPacktPub: DOTDPacktPub, processHTML: processHTML, URL: url };