Actualizar robot.js
This commit is contained in:
75
robot.js
75
robot.js
@@ -1,7 +1,7 @@
|
||||
import { chromium } from 'playwright';
|
||||
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;
|
||||
|
||||
if (!DATABASE_URL) {
|
||||
@@ -15,7 +15,7 @@ const pool = new pg.Pool({ connectionString: DATABASE_URL, ssl: false });
|
||||
const HEADLESS = true; // En servidor SIEMPRE true
|
||||
|
||||
async function main() {
|
||||
console.log("🤖 INICIANDO ROBOT UNIFICADO (MULTI + HOMESERVE)...");
|
||||
console.log("🤖 INICIANDO ROBOT UNIFICADO (MEJORADO)...");
|
||||
|
||||
while (true) {
|
||||
const client = await pool.connect();
|
||||
@@ -27,7 +27,6 @@ async function main() {
|
||||
console.log(`📋 Procesando ${credentials.length} cuentas de proveedores...`);
|
||||
|
||||
for (const cred of credentials) {
|
||||
// Desencriptar password (asumiendo base64 simple por ahora)
|
||||
let password = "";
|
||||
try {
|
||||
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) {
|
||||
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 });
|
||||
|
||||
// Login
|
||||
await page.fill('input[name="usuario"]', user);
|
||||
const userInput = await page.$('input[name="usuario"]') || await page.$('input[type="text"]');
|
||||
if(userInput) {
|
||||
await userInput.fill(user);
|
||||
await page.fill('input[type="password"]', pass);
|
||||
await page.click('input[type="submit"]');
|
||||
await page.waitForTimeout(4000);
|
||||
}
|
||||
|
||||
// Ir a listado
|
||||
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) {
|
||||
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 body = document.body.innerText;
|
||||
const getVal = (k) => {
|
||||
const regex = new RegExp(`${k}\\s*[:\\-]?\\s*([^\\n]+)`, 'i');
|
||||
return body.match(regex)?.[1]?.trim() || "";
|
||||
|
||||
// Función auxiliar para buscar en celdas vecinas
|
||||
const findValueByHeader = (keywords) => {
|
||||
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 {
|
||||
clientName: getVal("Nombre Cliente") || getVal("Asegurado"),
|
||||
address: getVal("Dirección") || getVal("Domicilio"),
|
||||
phone: (body.match(/[6789]\d{8}/) || [])[0] || "",
|
||||
description: getVal("Descripción")
|
||||
clientName: findValueByHeader(['Nombre Cliente', 'Asegurado']) || "Desconocido",
|
||||
address: fullAddress || "Sin dirección detectada",
|
||||
phone: (document.body.innerText.match(/[6789]\d{8}/) || [])[0] || "",
|
||||
description: findDescription()
|
||||
};
|
||||
});
|
||||
|
||||
if (data.clientName) {
|
||||
// Solo guardamos si tenemos al menos un nombre
|
||||
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) {
|
||||
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;
|
||||
if(k.includes("CLIENTE")) d.clientName = 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("COMENTARIOS")) d.description = v;
|
||||
if(k.includes("COMENTARIOS") || k.includes("AVERIA")) d.description = v;
|
||||
}
|
||||
});
|
||||
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]);
|
||||
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(`
|
||||
INSERT INTO scraped_services (owner_id, provider, service_ref, raw_data, status)
|
||||
VALUES ($1, $2, $3, $4, 'pending')
|
||||
|
||||
Reference in New Issue
Block a user