-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
165 lines (140 loc) · 6.12 KB
/
index.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
const puppeteer = require("puppeteer");
const imageFolder = './'
let screenShotCount = 0;
// takes array of selectors and return the selector which matched first
const checkAnySelector = async (page, selectors) => {
const jsHandle = await page.waitForFunction((selectors) => {
for (const selector of selectors) {
if (document.querySelector(selector) !== null) {
return selector;
}
}
return false;
}, { visible: true, timeout: 10000 }, selectors);
const selector = await jsHandle.jsonValue();
return selector;
}
async function clickLinkOrButton(frame, selector) {
await frame.waitForSelector(selector, { visible: true, timeout: 20000 })
await frame.hover(selector)
const button = await frame.$(selector);
await button.click();
}
async function enterText(page, selector, textToEnter) {
await page.waitForSelector(selector, { visible: true, timeout: 10000 })
const textBox = await page.$(selector);
await page.hover(selector)
await textBox.focus();
//await page.evaluate((val, selector) => document.querySelector(selector).value = val, textToEnter, selector);
await page.keyboard.type(textToEnter);
}
const checkForEach = async (browser, agegroup, industry, county, zip, final_address) => {
let page = await browser.newPage();
await page.setDefaultNavigationTimeout(20000);
await page.setUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36');
await page.setViewport({ width: 1366, height: 1024 });
let shouldProceed = true;
try {
page.on('response', async (response) => {
// check if eligible
if (response.url() == "https://api.myturn.ca.gov/public/eligibility" && response.request().method() == "POST") {
const locationResp = await response.json();
if (!locationResp.eligible) {
console.log('Not eligible for ' + industry + ' ' + county);
shouldProceed = false;
}
} else if (response.url().indexOf('availability') > -1 && response.url().indexOf('https://api.myturn.ca.gov/public/locations') > -1 && response.request().method() == "POST") {
// checking if slot available
const slots = await response.json();
if (slots.availability && slots.availability.length > 0) {
const availableSlots = slots.availability.filter(slot => slot.available);
if (availableSlots.length > 0) {
console.log('Slots found for ' + industry + ' ' + county)
} else {
console.log('No slots found for ' + industry + ' ' + county)
}
} else {
console.log('No slots found for ' + industry + ' ' + county)
}
}
});
await page.goto('https://myturn.ca.gov/', { waitUntil: 'networkidle2' });
// first page
await clickLinkOrButton(page, 'button[data-testid="landing-page-continue"]');
// industry, county, zip, final_address
// second page
await clickLinkOrButton(page, 'input[name="q-screening-18-yr-of-age"]');
await clickLinkOrButton(page, 'input[name="q-screening-health-data"]');
await clickLinkOrButton(page, 'input[name="q-screening-privacy-statement"]');
await clickLinkOrButton(page, 'input[value="' + agegroup +'"]');
await page.select("select#q-screening-eligibility-industry", industry);
await page.select("select#q-screening-eligibility-county", county);
await clickLinkOrButton(page, 'button[data-testid="continue-button"]');
await page.waitForTimeout(1000);
if (shouldProceed) {
// third page
await enterText(page, '#location-search-input', zip);
await page.waitForTimeout(1000);
const addressSelector = await page.$x('//span[contains(text(), "' + final_address + '")]');
await page.waitForTimeout(500);
await addressSelector[0].click();
const nextBtnSelector = 'button[data-testid="location-select-location-continue"';
const noLocationSelector = 'div[class="tw-pb-8"] > p'
let whichPage = await checkAnySelector(page, [nextBtnSelector, noLocationSelector]);
if (whichPage == nextBtnSelector) {
// if yes
await page.waitForSelector(nextBtnSelector, { visible: true, timeout: 10000 })
let allLocationButtons = await page.$$(nextBtnSelector);
for (let i=0; i<allLocationButtons.length; i++) {
await allLocationButtons[i].click();
await page.waitForTimeout(1000); // wait for api call to finish for slots
await clickLinkOrButton(page, 'button[data-testid="back-button"]');
allLocationButtons = await page.$$(nextBtnSelector);
}
for (const currentLocationButton of allLocationButtons) {
await currentLocationButton.click();
await page.waitForTimeout(1000); // wait for api call to finish for slots
await clickLinkOrButton(page, 'button[data-testid="back-button"]');
}
} else {
// check if any location listed
// if no then return
console.log('No location found for ' + industry + ' ' + county);
}
}
} catch (err) {
console.log("Error getting vaccine eligibility");
await page.screenshot({ path: imageFolder + '/error.png' });
console.log(err);
}
page.close();
}
const actionFunc = async (toCheck) => {
let browser = null;
try {
browser = await puppeteer.launch({
headless: true,
userDataDir: 'myturn',
excludeSwitches: 'enable-automation',
ignoreDefaultArgs: ["--enable-automation"],
args: ['--no-sandbox', '--disable-setuid-sandbox'],
slowMo: 10,
});
for (const entry of toCheck) {
await checkForEach(browser, ...entry);
};
} catch (error) {
// error handling
//console.log("Error getting vaccine eligibility");
console.log(error);
}
browser.close();
};
(async () => {
const toCheck = [
['16 - 49', 'Healthcare Worker', 'San Luis Obispo', '93433', 'Grover Beach, CA 93433, USA'],
['16 - 49', 'Healthcare Worker', 'Santa Clara', '94043', 'Mountain View, CA 94043, USA'],
['16 - 49', 'Communications and IT', 'Santa Clara', '94043', 'Mountain View, CA 94043, USA'],
];
await actionFunc(toCheck);
})();