Actualizar robot.js
This commit is contained in:
105
robot.js
105
robot.js
@@ -12,18 +12,17 @@ const pool = new pg.Pool({ connectionString: DATABASE_URL, ssl: false });
|
|||||||
const HEADLESS = true;
|
const HEADLESS = true;
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
console.log("🤖 ROBOT MODO: ASPIRADORA INDUSTRIAL (CAPTURA TODO)");
|
console.log("🤖 ROBOT MODO: CIRUJANO + ASPIRADORA INTELIGENTE");
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
const client = await pool.connect();
|
const client = await pool.connect();
|
||||||
try {
|
try {
|
||||||
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}`);
|
|
||||||
|
|
||||||
for (const cred of credentials) {
|
for (const cred of credentials) {
|
||||||
let password = Buffer.from(cred.password_hash, 'base64').toString('utf-8');
|
let password = Buffer.from(cred.password_hash, 'base64').toString('utf-8');
|
||||||
console.log(`\n🔄 Escaneando ${cred.provider.toUpperCase()} (ID: ${cred.owner_id})...`);
|
console.log(`\n🔄 Procesando ${cred.provider.toUpperCase()}...`);
|
||||||
|
|
||||||
if (cred.provider === 'multiasistencia') {
|
if (cred.provider === 'multiasistencia') {
|
||||||
await runMultiasistencia(cred.owner_id, cred.username, password);
|
await runMultiasistencia(cred.owner_id, cred.username, password);
|
||||||
@@ -43,7 +42,7 @@ async function main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ==========================================
|
// ==========================================
|
||||||
// 🧹 MULTIASISTENCIA (EXTRACCIÓN TOTAL)
|
// 🏥 MULTIASISTENCIA V3 (CIRUJANO)
|
||||||
// ==========================================
|
// ==========================================
|
||||||
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'] });
|
||||||
@@ -51,7 +50,7 @@ async function runMultiasistencia(ownerId, user, pass) {
|
|||||||
const page = await context.newPage();
|
const page = await context.newPage();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
console.log("🌍 [Multi] Entrando...");
|
console.log("🌍 [Multi] Conectando...");
|
||||||
await page.goto('https://web.multiasistencia.com/w3multi/acceso.php', { timeout: 60000 });
|
await page.goto('https://web.multiasistencia.com/w3multi/acceso.php', { timeout: 60000 });
|
||||||
|
|
||||||
const userInput = await page.$('input[name="usuario"]') || await page.$('input[type="text"]');
|
const userInput = await page.$('input[name="usuario"]') || await page.$('input[type="text"]');
|
||||||
@@ -69,45 +68,71 @@ 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.`);
|
console.log(`🔍 [Multi] ${expedientes.length} expedientes a analizar.`);
|
||||||
|
|
||||||
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' });
|
||||||
|
|
||||||
// --- LÓGICA AGRESIVA DE CAPTURA ---
|
|
||||||
const fullData = await page.evaluate(() => {
|
const fullData = await page.evaluate(() => {
|
||||||
const data = {};
|
const data = {};
|
||||||
|
const bodyText = document.body.innerText;
|
||||||
|
|
||||||
// 1. Recorrer TODAS las filas de TODAS las tablas
|
// --- 1. CIRUJANO (Extracción manual de lo importante) ---
|
||||||
|
// Intentamos sacar la dirección de la cabecera típica de Multi
|
||||||
|
const boldElements = Array.from(document.querySelectorAll('b, strong, .titulo'));
|
||||||
|
|
||||||
|
// Buscamos nombre: Suele estar cerca de "Asegurado" o es un texto en mayúsculas aislado
|
||||||
|
let clientNameCandidate = "";
|
||||||
|
// Buscamos dirección: Suele contener "CL", "AV", "PZ"
|
||||||
|
let addressCandidate = "";
|
||||||
|
|
||||||
|
// Estrategia por palabras clave vecinas (más seguro)
|
||||||
|
const findNeighbor = (keywords) => {
|
||||||
|
const cells = Array.from(document.querySelectorAll('td'));
|
||||||
|
for (let i = 0; i < cells.length; i++) {
|
||||||
|
const txt = cells[i].innerText.toUpperCase();
|
||||||
|
if (keywords.some(k => txt.includes(k))) {
|
||||||
|
// Devolvemos el texto de la siguiente celda que no esté vacía
|
||||||
|
for(let j=1; j<=3; j++) {
|
||||||
|
if(cells[i+j] && cells[i+j].innerText.trim().length > 2) return cells[i+j].innerText.trim();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
};
|
||||||
|
|
||||||
|
data.clientName_fixed = findNeighbor(['ASEGURADO', 'NOMBRE CLIENTE', 'CONTACTO']);
|
||||||
|
data.address_fixed = findNeighbor(['DIRECCIÓN', 'DOMICILIO', 'RIESGO', 'UBICACIÓN']);
|
||||||
|
data.phone_fixed = (bodyText.match(/[6789]\d{8}/) || [])[0] || "";
|
||||||
|
data.description_fixed = findNeighbor(['DESCRIPCIÓN', 'DAÑOS', 'AVERÍA']);
|
||||||
|
|
||||||
|
// --- 2. ASPIRADORA INTELIGENTE (Tablas) ---
|
||||||
const rows = document.querySelectorAll('tr');
|
const rows = document.querySelectorAll('tr');
|
||||||
rows.forEach(row => {
|
rows.forEach(row => {
|
||||||
const cells = Array.from(row.querySelectorAll('td, th'));
|
const cells = Array.from(row.querySelectorAll('td'));
|
||||||
// Intentamos emparejar: Celda 1 (Clave) -> Celda 2 (Valor)
|
|
||||||
for (let i = 0; i < cells.length - 1; i++) {
|
for (let i = 0; i < cells.length - 1; i++) {
|
||||||
let key = cells[i].innerText.trim().replace(':', '');
|
let key = cells[i].innerText.trim().replace(':', '');
|
||||||
let val = cells[i+1]?.innerText.trim();
|
let val = cells[i+1]?.innerText.trim();
|
||||||
|
|
||||||
// Filtros de limpieza básicos
|
// FILTRO DE SEGURIDAD PARA ETIQUETAS
|
||||||
if (key.length > 2 && key.length < 60 && val && val.length > 0) {
|
// 1. La clave no puede ser larguísima (eso es una descripción o dirección)
|
||||||
// Si la clave ya existe, no la machacamos (o le añadimos un indice)
|
if (key.length > 35) continue;
|
||||||
if (!data[key]) {
|
// 2. La clave no puede contener dígitos (ej: "28001" no es una clave)
|
||||||
data[key] = val;
|
if (/\d/.test(key) && key.length > 10) continue;
|
||||||
// Saltamos la celda de valor para no leerla como clave en la siguiente vuelta
|
// 3. La clave no debe empezar por tipo de vía
|
||||||
i++;
|
if (/^(CL|AV|PZ|UR|CJ)\s/.test(key.toUpperCase())) continue;
|
||||||
}
|
|
||||||
|
if (key.length > 2 && val && val.length > 0) {
|
||||||
|
// Guardamos normalizando la clave
|
||||||
|
data[key] = val;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Asegurar mínimos (fallback)
|
|
||||||
if (!data.clientName) data.clientName = data["Nombre Cliente"] || data["Asegurado"] || "Desconocido";
|
|
||||||
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] || "";
|
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
});
|
});
|
||||||
|
|
||||||
if (fullData && Object.keys(fullData).length > 2) {
|
if (fullData) {
|
||||||
await saveServiceToDB(ownerId, 'multiasistencia', ref, fullData);
|
await saveServiceToDB(ownerId, 'multiasistencia', ref, fullData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -115,78 +140,56 @@ async function runMultiasistencia(ownerId, user, pass) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ==========================================
|
// ==========================================
|
||||||
// 🧹 HOMESERVE (EXTRACCIÓN TOTAL)
|
// 🧹 HOMESERVE (SE MANTIENE IGUAL, FUNCIONABA BIEN)
|
||||||
// ==========================================
|
// ==========================================
|
||||||
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] Entrando...");
|
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"]')) {
|
||||||
await page.fill('input[name="CODIGO"]', user);
|
await page.fill('input[name="CODIGO"]', user);
|
||||||
await page.fill('input[type="password"]', pass);
|
await page.fill('input[type="password"]', pass);
|
||||||
await page.keyboard.press('Enter');
|
await page.keyboard.press('Enter');
|
||||||
await page.waitForTimeout(5000);
|
await page.waitForTimeout(5000);
|
||||||
}
|
}
|
||||||
|
|
||||||
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');
|
||||||
|
|
||||||
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'));
|
||||||
const found = [];
|
const found = [];
|
||||||
filas.forEach(tr => {
|
filas.forEach(tr => {
|
||||||
const txt = tr.innerText;
|
const txt = tr.innerText; const match = txt.match(/(\d{6,10})/);
|
||||||
const match = txt.match(/(\d{6,10})/);
|
|
||||||
if (match) found.push(match[1]);
|
if (match) found.push(match[1]);
|
||||||
});
|
});
|
||||||
return [...new Set(found)];
|
return [...new Set(found)];
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log(`🔍 [HomeServe] ${refs.length} expedientes.`);
|
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');
|
||||||
const link = await page.getByText(ref).first();
|
const link = await page.getByText(ref).first();
|
||||||
if (await link.isVisible()) {
|
if (await link.isVisible()) {
|
||||||
await link.click();
|
await link.click(); await page.waitForTimeout(1500);
|
||||||
await page.waitForTimeout(1500);
|
|
||||||
|
|
||||||
const fullData = await page.evaluate(() => {
|
const fullData = await page.evaluate(() => {
|
||||||
const d = {};
|
const d = {};
|
||||||
// HomeServe suele ser tablas limpias: TR -> TD (Clave) | TD (Valor)
|
|
||||||
const rows = Array.from(document.querySelectorAll('tr'));
|
const rows = Array.from(document.querySelectorAll('tr'));
|
||||||
rows.forEach(r => {
|
rows.forEach(r => {
|
||||||
const cells = r.querySelectorAll('td');
|
const cells = r.querySelectorAll('td');
|
||||||
// Si tiene al menos 2 celdas
|
|
||||||
if(cells.length >= 2) {
|
if(cells.length >= 2) {
|
||||||
const k = cells[0].innerText.toUpperCase().trim().replace(':', '');
|
const k = cells[0].innerText.toUpperCase().trim().replace(':', '');
|
||||||
const v = cells[1].innerText.trim();
|
const v = cells[1].innerText.trim();
|
||||||
if(k.length > 1 && v.length > 0) d[k] = v;
|
if(k.length > 1 && v.length > 0) d[k] = v;
|
||||||
}
|
}
|
||||||
// A veces son tablas de 4 columnas (Clave | Valor | Clave | Valor)
|
|
||||||
if(cells.length >= 4) {
|
if(cells.length >= 4) {
|
||||||
const k2 = cells[2].innerText.toUpperCase().trim().replace(':', '');
|
const k2 = cells[2].innerText.toUpperCase().trim().replace(':', '');
|
||||||
const v2 = cells[3].innerText.trim();
|
const v2 = cells[3].innerText.trim();
|
||||||
if(k2.length > 1 && v2.length > 0) d[k2] = v2;
|
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 (fullData) await saveServiceToDB(ownerId, 'homeserve', ref, fullData);
|
||||||
if (fullData) {
|
|
||||||
await saveServiceToDB(ownerId, 'homeserve', ref, fullData);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (errRef) { console.error(`⚠️ Error ref ${ref}`); }
|
} catch (errRef) { console.error(`⚠️ Error ref ${ref}`); }
|
||||||
}
|
}
|
||||||
@@ -194,7 +197,7 @@ async function runHomeserve(ownerId, user, pass) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function saveServiceToDB(ownerId, provider, ref, data) {
|
async function saveServiceToDB(ownerId, provider, ref, data) {
|
||||||
console.log(`💾 Guardando ${ref} con ${Object.keys(data).length} variables encontradas.`);
|
console.log(`💾 Guardando ${ref}...`);
|
||||||
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