Añadir index.js
This commit is contained in:
parent
dca3e09744
commit
2c0a1d02c3
|
|
@ -0,0 +1,124 @@
|
|||
// robot-multiasistencia/index.js
|
||||
const { chromium } = require('playwright');
|
||||
const admin = require('firebase-admin');
|
||||
|
||||
if (process.env.FIREBASE_PRIVATE_KEY) {
|
||||
try {
|
||||
admin.initializeApp({
|
||||
credential: admin.credential.cert({
|
||||
projectId: process.env.FIREBASE_PROJECT_ID,
|
||||
clientEmail: process.env.FIREBASE_CLIENT_EMAIL,
|
||||
privateKey: process.env.FIREBASE_PRIVATE_KEY.replace(/\\n/g, '\n'),
|
||||
}),
|
||||
});
|
||||
console.log("✅ Firebase inicializado correctamente");
|
||||
} catch (err) {
|
||||
console.error("❌ Error inicializando Firebase:", err);
|
||||
process.exit(1);
|
||||
}
|
||||
} else {
|
||||
console.error("❌ Falta FIREBASE_PRIVATE_KEY en las variables de entorno");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const db = admin.firestore();
|
||||
|
||||
const MULTI_USER = process.env.MULTI_USER;
|
||||
const MULTI_PASS = process.env.MULTI_PASS;
|
||||
|
||||
if (!MULTI_USER || !MULTI_PASS) {
|
||||
console.error("❌ Faltan MULTI_USER o MULTI_PASS en ENV");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
async function delay(ms) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const browser = await chromium.launch({
|
||||
headless: true,
|
||||
args: ['--no-sandbox', '--disable-setuid-sandbox'],
|
||||
});
|
||||
|
||||
const context = await browser.newContext();
|
||||
const page = await context.newPage();
|
||||
|
||||
try {
|
||||
console.log("🌐 Entrando a Multiasistencia...");
|
||||
await page.goto('https://web.multiasistencia.com/', { waitUntil: 'domcontentloaded', timeout: 120000 });
|
||||
|
||||
console.log("🔐 Login...");
|
||||
await page.waitForSelector('input[type="text"]', { timeout: 60000 });
|
||||
await page.fill('input[type="text"]', MULTI_USER);
|
||||
|
||||
await page.waitForSelector('input[type="password"]', { timeout: 60000 });
|
||||
await page.fill('input[type="password"]', MULTI_PASS);
|
||||
|
||||
const btn = await page.$('button[type="submit"]');
|
||||
if (btn) {
|
||||
await btn.click();
|
||||
} else {
|
||||
await page.keyboard.press('Enter');
|
||||
}
|
||||
|
||||
await page.waitForLoadState('networkidle', { timeout: 120000 });
|
||||
console.log("✅ Logueado");
|
||||
await delay(3000);
|
||||
|
||||
// Ir a servicios / pendientes (según portal)
|
||||
const pendientesLink = await page.$('text=Pendientes');
|
||||
if (pendientesLink) {
|
||||
await pendientesLink.click();
|
||||
await page.waitForLoadState('networkidle', { timeout: 120000 });
|
||||
await delay(2000);
|
||||
}
|
||||
|
||||
console.log("📥 Extrayendo servicios...");
|
||||
const servicios = await page.evaluate(() => {
|
||||
const rows =
|
||||
Array.from(document.querySelectorAll('table tbody tr')) ||
|
||||
Array.from(document.querySelectorAll('tbody tr'));
|
||||
|
||||
return rows.map(r => {
|
||||
const cells = Array.from(r.querySelectorAll('td')).map(td => td.innerText.trim());
|
||||
return { cells };
|
||||
}).filter(x => x && x.cells && x.cells.length > 0);
|
||||
});
|
||||
|
||||
console.log(`🧾 Encontrados ${servicios.length} servicios (filas)`);
|
||||
|
||||
const batch = db.batch();
|
||||
const col = db.collection('multiasistencia_pendientes');
|
||||
|
||||
let saved = 0;
|
||||
for (const item of servicios) {
|
||||
const ref = col.doc();
|
||||
batch.set(ref, {
|
||||
raw: item,
|
||||
createdAt: admin.firestore.FieldValue.serverTimestamp(),
|
||||
source: 'multiasistencia',
|
||||
});
|
||||
saved++;
|
||||
if (saved % 450 === 0) {
|
||||
await batch.commit();
|
||||
}
|
||||
}
|
||||
await batch.commit();
|
||||
|
||||
console.log(`✅ Guardados ${saved} registros en Firestore (multiasistencia_pendientes)`);
|
||||
|
||||
} catch (err) {
|
||||
console.error("❌ Error en robot Multiasistencia:", err);
|
||||
try {
|
||||
await page.screenshot({ path: '/tmp/error-multiasistencia.png', fullPage: true });
|
||||
console.log("📸 Screenshot guardada en /tmp/error-multiasistencia.png");
|
||||
} catch (e) {}
|
||||
process.exitCode = 1;
|
||||
} finally {
|
||||
await browser.close();
|
||||
console.log("🧹 Browser cerrado");
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
Loading…
Reference in New Issue