benchmark-communication.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /**
  2. * benchmark-communication.js — Veille communication concurrents (sites web + LinkedIn)
  3. *
  4. * Collecte :
  5. * 1. Sites web : fetch de la page d'accueil et pages offres — extraction du texte clé
  6. * 2. LinkedIn : génère des URLs de profil pour consultation manuelle (pas d'API publique)
  7. *
  8. * Prérequis :
  9. * - knowledge/shared/competitors.json rempli
  10. *
  11. * Usage :
  12. * node scripts/benchmark-communication.js [livinweb|livinia|all]
  13. *
  14. * Output :
  15. * knowledge/benchmarks/benchmark-communication-YYYY-WXX.md
  16. *
  17. * Note LinkedIn :
  18. * LinkedIn bloque le scraping automatisé. Ce script génère un fichier avec :
  19. * - Les URLs des profils à consulter
  20. * - Un formulaire de collecte manuelle que l'agent analyse ensuite
  21. * Pour une collecte automatisée, voir https://apify.com/apify/linkedin-company-posts
  22. */
  23. require('dotenv').config({ path: require('path').resolve(__dirname, '../.env') });
  24. const https = require('https');
  25. const http = require('http');
  26. const fs = require('fs');
  27. const path = require('path');
  28. const url = require('url');
  29. const COMPETITORS_FILE = path.resolve(__dirname, '../knowledge/shared/competitors.json');
  30. const BENCHMARKS_DIR = path.resolve(__dirname, '../knowledge/benchmarks');
  31. function getISOWeek(date) {
  32. const d = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()));
  33. const dayNum = d.getUTCDay() || 7;
  34. d.setUTCDate(d.getUTCDate() + 4 - dayNum);
  35. const yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
  36. return {
  37. year: d.getUTCFullYear(),
  38. week: Math.ceil((((d - yearStart) / 86400000) + 1) / 7),
  39. };
  40. }
  41. function fetchPage(targetUrl, maxRedirects = 3) {
  42. return new Promise((resolve) => {
  43. const parsedUrl = url.parse(targetUrl);
  44. const client = parsedUrl.protocol === 'https:' ? https : http;
  45. const options = {
  46. hostname: parsedUrl.hostname,
  47. path: parsedUrl.path || '/',
  48. method: 'GET',
  49. headers: {
  50. 'User-Agent': 'Mozilla/5.0 (compatible; BenchmarkBot/1.0; +https://livinweb.fr)',
  51. 'Accept': 'text/html',
  52. 'Accept-Language': 'fr-FR,fr;q=0.9',
  53. },
  54. timeout: 8000,
  55. };
  56. const req = client.request(options, (res) => {
  57. // Suivre les redirections
  58. if ([301, 302, 303, 307, 308].includes(res.statusCode) && res.headers.location && maxRedirects > 0) {
  59. const redirectUrl = res.headers.location.startsWith('http')
  60. ? res.headers.location
  61. : `${parsedUrl.protocol}//${parsedUrl.hostname}${res.headers.location}`;
  62. resolve(fetchPage(redirectUrl, maxRedirects - 1));
  63. return;
  64. }
  65. let data = '';
  66. res.on('data', (chunk) => { data += chunk; });
  67. res.on('end', () => resolve({ status: res.statusCode, html: data }));
  68. });
  69. req.on('error', (err) => resolve({ status: 0, html: '', error: err.message }));
  70. req.on('timeout', () => { req.destroy(); resolve({ status: 0, html: '', error: 'timeout' }); });
  71. req.end();
  72. });
  73. }
  74. function extractTextFromHtml(html) {
  75. // Suppression des balises script, style, nav, footer
  76. let text = html
  77. .replace(/<script[^>]*>[\s\S]*?<\/script>/gi, ' ')
  78. .replace(/<style[^>]*>[\s\S]*?<\/style>/gi, ' ')
  79. .replace(/<nav[^>]*>[\s\S]*?<\/nav>/gi, ' ')
  80. .replace(/<footer[^>]*>[\s\S]*?<\/footer>/gi, ' ')
  81. .replace(/<header[^>]*>[\s\S]*?<\/header>/gi, ' ');
  82. // Extraction des titres h1/h2/h3 et paragraphes
  83. const headings = [];
  84. const hMatches = text.matchAll(/<h[123][^>]*>([\s\S]*?)<\/h[123]>/gi);
  85. for (const m of hMatches) {
  86. const h = m[1].replace(/<[^>]+>/g, '').replace(/\s+/g, ' ').trim();
  87. if (h.length > 3) headings.push(h);
  88. }
  89. const paras = [];
  90. const pMatches = text.matchAll(/<p[^>]*>([\s\S]*?)<\/p>/gi);
  91. for (const m of pMatches) {
  92. const p = m[1].replace(/<[^>]+>/g, '').replace(/\s+/g, ' ').trim();
  93. if (p.length > 20) paras.push(p);
  94. }
  95. return {
  96. headings: headings.slice(0, 10),
  97. paragraphs: paras.slice(0, 8),
  98. };
  99. }
  100. async function analyzeWebsite(competitor) {
  101. const siteUrl = competitor.site;
  102. if (!siteUrl) return null;
  103. console.log(` 🌐 Scraping site : ${siteUrl}`);
  104. const result = await fetchPage(siteUrl);
  105. if (result.error || result.status === 0) {
  106. return { url: siteUrl, error: result.error || `HTTP ${result.status}` };
  107. }
  108. if (result.status >= 400) {
  109. return { url: siteUrl, error: `HTTP ${result.status}` };
  110. }
  111. const extracted = extractTextFromHtml(result.html);
  112. return {
  113. url: siteUrl,
  114. status: result.status,
  115. headings: extracted.headings,
  116. paragraphs: extracted.paragraphs,
  117. };
  118. }
  119. function formatWebsiteAnalysis(analysis, competitorName) {
  120. if (!analysis) return '_Site non configuré._';
  121. if (analysis.error) return `> ❌ Erreur : ${analysis.error}`;
  122. const lines = [];
  123. if (analysis.headings.length > 0) {
  124. lines.push('**Titres détectés :**');
  125. analysis.headings.forEach(h => lines.push(`- ${h}`));
  126. }
  127. if (analysis.paragraphs.length > 0) {
  128. lines.push('');
  129. lines.push('**Extraits de contenu :**');
  130. analysis.paragraphs.slice(0, 4).forEach(p => lines.push(`> ${p.slice(0, 200)}`));
  131. }
  132. return lines.join('\n') || '_Contenu non extractible._';
  133. }
  134. async function run() {
  135. const brand = process.argv[2] || 'all';
  136. if (!fs.existsSync(COMPETITORS_FILE)) {
  137. console.error('❌ Fichier competitors.json introuvable. Remplir knowledge/shared/competitors.json d\'abord.');
  138. process.exit(1);
  139. }
  140. const competitors = JSON.parse(fs.readFileSync(COMPETITORS_FILE, 'utf8'));
  141. const { year, week } = getISOWeek(new Date());
  142. const weekLabel = `${year}-W${String(week).padStart(2, '0')}`;
  143. const outputFile = path.join(BENCHMARKS_DIR, `benchmark-communication-${weekLabel}.md`);
  144. const brandsToProcess = brand === 'all'
  145. ? ['livinweb', 'livinia']
  146. : [brand];
  147. const lines = [
  148. `---`,
  149. `generated_date: "${new Date().toISOString().split('T')[0]}"`,
  150. `week: "${weekLabel}"`,
  151. `workflow: "benchmark-communication"`,
  152. `status: "draft"`,
  153. `---`,
  154. ``,
  155. `# Benchmark Communication — ${weekLabel}`,
  156. ``,
  157. `> Généré automatiquement par \`benchmark-communication.js\``,
  158. `> Sites : scraping automatique | LinkedIn : collecte manuelle (voir section dédiée)`,
  159. `> À analyser par l'agent benchmark-communication lors du workflow 6`,
  160. ``,
  161. ];
  162. for (const b of brandsToProcess) {
  163. const list = (competitors[b] || []).filter(c => c.active);
  164. lines.push(`## ${b.charAt(0).toUpperCase() + b.slice(1)}`);
  165. lines.push('');
  166. if (list.length === 0) {
  167. lines.push(`_Aucun concurrent actif configuré pour ${b}. Remplir competitors.json._`);
  168. lines.push('');
  169. continue;
  170. }
  171. for (const competitor of list) {
  172. console.log(`\n🔍 Analyse de ${competitor.name}...`);
  173. lines.push(`### ${competitor.name}`);
  174. lines.push('');
  175. // Site web
  176. lines.push('#### Site web');
  177. const websiteData = await analyzeWebsite(competitor);
  178. lines.push(formatWebsiteAnalysis(websiteData, competitor.name));
  179. lines.push('');
  180. // LinkedIn — collecte manuelle guidée
  181. lines.push('#### LinkedIn');
  182. if (competitor.linkedin_url) {
  183. lines.push(`**URL :** [${competitor.linkedin_url}](${competitor.linkedin_url})`);
  184. lines.push('');
  185. lines.push('> 📋 **Collecte manuelle requise** — LinkedIn bloque le scraping automatisé.');
  186. lines.push('> Visiter le profil et remplir les champs ci-dessous :');
  187. lines.push('');
  188. lines.push('| Champ | Observation |');
  189. lines.push('|-------|-------------|');
  190. lines.push('| Derniers posts (thèmes) | _à remplir_ |');
  191. lines.push('| Fréquence de publication | _à remplir_ |');
  192. lines.push('| Post avec le plus d\'engagement | _à remplir_ |');
  193. lines.push('| Angle/message dominant | _à remplir_ |');
  194. lines.push('| Changements récents (profil, bannière) | _à remplir_ |');
  195. } else {
  196. lines.push('_URL LinkedIn non configurée dans competitors.json._');
  197. }
  198. lines.push('');
  199. if (competitor.notes) {
  200. lines.push(`_Notes : ${competitor.notes}_`);
  201. lines.push('');
  202. }
  203. }
  204. }
  205. lines.push('---');
  206. lines.push('');
  207. lines.push('## Analyse agent — À compléter');
  208. lines.push('');
  209. lines.push('> Section remplie par l\'agent benchmark-communication lors du workflow 6');
  210. lines.push('');
  211. lines.push('- **Thèmes LinkedIn dominants chez les concurrents cette semaine :**');
  212. lines.push('- **Changements de positionnement détectés sur les sites :**');
  213. lines.push('- **Nouvelles offres ou angles de communication :**');
  214. lines.push('- **Opportunités différenciantes pour Livinweb/Livinia :**');
  215. fs.mkdirSync(BENCHMARKS_DIR, { recursive: true });
  216. fs.writeFileSync(outputFile, lines.join('\n'), 'utf8');
  217. console.log(`\n✅ Rapport communication généré : ${outputFile}`);
  218. console.log(` LinkedIn : sections manuelles à remplir dans le fichier`);
  219. console.log(` Semaine : ${weekLabel}`);
  220. }
  221. run().catch(err => {
  222. console.error('❌ Erreur fatale :', err.message);
  223. process.exit(1);
  224. });