198 lines
9.2 KiB
JavaScript
198 lines
9.2 KiB
JavaScript
import { chromium } from 'playwright';
|
|
import pg from 'pg';
|
|
|
|
const { DATABASE_URL } = process.env;
|
|
|
|
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 HEADLESS = true;
|
|
|
|
async function main() {
|
|
console.log("🤖 ROBOT MODO: CIRUJANO + ASPIRADORA INTELIGENTE");
|
|
|
|
while (true) {
|
|
const client = await pool.connect();
|
|
try {
|
|
const res = await client.query("SELECT * FROM provider_credentials WHERE status = 'active'");
|
|
const credentials = res.rows;
|
|
|
|
for (const cred of credentials) {
|
|
let password = Buffer.from(cred.password_hash, 'base64').toString('utf-8');
|
|
console.log(`\n🔄 Procesando ${cred.provider.toUpperCase()}...`);
|
|
|
|
if (cred.provider === 'multiasistencia') {
|
|
await runMultiasistencia(cred.owner_id, cred.username, password);
|
|
} else if (cred.provider === 'homeserve') {
|
|
await runHomeserve(cred.owner_id, cred.username, password);
|
|
}
|
|
await client.query("UPDATE provider_credentials SET last_sync = NOW() WHERE id = $1", [cred.id]);
|
|
}
|
|
} catch (e) {
|
|
console.error("❌ Error ciclo:", e.message);
|
|
} finally {
|
|
client.release();
|
|
}
|
|
console.log("\n💤 Durmiendo 15 minutos...");
|
|
await new Promise(r => setTimeout(r, 15 * 60 * 1000));
|
|
}
|
|
}
|
|
|
|
// ==========================================
|
|
// 🏥 MULTIASISTENCIA V7 (EXTRACCIÓN POR PATRONES DE TEXTO)
|
|
// ==========================================
|
|
async function runMultiasistencia(ownerId, user, pass) {
|
|
const browser = await chromium.launch({ headless: HEADLESS, args: ['--no-sandbox'] });
|
|
const context = await browser.newContext();
|
|
const page = await context.newPage();
|
|
|
|
try {
|
|
console.log("🌍 [Multi] Accediendo...");
|
|
await page.goto('https://web.multiasistencia.com/w3multi/acceso.php', { timeout: 60000 });
|
|
|
|
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(5000);
|
|
}
|
|
|
|
await page.goto('https://web.multiasistencia.com/w3multi/frepasos_new.php?refresh=1', { waitUntil: 'domcontentloaded' });
|
|
|
|
const expedientes = await page.evaluate(() => {
|
|
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)));
|
|
});
|
|
|
|
console.log(`🔍 [Multi] Procesando ${expedientes.length} expedientes.`);
|
|
|
|
for (const ref of expedientes) {
|
|
await page.goto(`https://web.multiasistencia.com/w3multi/repasos1.php?reparacion=${ref}`, { waitUntil: 'domcontentloaded' });
|
|
|
|
const fullData = await page.evaluate(() => {
|
|
const text = document.body.innerText;
|
|
const data = {};
|
|
|
|
// Función para extraer texto entre dos palabras clave
|
|
const extract = (start, end) => {
|
|
const regex = new RegExp(`${start}\\s*:?\\s*([\\s\\S]*?)(?=${end}|$)`, 'i');
|
|
const match = text.match(regex);
|
|
return match ? match[1].trim() : null;
|
|
};
|
|
|
|
// CAPTURA POR BLOQUES DE TEXTO (IGNORANDO TABLAS)
|
|
data['Número Reparación'] = extract('Número Reparación', 'Número de Póliza');
|
|
data['Número de Póliza'] = extract('Número de Póliza', 'Nombre Cliente');
|
|
data['Nombre Cliente'] = extract('Nombre Cliente', 'Dirección');
|
|
data['Dirección'] = extract('Dirección', 'Baremo');
|
|
data['Distrito Postal'] = extract('Distrito Postal', 'Más información');
|
|
data['Procedencia'] = extract('Procedencia', 'Forma de pago');
|
|
data['Descripción'] = extract('Descripción de la Reparación', 'Urgente');
|
|
data['Tramitador'] = extract('Tramitador', 'Teléfono del Cliente');
|
|
data['Urgente'] = extract('Urgente', 'Fecha realización');
|
|
data['Fecha realización'] = extract('Fecha realización', 'Estado');
|
|
|
|
// Teléfonos (Regex de 9 dígitos limpia)
|
|
const phones = text.match(/[6789]\d{8}/g);
|
|
if (phones) data['phone_fixed'] = [...new Set(phones)].join(' / ');
|
|
|
|
// Limpieza final: si el valor capturado contiene el nombre de otra etiqueta, está mal
|
|
const labels = ["Número Reparación", "Dirección", "Nombre Cliente", "Urgente", "Estado"];
|
|
Object.keys(data).forEach(k => {
|
|
if (data[k]) {
|
|
// Cortamos si se nos ha colado la siguiente etiqueta
|
|
labels.forEach(l => {
|
|
if (data[k].includes(l)) data[k] = data[k].split(l)[0].trim();
|
|
});
|
|
// Quitamos saltos de línea molestos
|
|
data[k] = data[k].replace(/\n/g, ' ').replace(/\s+/g, ' ');
|
|
}
|
|
});
|
|
|
|
return data;
|
|
});
|
|
|
|
if (fullData && Object.keys(fullData).length > 2) {
|
|
await saveServiceToDB(ownerId, 'multiasistencia', ref, fullData);
|
|
}
|
|
}
|
|
} catch (e) {
|
|
console.error("❌ [Multi] Error Crítico:", e.message);
|
|
} finally {
|
|
await browser.close();
|
|
}
|
|
}
|
|
|
|
// ... (Mantén el resto del archivo igual)
|
|
// ==========================================
|
|
// 🧹 HOMESERVE (MANTENIDO)
|
|
// ==========================================
|
|
async function runHomeserve(ownerId, user, pass) {
|
|
const browser = await chromium.launch({ headless: HEADLESS, args: ['--no-sandbox'] });
|
|
const page = await browser.newPage();
|
|
try {
|
|
console.log("🌍 [HomeServe] Entrando...");
|
|
await page.goto('https://www.clientes.homeserve.es/cgi-bin/fccgi.exe?w3exec=PROF_PASS', { timeout: 60000 });
|
|
if (await page.isVisible('input[name="CODIGO"]')) {
|
|
await page.fill('input[name="CODIGO"]', user);
|
|
await page.fill('input[type="password"]', pass);
|
|
await page.keyboard.press('Enter');
|
|
await page.waitForTimeout(5000);
|
|
}
|
|
await page.goto('https://www.clientes.homeserve.es/cgi-bin/fccgi.exe?w3exec=lista_servicios_total');
|
|
const refs = await page.evaluate(() => {
|
|
const filas = Array.from(document.querySelectorAll('table tr'));
|
|
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.`);
|
|
for (const ref of refs) {
|
|
try {
|
|
await page.goto('https://www.clientes.homeserve.es/cgi-bin/fccgi.exe?w3exec=lista_servicios_total');
|
|
const link = await page.getByText(ref).first();
|
|
if (await link.isVisible()) {
|
|
await link.click(); await page.waitForTimeout(1500);
|
|
const fullData = await page.evaluate(() => {
|
|
const d = {};
|
|
const rows = Array.from(document.querySelectorAll('tr'));
|
|
rows.forEach(r => {
|
|
const cells = r.querySelectorAll('td');
|
|
if(cells.length >= 2) {
|
|
const k = cells[0].innerText.toUpperCase().trim().replace(':', '');
|
|
const v = cells[1].innerText.trim();
|
|
if(k.length > 1 && v.length > 0) d[k] = 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;
|
|
}
|
|
});
|
|
return d;
|
|
});
|
|
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) {
|
|
console.log(`💾 Guardando ${ref}...`);
|
|
await pool.query(`
|
|
INSERT INTO scraped_services (owner_id, provider, service_ref, raw_data, status)
|
|
VALUES ($1, $2, $3, $4, 'pending')
|
|
ON CONFLICT (owner_id, provider, service_ref)
|
|
DO UPDATE SET raw_data = EXCLUDED.raw_data, created_at = NOW()
|
|
`, [ownerId, provider, ref, JSON.stringify(data)]);
|
|
}
|
|
|
|
main(); |