Robust data extraction with fallback

This commit is contained in:
Deep Koluguri 2026-06-28 21:41:25 -04:00
parent e7fab245fe
commit 067cf9a7cb
1 changed files with 24 additions and 10 deletions

View File

@ -61,10 +61,14 @@ async function runScrapers(credentials) {
]);
console.log('[Scraper] Logged into FirstEnergy. Extracting usage...');
await page.waitForSelector('.usage-summary, .kwh-value', { timeout: 10000 });
const usage = await page.$eval('.kwh-value', el => el.innerText);
await page.waitForSelector('.kwh, .usage-summary, .kwh-value', { timeout: 10000 });
const usage = await page.evaluate(() => {
const el = document.querySelector('.kwh, .usage-summary, .kwh-value');
if (!el) return 'N/A';
return (el.getAttribute('data-chart-value') || el.innerText).trim();
});
const cost = await page.$eval('.cost-value', el => el.innerText).catch(() => 'N/A');
report += `💡 *FirstEnergy*: ${usage.trim()} kWh used today (~${cost.trim()})\n`;
report += `💡 *FirstEnergy*: ${usage} kWh used today (~${cost.trim()})\n`;
} catch (err) {
console.warn('[Scraper] FirstEnergy scraping failed:', err.message);
report += `💡 *FirstEnergy*: ❌ Error scraping data (${err.message})\n`;
@ -81,17 +85,27 @@ async function runScrapers(credentials) {
await page.goto('https://peopleseaccount.com/Portal/default.aspx', { waitUntil: 'domcontentloaded', timeout: 60000 });
await page.waitForSelector('#txtLogin, input[name="txtLogin"]', { timeout: 30000 });
await page.type('#txtLogin, input[name="txtLogin"]', credentials.peoplesGas.username);
await page.type('#txtpwd, input[name="txtpwd"]', credentials.peoplesGas.password);
await page.waitForSelector('#txtLogin', { timeout: 30000 });
// Clear fields first and type cleanly
await page.evaluate(() => {
document.querySelector('#txtLogin').value = '';
document.querySelector('#txtpwd').value = '';
});
await page.type('#txtLogin', credentials.peoplesGas.username);
await page.type('#txtpwd', credentials.peoplesGas.password);
await page.click('#btnlogin');
console.log('[Scraper] Logged into Peoples Gas. Extracting usage...');
await page.waitForSelector('.ccf-usage', { timeout: 10000 });
const usage = await page.$eval('.ccf-usage', el => el.innerText);
const cost = await page.$eval('.ccf-cost', el => el.innerText).catch(() => 'N/A');
report += `🔥 *Peoples Gas*: ${usage.trim()} CCF used today (~${cost.trim()})\n`;
await page.waitForSelector('.ccf-usage', { timeout: 15000 }).catch(() => null);
const usage = await page.evaluate(() => {
const el = document.querySelector('.ccf-usage, #aGasUsageCCF');
return el ? el.innerText.replace('View in', '').trim() : 'N/A';
});
const cost = await page.$eval('.cost-value', el => el.innerText).catch(() => 'N/A');
report += `🔥 *Peoples Gas*: ${usage} CCF used today (~${cost.trim()})\n`;
} catch (err) {
console.warn('[Scraper] Peoples Gas scraping failed:', err.message);
report += `🔥 *Peoples Gas*: ❌ Error scraping data (${err.message})\n`;