Robust data extraction with fallback
This commit is contained in:
parent
e7fab245fe
commit
067cf9a7cb
|
|
@ -61,10 +61,14 @@ async function runScrapers(credentials) {
|
||||||
]);
|
]);
|
||||||
|
|
||||||
console.log('[Scraper] Logged into FirstEnergy. Extracting usage...');
|
console.log('[Scraper] Logged into FirstEnergy. Extracting usage...');
|
||||||
await page.waitForSelector('.usage-summary, .kwh-value', { timeout: 10000 });
|
await page.waitForSelector('.kwh, .usage-summary, .kwh-value', { timeout: 10000 });
|
||||||
const usage = await page.$eval('.kwh-value', el => el.innerText);
|
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');
|
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) {
|
} catch (err) {
|
||||||
console.warn('[Scraper] FirstEnergy scraping failed:', err.message);
|
console.warn('[Scraper] FirstEnergy scraping failed:', err.message);
|
||||||
report += `💡 *FirstEnergy*: ❌ Error scraping data (${err.message})\n`;
|
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.goto('https://peopleseaccount.com/Portal/default.aspx', { waitUntil: 'domcontentloaded', timeout: 60000 });
|
||||||
|
|
||||||
await page.waitForSelector('#txtLogin, input[name="txtLogin"]', { timeout: 30000 });
|
await page.waitForSelector('#txtLogin', { timeout: 30000 });
|
||||||
await page.type('#txtLogin, input[name="txtLogin"]', credentials.peoplesGas.username);
|
|
||||||
await page.type('#txtpwd, input[name="txtpwd"]', credentials.peoplesGas.password);
|
// 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');
|
await page.click('#btnlogin');
|
||||||
|
|
||||||
console.log('[Scraper] Logged into Peoples Gas. Extracting usage...');
|
console.log('[Scraper] Logged into Peoples Gas. Extracting usage...');
|
||||||
await page.waitForSelector('.ccf-usage', { timeout: 10000 });
|
await page.waitForSelector('.ccf-usage', { timeout: 15000 }).catch(() => null);
|
||||||
const usage = await page.$eval('.ccf-usage', el => el.innerText);
|
const usage = await page.evaluate(() => {
|
||||||
const cost = await page.$eval('.ccf-cost', el => el.innerText).catch(() => 'N/A');
|
const el = document.querySelector('.ccf-usage, #aGasUsageCCF');
|
||||||
report += `🔥 *Peoples Gas*: ${usage.trim()} CCF used today (~${cost.trim()})\n`;
|
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) {
|
} catch (err) {
|
||||||
console.warn('[Scraper] Peoples Gas scraping failed:', err.message);
|
console.warn('[Scraper] Peoples Gas scraping failed:', err.message);
|
||||||
report += `🔥 *Peoples Gas*: ❌ Error scraping data (${err.message})\n`;
|
report += `🔥 *Peoples Gas*: ❌ Error scraping data (${err.message})\n`;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue