-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
109 lines (97 loc) · 3.41 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const express = require("express");
const app = express();
const request = require("request-promise");
const cheerio = require("cheerio");
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.listen(80);
const getKqxs = async (day, month, year) => {
try {
var $ = await request({
uri: `https://xoso.com.vn/xsmb-${day}-${month}-${year}.html`,
transform: function (body) {
return cheerio.load(body);
},
});
} catch (error) {
return error;
}
let cnt_kqmb = $(`#kqngay_${day}${month}${year}_kq`);
if(!cnt_kqmb.html()) {
return {
success: false,
message: "Chưa có kết quả"
};
}
let tb_kqmb = cnt_kqmb.find('table.table-result');
if(Number.isNaN(parseInt(tb_kqmb.find('#mb_prizeDB_item0').text().trim())) || Number.isNaN(parseInt(tb_kqmb.find('#mb_prize7_item3').text().trim()))) {
return {
success: false,
message: "Chưa có kết quả"
};
}
let kq = {
g_DB: tb_kqmb.find('#mb_prizeDB_item0').text().trim(),
g_1: tb_kqmb.find('#mb_prize1_item0').text().trim(),
g_2: [
tb_kqmb.find('#mb_prize2_item0').text().trim(),
tb_kqmb.find('#mb_prize2_item1').text().trim(),
],
g_3: [
tb_kqmb.find('#mb_prize3_item0').text().trim(),
tb_kqmb.find('#mb_prize3_item1').text().trim(),
tb_kqmb.find('#mb_prize3_item2').text().trim(),
tb_kqmb.find('#mb_prize3_item3').text().trim(),
tb_kqmb.find('#mb_prize3_item4').text().trim(),
tb_kqmb.find('#mb_prize3_item5').text().trim(),
],
g_4: [
tb_kqmb.find('#mb_prize4_item0').text().trim(),
tb_kqmb.find('#mb_prize4_item1').text().trim(),
tb_kqmb.find('#mb_prize4_item2').text().trim(),
tb_kqmb.find('#mb_prize4_item3').text().trim(),
],
g_5: [
tb_kqmb.find('#mb_prize5_item0').text().trim(),
tb_kqmb.find('#mb_prize5_item1').text().trim(),
tb_kqmb.find('#mb_prize5_item2').text().trim(),
tb_kqmb.find('#mb_prize5_item3').text().trim(),
tb_kqmb.find('#mb_prize5_item4').text().trim(),
tb_kqmb.find('#mb_prize5_item5').text().trim(),
],
g_6: [
tb_kqmb.find('#mb_prize6_item0').text().trim(),
tb_kqmb.find('#mb_prize6_item1').text().trim(),
tb_kqmb.find('#mb_prize6_item2').text().trim(),
],
g_7: [
tb_kqmb.find('#mb_prize7_item0').text().trim(),
tb_kqmb.find('#mb_prize7_item1').text().trim(),
tb_kqmb.find('#mb_prize7_item2').text().trim(),
tb_kqmb.find('#mb_prize7_item3').text().trim(),
],
};
return {
success: true,
data: kq,
message: "Thao tác thành công"
};
};
app.get("/", async function(req, res) {
let date = now();
let day = addZero(req.query.day || date.getDate());
let month = addZero(req.query.month || (date.getMonth() + 1));
let year = addZero(req.query.year || date.getFullYear());
let kq = await getKqxs(day, month, year);
if(kq.success) {
res.json(kq);
} else {
res.status(400).json(kq);
}
});
function now() {
return new Date(Date.now());
}
function addZero(num) {
return num < 10 ? `0${num}` : num;
}