Actualizar robot.js
This commit is contained in:
163
robot.js
163
robot.js
@@ -2,28 +2,20 @@ import { chromium } from 'playwright';
|
|||||||
import pg from 'pg';
|
import pg from 'pg';
|
||||||
|
|
||||||
const { DATABASE_URL } = process.env;
|
const { DATABASE_URL } = process.env;
|
||||||
|
if (!DATABASE_URL) { console.error("❌ Error: No hay DATABASE_URL."); process.exit(1); }
|
||||||
if (!DATABASE_URL) {
|
|
||||||
console.error("❌ Error: No hay DATABASE_URL definida.");
|
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
const pool = new pg.Pool({ connectionString: DATABASE_URL, ssl: false });
|
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: CIRUJANO + ASPIRADORA INTELIGENTE");
|
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;
|
for (const cred of res.rows) {
|
||||||
|
|
||||||
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🔄 Procesando ${cred.provider.toUpperCase()}...`);
|
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);
|
||||||
} else if (cred.provider === 'homeserve') {
|
} else if (cred.provider === 'homeserve') {
|
||||||
@@ -31,80 +23,58 @@ async function main() {
|
|||||||
}
|
}
|
||||||
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 ciclo:", e.message); }
|
||||||
console.error("❌ Error ciclo:", e.message);
|
finally { client.release(); }
|
||||||
} finally {
|
|
||||||
client.release();
|
|
||||||
}
|
|
||||||
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ==========================================
|
||||||
|
// 🏥 MULTIASISTENCIA (FUNCIONANDO)
|
||||||
|
// ==========================================
|
||||||
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'] });
|
||||||
const context = await browser.newContext();
|
const context = await browser.newContext();
|
||||||
const page = await context.newPage();
|
const page = await context.newPage();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await page.goto('https://web.multiasistencia.com/w3multi/acceso.php', { timeout: 60000 });
|
await page.goto('https://web.multiasistencia.com/w3multi/acceso.php', { timeout: 60000 });
|
||||||
await page.fill('input[name="usuario"]', user);
|
await page.fill('input[name="usuario"]', user);
|
||||||
await page.fill('input[type="password"]', pass);
|
await page.fill('input[type="password"]', pass);
|
||||||
await page.click('input[type="submit"]');
|
await page.click('input[type="submit"]');
|
||||||
await page.waitForTimeout(4000);
|
await page.waitForTimeout(4000);
|
||||||
|
|
||||||
await page.goto('https://web.multiasistencia.com/w3multi/frepasos_new.php?refresh=1');
|
await page.goto('https://web.multiasistencia.com/w3multi/frepasos_new.php?refresh=1');
|
||||||
await page.waitForTimeout(2000);
|
await page.waitForTimeout(2000);
|
||||||
|
|
||||||
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)));
|
||||||
});
|
});
|
||||||
|
|
||||||
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' });
|
||||||
await page.waitForTimeout(1500);
|
await page.waitForTimeout(1500);
|
||||||
|
|
||||||
let scrapData = null;
|
let scrapData = null;
|
||||||
|
|
||||||
// RECORRIDO DE FRAMES + EXTRACCIÓN POR TABLAS
|
|
||||||
for (const frame of page.frames()) {
|
for (const frame of page.frames()) {
|
||||||
try {
|
try {
|
||||||
scrapData = await frame.evaluate(() => {
|
scrapData = await frame.evaluate(() => {
|
||||||
const clean = (t) => t ? t.replace(/\s+/g, ' ').trim() : "";
|
const clean = (t) => t ? t.replace(/\s+/g, ' ').trim() : "";
|
||||||
const body = document.body?.innerText || "";
|
const body = document.body?.innerText || "";
|
||||||
if (!body.includes("Nombre Cliente") && !body.includes("Asegurado")) return null;
|
if (!body.includes("Nombre Cliente") && !body.includes("Asegurado")) return null;
|
||||||
|
|
||||||
// Seleccionamos todas las celdas de cabecera y detalle
|
|
||||||
const cabeceras = Array.from(document.querySelectorAll('.tcab'));
|
const cabeceras = Array.from(document.querySelectorAll('.tcab'));
|
||||||
const detalles = Array.from(document.querySelectorAll('.tdet'));
|
const detalles = Array.from(document.querySelectorAll('.tdet'));
|
||||||
|
|
||||||
const findByCab = (texto) => {
|
const findByCab = (texto) => {
|
||||||
const idx = cabeceras.findIndex(el => el.innerText.includes(texto));
|
const idx = cabeceras.findIndex(el => el.innerText.includes(texto));
|
||||||
return idx !== -1 && detalles[idx] ? clean(detalles[idx].innerText) : null;
|
return idx !== -1 && detalles[idx] ? clean(detalles[idx].innerText) : null;
|
||||||
};
|
};
|
||||||
|
|
||||||
// 1. DESCRIPCIÓN QUIRÚRGICA
|
|
||||||
// Buscamos específicamente en la tabla de "Más información del servicio"
|
|
||||||
let rawDesc = "";
|
let rawDesc = "";
|
||||||
const descHeader = Array.from(document.querySelectorAll('td.tcab')).find(td => td.innerText.includes("Descripción de la Reparación"));
|
const descHeader = Array.from(document.querySelectorAll('td.tcab')).find(td => td.innerText.includes("Descripción de la Reparación"));
|
||||||
if (descHeader && descHeader.nextElementSibling) {
|
if (descHeader && descHeader.nextElementSibling) rawDesc = clean(descHeader.nextElementSibling.innerText);
|
||||||
rawDesc = clean(descHeader.nextElementSibling.innerText);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Cortamos la descripción en la primera fecha detectada (historial)
|
|
||||||
const idxDate = rawDesc.search(/\b\d{2}\/\d{2}\/\d{4}\b/);
|
const idxDate = rawDesc.search(/\b\d{2}\/\d{2}\/\d{4}\b/);
|
||||||
const cleanDesc = idxDate !== -1 ? rawDesc.substring(0, idxDate).trim() : rawDesc;
|
const cleanDesc = idxDate !== -1 ? rawDesc.substring(0, idxDate).trim() : rawDesc;
|
||||||
|
|
||||||
// 2. SEPARACIÓN DE C.P. Y POBLACIÓN
|
|
||||||
const rawCPField = findByCab("Distrito Postal") || "";
|
const rawCPField = findByCab("Distrito Postal") || "";
|
||||||
const cpMatch = rawCPField.match(/\b\d{5}\b/);
|
const cpMatch = rawCPField.match(/\b\d{5}\b/);
|
||||||
const cpOnly = cpMatch ? cpMatch[0] : "";
|
const cpOnly = cpMatch ? cpMatch[0] : "";
|
||||||
const popOnly = rawCPField.replace(cpOnly, '').replace('-', '').trim();
|
const popOnly = rawCPField.replace(cpOnly, '').replace('-', '').trim();
|
||||||
|
|
||||||
// 3. TELÉFONO LIMPIO
|
|
||||||
const phoneMatch = body.match(/[6789]\d{8}/);
|
const phoneMatch = body.match(/[6789]\d{8}/);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"Expediente": findByCab("Número Reparación") || "",
|
"Expediente": findByCab("Número Reparación") || "",
|
||||||
"Nombre Cliente": findByCab("Nombre Cliente") || "",
|
"Nombre Cliente": findByCab("Nombre Cliente") || "",
|
||||||
@@ -120,97 +90,98 @@ async function runMultiasistencia(ownerId, user, pass) {
|
|||||||
if (scrapData && scrapData['Nombre Cliente']) break;
|
if (scrapData && scrapData['Nombre Cliente']) break;
|
||||||
} catch (e) { continue; }
|
} catch (e) { continue; }
|
||||||
}
|
}
|
||||||
|
if (scrapData && scrapData['Nombre Cliente']) await saveServiceToDB(ownerId, 'multiasistencia', ref, scrapData);
|
||||||
if (scrapData && scrapData['Nombre Cliente']) {
|
|
||||||
await saveServiceToDB(ownerId, 'multiasistencia', ref, scrapData);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (e) { console.error("❌ Error Multi:", e.message); }
|
} catch (e) { console.error("❌ Error Multi:", e.message); } finally { await browser.close(); }
|
||||||
finally { await browser.close(); }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ==========================================
|
||||||
|
// 🧹 HOMESERVE (CORREGIDO)
|
||||||
|
// ==========================================
|
||||||
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 });
|
||||||
|
|
||||||
|
// Login
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Navegar al listado real (Usando la URL de tu log)
|
||||||
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.waitForTimeout(3000);
|
||||||
|
|
||||||
|
// EXTRAER LISTA DE EXPEDIENTES
|
||||||
const refs = await page.evaluate(() => {
|
const refs = await page.evaluate(() => {
|
||||||
const filas = Array.from(document.querySelectorAll('table tr'));
|
const cells = Array.from(document.querySelectorAll('td'));
|
||||||
const found = [];
|
const found = [];
|
||||||
filas.forEach(tr => {
|
cells.forEach(td => {
|
||||||
const txt = tr.innerText; const match = txt.match(/(\d{6,10})/);
|
const match = td.innerText.trim().match(/^15\d{6}$/); // Refs de HomeServe suelen ser 15XXXXXX
|
||||||
if (match) found.push(match[1]);
|
if (match) found.push(match[0]);
|
||||||
});
|
});
|
||||||
return [...new Set(found)];
|
return [...new Set(found)];
|
||||||
});
|
});
|
||||||
console.log(`🔍 [HomeServe] ${refs.length} expedientes.`);
|
|
||||||
|
console.log(`🔍 [HomeServe] ${refs.length} expedientes detectados.`);
|
||||||
|
|
||||||
for (const ref of refs) {
|
for (const ref of refs) {
|
||||||
try {
|
// Ir al detalle directo
|
||||||
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=ver_servicioencurso&Servicio=${ref}`);
|
||||||
const link = await page.getByText(ref).first();
|
await page.waitForTimeout(2000);
|
||||||
if (await link.isVisible()) {
|
|
||||||
await link.click(); await page.waitForTimeout(1500);
|
|
||||||
// ... dentro de la función runHomeserve, en el evaluate del detalle:
|
|
||||||
|
|
||||||
// ... dentro de la función runHomeserve ...
|
const scrapData = await page.evaluate(() => {
|
||||||
|
const d = {};
|
||||||
|
const clean = (t) => t ? t.replace(/\s+/g, ' ').trim() : "";
|
||||||
|
const rows = Array.from(document.querySelectorAll('tr'));
|
||||||
|
|
||||||
const fullData = await page.evaluate(() => {
|
rows.forEach(r => {
|
||||||
const d = {};
|
const cells = r.querySelectorAll('td');
|
||||||
const clean = (t) => t ? t.replace(/\s+/g, ' ').trim() : "";
|
if (cells.length >= 2) {
|
||||||
|
const k = cells[0].innerText.toUpperCase().trim().replace(':', '');
|
||||||
|
const v = cells[1].innerText.trim();
|
||||||
|
|
||||||
// 1. EXTRAER CAMPOS NORMALES (CLIENTE, DOMICILIO, ETC.)
|
if (k.includes("COMENTARIOS")) {
|
||||||
const rows = Array.from(document.querySelectorAll('tr'));
|
const txt = cells[1].querySelector('textarea')?.value || "";
|
||||||
rows.forEach(r => {
|
// Filtro quirúrgico de descripción
|
||||||
const cells = r.querySelectorAll('td');
|
const cleanDesc = txt.split('\n').filter(line => {
|
||||||
if (cells.length >= 2) {
|
const l = line.toUpperCase();
|
||||||
const k = cells[0].innerText.toUpperCase().trim().replace(':', '');
|
return !["CAMBIO DE ESTADO", "ESTADO ASIGNADO", "SMS NO ENVIADO", "CONTACTO CON PROF", "0000"].some(b => l.includes(b));
|
||||||
const v = cells[1].innerText.trim();
|
}).slice(0, 3).join('\n').trim();
|
||||||
|
d['Descripción'] = cleanDesc;
|
||||||
|
} else if (k.length > 1 && v.length > 0 && !k.includes("MENU")) {
|
||||||
|
d[k] = v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// 2. CAPTURA ESPECIAL DEL CUADRO DE COMENTARIOS (LA DESCRIPCIÓN)
|
// Separar Población y CP de HomeServe
|
||||||
if (k.includes("COMENTARIOS")) {
|
const rawPop = d['POBLACION-PROVINCIA'] || "";
|
||||||
const textarea = cells[1].querySelector('textarea');
|
const cpMatch = rawPop.match(/\b\d{5}\b/);
|
||||||
if (textarea) {
|
d['Código Postal'] = cpMatch ? cpMatch[0] : "";
|
||||||
const lines = textarea.value.split('\n');
|
d['Población'] = rawPop.replace(d['Código Postal'], '').replace('-', '').trim();
|
||||||
|
d['Compañía'] = "HOME - " + (d['COMPAÑIA'] || "HOMESERVE");
|
||||||
|
d['Nombre Cliente'] = d['CLIENTE'] || "";
|
||||||
|
d['Dirección'] = d['DOMICILIO'] || "";
|
||||||
|
|
||||||
// Filtro inteligente para quitar basura de sistema
|
return d;
|
||||||
const cleanLines = lines.filter(line => {
|
});
|
||||||
const l = line.toUpperCase();
|
|
||||||
if (l.includes("CAMBIO DE ESTADO")) return false;
|
|
||||||
if (l.includes("ESTADO ASIGNADO")) return false;
|
|
||||||
if (l.includes("SMS NO ENVIADO")) return false;
|
|
||||||
if (l.includes("CONTACTO CON PROF")) return false;
|
|
||||||
if (l.includes("tipo de cobro es banco")) return false;
|
|
||||||
if (l.includes("0000")) return false;
|
|
||||||
return true;
|
|
||||||
});
|
|
||||||
|
|
||||||
// Guardamos las primeras líneas útiles como "Descripción"
|
if (scrapData && scrapData['Nombre Cliente']) {
|
||||||
d['Descripción'] = cleanLines.slice(0, 3).join('\n').trim();
|
await saveServiceToDB(ownerId, 'homeserve', ref, scrapData);
|
||||||
}
|
|
||||||
} else if (k.length > 1 && v.length > 0 && !k.includes("MENU")) {
|
|
||||||
d[k] = v;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
} catch (e) { console.error("❌ [HomeServe] Error:", e.message); }
|
||||||
return d;
|
finally { await browser.close(); }
|
||||||
});
|
|
||||||
if (fullData) await saveServiceToDB(ownerId, 'homeserve', ref, fullData);
|
|
||||||
}
|
|
||||||
} catch (errRef) { console.error(`⚠️ Error ref ${ref}`); }
|
|
||||||
}
|
|
||||||
} catch (e) { console.error("❌ [HomeServe]", e.message); } finally { await browser.close(); }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function saveServiceToDB(ownerId, provider, ref, data) {
|
async function saveServiceToDB(ownerId, provider, ref, data) {
|
||||||
console.log(`💾 Guardando ${ref}...`);
|
console.log(`💾 Guardando ${provider.toUpperCase()} ${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