From ad0df701177e0af797adf7af622d591c688dee7d Mon Sep 17 00:00:00 2001 From: Deep Koluguri Date: Sat, 27 Jun 2026 16:51:44 -0400 Subject: [PATCH] Implement real Puppeteer scraping logic --- utility-agent/scraper.js | 67 +++++++++++++++++++++++++++++++--------- 1 file changed, 53 insertions(+), 14 deletions(-) diff --git a/utility-agent/scraper.js b/utility-agent/scraper.js index fffd219..5ea59e1 100644 --- a/utility-agent/scraper.js +++ b/utility-agent/scraper.js @@ -19,26 +19,65 @@ async function runScrapers(credentials) { try { if (credentials.firstEnergy?.username && credentials.firstEnergy?.password) { console.log('[Scraper] Scraping FirstEnergy...'); - // Implement the actual login flow and navigation here - // const page = await browser.newPage(); - // await page.goto('https://www.firstenergycorp.com/log_in.html'); - // ... + const page = await browser.newPage(); + // Stealth settings to avoid basic bot detection + await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36'); - // For now, simulated data - await new Promise(r => setTimeout(r, 2000)); - report += `💡 *FirstEnergy*: 24.5 kWh used today (~$3.15)\n`; + await page.goto('https://www.firstenergycorp.com/log_in.html', { waitUntil: 'networkidle2' }); + + // Wait for and fill login form + await page.waitForSelector('input[name="USER"]', { timeout: 15000 }); + await page.type('input[name="USER"]', credentials.firstEnergy.username); + await page.type('input[name="PASSWORD"]', credentials.firstEnergy.password); + + await Promise.all([ + page.waitForNavigation({ waitUntil: 'networkidle2' }), + page.click('button[type="submit"], input[type="submit"]') + ]); + + // Navigate to usage page or extract from dashboard + console.log('[Scraper] Logged into FirstEnergy. Extracting usage...'); + // Note: These selectors are standard guesses and will likely need adjustment + // based on the actual DOM structure of the authenticated dashboard. + try { + await page.waitForSelector('.usage-summary, .kwh-value', { timeout: 10000 }); + const usage = await page.$eval('.kwh-value', el => el.innerText); + const cost = await page.$eval('.cost-value', el => el.innerText).catch(() => 'N/A'); + report += `💡 *FirstEnergy*: ${usage.trim()} kWh used today (~${cost.trim()})\n`; + } catch (err) { + console.warn('[Scraper] Could not find FirstEnergy usage selectors. Falling back to mock data for demonstration.'); + report += `💡 *FirstEnergy (Simulated)*: 24.5 kWh used today (~$3.15)\n`; + } + await page.close(); } if (credentials.peoplesGas?.username && credentials.peoplesGas?.password) { console.log('[Scraper] Scraping Peoples Gas...'); - // Implement the actual login flow and navigation here - // const page = await browser.newPage(); - // await page.goto('https://www.peoples-gas.com/'); - // ... + const page = await browser.newPage(); + await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36'); - // For now, simulated data - await new Promise(r => setTimeout(r, 2000)); - report += `🔥 *Peoples Gas*: 3.2 CCF used today (~$2.40)\n`; + await page.goto('https://www.peoples-gas.com/login', { waitUntil: 'networkidle2' }); + + await page.waitForSelector('#username, input[name="username"]', { timeout: 15000 }); + await page.type('#username, input[name="username"]', credentials.peoplesGas.username); + await page.type('#password, input[name="password"]', credentials.peoplesGas.password); + + await Promise.all([ + page.waitForNavigation({ waitUntil: 'networkidle2' }), + page.click('button[type="submit"], .login-btn') + ]); + + console.log('[Scraper] Logged into Peoples Gas. Extracting usage...'); + try { + 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`; + } catch (err) { + console.warn('[Scraper] Could not find Peoples Gas usage selectors. Falling back to mock data for demonstration.'); + report += `🔥 *Peoples Gas (Simulated)*: 3.2 CCF used today (~$2.40)\n`; + } + await page.close(); } report += `\n_Stats pulled automatically by Utility Agent_`;