Skip to content

Instantly share code, notes, and snippets.

@jhadev
Last active December 14, 2023 19:13
Show Gist options
  • Save jhadev/867f5defde3835f2ba92a1523971a145 to your computer and use it in GitHub Desktop.
Save jhadev/867f5defde3835f2ba92a1523971a145 to your computer and use it in GitHub Desktop.
austin-power-outages-puppeteer
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