This repository has been archived by the owner on Jan 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
scraper.js
302 lines (290 loc) · 10.9 KB
/
scraper.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/usr/bin/env node
const fs = require("fs");
const path = require('path');
const puppeteer = require('puppeteer');
const nyGovSites = [
{
name: "NYS DOH Hospital / FQHC Scheduling COVID-19 Sutphin Health Center",
link: "https://apps3.health.ny.gov/doh2/applinks/cdmspr/2/counties?OpID=50501251",
address: {
name: "Sutphin Health Center",
street: "105-04 Sutphin Boulevard",
city: "Queens",
state: "NY",
zip: "11435"
}
},
{
name: "Northwell CoVID-19 Vaccination Program: Nassau County - Lake Success",
link: "https://apps3.health.ny.gov/doh2/applinks/cdmspr/2/counties?OpID=50500075",
address: {
name: "Lake Success/New Hyde Park",
street: "1111 Marcus Avenue",
city: "New Hyde Park",
state: "NY",
zip: "11042"
}
},
{
name: "NYS DOH Hospital/FQHC Appointment Scheduling COVID-19 at Nassau University Medical Center",
link: "https://apps3.health.ny.gov/doh2/applinks/cdmspr/2/counties?OpID=50500121",
address: {
name: "Nassau University Medical Center",
street: "2201 Hempstead Turnpike",
city: "East Meadow",
state: "NY",
zip: "11554"
}
},
{
name: "Northwell CoVID-19 Vaccination Program: Nassau County - Elmont",
link: "https://apps3.health.ny.gov/doh2/applinks/cdmspr/2/counties?OpID=50501190",
address: {
name: "NYRA/Belmont",
street: "2150 Hempstead Turnpike",
city: "Elmont",
state: "NY",
zip: "11053"
}
},
{
name: "FIRST DOSE - NYS DOH Hospital/FQHC Appointment Scheduling COVID-19 at Mount Sinai South Nassau",
link: "https://apps3.health.ny.gov/doh2/applinks/cdmspr/2/counties?OpID=50500074",
address: {
name: "Mount Sinai South Nassau",
street: "One Healthy Way",
city: "Oceanside",
state: "NY",
zip: "11598"
},
},
{
name: "NYS COVID Vaccine POD - JAVax - Manhattan, N.Y.",
link: "https://apps3.health.ny.gov/doh2/applinks/cdmspr/2/counties?OpID=50502320",
address: {
name: "JAVax Manhattan, N.Y.",
street: "429 11th Avenue",
city: "New York",
state: "NY",
zip: "10018"
}
},
{
name: "Northwell CoVID-19 Vaccination Program: Manhattan - Lenox Hill Hospital",
link: "https://apps3.health.ny.gov/doh2/applinks/cdmspr/2/counties?OpID=50501473",
address: {
name: "LHH Einhorn Auditorium",
street: "131 East 76th Street",
city: "New York",
state: "NY",
zip: "10021"
}
},
{
name: "NC Dept. of Health at Nassau Community College CCB",
link: "https://apps3.health.ny.gov/doh2/applinks/cdmspr/2/counties?OpID=50500881",
address: {
name: "Nassau Community College",
street: "1 Education Drive",
city: "Garden City",
state: "NY",
zip: "11530"
}
},
{
name: "NCDOH at Yes We Can Community Center",
link: "https://apps3.health.ny.gov/doh2/applinks/cdmspr/2/counties?OpID=50501806",
address: {
name: "Yes We Can Community Center",
street: "141 Garden Street",
city: "Westbury",
state: "NY",
zip: "11590"
}
},
{
name: "NYS COVID Vaccine POD - Jones Beach State Park - Field 3",
link: "https://apps3.health.ny.gov/doh2/applinks/cdmspr/2/counties?OpID=50502584",
address: {
name: "Jones Beach State Park",
street: "1901 Ocean Parkway",
city: "Wantagh",
state: "NY",
zip: "11793"
}
},
{
name: "NYS COVID Vaccine POD - Westchester County Center",
link: "https://apps3.health.ny.gov/doh2/applinks/cdmspr/2/counties?OpID=50502420",
address: {
name: "Westchester County Center",
street: "198 Central Avenue",
city: "White Plains",
state: "NY",
zip: "10607"
}
},
{
name: "NYS COVID Vaccine POD - Aqueduct Racetrack",
link: "https://apps3.health.ny.gov/doh2/applinks/cdmspr/2/counties?OpID=50503467",
address: {
name: "Aqueduct Racetrack - Racing Hall",
street: "110-00 Rockaway Blvd",
city: "Jamaica",
state: "NY",
zip: "11420"
}
},
{
name: "Northwell Health-GoHealth Urgent Care | Forest Hills",
link: "https://www.clockwisemd.com/hospitals/803/appointments/schedule_visit",
address: {
name: "Northwell Health-GoHealth Urgent Care",
street: "102-29 Queens Blvd",
city: "Forest Hills",
state: "NY",
zip: "11375"
}
},
{
name: "Northwell Health-GoHealth Urgent Care | Syosset",
link: "https://www.clockwisemd.com/hospitals/781/appointments/schedule_visit",
address: {
name: "Northwell Health-GoHealth Urgent Care",
street: "103 Jackson Ave",
city: "Syossett",
state: "NY",
zip: "11791"
}
},
{
name: "Northwell Health-GoHealth Urgent Care | Hewlett",
link: "https://www.clockwisemd.com/hospitals/3255/appointments/schedule_visit",
address: {
name: "Northwell Health-GoHealth Urgent Care",
street: "1332 Peninsula Blvd",
city: "Hewlett",
state: "NY",
zip: "11557"
}
},
{
name: "Northwell Health-GoHealth Urgent Care | Riverhead",
link: "https://www.clockwisemd.com/hospitals/1866/appointments/schedule_visit",
address: {
name: "Northwell Health-GoHealth Urgent Care",
street: "1842 Old Country Road",
city: "Riverhead",
state: "NY",
zip: "11901"
}
}
];
(async () => {
const browser = await puppeteer.launch();
const promises = [];
for (const site of nyGovSites) {
promises.push((async () => {
site.events = [];
const page = await browser.newPage();
await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36 Edg/87.0.664.75');
await page.exposeFunction("getApproxAppointmentCount", getApproxAppointmentCount);
// let one retry and use default timeouts of 30s
for (let i = 0; i < 2; i++) {
try {
await page.goto(site.link);
await waitForPageLoad(page);
const isSuccess = await page.$(getSucccessSelectorForSite(site.link));
if (isSuccess) {
site.events = await page.evaluate(getEventsForSite(site.link));
await page.close();
break; // if succesful, don't retry
}
} catch (err) {
console.error(`try: [${i}] ${site.link} ${err}`);
}
}
})())
}
await Promise.all(promises);
await browser.close();
const data = JSON.stringify({ sites: nyGovSites }, null, 2);
const filename = path.join("data", "ny_state.json");
fs.writeFileSync(path.resolve(filename), data);
})();
async function waitForPageLoad(page) {
if (page.url().startsWith("https://apps3.health.ny.gov/doh2/applinks/cdmspr/2/counties")) {
return await page.waitForSelector("#pagetitle, h1, #notfound");
}
else if (page.url().startsWith("https://www.clockwisemd.com/hospitals/")) {
// https://github.com/puppeteer/puppeteer/issues/4356
// https://stackoverflow.com/a/61917280
return await Promise.race([
page.waitForSelector("#reason-and-provider-container", { visible: true }).catch(),
page.waitForSelector(".time-selector", { visible: true }).catch(),
]);
}
}
function getSucccessSelectorForSite(siteLink) {
if (siteLink.startsWith("https://apps3.health.ny.gov/doh2/applinks/cdmspr/2/counties")) {
return "#pagetitle";
}
else if (siteLink.startsWith("https://www.clockwisemd.com/hospitals/")) {
return "#available-times-table div";
}
}
function getEventsForSite(siteLink) {
if (siteLink.startsWith("https://apps3.health.ny.gov/doh2/applinks/cdmspr/2/counties")) {
return getEventsForNYGovSites;
}
else if (siteLink.startsWith("https://www.clockwisemd.com/hospitals/")) {
return getEventsForNorthwell;
}
}
function getApproxAppointmentCount(appointmentCount) {
const actualAppointmentCount = parseInt(appointmentCount);
const buckets = [0, 5, 10, 25, 50, 100, 150, 200, 250]
for (let i = 1; i < buckets.length; i++) {
if (actualAppointmentCount < buckets[i]) {
return buckets[i - 1];
}
}
return buckets[buckets.length - 1];
}
async function getEventsForNYGovSites() {
const events = [];
const eventsWeb = $(".event-type");
for (const event of eventsWeb) {
const isAvailable = $(event).find("button").text() !== "Event Full";
var approxAppointmentCount = await window.getApproxAppointmentCount($(event).find("div div:contains('Appointments Available:'):last").first().text().substring(24));
if (isAvailable && approxAppointmentCount > 0) {
events.push({
date: new Date($(event).find("div div:contains('Date:'):last").first().text().substring(6) + " UTC").toISOString(),
time: $(event).find("div div:contains('Time:'):last").first().text().substring(6),
appointments: approxAppointmentCount,
linkId: $(event).parent().attr("id")
});
}
}
return events;
}
async function getEventsForNorthwell(){
console.log("in northwell")
const events = [];
for(let i = 0; i < 5; i++) {
const eventsWeb = $(".times-column");
for (const event of eventsWeb) {
const isAvailable = $(event).find(".clockwise-time").length > 0;
var approxAppointmentCount = await window.getApproxAppointmentCount($(event).find(".clockwise-time").length);
if (isAvailable && approxAppointmentCount > 0) {
events.push({
date: new Date($(event).find(".clockwise-time").first().data("value").substring.substring(0, 10)).toISOString(),
time: $(event).find(".clockwise-time").first().text() + " - " + $(event).find(".clockwise-time").last().text(),
appointments: approxAppointmentCount
});
}
}
$("#later-dates").click(); // week by week
}
return events;
}