-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
53 lines (49 loc) · 1.47 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
const axios = require("axios");
const cheerio = require("cheerio");
const tabletojson = require("tabletojson").Tabletojson;
class KamarNotices {
constructor(portal) {
//check if URL
var pattern = new RegExp(
"^(https?:\\/\\/)?" + // protocol
"((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|" + // domain name
"((\\d{1,3}\\.){3}\\d{1,3}))" + // OR ip (v4) address
"(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*" + // port and path
"(\\?[;&a-z\\d%_.~+=-]*)?" + // query string
"(\\#[-a-z\\d_]*)?$", // fragment locator
"i"
);
if (pattern.test(portal)) {
this.portal = `${portal}/index.php`;
} else {
throw new Error("Portal must be a valid URL");
}
}
getNotices() {
return new Promise((resolve, reject) => {
axios
.get(this.portal)
.then((data) => {
const $ = cheerio.load(data.data);
const html = $(".table-responsive").last().html();
const json = tabletojson.convert(html)[0];
const result = [];
for (let i = 0; i < json.length; i += 2) {
let header = json[i];
let content = json[i + 1];
result.push({
for: header["0"],
title: header["1"],
staff: header["2"],
content: content["0"],
});
}
resolve(result);
})
.catch((e) => {
reject(e);
});
});
}
}
module.exports = KamarNotices;