Last active
December 14, 2023 19:13
-
-
Save jhadev/867f5defde3835f2ba92a1523971a145 to your computer and use it in GitHub Desktop.
austin-power-outages-puppeteer
This file contains 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"); | |
async function getAustinPowerOutages(endpoint) { | |
try { | |
const res = await fetch(endpoint); | |
if (!res.ok) { | |
throw new Error(`Austin Energy ${endpoint} is not available.`); | |
} | |
const data = await res.json(); | |
return data; | |
} catch (err) { | |
console.error("idk man, error: " + err.message); | |
} | |
} | |
(async function getDynamicEndpoint() { | |
const browser = await puppeteer.launch({ headless: true }); | |
const page = await browser.newPage(); | |
await page.setDefaultNavigationTimeout(0); | |
await page.setRequestInterception(true); | |
page.on("request", async (interceptedRequest) => { | |
if (!interceptedRequest.url().endswith("data.json")) { | |
interceptedRequest.continue(); | |
} | |
if (interceptedRequest.url().endsWith("/summary-1/data.json")) { | |
const outages = await getAustinPowerOutages(interceptedRequest.url()); | |
const totals = outages.summaryFileData.totals[0]; | |
const totalCustomersAffected = totals.total_cust_a.val; | |
const totalOutages = totals.total_outages; | |
const totalPercentCustomersActive = `${totals.total_percent_cust_active.val}%`; | |
const outageData = { | |
totalCustomersAffected, | |
totalOutages, | |
totalPercentCustomersActive | |
}; | |
console.log(outageData); | |
} | |
}); | |
await page.goto("https://outagemap.austinenergy.com/", { | |
timeout: 30000, | |
waitUntil: "networkidle2" | |
}); | |
await browser.close(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment