-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnotices.js
68 lines (63 loc) · 2.08 KB
/
notices.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const request = require("request");
const cheerio = require("cheerio");
const htmlToMd = require("html-to-md");
exports.getNotices = async (url, callback) => {
request(url, async (error, response, html) => {
if (!error && response.statusCode == 200) {
const $ = cheerio.load(html);
var list = [];
$(".card-columns > .card").each(function () {
var date = $(this).find("div > div > p").text();
var title = $(this).find("div > div > a").text();
var link = $(this).find("div > div > a").attr("href");
// console.log(data);
list.push({
date: date,
title: title,
link: link,
});
});
return callback(list);
}
});
};
exports.getNoticeContent = async (url, callback) => {
request(url, async (error, response, html) => {
if (!error && response.statusCode == 200) {
const $ = cheerio.load(html);
const Entities = require("html-entities").AllHtmlEntities;
const entities = new Entities();
$(".dpsp-share-text").remove();
$("#dpsp-content-bottom").remove();
const htmlContent = $(
"body > div.main-page-wrapper > section.single-notice > div > div > main > div > div"
);
const title = htmlContent.find("h4").text();
const content = htmlContent.find("p").text();
const images = [];
htmlContent.find("img").each(function () {
var imageUrl = $(this).attr("src");
images.push(imageUrl);
});
const attachments = [];
htmlContent.find("a").each(function () {
var attachmentUrl = $(this).attr("href");
var attachmentTitle = $(this).text();
attachments.push({
title: attachmentTitle,
url: attachmentUrl,
});
});
const htmlData = entities.decode(htmlContent.html());
const markdown = htmlToMd(htmlData);
return callback({
title: title,
content: content.replace("Sharing is caring!", ""),
html: htmlData,
markdown: markdown,
images: images,
attachments: attachments,
});
}
});
};