-
Notifications
You must be signed in to change notification settings - Fork 0
/
pup_scrape_vaccine_zipslots.js
519 lines (418 loc) · 18.5 KB
/
pup_scrape_vaccine_zipslots.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
require('dotenv').config();
let exceptionAttempts = 0;
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const client = require('twilio')(accountSid, authToken);
const fs = require('fs')
const simpleGit = require('simple-git');
const git = simpleGit();
const puppeteer = require('puppeteer-extra')
const StealthPlugin = require('puppeteer-extra-plugin-stealth')
puppeteer.use(StealthPlugin())
startTime = new Date()
awsUploadTime = startTime
zipStartTime = new Date()
storesDir = "ZipLocationsVaccines/"
zipParam = []
console.log("APPLICATION START:"+startTime)
function delay(time) {
return new Promise(function(resolve) {
setTimeout(resolve, time)
});
}
const EventEmitter = require('events');
class ScrapeEmitter extends EventEmitter {}
const myEmitter = new ScrapeEmitter();
myEmitter.on('processZipCodes', async () => {
console.log(new Date()+'::Start processZipCode Event')
zipParam = JSON.parse(fs.readFileSync('kroger_zipcodes.json'))
//zipParam = zipParam.slice(0,10)
let storesProcessed = fs.readdirSync(storesDir)
storesProcessed = storesProcessed.filter((s) => s.endsWith('.json'))
//console.log(storesProcessed)
zipProcessed = storesProcessed.map((z) => {
zipcode = z.split('_')[2].split('.')[0]
return zipcode
})
console.log(zipProcessed)
zipcodesToReprocess = storesProcessed.filter((f) => {
time = parseInt(f.split('_')[1])
return (time <= (new Date().getTime() - (1000 * 60 * 60 * 7)))
})
zipcodesToReprocess = zipcodesToReprocess.map((f) => {
zipcode = f.split('_')[2].split('.')[0]
return zipcode
})
//console.log("zips to reprocess")
//console.log(zipcodesToReprocess)
zipcodesToProcess = zipParam.filter((z) => !zipProcessed.includes(z))
//console.log("zip to process first time")
//console.log(zipcodesToProcess)
zipcodesToProcess.push(...zipcodesToReprocess)
//console.log("Zip Codes Available to Process:"+zipcodesToProcess.length)
//console.log(zipcodesToProcess)
zipParam = zipcodesToProcess
// if(!browser)
// browser = await puppeteer.launch({headless:false, executablePath: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'});
let page = await browser.newPage();
await page.goto('https://www.kroger.com/rx/guest/get-vaccinated',{waitUntil: 'networkidle0'});
if(zipParam.length > 0){
zipToProcess = zipParam[0]
myEmitter.emit("searchStores", zipToProcess, page)
} else {
console.log("START:"+startTime)
console.log("END:"+new Date())
console.log("Nothing to process, will try again in 10 minutes...")
await delay(1000 * 60 * 10) //wait 10 minutes and try again
myEmitter.emit('processZipCodes');
}
console.log(new Date()+'::End processZipCode Event')
})
myEmitter.on('searchStores', async (zip, page) => {
startSearchStores = new Date()
endSearchStores = startSearchStores
console.log(startSearchStores+'::Start searchStores Event')
try{
zipStartTime = new Date()
zipParam = zipParam.slice(1)
console.log(zip)
gotResponse = false
await page.on('response', async (response) => {
// console.log(response.status()+"::"+response.url())
if ((response.url().endsWith(zip) || response.url().indexOf('timeslots/list?stores') > 0 && !gotResponse) && response.status() == 200){
gotResponse = true
console.log(response.url())
console.log(response.status())
json = await response.json()
console.log('an searchStores event occurred!'+zip);
let dt = new Date()
let dtEnd = new Date(dt.getTime()+(1000*60*60*24*14))
let dateStart = dt.getFullYear()+'-'+String((dt.getMonth()+1)).padStart(2, '0')+'-'+String((dt.getDate())).padStart(2, '0')
let dateEnd = dtEnd.getFullYear()+'-'+String((dtEnd.getMonth()+1)).padStart(2, '0')+'-'+String((dtEnd.getDate())).padStart(2, '0')
await page.goto("https://www.kroger.com/rx/api/anonymous/scheduler/slots/locationsearch/"+zip+"/"+dateStart+"/"+dateEnd+"/50?appointmentReason=131&appointmentReason=134&appointmentReason=137&appointmentReason=122&appointmentReason=125&appointmentReason=129&benefitCode=null",{waitUntil: 'networkidle0'});
}
if (!response.url().endsWith(zip) && response.url().indexOf('rx-security-bff') < 0 && response.url().indexOf(zip) > 0 && response.status() == 200){
console.log(response.url())
console.log(response.status())
json = await response.json()
await delay(2000)
myEmitter.emit('foundStores', zip, json, page);
}
if (!response.url().endsWith(zip) && response.url().indexOf(zip) > 0 && response.status() != 200){
console.log(response.url())
console.log(response.status())
console.log("NOT 200 ERROR START:"+startTime)
console.log("NOT 200 ERROR END:"+new Date())
console.log("will try again in 5 minutes....")
await delay(300000)
if(!page.isClosed())
await page.close()
myEmitter.emit('processZipCodes');
}
});
await page.goto('https://www.kroger.com/rx/guest/get-vaccinated',{waitUntil: 'networkidle0'});
page.waitForSelector('.PharmacyLocator [name="findAStore"]')
const input = await page.$('.PharmacyLocator [name="findAStore"]');
await input.click({ clickCount: 3 })
for(let c of zip+""){
page.waitForTimeout(200)
await page.type('.PharmacyLocator [name="findAStore"]', c)
}
processing = true
await delay(2000)
page.click('.PharmacyLocator [aria-label="search"]')
console.log('get stores for zip:'+zip)
await delay(4000)
let securityCheck = await page.evaluate(() => {
let el = document.querySelector("#sec-overlay")
val = "none"
if(el){
val = window.getComputedStyle(el).getPropertyValue("display")
}
return val != "none"
})
console.log(securityCheck)
await delay(5000)
if(securityCheck){
exceptionAttempts++
if(exceptionAttempts > 2){
console.log("Hist exception twice, in security check!!!")
sendSMS("KROGER Hit an Security Check three times!!!!")
} else {
console.log("prompted with captcha security; will try again in 15 minutes.")
await delay(900000)
await page.goto('https://www.kroger.com/',{waitUntil: 'networkidle0'})
await delay(2000)
if(!page.isClosed())
await page.close()
myEmitter.emit('processZipCodes');
}
}
endSearchStores = new Date()
if(endSearchStores.getTime()-startSearchStores.getTime() > 33000){
console.log("ERROR:::Likely Failed getting request data")
}
console.log(endSearchStores+'::End searchStores Event')
}catch(ex){
console.log(ex)
console.log('caught exception... close page and try processZipCodes again... ')
await delay(2000)
if(!page.isClosed())
await page.close()
myEmitter.emit('processZipCodes');
}
});
myEmitter.on('foundStores', async (zip, stores, page) => {
console.log(new Date()+'::Start foundStores Event')
console.log("Current Working Directory...")
console.log(process.cwd())
/* Delete older file(s) for this zip code */
let files = fs.readdirSync(storesDir)
files = files.filter((f) => f.indexOf('_'+zip+'.json') > 0)
files.forEach((f) => {
fs.unlinkSync(storesDir+f)
})
/* Write new file for this zip code */
console.log('an foundStores event occurred! '+zip);
fs.writeFileSync(storesDir+'slots_'+new Date().getTime()+'_'+zip+'.json', JSON.stringify(stores, null, 2))
console.log("ZIP PROCESS START:"+zip+":"+zipStartTime)
console.log("ZIP PROCESS END:"+zip+":"+new Date())
/* Make change to git and push */
try{
console.log("Git pull...")
await git.pull()
console.log("Git pull...FINISHED")
/* Make change to git and push */
console.log('Git add, commit, push...')
await git.add('.')
await git.commit('Sent Processed Zip Code:'+zip)
await git.push()
console.log('Git add, commit, push...FINISHED')
} catch (ex) {
console.log("Try again... wait 1000")
await delay(1000)
console.log("Git pull...")
await git.pull()
console.log("Git pull...FINISHED")
/* Make change to git and push */
console.log('Git add, commit, push...')
await git.add('.')
await git.commit('Sent Processed Zip Code:'+zip)
await git.push()
console.log('Git add, commit, push...FINISHED')
}
let currentTime = new Date()
if(currentTime.getTime() > (awsUploadTime.getTime()+(1000*60*10))){
await reformatZipCodeDataIntoLocationAvailability(storesDir)
awsUploadTime = currentTime
}
await delay(120000)
if(!page.isClosed())
await page.close()
myEmitter.emit('processZipCodes');
console.log(new Date()+'::End foundStores Event')
});
try{
let browser;
}catch(ex){
console.log("Browser based exception")
console.log(ex)
console.log("Browser based exception")
}
(async () => {
console.log('zip codes:'+JSON.stringify(zipParam))
browser = await puppeteer.launch({headless:true, executablePath: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'});
processing = false
processedCount = 0
// add this handler before emitting any events
process.on('uncaughtException', async function (err) {
console.log('UNCAUGHT EXCEPTION - keeping process alive:', err); // err.message is "foobar"
if(exceptionAttempts == 0){
console.log('delay for a minute... and then try again... ')
await delay(60000)
myEmitter.emit('processZipCodes');
} else {
console.log("Hit an exception twice!!! And, sending an SMS....")
sendSMS("Hit an Excpetion Twice!!!!")
}
exceptionAttempts++
});
process.on('unhandledRejection', async function(err, promise) {
console.error('Unhandled rejection (promise: ', promise, ', reason: ', err, ').');
console.log('UNCAUGHT EXCEPTION - keeping process alive:', err); // err.message is "foobar"
if(exceptionAttempts == 0){
console.log('delay for two minutes... and then try again... ')
await delay(120000)
myEmitter.emit('processZipCodes');
} else {
console.log("Hit an unhandledRejection exception twice!!! And, sending an SMS....")
sendSMS("KROGER Hit an Promise Rejection Twice!!!!")
}
exceptionAttempts++
});
myEmitter.emit('processZipCodes');
//await reformatZipCodeDataIntoLocationAvailability(storesDir)
})();
/** Function Calls */
function sendSMS(message){
/*
client.messages
.create({
body: message,
from: '+17629997107',
to: '+16148357383'
})
.then(message => console.log(message.sid));
*/
}
async function reformatZipCodeDataIntoLocationAvailability(dir, awsUpload = true){
let zipFiles = fs.readdirSync(dir)
zipFiles = zipFiles.filter((z) => z.endsWith('.json'))
let storeAvailability = []
let locationsAlreadyProcessed = {}
for(let h=0; h<zipFiles.length; h++){
let zip = zipFiles[h]
let zip_unixtime = parseInt(zipFiles[h].split('_')[1])
let stores = JSON.parse(fs.readFileSync(dir+zip))
let start_date = null
let end_date = null
for(let i=0; i<stores.length; i++){
let start_date = null
let end_date = null
let s = stores[i]
let foundAlreadyIndex = -1
if(Object.keys(locationsAlreadyProcessed).includes(s.loc_no)){
if(locationsAlreadyProcessed[s.loc_no] > zip_unixtime){
continue
} else {
for(let m=0; i<storeAvailability.length; m++){
if(storeAvailability[m].original_data.loc_no == s.loc_no){
foundAlreadyIndex = m
break;
}
}
}
}
//weird state 45373 zip code results wihtout the store.facilityDetails?
if(!s.facilityDetails){
console.log(zip)
console.log(JSON.stringify(s, null, 2))
continue;
}
let storeDataFormat = {
//location_id: 123, // from https://app.vaccinateoh.org/api/locations
address: s.facilityDetails.address.address1+', '+s.facilityDetails.address.city+', '+s.facilityDetails.address.state+' '+s.facilityDetails.address.zipCode,
start_date: null,
end_date: null,
clear_existing: true, // true if we should delete all previous availability within this range
availability: []
}
for(let j=0; j<s.dates.length; j++){
let date = s.dates[j].date
if(end_date == null || new Date(date) > new Date(end_date)){
end_date = new Date(date)
}
if(start_date == null || new Date(date) < new Date(start_date)){
start_date = new Date(date)
}
let slots = s.dates[j].slots
let store_availability = []
for(let k=0; k<slots.length; k++){
let slot = slots[k]
let available = {
availability_time: date+' '+slot.start_time,
brand: getBrandCode(slot.ar_reason)
}
store_availability.push(available)
}
if(store_availability.length > 0){
storeDataFormat.availability = store_availability
}
}
storeDataFormat.start_date = start_date
storeDataFormat.end_date = end_date
storeDataFormat.original_data = s
storeDataFormat.original_data_unix_time = zip_unixtime
storeDataFormat.origina_data_time = new Date().toISOString()
if(foundAlreadyIndex >= 0){
storeAvailability[foundAlreadyIndex] = storeDataFormat
} else {
storeAvailability.push(storeDataFormat)
}
locationsAlreadyProcessed[s.loc_no] = zip_unixtime
}
}
/* Delete previous availability files */
let files = fs.readdirSync(process.cwd())
files = files.filter((f) => { return (f.indexOf('kroger_availability_') > -1 ) })
files.forEach((f) => fs.unlinkSync(f))
/* Create/Write new availability file */
let current_time = new Date().getTime();
let filename = 'kroger_availability_'+current_time+'.json'
fs.writeFileSync(filename, JSON.stringify(storeAvailability))
/* Make change to git and push */
try{
console.log("Git pull...")
await git.pull()
console.log("Git pull...FINISHED")
/* Make change to git and push */
console.log('Git add, commit, push...')
await git.add('.')
await git.commit('Prepare File for S3')
await git.push()
console.log('Git add, commit, push...FINISHED')
} catch (ex) {
console.log("Try again... wait 1000")
await delay(2000)
console.log("Git pull...")
await git.pull()
console.log("Git pull...FINISHED")
/* Make change to git and push */
console.log('Git add, commit, push...')
await git.add('.')
await git.commit('Prepare File for S3')
await git.push()
console.log('Git add, commit, push...FINISHED')
}
/* Upload availability file to AWS S3 BUCKET */
if(awsUpload){
console.log('do aws upload...')
// snippet-start:[s3.JavaScript.buckets.upload]
// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Set the region
AWS.config.update({region: process.env.AWS_REGION});
// Create S3 service object
s3 = new AWS.S3({apiVersion: '2006-03-01'});
// call S3 to retrieve upload file to specified bucket
var uploadParams = {Bucket: process.env.AWS_S3_BUCKET, Key: '', Body: ''};
var file = filename;
// Configure the file stream and obtain the upload parameters
var fileStream = fs.createReadStream(file);
fileStream.on('error', function(err) {
console.log('File Error', err);
});
uploadParams.Body = fileStream;
var path = require('path');
uploadParams.Key = path.basename(file);
// call S3 to retrieve upload file to specified bucket
s3.upload (uploadParams, function (err, data) {
if (err) {
console.log("Error", err);
} if (data) {
console.log("Upload Success", data.Location);
}
});
}
}
function getBrandCode(str){
if(str.toLowerCase().indexOf('moderna') >= 0){
return 'm'
} else if (str.toLowerCase().indexOf('pfizer') >= 0){
return 'p'
} else if (str.toLowerCase().indexOf('john') >= 0){
return 'j'
} else {
return ''
}
}