-
Notifications
You must be signed in to change notification settings - Fork 0
/
pageScraper.js
335 lines (293 loc) · 11.4 KB
/
pageScraper.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
const player = require("play-sound")();
const fs = require("fs");
const Twitter = require("twitter-lite");
const config = require("./config");
const zips = require("./json/zips.json");
const stateSelectors = require("./json/stateSelectors.json");
const { GoogleSpreadsheet } = require("google-spreadsheet");
const sensitiveData = require("./env-data.js");
const doc = new GoogleSpreadsheet("10SnHpQwaspZ8XWpsq1cEThYvKfCQjoD1GV_AAiImOKM");
const client = new Twitter(config);
// inputs
const age = "18";
const startingI = 20;
const currentState = "Alabama";
const stateSelector = stateSelectors.find((x) => x.state === currentState)
.selector;
const zipcodes = zips.filter(function (e) {
return e.state === currentState;
});
console.log(zipcodes);
const scraperObject = {
url:
"https://www.cvs.com/immunizations/covid-19-vaccine?icid=cvs-home-hero1-link2-coronavirus-vaccine",
async scraper(browser) {
await doc.useServiceAccountAuth(require("./creds-from-google.json"));
await doc.loadInfo();
const sheet = doc.sheetsByIndex[0];
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive
}
function wait() {
return getRandomInt(1000, 2500);
}
function newPageWait() {
return getRandomInt(4000, 6000);
}
function timeoutWait() {
return getRandomInt(3000, 5000);
}
function typeDelay() {
return getRandomInt(175, 400);
}
let page = (await browser.pages())[0];
await page.setExtraHTTPHeaders({ "Accept-Language": "en-US,en;q=0.9" });
console.log(`Navigating to ${this.url}...`);
await page.goto(this.url);
await page.waitForTimeout(timeoutWait());
async function checkForSelector(selector, errorMessage) {
try {
await page.waitForSelector(selector, { timeout: timeoutWait() });
await clickButton(selector, "selector " + selector + " found");
} catch (error) {
console.log(errorMessage);
}
}
async function typeText(selector, input, id) {
try {
await page.waitForTimeout(wait());
await page.focus(selector);
await page.keyboard.type(input, { delay: typeDelay() });
await page.waitForTimeout(wait());
console.log(`${id} input typed in`);
} catch (err) {
console.log(`${id} input not found`, err);
player.play("./media/roadrunner.mp3", (err) => {
if (err) console.log(`Could not play sound: ${err}`);
});
}
}
async function clearText(selector) {
await page.waitForTimeout(wait());
await page.evaluate((selector) => {
document.querySelector(selector).value = "";
}, selector);
await page.waitForTimeout(wait());
}
async function clickButton(selector, id) {
await page.waitForTimeout(wait());
try {
console.log(`${id} button pressed`);
await page.click(selector);
} catch (err) {
console.error(`${id} button not found`, err);
player.play("./media/roadrunner.mp3", (err) => {
if (err) console.log(`Could not play sound: ${err}`);
});
}
await page.waitForTimeout(wait());
}
async function clickNextPage(selector, id) {
await page.waitForTimeout(wait());
try {
console.log(`${id} clicked, moved to next page`);
await page.click(selector);
} catch (err) {
console.error(`${id} button for new page not found`, err);
player.play("./media/roadrunner.mp3", (err) => {
if (err) console.log(`Could not play sound: ${err}`);
});
}
await page.waitForTimeout(newPageWait());
await checkForSelector("#acsFocusFirst", "Popup didn't appear.");
}
await clickNextPage(
"#acc_3 > div > div.content__wrapper.parsecondary > div > div.bodytext.aem-GridColumn--default--none.aem-GridColumn.aem-GridColumn--default--8.aem-GridColumn--offset--md--0.aem-GridColumn--offset--default--2.aem-GridColumn--md--none.aem-GridColumn--md--12 > div > div > div > div > div > div.textonly__text.body__copy > div > p:nth-child(2) > a",
"Get to past two weeks questionnaire"
);
await clickButton(
"#questionnaire > fieldset > section > div:nth-child(3) > fieldset > div.radio-btn-wrapper.answer-wrapper.flex-dir-col > div:nth-child(2)",
"Past two weeks questionnaire Q1"
);
await clickButton(
"#questionnaire > fieldset > section > div:nth-child(4) > fieldset > div.radio-btn-wrapper.answer-wrapper.flex-dir-col > div:nth-child(2)",
"Past two weeks questionnaire Q1"
);
await clickButton(
"#questionnaire > fieldset > section > div:nth-child(5) > fieldset > div.radio-btn-wrapper.answer-wrapper.flex-dir-col > div:nth-child(2)",
"Past two weeks questionnaire Q3"
);
await clickNextPage(
"#content > div.footer-content-wrapper > button",
"first continue button"
);
await clickButton(
"#generic > section > div.form-group > div > div > div:nth-child(1)",
"start vaccination"
);
await clickNextPage(
"#content > div.footer-content-wrapper > button",
"continue scheduling button"
);
await page.waitForTimeout(wait());
await page.waitForTimeout(wait());
await page.select("#jurisdiction", stateSelector);
await page.waitForTimeout(wait());
await clickNextPage(
"#content > div.footer-content-wrapper > button",
"click next button"
);
await typeText("#q1_0", age, "enter age");
await clickButton(
"#generic > fieldset > section > div:nth-child(5) > div > div > div:nth-child(2)",
"click 16 and above with conditions"
);
await clickButton(
"#generic > fieldset > section > div:nth-child(6)",
"click affirm"
);
await clickNextPage(
"#content > div.footer-content-wrapper > button",
"to next page"
);
await clickNextPage(
"#content > div.footer-content-wrapper > button",
"start scheduling"
);
let tweetedAddressObject = {};
for (let i = startingI; i < zipcodes.length; i++) {
console.log(`iteration number ${i}`);
let currentZipcode = zipcodes[i]["zip"].toString();
let consoleMessage = `at ${currentZipcode}`;
await typeText("#address", currentZipcode, consoleMessage);
await page.waitForTimeout(wait());
await page.keyboard.press("Enter");
// await clickNextPage(
// "#generic > div > div > div.flex-container > button",
// "searching for appointments"
// );
await page.waitForTimeout(wait());
let availableAppoinments = await page.evaluate(() => {
let el = document.querySelector("#availableDate");
return el ? true : false;
});
if (availableAppoinments) {
console.log("Appointments are available!");
async function checkForMoreAppointments() {
await page.waitForTimeout(wait());
let loadNextSelector = await page.evaluate(() => {
let el = document.querySelector(
"#content > div.main-content-wrapper > cvs-store-locator > div > div > div.see-more-locations > button"
);
return el ? true : false;
});
if (!loadNextSelector) {
return;
} else {
await page.waitForTimeout(wait());
await clickButton(
"#content > div.main-content-wrapper > cvs-store-locator > div > div > div.see-more-locations > button",
"check for more locations"
);
return checkForMoreAppointments();
}
}
await checkForMoreAppointments();
let loadNextSelector = await page.evaluate(() => {
let el = document.querySelector(
"#content > div.main-content-wrapper > cvs-store-locator > div > div > div.see-more-locations > button"
);
return el ? true : false;
});
if (loadNextSelector) {
await clickButton(loadNextSelector, "check for more locations");
}
let result = await page.evaluate(() => {
let resultArray = [];
let addresses = document.querySelectorAll(
"#clinic-address-detail > address"
);
for (let address of addresses) {
let object = {};
let pharmacyName = address.children[0].innerText;
let pharmacyAddress = address.children[1].innerText;
let availableDate = address.children[2].innerText;
let vaccine = address.children[3].innerText
.replace(/Vaccine:/, "")
.trim();
object = {
pharmacyName: pharmacyName,
pharmacyAddress: pharmacyAddress,
availableDate: availableDate,
vaccine: vaccine,
};
resultArray.push(object);
}
return resultArray;
});
console.log(result);
for (let j = 0; j < result.length; j++) {
fs.appendFile(
"./logs.txt",
JSON.stringify(result[j]) + "\n",
"utf-8",
function (err) {
if (err) throw err;
}
);
let vaccineName = result[j].vaccine.substr(
0,
result[j].vaccine.indexOf(" ")
);
let resultingAreaName = result[j].pharmacyAddress
.substr(0, result[j].pharmacyAddress.indexOf(","))
.replace(" ", "")
.toLowerCase();
let pharmacyName = result[j].pharmacyName;
let cityName = zipcodes[i]["city"].replace(" ", "");
let tweetContent = `${result[j].vaccine} vaccine available at CVS Pharmacy ${pharmacyName} ${result[j].pharmacyAddress} on ${result[j].availableDate}. Schedule here: https://www.cvs.com/vaccine/intake/store/cvd-schedule?icid=cvd_bstr_only #${vaccineName} #${cityName} #${resultingAreaName} #${currentState} #Covid #Vaccine #Appointments #vaccinebot`;
let googleObject = {
pharmacyName: pharmacyName,
pharmacyAddress: result[j].pharmacyAddress,
availableDate: result[j].availableDate,
vaccine: vaccineName,
zipcodeSearched: currentZipcode,
city: zipcodes[i]["city"],
state: currentState,
dateSearched: Date.now(),
};
await sheet.addRow(googleObject);
if (!tweetedAddressObject.hasOwnProperty(pharmacyName)) {
tweetedAddressObject[pharmacyName] = true;
client
.post("statuses/update", { status: tweetContent })
.then((result) => {
console.log(
'You successfully tweeted this : "' + result.text + '"'
);
player.play("./media/success.wav", (err) => {
if (err) console.log(`Could not play sound: ${err}`);
});
})
.catch(console.error);
await page.waitForTimeout(wait());
} else {
console.log("Already tweeted about this CVS");
}
}
await clearText("#address");
} else {
await page.waitForTimeout(wait());
console.log("No appointments available");
await clearText("#address");
}
}
//Program successfully completed
// await browser.close();
console.log("Program completed!");
tweetedAddressObject = {};
},
};
module.exports = scraperObject;