-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheb.js
163 lines (140 loc) · 4.81 KB
/
heb.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
const axios = require('axios').default
const geolib = require('geolib')
const playwright = require('playwright')
const { sleep, genUserAgent, noop, randomNumber } = require('./utils')
const METERS_TO_MILES = 1609.34
const HEB_GET_SLOTS = 'https://heb-ecom-covid-vaccine.hebdigital-prd.com/vaccine_locations.json'
/**
* @typedef {'Pfizer' | 'Moderna' | 'Janssen'} VaxTypes
*
* @typedef {object} slotDetails
* @property {number} openTimeslots
* @property {number} openAppointmentSlots
* @property {string} manufacturer
*
* @typedef {object} Location
* @property {string} zip
* @property {string} type
* @property {string} street
* @property {number} storeNumber
* @property {string} state
* @property {slotDetails[]} slotDetails
* @property {number} openTimeslots
* @property {number} openAppointmentSlots
* @property {string} name
* @property {number} [longitude]
* @property {number} [latitude]
* @property {string} city
*
* @typedef {Location & { distance: number }} AppointmentLocation
*
* @typedef {object} Data
* @property {Location[]} locations
*/
/**
* Check vaccines and once you find one in your area, try to grab it
*
* @param {object} props
* @param {number | string} props.homeLatitude
* @param {number | string} props.homeLongitude
* @param {number} props.maxDistance distance in miles
* @param {VaxTypes[]} props.types
*
* @returns {Promise<boolean>}
*/
module.exports = async function heb({
homeLatitude = 30.267153,
homeLongitude = -97.743057,
maxDistance = 25,
types = ['Pfizer', 'Moderna', 'Janssen'],
} = {}) {
console.log(`Searching for ${types.join(', ')} appointments within ${maxDistance} miles of (${homeLatitude}, ${homeLongitude})`)
const result = await axios.get(HEB_GET_SLOTS, {
headers: { 'User-Agent': genUserAgent() },
}).catch(console.log)
/** @type {Data} */
const { locations } = result.data
if (result.status !== 200) {
console.log(result.statusText)
return false
}
/**
* Reduce locations to the ones that have appointments and meet the supplied criteria
* @param {AppointmentLocation[]} appointmentLocations
* @param {Location} location
*/
const findAppointments = (appointmentLocations, location) => {
const {
latitude,
longitude,
openAppointmentSlots,
slotDetails,
} = location
if (
!latitude ||
!openAppointmentSlots ||
!slotDetails.some(slot => types.includes(slot.manufacturer))
) {
return appointmentLocations
}
const distance = geolib.getDistance(
{ latitude: homeLatitude, longitude: homeLongitude },
{ latitude, longitude },
) / METERS_TO_MILES
return distance <= maxDistance
? [...appointmentLocations, { ...location, distance }]
: appointmentLocations
}
const availableAppointments = locations.reduce(findAppointments, [])
.sort((a, b) => a.distance - b.distance)
if (availableAppointments.length) {
console.log('Found an appointment!')
return browser(availableAppointments[0].url)
}
console.log('No appointments found')
return new Promise((resolve) => resolve(false))
}
/**
* Launch browser and pick a slot
* @param {string} url
*/
async function browser(url) {
const DATE_SELECTOR = 'input[name="Appointment_Date__c"]'
const TIME_SELECTOR = 'input[name="Event_Session__c"]'
const OPTION_SELECTOR = '[role="option"]'
const SUBMIT_BUTTON_SELECTOR = 'lightning-button'
const browser = await playwright.firefox.launch({ headless: false })
const context = await browser.newContext({ viewport: { width: 1280, height: 800 } })
let foundAppointment = false
const page = await context.newPage()
await page.goto(url)
const closeIfFound = match => value => {
if (value.trim() === match) {
console.log('Appointment no longer available')
return page.close()
}
}
/** If slot is already taken, close the page. */
page.textContent('h1').then(
closeIfFound('Appointments are no longer available for this location.'),
noop,
)
page.textContent('.slds-m-bottom_small.slds-text-color_error.slds-text-title_bold').then(
closeIfFound('There are no available time slots.'),
noop,
)
await sleep(randomNumber(400, 550))
await page.click(DATE_SELECTOR).catch(noop)
const dateSelector = await page.getAttribute(DATE_SELECTOR, 'aria-owns').catch(noop)
await page.click(`#${dateSelector} > ${OPTION_SELECTOR}`).catch(noop)
await sleep(randomNumber(400, 550))
await page.click(TIME_SELECTOR).catch(noop)
const timeSelector = await page.getAttribute(TIME_SELECTOR, 'aria-owns').catch(noop)
await page.click(`#${timeSelector} > ${OPTION_SELECTOR}`).catch(noop)
await sleep(randomNumber(400, 550))
await page.click(SUBMIT_BUTTON_SELECTOR).then(
() => foundAppointment = true,
() => browser.close()
)
return new Promise((resolve) => resolve(foundAppointment))
}