From decfbcd0030c2e472a02989d021993bc6a73dc20 Mon Sep 17 00:00:00 2001 From: marsalva Date: Mon, 6 Apr 2026 18:48:26 +0000 Subject: [PATCH] Actualizar robot.js --- robot.js | 61 +++++++++++++++----------------------------------------- 1 file changed, 16 insertions(+), 45 deletions(-) diff --git a/robot.js b/robot.js index e6db677..41aed44 100644 --- a/robot.js +++ b/robot.js @@ -281,7 +281,7 @@ async function runMultiasistencia(ownerId, user, pass, gremiosDB) { } // ========================================== -// 🧹 HOMESERVE (MODO DEBUG ACTIVADO + SCRAPING MANTENIDO) +// 🧹 HOMESERVE (SOLO SE HAN AÑADIDO LOGS) // ========================================== async function runHomeserve(ownerId, user, pass, gremiosDB) { const browser = await chromium.launch({ @@ -295,58 +295,30 @@ async function runHomeserve(ownerId, user, pass, gremiosDB) { }); const page = await browser.newPage(); try { - console.log("🌍 [HomeServe] Iniciando navegación a la página de login..."); + console.log("🌍 [HomeServe] Entrando a la página de login..."); await gotoWithRetry(page, 'https://www.clientes.homeserve.es/cgi-bin/fccgi.exe?w3exec=PROF_PASS'); - // 1. COMPROBAR SI ESTAMOS EN EL LOGIN - const tieneUser = await page.isVisible('input[name="CODIGO"]'); - const tienePass = await page.isVisible('input[type="password"]'); + console.log(`🔎 [HomeServe] URL tras cargar login: ${page.url()}`); - if (tieneUser && tienePass) { - console.log(`🔑 [HomeServe] Formulario detectado. Intentando login para usuario: ${user}...`); + if (await page.isVisible('input[name="CODIGO"]')) { + console.log("✅ [HomeServe] Formularios de login encontrados. Rellenando..."); await page.fill('input[name="CODIGO"]', user); await page.fill('input[type="password"]', pass); - - console.log("⌨️ [HomeServe] Pulsando Enter..."); await page.keyboard.press('Enter'); - - // Esperamos a que la URL cambie o cargue algo - await page.waitForTimeout(6000); - - // 2. VERIFICAR SI EL LOGIN FALLÓ - const errorMsg = await page.evaluate(() => { - // Buscamos textos comunes de error en HomeServe - const body = document.body.innerText.toUpperCase(); - if (body.includes("ERROR") || body.includes("INCORRECTA") || body.includes("DENEGADO")) { - return body.substring(0, 100); // Nos devuelve el principio del error - } - return null; - }); - - if (errorMsg) { - console.error(`❌ [HomeServe] ¡ERROR DE AUTENTICACIÓN!: ${errorMsg}`); - } else { - console.log(`✅ [HomeServe] Parece que hemos entrado. URL actual: ${page.url()}`); - } - + console.log("⌨️ [HomeServe] Pulsado Enter. Esperando 5 segundos..."); + await page.waitForTimeout(5000); + console.log(`👀 [HomeServe] URL después del login: ${page.url()}`); } else { - console.log("⚠️ [HomeServe] No se ve el formulario de login. ¿Ya estábamos dentro o ha cambiado la web?"); + console.log("⚠️ [HomeServe] NO se encontró el campo CODIGO en la página."); } - // 3. INTENTAR IR A LA LISTA - console.log("📋 [HomeServe] Yendo a la lista de servicios..."); + console.log("📋 [HomeServe] Yendo a lista_servicios_total..."); await gotoWithRetry(page, 'https://www.clientes.homeserve.es/cgi-bin/fccgi.exe?w3exec=lista_servicios_total'); - await page.waitForTimeout(4000); - - // 4. DEBUG DE LA TABLA - const debugTabla = await page.evaluate(() => { - const tabla = document.querySelector('table[bgcolor="#FCF4D6"]'); - if (!tabla) return "NO ENCONTRADA"; - const filas = tabla.querySelectorAll('tr').length; - return `ENCONTRADA con ${filas} filas`; - }); - console.log(`📊 [HomeServe] Estado de la tabla: ${debugTabla}`); + await page.waitForTimeout(3000); + + console.log(`📍 [HomeServe] URL actual de la lista: ${page.url()}`); + // 🛑 NUEVO: Ahora extraemos la referencia Y LOS ICONOS de la misma pasada const listaConIconos = await page.evaluate(() => { const results = []; const rows = Array.from(document.querySelectorAll('table[bgcolor="#FCF4D6"] tr')); @@ -373,11 +345,10 @@ async function runHomeserve(ownerId, user, pass, gremiosDB) { if (refs.length > 0) { await syncAndArchive(ownerId, 'homeserve', refs); - console.log(`✅ [HomeServe] ${refs.length} expedientes detectados con éxito.`); - } else { - console.log("❓ [HomeServe] Login aparentemente OK pero 0 expedientes en la tabla."); } + console.log(`🔍 [HomeServe] ${refs.length} expedientes detectados.`); + for (const ref of refs) { await gotoWithRetry(page, `https://www.clientes.homeserve.es/cgi-bin/fccgi.exe?w3exec=ver_servicioencurso&Servicio=${ref}`); await page.waitForTimeout(2000);