Actualizar robot.js
This commit is contained in:
55
robot.js
55
robot.js
@@ -67,6 +67,7 @@ async function runMultiasistencia(ownerId, user, pass) {
|
||||
|
||||
let scrapData = null;
|
||||
|
||||
// RECORRIDO DE FRAMES + EXTRACCIÓN POR TABLAS
|
||||
for (const frame of page.frames()) {
|
||||
try {
|
||||
scrapData = await frame.evaluate(() => {
|
||||
@@ -74,52 +75,46 @@ async function runMultiasistencia(ownerId, user, pass) {
|
||||
const body = document.body?.innerText || "";
|
||||
if (!body.includes("Nombre Cliente") && !body.includes("Asegurado")) return null;
|
||||
|
||||
const allCells = Array.from(document.querySelectorAll('td, th, b, div'));
|
||||
const getH = (keys) => {
|
||||
const header = allCells.find(el => keys.some(k => el.innerText.toUpperCase().includes(k.toUpperCase())));
|
||||
return header && header.nextElementSibling ? clean(header.nextElementSibling.innerText) : null;
|
||||
// Seleccionamos todas las celdas de cabecera y detalle
|
||||
const cabeceras = Array.from(document.querySelectorAll('.tcab'));
|
||||
const detalles = Array.from(document.querySelectorAll('.tdet'));
|
||||
|
||||
const findByCab = (texto) => {
|
||||
const idx = cabeceras.findIndex(el => el.innerText.includes(texto));
|
||||
return idx !== -1 && detalles[idx] ? clean(detalles[idx].innerText) : null;
|
||||
};
|
||||
const getV = (keys) => {
|
||||
const header = allCells.find(el => keys.some(k => el.innerText.trim().toUpperCase() === k.toUpperCase()));
|
||||
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;
|
||||
|
||||
// 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);
|
||||
}
|
||||
return nextRow && nextRow.cells[idx] ? clean(nextRow.cells[idx].innerText) : null;
|
||||
};
|
||||
|
||||
// --- LÓGICA DE LIMPIEZA EN ORIGEN ---
|
||||
|
||||
// 1. DESCRIPCIÓN TÉCNICA (Corte por fecha)
|
||||
let rawDesc = getH(["Descripción de la Reparación", "Descripción", "Daños"]) || "";
|
||||
// Cortamos la descripción en la primera fecha detectada (historial)
|
||||
const idxDate = rawDesc.search(/\b\d{2}\/\d{2}\/\d{4}\b/);
|
||||
const cleanDesc = idxDate !== -1 ? rawDesc.substring(0, idxDate).trim() : rawDesc;
|
||||
|
||||
// 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 cpOnly = cpMatch ? cpMatch[0] : "";
|
||||
// Extraemos la población eliminando el CP y el guion
|
||||
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}/);
|
||||
|
||||
return {
|
||||
"Expediente": getH(["Número Reparación", "Número de Servicio"]) || "",
|
||||
"Nombre Cliente": getV(["Nombre Cliente", "Asegurado"]) || "",
|
||||
"Dirección": getV(["Dirección", "Domicilio"]) || "",
|
||||
"Población": popOnly, // Nueva clave limpia
|
||||
"Código Postal": cpOnly, // Nueva clave limpia
|
||||
"Compañía": getH(["Procedencia", "Compañía"]) || "MULTI",
|
||||
"Expediente": findByCab("Número Reparación") || "",
|
||||
"Nombre Cliente": findByCab("Nombre Cliente") || "",
|
||||
"Dirección": findByCab("Dirección") || "",
|
||||
"Población": popOnly,
|
||||
"Código Postal": cpOnly,
|
||||
"Compañía": "MULTI - " + (findByCab("Procedencia") || "MULTIASISTENCIA"),
|
||||
"Descripción": cleanDesc,
|
||||
"Teléfono": phoneMatch ? phoneMatch[0] : "",
|
||||
"Urgente": getH(["Urgente"]) || "No"
|
||||
"Urgente": findByCab("Urgente") || "No"
|
||||
};
|
||||
});
|
||||
if (scrapData && scrapData['Nombre Cliente']) break;
|
||||
|
||||
Reference in New Issue
Block a user