-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
182 lines (155 loc) · 6.16 KB
/
app.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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
const request = require('request');
const jsdom = require('jsdom');
const { JSDOM } = jsdom;
const argv = require('minimist')(process.argv.slice(2));
const fs = require('fs');
const EventEmitter = require('events');
const mEmitter = new EventEmitter();
const csv = require('csv');
const { promisify } = require('util');
const moment = require('moment');
const { exec } = require('child_process');
const csvParse = promisify(csv.parse);
const csvStringify = promisify(csv.stringify);
const requestPost = promisify(request.post);
// check that we have a username and password
if(!argv.u || !argv.p) {
console.log('Error: missing username or password');
process.exit(0);
}
// check that we have a valid csv file
if(!argv.f || !fs.existsSync(argv.f)) {
console.log('Error: missing source file; check the file path');
process.exit(0);
}
// if all is well, load the file
const rawCsv = fs.readFileSync(argv.f);
console.log('Successfully loaded CSV file');
// somewhere to store the session cookie
let sessionCookie = '';
// login to the FIV portal and fetch a session cookie
requestPost({
url: 'https://www.npcloud.it/fiv/main.aspx?WCI=F_Login&WCE=Login&WCU=01',
form: {
txtUTE: argv.u,
txtPWD: argv.p
}
}).then(res => {
// check if login was successful
if(res.body.includes('PASSWORD NON VALIDA')) {
console.log('Error: login failed; check your username and password');
process.exit(0);
}
// all was well
else {
console.log('Login successful');
// store session cookie in variable
sessionCookie = res.headers['set-cookie'];
// emit event saying we have a session cookie we can use
mEmitter.emit('sessionCookieStored');
}
}).catch(err => {
// usually network errors?
console.log('Critical error; exiting...');
process.exit(1);
});
const getPersonDataPromises = [];
const onSessionCookieStored = async () => {
try {
// parse contents of csv data
const entries = await csvParse(rawCsv);
// get the length of the list of entries so that, on the last entry, we can write the list back to a csv
const numberOfEntries = entries.length;
// array that will contain the final list of entries with the additional data from the FIV portal
const entriesFinal = [];
console.log('Retrieving information from the FIV portal...');
// recursively check each entry
entries.forEach(async (entry, index) => {
getPersonDataPromises.push(getPersonData(entry[0], entry[1], entry[2], index));
// on the last entry...
if(index === numberOfEntries - 1) {
// wait until all promises are resolved and then combine entries and results
const results = await Promise.all(getPersonDataPromises);
combineEntriesAndResults(entries, results);
// use the entries array (which is now updated with the results data) and analyze data
analyzeData(entries);
// write data to new csv file
const headerString = 'firstName,lastName,membNo,dob,medCert,renewalDate,club,isCurrent,isMedCertValid';
const csvString = await csvStringify(entries);
fs.writeFileSync('./results.csv', `${headerString}\n${csvString}`);
// open file in excel
console.log('All done. Opening excel...');
exec('start excel ./results.csv');
}
});
}
catch(err) {
console.log(`Error: ${err.message}`);
process.exit(1);
}
}
mEmitter.on('sessionCookieStored', onSessionCookieStored);
async function getPersonData(firstName, lastName, membNo, index) {
const res = await requestPost({
url: 'https://www.npcloud.it/fiv/Main.aspx?WCI=F_Ricerca&WCE=Invia&WCU=01',
headers: {
'Cookie': sessionCookie
},
form: {
txtCOG: lastName,
txtNOM: firstName,
txtTESS: membNo
}
});
const dom = new JSDOM(res.body);
// if a table does not exist it means a match was not found
if(!dom.window.document.body.querySelector('tr.listlight > td')) {
const data = ['NOT FOUND'];
return {
index: index,
data: data
};
}
const data = [
// dom.window.document.body.querySelector('tr.listlight > td').textContent,
dom.window.document.body.querySelector('tr.listlight > td:nth-child(2)').textContent,
dom.window.document.body.querySelector('tr.listlight > td:nth-child(3)').textContent,
// dom.window.document.body.querySelector('tr.listlight > td:nth-child(4)').textContent,
dom.window.document.body.querySelector('tr.listlight > td:nth-child(5)').textContent,
dom.window.document.body.querySelector('tr.listlight > td:nth-child(6)').textContent
];
return {
index: index,
data: data
};
}
function combineEntriesAndResults(entries, results) {
results.forEach(result => {
result.data.forEach(data => {
entries[result.index].push(data);
});
});
}
function analyzeData(entries) {
// get date
const currentDate = moment();
const currentYear = parseInt(currentDate.format('YYYY'));
entries.forEach(entry => {
// if the array length is just 4 it means that data was not found so we can skip the checks
if (entry.length !== 4) {
// check renewal is current
const renewalYear = parseInt(entry[5]);
if(renewalYear < currentYear) { entry.push('NOT CURRENT'); }
else { entry.push('CURRENT'); }
// check if medical certificate is current
if(entry[4] === ' - ') { entry.push('NO MED CERT'); }
else {
// get the medical certificate expiry date
const medCertExpiryDateString = entry[4].split(' ').pop();
const medCertExpiryDate = moment(medCertExpiryDateString, 'DD/MM/YY');
if(medCertExpiryDate.isBefore(currentDate)) { entry.push('MED CERT EXPIRED'); }
else { entry.push('MED CERT VALID'); }
}
}
});
}