Actualizar robot.js

This commit is contained in:
2026-02-19 08:21:40 +00:00
parent 031479e741
commit 76f2bbaca2

129
robot.js
View File

@@ -122,7 +122,7 @@ async function main() {
} }
// ========================================== // ==========================================
// 🏥 MULTIASISTENCIA (CON INTELIGENCIA Y PAGINACIÓN) // 🏥 MULTIASISTENCIA (CON INTELIGENCIA Y PAGINACIÓN REAL)
// ========================================== // ==========================================
async function runMultiasistencia(ownerId, user, pass, gremiosDB) { async function runMultiasistencia(ownerId, user, pass, gremiosDB) {
const browser = await chromium.launch({ headless: HEADLESS, args: ['--no-sandbox'] }); const browser = await chromium.launch({ headless: HEADLESS, args: ['--no-sandbox'] });
@@ -144,7 +144,7 @@ async function runMultiasistencia(ownerId, user, pass, gremiosDB) {
while (true) { while (true) {
console.log(`📄 [Multi] Escaneando página ${paginaActual}...`); console.log(`📄 [Multi] Escaneando página ${paginaActual}...`);
// Extraer expedientes de esta página // 1. Extraer expedientes de la página actual
const expedientesPagina = await page.evaluate(() => { const expedientesPagina = await page.evaluate(() => {
const links = Array.from(document.querySelectorAll('a[href*="reparacion="]')); const links = Array.from(document.querySelectorAll('a[href*="reparacion="]'));
return links.map(a => a.href.match(/reparacion=(\d+)/)?.[1]).filter(Boolean); return links.map(a => a.href.match(/reparacion=(\d+)/)?.[1]).filter(Boolean);
@@ -152,54 +152,64 @@ async function runMultiasistencia(ownerId, user, pass, gremiosDB) {
expedientesPagina.forEach(ref => todosExpedientes.add(ref)); expedientesPagina.forEach(ref => todosExpedientes.add(ref));
// Buscar botón de siguiente página y hacer clic si existe // 2. Buscar el botón "Página siguiente"
const clickedNext = await page.evaluate(() => { // Basado en el HTML: <a href="javascript:document.filtros.submit();" onclick="javascript:document.filtros.paginasiguiente.value=X+1">Página siguiente</a>
const links = Array.from(document.querySelectorAll('a')); const hasNextPage = await page.evaluate(() => {
const nextBtn = links.find(a => { const links = Array.from(document.querySelectorAll('a.lnkheader'));
const txt = a.innerText.trim(); const nextBtn = links.find(a => a.innerText.trim() === 'Página siguiente');
const title = (a.title || "").toLowerCase();
return txt === '>' || txt.toLowerCase() === 'siguiente' || title.includes('siguient') || a.querySelector('img[alt*="iguient"]');
});
if (nextBtn) { if (nextBtn) {
nextBtn.click(); nextBtn.click(); // Ejecuta el javascript interno de la web
return true; return true;
} }
return false; return false;
}); });
if (clickedNext) { if (hasNextPage) {
await page.waitForTimeout(3000); // Dar tiempo a que cargue la siguiente página // Esperamos a que la red se calme tras el submit del formulario de la web
await page.waitForLoadState('networkidle');
await page.waitForTimeout(2000); // Un pequeño margen extra
paginaActual++; paginaActual++;
// Medida de seguridad antbucle infinito
if(paginaActual > 15) {
console.log("⚠️ [Multi] Límite de 15 páginas alcanzado por seguridad.");
break;
}
} else { } else {
break; // Se acabaron las páginas console.log("🛑 [Multi] No hay más páginas.");
break; // Se acabó la paginación
} }
} }
const expedientes = Array.from(todosExpedientes); const expedientes = Array.from(todosExpedientes);
console.log(`✅ [Multi] Total expedientes detectados: ${expedientes.length}`); console.log(`✅ [Multi] Total expedientes únicos detectados: ${expedientes.length}`);
// --- MEJORA: ARCHIVADO --- // --- MEJORA: ARCHIVADO ---
if (expedientes.length > 0) { if (expedientes.length > 0) {
await syncAndArchive(ownerId, 'multiasistencia', expedientes); await syncAndArchive(ownerId, 'multiasistencia', expedientes);
} }
// --- EXTRACCIÓN DE DATOS ---
for (const ref of expedientes) { for (const ref of expedientes) {
await page.goto(`https://web.multiasistencia.com/w3multi/repasos1.php?reparacion=${ref}`, { waitUntil: 'domcontentloaded' }); await page.goto(`https://web.multiasistencia.com/w3multi/repasos1.php?reparacion=${ref}`, { waitUntil: 'domcontentloaded' });
await page.waitForTimeout(1500); await page.waitForTimeout(1500);
let scrapData = null; let scrapData = null;
for (const frame of page.frames()) { for (const frame of page.frames()) {
try { try {
scrapData = await frame.evaluate(() => { scrapData = await frame.evaluate(() => {
const clean = (t) => t ? t.replace(/\s+/g, ' ').trim() : ""; const clean = (t) => t ? t.replace(/\s+/g, ' ').trim() : "";
const body = document.body?.innerText || ""; const body = document.body?.innerText || "";
if (!body.includes("Nombre Cliente") && !body.includes("Asegurado")) return null; if (!body.includes("Nombre Cliente") && !body.includes("Asegurado")) return null;
const cabeceras = Array.from(document.querySelectorAll('.tcab')); const cabeceras = Array.from(document.querySelectorAll('.tcab'));
const detalles = Array.from(document.querySelectorAll('.tdet')); const detalles = Array.from(document.querySelectorAll('.tdet'));
const findByCab = (texto) => { const findByCab = (texto) => {
const idx = cabeceras.findIndex(el => el.innerText.includes(texto)); const idx = cabeceras.findIndex(el => el.innerText.includes(texto));
return idx !== -1 && detalles[idx] ? clean(detalles[idx].innerText) : null; return idx !== -1 && detalles[idx] ? clean(detalles[idx].innerText) : null;
}; };
let rawDesc = ""; let rawDesc = "";
const descHeader = Array.from(document.querySelectorAll('td.tcab')).find(td => td.innerText.includes("Descripción de la Reparación")); const descHeader = Array.from(document.querySelectorAll('td.tcab')).find(td => td.innerText.includes("Descripción de la Reparación"));
if (descHeader && descHeader.nextElementSibling) rawDesc = clean(descHeader.nextElementSibling.innerText); if (descHeader && descHeader.nextElementSibling) rawDesc = clean(descHeader.nextElementSibling.innerText);
@@ -226,6 +236,7 @@ async function runMultiasistencia(ownerId, user, pass, gremiosDB) {
if (scrapData && scrapData['Nombre Cliente']) break; if (scrapData && scrapData['Nombre Cliente']) break;
} catch (e) { continue; } } catch (e) { continue; }
} }
if (scrapData && scrapData['Nombre Cliente']) { if (scrapData && scrapData['Nombre Cliente']) {
// 🪄 MAGIA: Clasificamos el gremio basándonos en la descripción leída de la web // 🪄 MAGIA: Clasificamos el gremio basándonos en la descripción leída de la web
const idGremioDetectado = clasificarGremio(scrapData['Descripción'], gremiosDB); const idGremioDetectado = clasificarGremio(scrapData['Descripción'], gremiosDB);
@@ -235,91 +246,11 @@ async function runMultiasistencia(ownerId, user, pass, gremiosDB) {
await saveServiceToDB(ownerId, 'multiasistencia', ref, scrapData); await saveServiceToDB(ownerId, 'multiasistencia', ref, scrapData);
} }
} }
} catch (e) { console.error("❌ Error Multi:", e.message); } finally { await browser.close(); } } catch (e) {
console.error("❌ Error Multi:", e.message);
} finally {
await browser.close();
} }
// ==========================================
// 🧹 HOMESERVE (CON INTELIGENCIA)
// ==========================================
async function runHomeserve(ownerId, user, pass, gremiosDB) {
const browser = await chromium.launch({ headless: HEADLESS, args: ['--no-sandbox'] });
const page = await browser.newPage();
try {
console.log("🌍 [HomeServe] Entrando...");
await page.goto('https://www.clientes.homeserve.es/cgi-bin/fccgi.exe?w3exec=PROF_PASS', { timeout: 60000 });
if (await page.isVisible('input[name="CODIGO"]')) {
await page.fill('input[name="CODIGO"]', user);
await page.fill('input[type="password"]', pass);
await page.keyboard.press('Enter');
await page.waitForTimeout(5000);
}
await page.goto('https://www.clientes.homeserve.es/cgi-bin/fccgi.exe?w3exec=lista_servicios_total');
await page.waitForTimeout(3000);
const refs = await page.evaluate(() => {
const cells = Array.from(document.querySelectorAll('td'));
const found = [];
cells.forEach(td => {
const match = td.innerText.trim().match(/^15\d{6}$/);
if (match) found.push(match[0]);
});
return [...new Set(found)];
});
// --- MEJORA: ARCHIVADO ---
if (refs.length > 0) {
await syncAndArchive(ownerId, 'homeserve', refs);
}
console.log(`🔍 [HomeServe] ${refs.length} expedientes detectados.`);
for (const ref of refs) {
await page.goto(`https://www.clientes.homeserve.es/cgi-bin/fccgi.exe?w3exec=ver_servicioencurso&Servicio=${ref}`);
await page.waitForTimeout(2000);
const scrapData = await page.evaluate(() => {
const d = {};
const rows = Array.from(document.querySelectorAll('tr'));
rows.forEach(r => {
const cells = r.querySelectorAll('td');
if (cells.length >= 2) {
const k = cells[0].innerText.toUpperCase().trim().replace(':', '');
const v = cells[1].innerText.trim();
if (k.includes("COMENTARIOS")) {
const txt = cells[1].querySelector('textarea')?.value || "";
const cleanDesc = txt.split('\n').filter(line => {
const l = line.toUpperCase();
return !["CAMBIO DE ESTADO", "ESTADO ASIGNADO", "SMS NO ENVIADO", "CONTACTO CON PROF", "0000"].some(b => l.includes(b));
}).slice(0, 3).join('\n').trim();
d['Descripción'] = cleanDesc;
} else if (k.length > 1 && v.length > 0 && !k.includes("MENU")) {
d[k] = v;
}
}
});
const rawPop = d['POBLACION-PROVINCIA'] || "";
const cpMatch = rawPop.match(/\b\d{5}\b/);
d['Código Postal'] = cpMatch ? cpMatch[0] : "";
d['Población'] = rawPop.replace(d['Código Postal'], '').replace('-', '').trim();
d['Compañía'] = "HOME - " + (d['COMPAÑIA'] || "HOMESERVE");
d['Nombre Cliente'] = d['CLIENTE'] || "";
d['Dirección'] = d['DOMICILIO'] || "";
return d;
});
if (scrapData && scrapData['Nombre Cliente']) {
// 🪄 MAGIA: Clasificamos el gremio basándonos en la descripción leída de la web
const idGremioDetectado = clasificarGremio(scrapData['Descripción'], gremiosDB);
if (idGremioDetectado) {
scrapData['guild_id'] = idGremioDetectado;
}
await saveServiceToDB(ownerId, 'homeserve', ref, scrapData);
}
}
} catch (e) { console.error("❌ [HomeServe] Error:", e.message); }
finally { await browser.close(); }
} }
// --- NUEVA FUNCIÓN: SINCRONIZACIÓN Y ARCHIVADO --- // --- NUEVA FUNCIÓN: SINCRONIZACIÓN Y ARCHIVADO ---