Actualizar robot.js

This commit is contained in:
2026-02-13 20:48:39 +00:00
parent 6f47a668c0
commit 890da83cab

View File

@@ -41,11 +41,13 @@ async function main() {
} }
} }
// ... (Mantén tus imports y la función main igual)
// ========================================== // ==========================================
// 🏥 MULTIASISTENCIA V4 (BÚSQUEDA POR ANCLAS) // 🏥 MULTIASISTENCIA V5 (EXTRACCIÓN QUIRÚRGICA)
// ========================================== // ==========================================
async function runMultiasistencia(ownerId, user, pass) { async function runMultiasistencia(ownerId, user, pass) {
const browser = await chromium.launch({ headless: HEADLESS, args: ['--no-sandbox'] }); const browser = await chromium.launch({ headless: true, args: ['--no-sandbox'] });
const context = await browser.newContext(); const context = await browser.newContext();
const page = await context.newPage(); const page = await context.newPage();
@@ -68,59 +70,52 @@ async function runMultiasistencia(ownerId, user, pass) {
return Array.from(new Set(links.map(a => a.href.match(/reparacion=(\d+)/)?.[1]).filter(Boolean))); return Array.from(new Set(links.map(a => a.href.match(/reparacion=(\d+)/)?.[1]).filter(Boolean)));
}); });
console.log(`🔍 [Multi] ${expedientes.length} expedientes encontrados.`); console.log(`🔍 [Multi] Analizando ${expedientes.length} expedientes.`);
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' });
const fullData = await page.evaluate(() => { const fullData = await page.evaluate(() => {
const data = {}; const data = {};
const allElements = Array.from(document.querySelectorAll('td, b, span, div, font'));
// 1. EXTRAER TODOS LOS ELEMENTOS CON TEXTO // Función interna para buscar el valor que está JUSTO después de un texto
const allElements = Array.from(document.querySelectorAll('td, b, span, div')); const findValueAfter = (label) => {
const found = allElements.find(el => el.innerText.trim().replace(':', '') === label);
// 2. DICCIONARIO DE ETIQUETAS "ANCLA" if (!found) return null;
const keywords = [
{ key: 'expediente', search: 'Número Reparación' },
{ key: 'poliza', search: 'Número de Póliza' },
{ key: 'clientName', search: 'Nombre Cliente' },
{ key: 'address', search: 'Dirección' },
{ key: 'poblacion_cp', search: 'Distrito Postal' },
{ key: 'procedencia', search: 'Procedencia' },
{ key: 'descripcion', search: 'Descripción de la Reparación' },
{ key: 'tramitador', search: 'Tramitador' },
{ key: 'urgente', search: 'Urgente' },
{ key: 'fecha_realizacion', search: 'Fecha realización' },
{ key: 'forma_pago', search: 'Forma de pago' }
];
keywords.forEach(item => { // 1. Probar con el siguiente elemento hermano
// Buscamos el elemento que contenga el texto exacto del campo if (found.nextElementSibling) return found.nextElementSibling.innerText.trim();
const found = allElements.find(el => el.innerText.trim() === item.search);
if (found) { // 2. Probar con la celda de al lado (si es una tabla)
// El valor suele estar en el siguiente elemento hermano if (found.tagName === 'TD' && found.parentElement) {
let val = ""; const cells = Array.from(found.parentElement.children);
if (found.nextElementSibling) { const index = cells.indexOf(found);
val = found.nextElementSibling.innerText.trim(); if (cells[index + 1]) return cells[index + 1].innerText.trim();
} else if (found.parentElement && found.parentElement.nextElementSibling) {
// O en la celda de al lado
val = found.parentElement.nextElementSibling.innerText.trim();
}
// Si lo que encontramos no es el mismo nombre de la etiqueta, lo guardamos
if (val && val !== item.search) {
data[item.search] = val;
}
} }
}); return null;
};
// 3. CAPTURA ESPECIAL DE TELÉFONOS (CIRUJANO) // Mapeo directo de etiquetas reales de la web
data['Número Reparación'] = findValueAfter('Número Reparación');
data['Número de Póliza'] = findValueAfter('Número de Póliza');
data['Nombre Cliente'] = findValueAfter('Nombre Cliente');
data['Dirección'] = findValueAfter('Dirección');
data['Distrito Postal'] = findValueAfter('Distrito Postal');
data['Procedencia'] = findValueAfter('Procedencia');
data['Descripción'] = findValueAfter('Descripción de la Reparación');
data['Tramitador'] = findValueAfter('Tramitador');
data['Urgente'] = findValueAfter('Urgente');
data['Fecha realización'] = findValueAfter('Fecha realización');
// Teléfonos (Busca cualquier número de 9 cifras que empiece por 6, 7, 8 o 9)
const bodyText = document.body.innerText; const bodyText = document.body.innerText;
const phones = bodyText.match(/[6789]\d{8}/g); const phones = bodyText.match(/[6789]\d{8}/g);
if (phones) { if (phones) data['Teléfono Detectado'] = [...new Set(phones)].join(' / ');
data['phone_fixed'] = [...new Set(phones)].join(' / ');
}
// Limpiar nulos
Object.keys(data).forEach(key => (data[key] === null || data[key] === "") && delete data[key]);
return data; return data;
}); });
@@ -128,13 +123,10 @@ async function runMultiasistencia(ownerId, user, pass) {
await saveServiceToDB(ownerId, 'multiasistencia', ref, fullData); await saveServiceToDB(ownerId, 'multiasistencia', ref, fullData);
} }
} }
} catch (e) { } catch (e) { console.error("❌ [Multi]", e.message); } finally { await browser.close(); }
console.error("❌ [Multi]", e.message);
} finally {
await browser.close();
}
} }
// ... (Mantén el resto del archivo igual)
// ========================================== // ==========================================
// 🧹 HOMESERVE (MANTENIDO) // 🧹 HOMESERVE (MANTENIDO)
// ========================================== // ==========================================