-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
106 lines (94 loc) · 3.28 KB
/
server.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
const puppeteer = require('puppeteer')
const dados = {
concurso: 0,
data: null,
numeros: [],
premiacao: [],
arrecadacaoTotal: null,
detalhamento: [],
localSorteio: null
};
(async () => {
const browser = await puppeteer.launch({ product: 'firefox', headless: true })
const page = await browser.newPage()
await page.goto('http://loterias.caixa.gov.br/wps/portal/loterias/landing/lotofacil/')
/// ## Concurso NUMERO e DATA
const concurso = await page.evaluate(() => document
.querySelector('.title-bar > h2:nth-child(2) > span:nth-child(1)')
.innerText.trim()
.split(/[ |\n|(|)]/).map(txt => txt.trim())
.filter(txt => txt)
.reduce((acc, item, idx) => {
const prop = [false, 'concurso', 'data'][idx]
if (prop) acc[prop] = item
console.log('acc', acc)
return acc
}, {}))
concurso.concurso = Number.parseInt(concurso.concurso)
Object.assign(dados, concurso)
/// ## NUMEROS sorteados
const numeros = await page.evaluate(() => Array.from(document
.querySelectorAll('tr.ng-scope > td'))
.map(dom => Number.parseInt(dom.innerText))
)
dados.numeros = numeros
/// ## ARRECADACAO total
const arrecadacaoTotal = await page.evaluate(() => document
.querySelector('div.related-box:nth-child(1) > p:nth-child(9) > strong:nth-child(1)')
.innerText.trim().split(' ')[1].replace(/[.]/g, '').replace(',', '.')
)
dados.arrecadacaoTotal = Number.parseFloat(arrecadacaoTotal)
/// ## PREMIACAO
const premiacao = await page.evaluate(() => Array.from(document
.querySelectorAll('div.related-box:nth-child(1) > p.description.ng-binding'))
.map(i => i.innerText.trim().split(/\t|\n/).filter(txt => txt.trim()).join(', '))
.reduce((acc, item) => {
const values = item.split(/ acertos, | aposta ganhadora, R\$ | apostas ganhadoras, R\$ /)
acc.push({
acertos: Number.parseInt(values[0]),
quantidadeApostaGanhadora: Number.parseInt(values[1]),
valor: Number.parseFloat(values[2].replace(/[.]/g, '').replace(',', '.'))
})
return acc
}, [])
)
dados.premiacao = premiacao
/// ## DETALHAMENTO
const detalhamento = await page.evaluate(() => Array.from(document
.querySelectorAll('p.ng-scope'))
.map(dom => Array.from(dom
.querySelectorAll('span:not(.ng-hide):not(.ng-scope):not(.ng-binding)'))
.map(txt => txt.innerText.trim())
).reduce((acc, item) => {
const localizacao = item[0].split(' - ')
acc.push({
localizacao: {
cidade: localizacao[0],
uf: localizacao[1]
},
descricao: item[1]
})
return acc
}, [])
)
dados.detalhamento = detalhamento
/// ## LOCAL do sorteio
const localSorteio = await page.evaluate(() => document
.querySelector('.resultado-loteria > p:nth-child(2)').innerText
.split(/Sorteio realizado n[oa] | em |, /).filter(txt => txt.trim())
.reduce((acc, item, idx) => {
acc[['descricao', 'cidade', 'uf'][idx]] = item
return acc
}, {})
)
dados.localSorteio = localSorteio
console.log('')
console.log('### DADOS ')
console.log(dados)
console.log('')
console.log('### JSON ')
console.log(JSON.stringify(dados, null, 2))
console.log('')
await page.screenshot({ path: 'lotofacil.png', fullPage: true })
await browser.close()
})()