62 lines
2.3 KiB
JavaScript
62 lines
2.3 KiB
JavaScript
const puppeteer = require('puppeteer');
|
|
|
|
const WHATSAPP_GATEWAY_URL = process.env.WHATSAPP_GATEWAY_URL || 'http://whatsapp-gateway.ai-agents.svc.cluster.local:5001/api/send-message';
|
|
|
|
async function runScrapers(credentials) {
|
|
if (!credentials.firstEnergy && !credentials.peoplesGas) {
|
|
console.log('[Scraper] No credentials configured. Skipping run.');
|
|
return;
|
|
}
|
|
|
|
let report = "⚡ *Daily Utility Usage Report*\n\n";
|
|
|
|
const browser = await puppeteer.launch({
|
|
headless: 'new',
|
|
executablePath: '/usr/bin/google-chrome',
|
|
args: ['--no-sandbox', '--disable-setuid-sandbox']
|
|
});
|
|
|
|
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');
|
|
// ...
|
|
|
|
// For now, simulated data
|
|
await new Promise(r => setTimeout(r, 2000));
|
|
report += `💡 *FirstEnergy*: 24.5 kWh used today (~$3.15)\n`;
|
|
}
|
|
|
|
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/');
|
|
// ...
|
|
|
|
// For now, simulated data
|
|
await new Promise(r => setTimeout(r, 2000));
|
|
report += `🔥 *Peoples Gas*: 3.2 CCF used today (~$2.40)\n`;
|
|
}
|
|
|
|
report += `\n_Stats pulled automatically by Utility Agent_`;
|
|
|
|
console.log('[Scraper] Report generated, sending to WhatsApp...');
|
|
await fetch(WHATSAPP_GATEWAY_URL, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ to: 'default', message: report })
|
|
});
|
|
console.log('[Scraper] Report sent successfully.');
|
|
|
|
} catch (e) {
|
|
console.error('[Scraper] Error during scraping:', e);
|
|
} finally {
|
|
await browser.close();
|
|
}
|
|
}
|
|
|
|
module.exports = { runScrapers };
|