-
Notifications
You must be signed in to change notification settings - Fork 0
/
get539Data.js
85 lines (80 loc) · 2.54 KB
/
get539Data.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
/*eslint-disable*/
const request = require('request')
const $ = require('cheerio')
const url = 'https://www.taiwanlottery.com.tw/lotto/dailycash/history.aspx'
const fs = require('fs')
let postData = {
D539Control_history1$DropDownList1: 5,
D539Control_history1$chk: 'radYM',
D539Control_history1$btnSubmit: '查詢',
__VIEWSTATEGENERATOR: '09BD3138',
}
// postData.D539Control_history1$dropYear = 109
// postData.D539Control_history1$dropMonth = 8
const getToken = () => {
return new Promise((resolve, reject) => {
request.post(url, { form: {} }, function(error, response, html) {
const __EVENTVALIDATION = $('#__EVENTVALIDATION', html).val()
const __VIEWSTATE = $('#__VIEWSTATE', html).val()
resolve({ __VIEWSTATE, __EVENTVALIDATION })
})
})
}
const getData = (year, month) => {
return new Promise((resolve, reject) => {
const dataList = []
const pagePostData = {
...postData,
D539Control_history1$dropYear: year,
D539Control_history1$dropMonth: month,
}
request.post(url, { form: pagePostData }, function(error, response, html) {
const tableList = $('table.td_hm', html)
tableList.each(function(index, element) {
const newsItem = {
date: $(element)
.find(`#D539Control_history1_dlQuery_D539_DDate_${index}`)
.text()
.replace(/\//g, '-')
.replace(/^.\d*-/, `${year + 1911}-`),
num: [
$(element)
.find(`#D539Control_history1_dlQuery_No1_${index}`)
.text(),
$(element)
.find(`#D539Control_history1_dlQuery_No2_${index}`)
.text(),
$(element)
.find(`#D539Control_history1_dlQuery_No3_${index}`)
.text(),
$(element)
.find(`#D539Control_history1_dlQuery_No4_${index}`)
.text(),
$(element)
.find(`#D539Control_history1_dlQuery_No5_${index}`)
.text(),
],
}
newsItem.num.sort()
dataList.push(newsItem)
})
resolve(dataList)
})
})
}
;(async () => {
// const year = 109
const allYearData = []
postData = { ...postData, ...(await getToken()) }
for (let year = 103; year <= 109 ; year++) {
for (let m = 1; m <= 12; m++) {
allYearData.push(...(await getData(year, m)))
}
}
const sortData = allYearData.sort((a, b) => {
if (a.date < b.date) return -1
if (a.date > b.date) return 1
return 0
})
fs.writeFileSync(`./src/assets/Data.json`, JSON.stringify(sortData, null, 2))
})()