Actualizar robot.js
This commit is contained in:
82
robot.js
82
robot.js
@@ -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,58 +70,51 @@ 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);
|
||||||
|
if (!found) return null;
|
||||||
|
|
||||||
// 2. DICCIONARIO DE ETIQUETAS "ANCLA"
|
// 1. Probar con el siguiente elemento hermano
|
||||||
const keywords = [
|
if (found.nextElementSibling) return found.nextElementSibling.innerText.trim();
|
||||||
{ 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 => {
|
// 2. Probar con la celda de al lado (si es una tabla)
|
||||||
// Buscamos el elemento que contenga el texto exacto del campo
|
if (found.tagName === 'TD' && found.parentElement) {
|
||||||
const found = allElements.find(el => el.innerText.trim() === item.search);
|
const cells = Array.from(found.parentElement.children);
|
||||||
if (found) {
|
const index = cells.indexOf(found);
|
||||||
// El valor suele estar en el siguiente elemento hermano
|
if (cells[index + 1]) return cells[index + 1].innerText.trim();
|
||||||
let val = "";
|
|
||||||
if (found.nextElementSibling) {
|
|
||||||
val = found.nextElementSibling.innerText.trim();
|
|
||||||
} else if (found.parentElement && found.parentElement.nextElementSibling) {
|
|
||||||
// O en la celda de al lado
|
|
||||||
val = found.parentElement.nextElementSibling.innerText.trim();
|
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
// Si lo que encontramos no es el mismo nombre de la etiqueta, lo guardamos
|
// Mapeo directo de etiquetas reales de la web
|
||||||
if (val && val !== item.search) {
|
data['Número Reparación'] = findValueAfter('Número Reparación');
|
||||||
data[item.search] = val;
|
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');
|
||||||
|
|
||||||
// 3. CAPTURA ESPECIAL DE TELÉFONOS (CIRUJANO)
|
// 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)
|
||||||
// ==========================================
|
// ==========================================
|
||||||
|
|||||||
Reference in New Issue
Block a user