-
Notifications
You must be signed in to change notification settings - Fork 0
/
CovidResults.js
217 lines (182 loc) · 7.98 KB
/
CovidResults.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
'use strict';
const EventEmitter = require('events');
const puppeteer = require('puppeteer');
const { DateTime } = require("luxon");
const covidResultEvent = new EventEmitter();
function theHumanDelay(t1 = 77, t2 = 47) {
return Math.round(Math.random()*t1) + t2;
}
class CovidResults {
constructor(args = null) {
this.browser = {};
this.page = {};
}
async checkResults(patients) {
this.browser = await puppeteer.launch({
// headless: false
});
this.page = await this.browser.newPage();
const page = this.page;
for(const patient of patients) {
await page.waitForTimeout(theHumanDelay());
let results = { result: false };
try {
switch(patient.facility) {
case "NYGH":
results = await this.nygh(patient);
break;
case "OHIP":
results = await this.ohip(patient);
break;
default:
throw 'error: no function matching ' + patient.facility;
}
} catch(err) {
covidResultEvent.emit('error', patient, err);
continue;
}
if(results.result) {
covidResultEvent.emit('results', patient, results.data);
} else {
covidResultEvent.emit('noResults', patient, results.data);
}
}
this.browser.close();
}
// check North York General Hospital website
async nygh(patient) {
const page = this.page;
await page.goto('https://connect.nygh.on.ca/covid/SitePages/covid.aspx');
await page.waitForSelector('#fname', {
visible: true,
});
await page.waitForTimeout(theHumanDelay());
await page.type('input#fname', patient.fname);
await page.type('input#lname', patient.lname);
await page.type('input#pcode', patient.pcode.substring(0,3));
await page.type('input#pcode1', patient.pcode.substring(3,6));
await page.type('input#mrn', patient.mrn);
await page.click('input#button');
await page.waitForSelector('#info > table', {
visible: true,
});
const tableData = await page.evaluate(() => {
const tdArray = Array.from(document.querySelectorAll('#info > table tr td'));
return tdArray.map(td => td.innerText);
});
if(tableData[6] == 'In Progress') {
return {
result: false,
data: null,
};
} else {
const tableHTML = await page.evaluate(() => document.querySelector('#info').innerHTML);
return {
result: true,
data: tableHTML,
};
}
}
// check Ontario Ministry Of Health website
async ohip(patient) {
const page = this.page;
await page.goto('https://covid19results.ehealthontario.ca:4443/agree');
await page.waitForSelector('label[for="id-type-ohc"]');
await page.click('label[for="id-type-ohc"]');
await page.waitForTimeout(theHumanDelay());
await page.waitForSelector('#btn_step2_ohc_known');
await page.click('#btn_step2_ohc_known');
await page.waitForSelector('label[for="acceptedTerm1"]')
await page.click('label[for="acceptedTerm1"]');
await page.waitForSelector('button#button_submit:not([disabled])');
await page.waitForTimeout(theHumanDelay());
await page.click('button#button_submit');
await page.waitForSelector('input#hcn');
await page.waitForTimeout(theHumanDelay());
await page.focus('input#hcn');
await page.type('input#hcn', patient.hcn, {delay: theHumanDelay()});
await page.waitForTimeout(theHumanDelay());
await page.type('input#vCode', patient.vc, {delay: theHumanDelay()});
await page.waitForTimeout(theHumanDelay());
await page.focus('input#scn');
await page.type('input#scn', patient.scn, {delay: theHumanDelay()});
await page.waitForTimeout(theHumanDelay());
await page.focus('input#fname');
await page.type('input#fname', patient.fname, {delay: theHumanDelay()});
await page.keyboard.press("Tab");
await page.waitForTimeout(theHumanDelay());
await page.type('input#lname', patient.lname, {delay: theHumanDelay()});
let dt = DateTime.fromISO(patient.dob);
await page.waitForTimeout(theHumanDelay());
await page.focus('input#dob-igc-yyyy');
await page.type('input#dob-igc-yyyy', dt.toFormat('y'), {delay: theHumanDelay()});
await page.keyboard.press("Tab");
await page.waitForTimeout(theHumanDelay());
await page.type('input#dob-igc-mm', dt.toFormat('LL'), {delay: theHumanDelay()});
await page.keyboard.press("Tab");
await page.waitForTimeout(theHumanDelay());
await page.type('input#dob-igc-dd', dt.toFormat('dd'), {delay: theHumanDelay()});
await page.waitForTimeout(theHumanDelay());
switch(patient.sex) {
case "M":
await page.hover('label[for="gender-male"]');
await page.click('label[for="gender-male"]');
break;
case "F":
await page.hover('label[for="gender-female"]');
await page.click('label[for="gender-female"]');
break;
case "O":
await page.hover('label[for="gender-other"]');
await page.click('label[for="gender-other"]');
break;
}
await page.waitForTimeout(theHumanDelay());
await page.type('input#pCode', `${patient.pcode.substring(0,3)} ${patient.pcode.substring(3,6)}`, {delay: theHumanDelay()});
await page.waitForTimeout(theHumanDelay(750,250));
await page.waitForSelector('button#button_verify:not([disabled])');
await page.click('button#button_verify');
await page.waitForNavigation();
let y = 0;
login: while(y < 9000) {
if(await page.$('section.result-section')) {
let x = 0;
results: while(x < 12000) {
if(await page.$('#result_no_results_text')) {
return {
result: false,
data: null,
};
} else if (
await page.$('#result_negative_title_card') ||
await page.$('#result_positive_detected_title_card') ||
await page.$('#result_withdrawn_title_card') ||
await page.$('#result_unavailable_title_card') ||
await page.$('#result_result_unavailable_title_invalid')
) {
await page.waitForSelector('section.result-section ~ div:nth-of-type(2)');
let resultsHTML = await page.$eval('section.result-section ~ div:nth-of-type(2)', (element) => {
return element.innerHTML;
});
return {
result: true,
data: resultsHTML,
};
}
await page.waitForTimeout(50);
x++;
}
break login;
} else if(await page.$('#error_msg_div')) {
let loginError = await page.$eval('#error_msg_div', (element) => {
return element.innerText;
});
throw `login error: ${loginError}`;
}
await page.waitForTimeout(50);
y++;
}
throw 'something went wrong wait for results vs no results sections';
}
}
module.exports = { CovidResults, covidResultEvent };