-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
140 lines (117 loc) · 3.96 KB
/
index.js
File metadata and controls
140 lines (117 loc) · 3.96 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
const express = require('express')
const axios = require('axios')
const app = express()
const cheerio = require('cheerio')
const url = `http://ketqua.net/xo-so-truyen-thong.php?ngay=`
const index = require('./components/index')
// firebase
var admin = require('firebase-admin')
var serviceAccount = require("./soicau247-567bf-firebase-adminsdk-3p9te-eaea471ce1.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://soicau247-567bf.firebaseio.com"
})
var database = admin.firestore()
app.use(express.static('public'))
app.get('/', async function (req, res) {
res.send(index)
})
app.listen(3000, function () {
console.log('server start')
})
function fTime(date) {
return date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear()
}
async function getUrlContent(url) {
let res = await axios.get(url)
return res.data
}
app.get('/today', async function (req, res) {
let date = new Date()
let content = await getUrlContent(url + fTime(date))
const $ = cheerio.load(content)
var ketqua = $('#result_tab_mb').find('td[rs_len]')
let array = []
for(let i = 0; i <ketqua.length; i++) {
array[i] = $(ketqua[i]).text()
}
database.collection("results").doc(fTime(date)).set({
prizes: array
})
// let content = await getUrlContent(url + fTime(date))
// const $ = cheerio.load(content)
// let array = []
// var ketqua = $('#result_tab_mb').find('td[rs_len]')
// for (let i = 0; i < ketqua.length; i++) {
// array[i] = $(ketqua[i]).text()
// }
let doc = await database.collection("results").doc(fTime(date)).get()
if(doc.data() == undefined) {
date.setDate(date.getDate() - 1)
doc = await database.collection("results").doc(fTime(date)).get()
}
array = []
for(let prize of doc.data().prizes) {
array.push(prize)
}
let html = `
<table class="table">
<tr>
<td>Đặc biệt</td>
<td colspan="12">${array[0]}</td>
</tr>
<tr>
<td>Nhất</td>
<td colspan="12">${array[1]}</td>
</tr>
<tr>
<td>Nhì</td>
<td colspan="6">${array[2]}</td>
<td colspan="6">${array[3]}</td>
</tr>
<tr>
<td rowspan="2">Ba</td>
<td colspan="4">${array[4]}</td>
<td colspan="4">${array[5]}</td>
<td colspan="4">${array[6]}</td>
</tr>
<tr>
<td colspan="4">${array[7]}</td>
<td colspan="4">${array[8]}</td>
<td colspan="4">${array[9]}</td>
</tr>
<tr>
<td>TƯ</td>
<td colspan="3">${array[10]}</td>
<td colspan="3">${array[11]}</td>
<td colspan="3">${array[12]}</td>
<td colspan="3">${array[13]}</td>
</tr>
<tr>
<td rowspan="2">Năm</td>
<td colspan="4">${array[14]}</td>
<td colspan="4">${array[15]}</td>
<td colspan="4">${array[16]}</td>
</tr>
<tr>
<td colspan="4">${array[17]}</td>
<td colspan="4">${array[18]}</td>
<td colspan="4">${array[19]}</td>
</tr>
<tr>
<td>Sáu</td>
<td colspan="4">${array[20]}</td>
<td colspan="4">${array[21]}</td>
<td colspan="4">${array[22]}</td>
</tr>
<tr>
<td>Bảy</td>
<td colspan="3">${array[23]}</td>
<td colspan="3">${array[24]}</td>
<td colspan="3">${array[25]}</td>
<td colspan="3">${array[26]}</td>
</tr>
</table>
`
res.send(html);
})