Actualizar robot.js

This commit is contained in:
2026-02-13 23:10:45 +00:00
parent 60956e5c5c
commit 41e4032883

View File

@@ -67,6 +67,7 @@ async function runMultiasistencia(ownerId, user, pass) {
let scrapData = null; let scrapData = null;
// RECORRIDO DE FRAMES + EXTRACCIÓN POR TABLAS
for (const frame of page.frames()) { for (const frame of page.frames()) {
try { try {
scrapData = await frame.evaluate(() => { scrapData = await frame.evaluate(() => {
@@ -74,52 +75,46 @@ async function runMultiasistencia(ownerId, user, pass) {
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 allCells = Array.from(document.querySelectorAll('td, th, b, div')); // Seleccionamos todas las celdas de cabecera y detalle
const getH = (keys) => { const cabeceras = Array.from(document.querySelectorAll('.tcab'));
const header = allCells.find(el => keys.some(k => el.innerText.toUpperCase().includes(k.toUpperCase()))); const detalles = Array.from(document.querySelectorAll('.tdet'));
return header && header.nextElementSibling ? clean(header.nextElementSibling.innerText) : null;
}; const findByCab = (texto) => {
const getV = (keys) => { const idx = cabeceras.findIndex(el => el.innerText.includes(texto));
const header = allCells.find(el => keys.some(k => el.innerText.trim().toUpperCase() === k.toUpperCase())); return idx !== -1 && detalles[idx] ? clean(detalles[idx].innerText) : null;
if (!header) return null;
const idx = header.cellIndex;
const row = header.parentElement;
let nextRow = row.nextElementSibling;
if (!nextRow) {
const table = header.closest('table');
const tbody = table ? table.querySelector('tbody') : null;
nextRow = tbody ? tbody.rows[0] : null;
}
return nextRow && nextRow.cells[idx] ? clean(nextRow.cells[idx].innerText) : null;
}; };
// --- LÓGICA DE LIMPIEZA EN ORIGEN --- // 1. DESCRIPCIÓN QUIRÚRGICA
// Buscamos específicamente en la tabla de "Más información del servicio"
let rawDesc = "";
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);
}
// 1. DESCRIPCIÓN TÉCNICA (Corte por fecha) // Cortamos la descripción en la primera fecha detectada (historial)
let rawDesc = getH(["Descripción de la Reparación", "Descripción", "Daños"]) || "";
const idxDate = rawDesc.search(/\b\d{2}\/\d{2}\/\d{4}\b/); const idxDate = rawDesc.search(/\b\d{2}\/\d{2}\/\d{4}\b/);
const cleanDesc = idxDate !== -1 ? rawDesc.substring(0, idxDate).trim() : rawDesc; const cleanDesc = idxDate !== -1 ? rawDesc.substring(0, idxDate).trim() : rawDesc;
// 2. SEPARACIÓN DE C.P. Y POBLACIÓN // 2. SEPARACIÓN DE C.P. Y POBLACIÓN
const rawCPField = getV(["Distrito Postal", "C.P", "Distrito"]) || ""; const rawCPField = findByCab("Distrito Postal") || "";
const cpMatch = rawCPField.match(/\b\d{5}\b/); const cpMatch = rawCPField.match(/\b\d{5}\b/);
const cpOnly = cpMatch ? cpMatch[0] : ""; const cpOnly = cpMatch ? cpMatch[0] : "";
// Extraemos la población eliminando el CP y el guion
const popOnly = rawCPField.replace(cpOnly, '').replace('-', '').trim(); const popOnly = rawCPField.replace(cpOnly, '').replace('-', '').trim();
// 3. TELÉFONO LIMPIO (Solo 9 dígitos) // 3. TELÉFONO LIMPIO
const phoneMatch = body.match(/[6789]\d{8}/); const phoneMatch = body.match(/[6789]\d{8}/);
return { return {
"Expediente": getH(["Número Reparación", "Número de Servicio"]) || "", "Expediente": findByCab("Número Reparación") || "",
"Nombre Cliente": getV(["Nombre Cliente", "Asegurado"]) || "", "Nombre Cliente": findByCab("Nombre Cliente") || "",
"Dirección": getV(["Dirección", "Domicilio"]) || "", "Dirección": findByCab("Dirección") || "",
"Población": popOnly, // Nueva clave limpia "Población": popOnly,
"Código Postal": cpOnly, // Nueva clave limpia "Código Postal": cpOnly,
"Compañía": getH(["Procedencia", "Compañía"]) || "MULTI", "Compañía": "MULTI - " + (findByCab("Procedencia") || "MULTIASISTENCIA"),
"Descripción": cleanDesc, "Descripción": cleanDesc,
"Teléfono": phoneMatch ? phoneMatch[0] : "", "Teléfono": phoneMatch ? phoneMatch[0] : "",
"Urgente": getH(["Urgente"]) || "No" "Urgente": findByCab("Urgente") || "No"
}; };
}); });
if (scrapData && scrapData['Nombre Cliente']) break; if (scrapData && scrapData['Nombre Cliente']) break;