Actualizar robot.js

This commit is contained in:
2026-02-13 08:25:06 +00:00
parent d08c190b3a
commit 17bea49598

View File

@@ -1,7 +1,7 @@
import { chromium } from 'playwright'; import { chromium } from 'playwright';
import pg from 'pg'; import pg from 'pg';
// En Coolify las variables se inyectan solas, pero mantenemos esto por si pruebas en local // En Coolify las variables se inyectan solas
const { DATABASE_URL } = process.env; const { DATABASE_URL } = process.env;
if (!DATABASE_URL) { if (!DATABASE_URL) {
@@ -15,7 +15,7 @@ const pool = new pg.Pool({ connectionString: DATABASE_URL, ssl: false });
const HEADLESS = true; // En servidor SIEMPRE true const HEADLESS = true; // En servidor SIEMPRE true
async function main() { async function main() {
console.log("🤖 INICIANDO ROBOT UNIFICADO (MULTI + HOMESERVE)..."); console.log("🤖 INICIANDO ROBOT UNIFICADO (MEJORADO)...");
while (true) { while (true) {
const client = await pool.connect(); const client = await pool.connect();
@@ -27,7 +27,6 @@ async function main() {
console.log(`📋 Procesando ${credentials.length} cuentas de proveedores...`); console.log(`📋 Procesando ${credentials.length} cuentas de proveedores...`);
for (const cred of credentials) { for (const cred of credentials) {
// Desencriptar password (asumiendo base64 simple por ahora)
let password = ""; let password = "";
try { try {
password = Buffer.from(cred.password_hash, 'base64').toString('utf-8'); password = Buffer.from(cred.password_hash, 'base64').toString('utf-8');
@@ -61,7 +60,7 @@ async function main() {
} }
// ========================================== // ==========================================
// 🛠️ ROBOT MULTIASISTENCIA // 🛠️ ROBOT MULTIASISTENCIA (CORREGIDO)
// ========================================== // ==========================================
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: HEADLESS, args: ['--no-sandbox'] });
@@ -73,10 +72,13 @@ async function runMultiasistencia(ownerId, user, pass) {
await page.goto('https://web.multiasistencia.com/w3multi/acceso.php', { timeout: 60000 }); await page.goto('https://web.multiasistencia.com/w3multi/acceso.php', { timeout: 60000 });
// Login // Login
await page.fill('input[name="usuario"]', user); const userInput = await page.$('input[name="usuario"]') || await page.$('input[type="text"]');
await page.fill('input[type="password"]', pass); if(userInput) {
await page.click('input[type="submit"]'); await userInput.fill(user);
await page.waitForTimeout(4000); await page.fill('input[type="password"]', pass);
await page.click('input[type="submit"]');
await page.waitForTimeout(4000);
}
// Ir a listado // Ir a listado
await page.goto('https://web.multiasistencia.com/w3multi/frepasos_new.php?refresh=1', { waitUntil: 'domcontentloaded' }); await page.goto('https://web.multiasistencia.com/w3multi/frepasos_new.php?refresh=1', { waitUntil: 'domcontentloaded' });
@@ -92,21 +94,59 @@ async function runMultiasistencia(ownerId, user, pass) {
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' });
// --- NUEVA LÓGICA DE EXTRACCIÓN POR CELDAS ---
const data = await page.evaluate(() => { const data = await page.evaluate(() => {
const body = document.body.innerText;
const getVal = (k) => { // Función auxiliar para buscar en celdas vecinas
const regex = new RegExp(`${k}\\s*[:\\-]?\\s*([^\\n]+)`, 'i'); const findValueByHeader = (keywords) => {
return body.match(regex)?.[1]?.trim() || ""; const cells = Array.from(document.querySelectorAll('td, th'));
for (const cell of cells) {
const text = (cell.innerText || "").toUpperCase().trim();
// Si la celda contiene una de las palabras clave
if (keywords.some(k => text.includes(k.toUpperCase()))) {
// Devolvemos el texto de la celda SIGUIENTE
const nextCell = cell.nextElementSibling;
if (nextCell) return nextCell.innerText.trim();
}
}
return "";
}; };
// Función auxiliar para descripción (busca bloques de texto grandes)
const findDescription = () => {
// Intento 1: Buscar celda "Descripción"
let desc = findValueByHeader(['Descripción', 'Daños', 'Solicitud']);
if(desc) return desc;
// Intento 2: Buscar en el cuerpo general si falla la tabla
const body = document.body.innerText;
const match = body.match(/Descripción\s*[:\-]?\s*([^\n]+)/i);
return match ? match[1].trim() : "";
}
// Extracción más precisa
let address = findValueByHeader(['Dirección', 'Domicilio', 'Riesgo']);
let pob = findValueByHeader(['Población', 'Localidad']);
let cp = findValueByHeader(['C.P.', 'Postal']);
// Limpieza de dirección (quitar "Baremo" si se cuela)
if (address.includes("Baremo")) address = address.split("Baremo")[0].trim();
// Unir dirección completa
let fullAddress = address;
if (pob) fullAddress += `, ${pob}`;
if (cp) fullAddress += ` (${cp})`;
return { return {
clientName: getVal("Nombre Cliente") || getVal("Asegurado"), clientName: findValueByHeader(['Nombre Cliente', 'Asegurado']) || "Desconocido",
address: getVal("Dirección") || getVal("Domicilio"), address: fullAddress || "Sin dirección detectada",
phone: (body.match(/[6789]\d{8}/) || [])[0] || "", phone: (document.body.innerText.match(/[6789]\d{8}/) || [])[0] || "",
description: getVal("Descripción") description: findDescription()
}; };
}); });
if (data.clientName) { if (data.clientName) {
// Solo guardamos si tenemos al menos un nombre
await saveServiceToDB(ownerId, 'multiasistencia', ref, data); await saveServiceToDB(ownerId, 'multiasistencia', ref, data);
} }
} }
@@ -119,7 +159,7 @@ async function runMultiasistencia(ownerId, user, pass) {
} }
// ========================================== // ==========================================
// 🛠️ ROBOT HOMESERVE // 🛠️ ROBOT HOMESERVE (MANTENIDO)
// ========================================== // ==========================================
async function runHomeserve(ownerId, user, pass) { async function runHomeserve(ownerId, user, pass) {
const browser = await chromium.launch({ headless: HEADLESS, args: ['--no-sandbox'] }); const browser = await chromium.launch({ headless: HEADLESS, args: ['--no-sandbox'] });
@@ -161,8 +201,9 @@ async function runHomeserve(ownerId, user, pass) {
const v = cells[1].innerText; const v = cells[1].innerText;
if(k.includes("CLIENTE")) d.clientName = v; if(k.includes("CLIENTE")) d.clientName = v;
if(k.includes("DOMICILIO")) d.address = v; if(k.includes("DOMICILIO")) d.address = v;
if(k.includes("POBLACION")) d.address += ", " + v;
if(k.includes("TELEFONOS")) d.phone = v; if(k.includes("TELEFONOS")) d.phone = v;
if(k.includes("COMENTARIOS")) d.description = v; if(k.includes("COMENTARIOS") || k.includes("AVERIA")) d.description = v;
} }
}); });
return d; return d;
@@ -190,7 +231,9 @@ async function saveServiceToDB(ownerId, provider, ref, data) {
const exists = await pool.query("SELECT id FROM services WHERE company_ref = $1 AND owner_id = $2", [ref, ownerId]); const exists = await pool.query("SELECT id FROM services WHERE company_ref = $1 AND owner_id = $2", [ref, ownerId]);
if (exists.rowCount > 0) return; if (exists.rowCount > 0) return;
console.log(`💾 Guardando en buzón: ${ref}`); // Verificar si ya existe en buzón, si existe ACTUALIZAMOS los datos (por si hemos mejorado el scraping)
console.log(`💾 Guardando/Actualizando: ${ref} (${data.address})`);
await pool.query(` await pool.query(`
INSERT INTO scraped_services (owner_id, provider, service_ref, raw_data, status) INSERT INTO scraped_services (owner_id, provider, service_ref, raw_data, status)
VALUES ($1, $2, $3, $4, 'pending') VALUES ($1, $2, $3, $4, 'pending')