Actualizar robot.js
This commit is contained in:
197
robot.js
197
robot.js
@@ -1,7 +1,6 @@
|
|||||||
import { chromium } from 'playwright';
|
import { chromium } from 'playwright';
|
||||||
import pg from 'pg';
|
import pg from 'pg';
|
||||||
|
|
||||||
// En Coolify las variables se inyectan solas
|
|
||||||
const { DATABASE_URL } = process.env;
|
const { DATABASE_URL } = process.env;
|
||||||
|
|
||||||
if (!DATABASE_URL) {
|
if (!DATABASE_URL) {
|
||||||
@@ -10,57 +9,41 @@ if (!DATABASE_URL) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const pool = new pg.Pool({ connectionString: DATABASE_URL, ssl: false });
|
const pool = new pg.Pool({ connectionString: DATABASE_URL, ssl: false });
|
||||||
|
const HEADLESS = true;
|
||||||
// CONFIGURACIÓN
|
|
||||||
const HEADLESS = true; // En servidor SIEMPRE true
|
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
console.log("🤖 INICIANDO ROBOT UNIFICADO (MEJORADO)...");
|
console.log("🤖 ROBOT MODO: ASPIRADORA INDUSTRIAL (CAPTURA TODO)");
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
const client = await pool.connect();
|
const client = await pool.connect();
|
||||||
try {
|
try {
|
||||||
// 1. Obtener credenciales activas
|
|
||||||
const res = await client.query("SELECT * FROM provider_credentials WHERE status = 'active'");
|
const res = await client.query("SELECT * FROM provider_credentials WHERE status = 'active'");
|
||||||
const credentials = res.rows;
|
const credentials = res.rows;
|
||||||
|
console.log(`📋 Cuentas activas: ${credentials.length}`);
|
||||||
console.log(`📋 Procesando ${credentials.length} cuentas de proveedores...`);
|
|
||||||
|
|
||||||
for (const cred of credentials) {
|
for (const cred of credentials) {
|
||||||
let password = "";
|
let password = Buffer.from(cred.password_hash, 'base64').toString('utf-8');
|
||||||
try {
|
console.log(`\n🔄 Escaneando ${cred.provider.toUpperCase()} (ID: ${cred.owner_id})...`);
|
||||||
password = Buffer.from(cred.password_hash, 'base64').toString('utf-8');
|
|
||||||
} catch (e) {
|
|
||||||
console.error(`Error decodificando pass para usuario ${cred.owner_id}`);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log(`\n🔄 Sincronizando ${cred.provider.toUpperCase()} para usuario ID ${cred.owner_id}...`);
|
|
||||||
|
|
||||||
if (cred.provider === 'multiasistencia') {
|
if (cred.provider === 'multiasistencia') {
|
||||||
await runMultiasistencia(cred.owner_id, cred.username, password);
|
await runMultiasistencia(cred.owner_id, cred.username, password);
|
||||||
} else if (cred.provider === 'homeserve') {
|
} else if (cred.provider === 'homeserve') {
|
||||||
await runHomeserve(cred.owner_id, cred.username, password);
|
await runHomeserve(cred.owner_id, cred.username, password);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Actualizar timestamp
|
|
||||||
await client.query("UPDATE provider_credentials SET last_sync = NOW() WHERE id = $1", [cred.id]);
|
await client.query("UPDATE provider_credentials SET last_sync = NOW() WHERE id = $1", [cred.id]);
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("❌ Error en ciclo principal:", e);
|
console.error("❌ Error ciclo:", e.message);
|
||||||
} finally {
|
} finally {
|
||||||
client.release();
|
client.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
// ESPERA DE 15 MINUTOS
|
|
||||||
console.log("\n💤 Durmiendo 15 minutos...");
|
console.log("\n💤 Durmiendo 15 minutos...");
|
||||||
await new Promise(r => setTimeout(r, 15 * 60 * 1000));
|
await new Promise(r => setTimeout(r, 15 * 60 * 1000));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ==========================================
|
// ==========================================
|
||||||
// 🛠️ ROBOT MULTIASISTENCIA (CORREGIDO)
|
// 🧹 MULTIASISTENCIA (EXTRACCIÓN TOTAL)
|
||||||
// ==========================================
|
// ==========================================
|
||||||
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'] });
|
||||||
@@ -68,10 +51,9 @@ async function runMultiasistencia(ownerId, user, pass) {
|
|||||||
const page = await context.newPage();
|
const page = await context.newPage();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
console.log("🌍 [Multi] Conectando...");
|
console.log("🌍 [Multi] Entrando...");
|
||||||
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
|
|
||||||
const userInput = await page.$('input[name="usuario"]') || await page.$('input[type="text"]');
|
const userInput = await page.$('input[name="usuario"]') || await page.$('input[type="text"]');
|
||||||
if(userInput) {
|
if(userInput) {
|
||||||
await userInput.fill(user);
|
await userInput.fill(user);
|
||||||
@@ -80,93 +62,67 @@ async function runMultiasistencia(ownerId, user, pass) {
|
|||||||
await page.waitForTimeout(4000);
|
await page.waitForTimeout(4000);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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' });
|
||||||
|
|
||||||
// Sacar expedientes
|
|
||||||
const expedientes = await page.evaluate(() => {
|
const expedientes = await page.evaluate(() => {
|
||||||
const links = Array.from(document.querySelectorAll('a[href*="reparacion="]'));
|
const links = Array.from(document.querySelectorAll('a[href*="reparacion="]'));
|
||||||
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] ${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' });
|
||||||
|
|
||||||
// --- NUEVA LÓGICA DE EXTRACCIÓN POR CELDAS ---
|
// --- LÓGICA AGRESIVA DE CAPTURA ---
|
||||||
const data = await page.evaluate(() => {
|
const fullData = await page.evaluate(() => {
|
||||||
|
const data = {};
|
||||||
|
|
||||||
// Función auxiliar para buscar en celdas vecinas
|
// 1. Recorrer TODAS las filas de TODAS las tablas
|
||||||
const findValueByHeader = (keywords) => {
|
const rows = document.querySelectorAll('tr');
|
||||||
const cells = Array.from(document.querySelectorAll('td, th'));
|
rows.forEach(row => {
|
||||||
for (const cell of cells) {
|
const cells = Array.from(row.querySelectorAll('td, th'));
|
||||||
const text = (cell.innerText || "").toUpperCase().trim();
|
// Intentamos emparejar: Celda 1 (Clave) -> Celda 2 (Valor)
|
||||||
// Si la celda contiene una de las palabras clave
|
for (let i = 0; i < cells.length - 1; i++) {
|
||||||
if (keywords.some(k => text.includes(k.toUpperCase()))) {
|
let key = cells[i].innerText.trim().replace(':', '');
|
||||||
// Devolvemos el texto de la celda SIGUIENTE
|
let val = cells[i+1]?.innerText.trim();
|
||||||
const nextCell = cell.nextElementSibling;
|
|
||||||
if (nextCell) return nextCell.innerText.trim();
|
// Filtros de limpieza básicos
|
||||||
|
if (key.length > 2 && key.length < 60 && val && val.length > 0) {
|
||||||
|
// Si la clave ya existe, no la machacamos (o le añadimos un indice)
|
||||||
|
if (!data[key]) {
|
||||||
|
data[key] = val;
|
||||||
|
// Saltamos la celda de valor para no leerla como clave en la siguiente vuelta
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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: 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) {
|
// Asegurar mínimos (fallback)
|
||||||
// Solo guardamos si tenemos al menos un nombre
|
if (!data.clientName) data.clientName = data["Nombre Cliente"] || data["Asegurado"] || "Desconocido";
|
||||||
await saveServiceToDB(ownerId, 'multiasistencia', ref, data);
|
if (!data.address) data.address = data["Dirección"] || data["Domicilio"] || data["Riesgo"] || "";
|
||||||
}
|
if (!data.phone) data.phone = (document.body.innerText.match(/[6789]\d{8}/) || [])[0] || "";
|
||||||
}
|
|
||||||
|
|
||||||
} catch (e) {
|
return data;
|
||||||
console.error("❌ [Multi] Error:", e.message);
|
});
|
||||||
} finally {
|
|
||||||
await browser.close();
|
if (fullData && Object.keys(fullData).length > 2) {
|
||||||
|
await saveServiceToDB(ownerId, 'multiasistencia', ref, fullData);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
} catch (e) { console.error("❌ [Multi]", e.message); } finally { await browser.close(); }
|
||||||
}
|
}
|
||||||
|
|
||||||
// ==========================================
|
// ==========================================
|
||||||
// 🛠️ ROBOT HOMESERVE (MANTENIDO)
|
// 🧹 HOMESERVE (EXTRACCIÓN TOTAL)
|
||||||
// ==========================================
|
// ==========================================
|
||||||
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'] });
|
||||||
const page = await browser.newPage();
|
const page = await browser.newPage();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
console.log("🌍 [HomeServe] Conectando...");
|
console.log("🌍 [HomeServe] Entrando...");
|
||||||
await page.goto('https://www.clientes.homeserve.es/cgi-bin/fccgi.exe?w3exec=PROF_PASS', { timeout: 60000 });
|
await page.goto('https://www.clientes.homeserve.es/cgi-bin/fccgi.exe?w3exec=PROF_PASS', { timeout: 60000 });
|
||||||
|
|
||||||
if (await page.isVisible('input[name="CODIGO"]')) {
|
if (await page.isVisible('input[name="CODIGO"]')) {
|
||||||
@@ -180,60 +136,65 @@ async function runHomeserve(ownerId, user, pass) {
|
|||||||
|
|
||||||
const refs = await page.evaluate(() => {
|
const refs = await page.evaluate(() => {
|
||||||
const filas = Array.from(document.querySelectorAll('table tr'));
|
const filas = Array.from(document.querySelectorAll('table tr'));
|
||||||
return filas.map(tr => tr.querySelector('td')?.innerText.trim()).filter(t => /^\d{4,}$/.test(t));
|
const found = [];
|
||||||
|
filas.forEach(tr => {
|
||||||
|
const txt = tr.innerText;
|
||||||
|
const match = txt.match(/(\d{6,10})/);
|
||||||
|
if (match) found.push(match[1]);
|
||||||
|
});
|
||||||
|
return [...new Set(found)];
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log(`🔍 [HomeServe] ${refs.length} expedientes encontrados.`);
|
console.log(`🔍 [HomeServe] ${refs.length} expedientes.`);
|
||||||
|
|
||||||
for (const ref of refs) {
|
for (const ref of refs) {
|
||||||
try {
|
try {
|
||||||
await page.goto('https://www.clientes.homeserve.es/cgi-bin/fccgi.exe?w3exec=lista_servicios_total');
|
await page.goto('https://www.clientes.homeserve.es/cgi-bin/fccgi.exe?w3exec=lista_servicios_total');
|
||||||
await page.click(`text="${ref}"`, { timeout: 5000 });
|
const link = await page.getByText(ref).first();
|
||||||
await page.waitForTimeout(1000);
|
if (await link.isVisible()) {
|
||||||
|
await link.click();
|
||||||
|
await page.waitForTimeout(1500);
|
||||||
|
|
||||||
const data = await page.evaluate(() => {
|
const fullData = await page.evaluate(() => {
|
||||||
const rows = Array.from(document.querySelectorAll('tr'));
|
|
||||||
const d = {};
|
const d = {};
|
||||||
|
// HomeServe suele ser tablas limpias: TR -> TD (Clave) | TD (Valor)
|
||||||
|
const rows = Array.from(document.querySelectorAll('tr'));
|
||||||
rows.forEach(r => {
|
rows.forEach(r => {
|
||||||
const cells = r.querySelectorAll('td');
|
const cells = r.querySelectorAll('td');
|
||||||
if(cells.length > 1) {
|
// Si tiene al menos 2 celdas
|
||||||
const k = cells[0].innerText.toUpperCase();
|
if(cells.length >= 2) {
|
||||||
const v = cells[1].innerText;
|
const k = cells[0].innerText.toUpperCase().trim().replace(':', '');
|
||||||
if(k.includes("CLIENTE")) d.clientName = v;
|
const v = cells[1].innerText.trim();
|
||||||
if(k.includes("DOMICILIO")) d.address = v;
|
if(k.length > 1 && v.length > 0) d[k] = v;
|
||||||
if(k.includes("POBLACION")) d.address += ", " + v;
|
}
|
||||||
if(k.includes("TELEFONOS")) d.phone = v;
|
// A veces son tablas de 4 columnas (Clave | Valor | Clave | Valor)
|
||||||
if(k.includes("COMENTARIOS") || k.includes("AVERIA")) d.description = v;
|
if(cells.length >= 4) {
|
||||||
|
const k2 = cells[2].innerText.toUpperCase().trim().replace(':', '');
|
||||||
|
const v2 = cells[3].innerText.trim();
|
||||||
|
if(k2.length > 1 && v2.length > 0) d[k2] = v2;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Mapeo mínimo
|
||||||
|
d.clientName = d["CLIENTE"] || d["ASEGURADO"] || "Desconocido";
|
||||||
|
d.phone = d["TELEFONOS"] || d["MOVIL"] || "";
|
||||||
|
d.address = d["DOMICILIO"] || d["DIRECCION"] || "";
|
||||||
|
if(d["POBLACION"]) d.address += ", " + d["POBLACION"];
|
||||||
|
|
||||||
return d;
|
return d;
|
||||||
});
|
});
|
||||||
|
|
||||||
if (data.clientName) {
|
if (fullData) {
|
||||||
await saveServiceToDB(ownerId, 'homeserve', ref, data);
|
await saveServiceToDB(ownerId, 'homeserve', ref, fullData);
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (e) { console.error(`Error leyendo ${ref}: ${e.message}`); }
|
|
||||||
}
|
}
|
||||||
|
} catch (errRef) { console.error(`⚠️ Error ref ${ref}`); }
|
||||||
} catch (e) {
|
|
||||||
console.error("❌ [HomeServe] Error:", e.message);
|
|
||||||
} finally {
|
|
||||||
await browser.close();
|
|
||||||
}
|
}
|
||||||
|
} catch (e) { console.error("❌ [HomeServe]", e.message); } finally { await browser.close(); }
|
||||||
}
|
}
|
||||||
|
|
||||||
// ==========================================
|
|
||||||
// 💾 GUARDADO EN BASE DE DATOS
|
|
||||||
// ==========================================
|
|
||||||
async function saveServiceToDB(ownerId, provider, ref, data) {
|
async function saveServiceToDB(ownerId, provider, ref, data) {
|
||||||
// Verificar si ya existe en servicios reales (para no duplicar trabajo)
|
console.log(`💾 Guardando ${ref} con ${Object.keys(data).length} variables encontradas.`);
|
||||||
const exists = await pool.query("SELECT id FROM services WHERE company_ref = $1 AND owner_id = $2", [ref, ownerId]);
|
|
||||||
if (exists.rowCount > 0) return;
|
|
||||||
|
|
||||||
// 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')
|
||||||
|
|||||||
Reference in New Issue
Block a user