Created
March 11, 2020 22:18
-
-
Save chrisjpatty/4e456bcd08d02d0743f0e97bc64b4476 to your computer and use it in GitHub Desktop.
A Slack bot for tracking Covid-19 cases and deaths in your state.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const puppeteer = require("puppeteer"); | |
const axios = require("axios"); | |
let lastSeenStats = { cases: 0, deaths: 0 }; | |
const STATE = "Utah" // Change this to the state you want to test for | |
const sendSlack = stats => { | |
axios.post( | |
"YOUR_SLACK_WEBHOOK_URL HERE", | |
{ | |
username: `${STATE} Covid 19 Tracker Bot`, | |
icon_emoji: ":mask:", | |
text: `As of the time of posting, ${STATE} now has:`, | |
attachments: [ | |
{ | |
fallback: `There are now ${stats.cases} confirmed cases, and ${stats.deaths} confirmed deaths.`, | |
fields: [ | |
{ | |
title: "Confirmed Cases:", | |
value: stats.cases | |
}, | |
{ | |
title: "Confirmed Deaths:", | |
value: stats.deaths | |
} | |
] | |
} | |
] | |
} | |
); | |
}; | |
const getStats = async () => { | |
const browser = await puppeteer.launch({}); | |
const page = await browser.newPage(); | |
await page.goto( | |
"https://www.nytimes.com/interactive/2020/us/coronavirus-us-cases.html" | |
); | |
const stats = await page.evaluate(async () => { | |
const delay = time => { | |
return new Promise(function(resolve) { | |
setTimeout(resolve, time); | |
}); | |
}; | |
await delay(1000); | |
document.querySelector("#g-cases-by-state button").click(); | |
await delay(1000); | |
const allCells = document.querySelectorAll("#g-cases-by-state tbody td"); | |
const utahRow = Array.from(allCells).find(t => t.innerText === STATE); | |
const cells = utahRow.parentNode.childNodes; | |
const cellsArray = Array.from(cells).map(t => t.innerText); | |
const cases = cellsArray[2]; | |
const deaths = cellsArray[4]; | |
return { cases, deaths }; | |
}); | |
await browser.close(); | |
lastSeenStats = stats; | |
if ( | |
lastSeenStats.cases !== stats.cases || | |
lastSeenStats.deaths !== stats.deaths | |
) { | |
sendSlack(stats); | |
} | |
}; | |
setInterval(getStats, 600000); //Change this number to check more/less often. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment