-
Notifications
You must be signed in to change notification settings - Fork 1
/
check.js
64 lines (51 loc) · 1.83 KB
/
check.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
const cheerio = require('cheerio'); // Basically jQuery for node.js
const rp = require('request-promise-native');
const fieldPrefix = '_historiapojazduportlet_WAR_historiapojazduportlet_';
const daysInMonth = (month, year) => new Date(year, month, 0).getDate();
const getFormUrl = $form => $form.attr('action');
const getFormData = ($form, plate, vin, date) => {
const data = $form.serializeArray().reduce((data, field) => {
data[field.name] = field.value;
return data;
}, {});
data[`${fieldPrefix}:rej`] = plate;
data[`${fieldPrefix}:vin`] = vin;
data[`${fieldPrefix}:data`] = date;
data[`${fieldPrefix}:btnSprawdz`] = 'Sprawdź pojazd »';
return data;
};
const zeroPad = number => ('0' + number).slice(-2);
const generateDates = year => {
const dates = [];
for (let month = 1; month <= 12; month++) {
for (let day = 1; day <= daysInMonth(month, year); day++) {
dates.push(`${zeroPad(day)}.${zeroPad(month)}.${year}`);
}
}
return dates;
};
module.exports = async function check(plate, vin, year) {
const $ = await rp({
uri: 'https://historiapojazdu.gov.pl/strona-glowna',
transform: body => cheerio.load(body),
strictSSL: false,
jar: true,
});
const $form = $(`#${fieldPrefix}\\:formularz`);
const uri = getFormUrl($form);
const dates = generateDates(year);
for (const date of dates) {
console.log(`Checking database for ${date}...`);
const response = await rp({
uri,
strictSSL: false,
jar: true,
method: 'POST',
form: getFormData($form, plate, vin, date),
});
if (response.includes('raport-main-information')) {
console.log(`Valid first registration date found: ${date}`);
return;
}
}
};