/** * benchmark-communication.js — Veille communication concurrents (sites web + LinkedIn) * * Collecte : * 1. Sites web : fetch de la page d'accueil et pages offres — extraction du texte clé * 2. LinkedIn : génère des URLs de profil pour consultation manuelle (pas d'API publique) * * Prérequis : * - knowledge/shared/competitors.json rempli * * Usage : * node scripts/benchmark-communication.js [livinweb|livinia|all] * * Output : * knowledge/benchmarks/benchmark-communication-YYYY-WXX.md * * Note LinkedIn : * LinkedIn bloque le scraping automatisé. Ce script génère un fichier avec : * - Les URLs des profils à consulter * - Un formulaire de collecte manuelle que l'agent analyse ensuite * Pour une collecte automatisée, voir https://apify.com/apify/linkedin-company-posts */ require('dotenv').config({ path: require('path').resolve(__dirname, '../.env') }); const https = require('https'); const http = require('http'); const fs = require('fs'); const path = require('path'); const url = require('url'); const COMPETITORS_FILE = path.resolve(__dirname, '../knowledge/shared/competitors.json'); const BENCHMARKS_DIR = path.resolve(__dirname, '../knowledge/benchmarks'); function getISOWeek(date) { const d = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate())); const dayNum = d.getUTCDay() || 7; d.setUTCDate(d.getUTCDate() + 4 - dayNum); const yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1)); return { year: d.getUTCFullYear(), week: Math.ceil((((d - yearStart) / 86400000) + 1) / 7), }; } function fetchPage(targetUrl, maxRedirects = 3) { return new Promise((resolve) => { const parsedUrl = url.parse(targetUrl); const client = parsedUrl.protocol === 'https:' ? https : http; const options = { hostname: parsedUrl.hostname, path: parsedUrl.path || '/', method: 'GET', headers: { 'User-Agent': 'Mozilla/5.0 (compatible; BenchmarkBot/1.0; +https://livinweb.fr)', 'Accept': 'text/html', 'Accept-Language': 'fr-FR,fr;q=0.9', }, timeout: 8000, }; const req = client.request(options, (res) => { // Suivre les redirections if ([301, 302, 303, 307, 308].includes(res.statusCode) && res.headers.location && maxRedirects > 0) { const redirectUrl = res.headers.location.startsWith('http') ? res.headers.location : `${parsedUrl.protocol}//${parsedUrl.hostname}${res.headers.location}`; resolve(fetchPage(redirectUrl, maxRedirects - 1)); return; } let data = ''; res.on('data', (chunk) => { data += chunk; }); res.on('end', () => resolve({ status: res.statusCode, html: data })); }); req.on('error', (err) => resolve({ status: 0, html: '', error: err.message })); req.on('timeout', () => { req.destroy(); resolve({ status: 0, html: '', error: 'timeout' }); }); req.end(); }); } function extractTextFromHtml(html) { // Suppression des balises script, style, nav, footer let text = html .replace(/]*>[\s\S]*?<\/script>/gi, ' ') .replace(/]*>[\s\S]*?<\/style>/gi, ' ') .replace(/]*>[\s\S]*?<\/nav>/gi, ' ') .replace(/]*>[\s\S]*?<\/footer>/gi, ' ') .replace(/]*>[\s\S]*?<\/header>/gi, ' '); // Extraction des titres h1/h2/h3 et paragraphes const headings = []; const hMatches = text.matchAll(/]*>([\s\S]*?)<\/h[123]>/gi); for (const m of hMatches) { const h = m[1].replace(/<[^>]+>/g, '').replace(/\s+/g, ' ').trim(); if (h.length > 3) headings.push(h); } const paras = []; const pMatches = text.matchAll(/]*>([\s\S]*?)<\/p>/gi); for (const m of pMatches) { const p = m[1].replace(/<[^>]+>/g, '').replace(/\s+/g, ' ').trim(); if (p.length > 20) paras.push(p); } return { headings: headings.slice(0, 10), paragraphs: paras.slice(0, 8), }; } async function analyzeWebsite(competitor) { const siteUrl = competitor.site; if (!siteUrl) return null; console.log(` 🌐 Scraping site : ${siteUrl}`); const result = await fetchPage(siteUrl); if (result.error || result.status === 0) { return { url: siteUrl, error: result.error || `HTTP ${result.status}` }; } if (result.status >= 400) { return { url: siteUrl, error: `HTTP ${result.status}` }; } const extracted = extractTextFromHtml(result.html); return { url: siteUrl, status: result.status, headings: extracted.headings, paragraphs: extracted.paragraphs, }; } function formatWebsiteAnalysis(analysis, competitorName) { if (!analysis) return '_Site non configuré._'; if (analysis.error) return `> ❌ Erreur : ${analysis.error}`; const lines = []; if (analysis.headings.length > 0) { lines.push('**Titres détectés :**'); analysis.headings.forEach(h => lines.push(`- ${h}`)); } if (analysis.paragraphs.length > 0) { lines.push(''); lines.push('**Extraits de contenu :**'); analysis.paragraphs.slice(0, 4).forEach(p => lines.push(`> ${p.slice(0, 200)}`)); } return lines.join('\n') || '_Contenu non extractible._'; } async function run() { const brand = process.argv[2] || 'all'; if (!fs.existsSync(COMPETITORS_FILE)) { console.error('❌ Fichier competitors.json introuvable. Remplir knowledge/shared/competitors.json d\'abord.'); process.exit(1); } const competitors = JSON.parse(fs.readFileSync(COMPETITORS_FILE, 'utf8')); const { year, week } = getISOWeek(new Date()); const weekLabel = `${year}-W${String(week).padStart(2, '0')}`; const outputFile = path.join(BENCHMARKS_DIR, `benchmark-communication-${weekLabel}.md`); const brandsToProcess = brand === 'all' ? ['livinweb', 'livinia'] : [brand]; const lines = [ `---`, `generated_date: "${new Date().toISOString().split('T')[0]}"`, `week: "${weekLabel}"`, `workflow: "benchmark-communication"`, `status: "draft"`, `---`, ``, `# Benchmark Communication — ${weekLabel}`, ``, `> Généré automatiquement par \`benchmark-communication.js\``, `> Sites : scraping automatique | LinkedIn : collecte manuelle (voir section dédiée)`, `> À analyser par l'agent benchmark-communication lors du workflow 6`, ``, ]; for (const b of brandsToProcess) { const list = (competitors[b] || []).filter(c => c.active); lines.push(`## ${b.charAt(0).toUpperCase() + b.slice(1)}`); lines.push(''); if (list.length === 0) { lines.push(`_Aucun concurrent actif configuré pour ${b}. Remplir competitors.json._`); lines.push(''); continue; } for (const competitor of list) { console.log(`\n🔍 Analyse de ${competitor.name}...`); lines.push(`### ${competitor.name}`); lines.push(''); // Site web lines.push('#### Site web'); const websiteData = await analyzeWebsite(competitor); lines.push(formatWebsiteAnalysis(websiteData, competitor.name)); lines.push(''); // LinkedIn — collecte manuelle guidée lines.push('#### LinkedIn'); if (competitor.linkedin_url) { lines.push(`**URL :** [${competitor.linkedin_url}](${competitor.linkedin_url})`); lines.push(''); lines.push('> 📋 **Collecte manuelle requise** — LinkedIn bloque le scraping automatisé.'); lines.push('> Visiter le profil et remplir les champs ci-dessous :'); lines.push(''); lines.push('| Champ | Observation |'); lines.push('|-------|-------------|'); lines.push('| Derniers posts (thèmes) | _à remplir_ |'); lines.push('| Fréquence de publication | _à remplir_ |'); lines.push('| Post avec le plus d\'engagement | _à remplir_ |'); lines.push('| Angle/message dominant | _à remplir_ |'); lines.push('| Changements récents (profil, bannière) | _à remplir_ |'); } else { lines.push('_URL LinkedIn non configurée dans competitors.json._'); } lines.push(''); if (competitor.notes) { lines.push(`_Notes : ${competitor.notes}_`); lines.push(''); } } } lines.push('---'); lines.push(''); lines.push('## Analyse agent — À compléter'); lines.push(''); lines.push('> Section remplie par l\'agent benchmark-communication lors du workflow 6'); lines.push(''); lines.push('- **Thèmes LinkedIn dominants chez les concurrents cette semaine :**'); lines.push('- **Changements de positionnement détectés sur les sites :**'); lines.push('- **Nouvelles offres ou angles de communication :**'); lines.push('- **Opportunités différenciantes pour Livinweb/Livinia :**'); fs.mkdirSync(BENCHMARKS_DIR, { recursive: true }); fs.writeFileSync(outputFile, lines.join('\n'), 'utf8'); console.log(`\n✅ Rapport communication généré : ${outputFile}`); console.log(` LinkedIn : sections manuelles à remplir dans le fichier`); console.log(` Semaine : ${weekLabel}`); } run().catch(err => { console.error('❌ Erreur fatale :', err.message); process.exit(1); });