-
Notifications
You must be signed in to change notification settings - Fork 0
/
prize-checker.js
69 lines (57 loc) · 1.63 KB
/
prize-checker.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
const got = require('got');
const cheerio = require('cheerio')
const FormData = require('form-data');
function getCardInfo(cardNumber) {
return new Promise(resolve => {
const form = new FormData();
form.append('User', cardNumber);
form.append('Password', cardNumber.slice(-7));
got.post('https://hbprepagos.unicre.pt', {
body: form,
followRedirect: true,
retry: 0,
timeout: 5000,
headers: {'Host': 'hbprepagos.unicre.pt'},
hooks: {
afterResponse: [
response => {
resolve({ card: cardNumber, amount: 0, isValid: false });
return response;
}
],
beforeRedirect: [
(options, response) => {
got.get('https://hbprepagos.unicre.pt/area-cliente', {
headers: {cookie: response.headers['set-cookie']}
}).then(data => {
const $ = cheerio.load(data.body);
resolve({
card: cardNumber,
amount: parseFloat($('.valueLabel').last().text()),
isValid: true
});
});
}
]
}
}).catch(() => {});
});
}
async function getCardsInfo(cards) {
const requests = [];
cards.forEach(card => {
requests.push(getCardInfo(card));
});
const cardsInfo = await Promise.all(requests);
const info = cardsInfo.reduce((acc, element) => {
acc.cards.push(element);
acc.totalAmount += element.amount;
return acc;
}, { cards: [], totalAmount: 0 });
info.totalAmount = parseFloat(info.totalAmount.toFixed(2));
return info;
}
module.exports = {
getCardInfo,
getCardsInfo
}