auths
objectreturn
or this.key = 'value'
, pass input data to your code viaparams
, and maintain state across executions with$checkpoint.async
(event, steps, auths) => {
}
const axios = require("axios")
const cheerio = require("cheerio")
const puppeteer = require('puppeteer-core')
// Use Browserless to hit sites that render content via JS
const browser = await puppeteer.connect({
browserWSEndpoint: `wss://chrome.browserless.io?token=${auths.browserless.api_key}`
})
const page = await browser.newPage()
async function fetchHTML(url) {
const { data } = await axios.get(url)
return cheerio.load(data)
}
// Retrieve last known text from each site
const checkpoint = $checkpoint || {}
try {
// Hackensack
/* const hackensackHealthURL = "https://www.hackensackmeridianhealth.org/covid19/"
const hackensackHealth = await fetchHTML(hackensackHealthURL)
this.hackensackHealthText = hackensackHealth('#pagealert2 > strong').text()
if (checkpoint['hackensacker'] !== this.hackensackHealthText) {
$send.email({
subject: "COVID ALERT",
text: `Hackensack Meridian: ${hackensackHealthURL}`,
})
checkpoint['hackensacker'] = this.hackensackHealthText
} */
// Princeton Health
/* const princetonURL = "https://www.princetonhcs.org/"
const princeton = await fetchHTML(princetonURL)
this.princetonText = princeton('body > div > div.emergency-notice > div > div.emergency-notice__blurb > div > div > p').text()
if (checkpoint['princeton'] !== this.princetonText) {
$send.email({
subject: "COVID ALERT",
text: `Princeton Health: ${princetonURL}`,
})
checkpoint['princeton'] = this.princetonText
} */
// ShopRite
const shopRiteURL = "https://vaccines.shoprite.com/"
await page.goto(shopRiteURL)
const sselector = '#main > div:nth-child(2) > div > div'
await page.waitForSelector(sselector);
const shopRiteDiv = await page.$(sselector)
this.shopRiteText = await page.evaluate(shopRiteDiv => shopRiteDiv.textContent, shopRiteDiv);
if (checkpoint['shopRite'] !== this.shopRiteText) {
$send.email({
subject: "COVID ALERT",
text: `ShopRite: ${shopRiteURL}`,
})
checkpoint['shopRite'] = this.shopRiteText
}
// CVS
const cvsURL = "https://www.cvs.com/immunizations/covid-19-vaccine#statetool"
await page.goto(cvsURL)
// First, select the New Jersey link to appear
const stateSelector = 'a[data-analytics-name="New Jersey"]'
await page.waitForSelector(stateSelector);
await page.click(stateSelector)
// Then get the vaccine status text that appears
const cvsSelector = '#vaccineinfo-NJ > div > div > div > div.aem-Grid.aem-Grid--12.aem-Grid--default--12 > div.box.parbase.aem-GridColumn.aem-GridColumn--default--12 > div > div > div.content__wrapper.parsecondary > div > div.covid-updates.aem-GridColumn.aem-GridColumn--default--12 > div > div'
await page.waitForSelector(cvsSelector);
const cvsSpan = await page.$(cvsSelector)
this.cvsText = await page.evaluate(cvsSpan => cvsSpan.textContent, cvsSpan);
if (checkpoint['cvs'] !== this.cvsText) {
$send.email({
subject: "COVID ALERT",
text: `CVS: ${cvsURL}`,
})
checkpoint['cvs'] = this.cvsText
}
// Riverside Medical Group
/* const riversideURL = "https://form.jotform.com/210033944227044"
const riverside = await fetchHTML(riversideURL)
this.riversideText = riverside('#header_8').text()
if (checkpoint['riverside'] !== this.riversideText) {
$send.email({
subject: "COVID ALERT",
text: `Riverside Medical Group: ${riversideURL}`,
})
checkpoint['riverside'] = this.riversideText
} */
} catch (err) {
$send.email({
subject: "Error in COVID script",
text: err,
})
} finally {
// Persist new text to $checkpoint
$checkpoint = checkpoint
browser.close();
}