import os import requests import xml.etree.ElementTree as ET from datetime import datetime from modules.logger import log_event RAG_DIR = "/DATA/AppData/MULTI-IA-AETHAS38/RAG/Education_Nationale" def maj_base_education(): """Scrape le flux RSS du Bulletin Officiel en contournant les protections WAF.""" os.makedirs(RAG_DIR, exist_ok=True) url_rss = "https://www.education.gouv.fr/bo/rss.xml" # En-têtes complets simulant une vraie navigation web humaine headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36", "Accept": "application/rss+xml,application/xml,text/xml;q=0.9,*/*;q=0.8", "Accept-Language": "fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7", "Referer": "https://www.education.gouv.fr/bo/le-bulletin-officiel-de-l-education-nationale", "Sec-Ch-Ua": '"Chromium";v="122", "Not(A:Brand";v="24", "Google Chrome";v="122"', "Sec-Ch-Ua-Mobile": "?0", "Sec-Ch-Ua-Platform": '"Windows"', "Upgrade-Insecure-Requests": "1" } try: response = requests.get(url_rss, headers=headers, timeout=15) if response.status_code == 200: root = ET.fromstring(response.content) contenu_md = f"# Veille Pédagogique - Bulletin Officiel (B.O.)\n" contenu_md += f"*Date de mise à jour RAG : {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}*\n\n" for item in root.findall('.//item')[:20]: titre = item.find('title').text if item.find('title'] is not None else "Document sans titre" lien = item.find('link').text if item.find('link') is not None else "Aucun lien" desc = item.find('description').text if item.find('description') is not None else "Pas de résumé disponible." contenu_md += f"## {titre}\n" contenu_md += f"**Source officielle :** {lien}\n" contenu_md += f"> {desc}\n\n---\n" fichier_bo = os.path.join(RAG_DIR, "bulletin_officiel_recent.md") with open(fichier_bo, "w", encoding="utf-8") as f: f.write(contenu_md) log_event("INFO", "SCRAPER_EDU", "Base RAG Éducation (B.O.) mise à jour avec succès.") else: log_event("WARNING", "SCRAPER_EDU", f"Le site du ministère a retourné le code {response.status_code}.") except Exception as e: log_event("ERROR", "SCRAPER_EDU", f"Erreur lors de la MAJ de la base Éducation : {e}") if __name__ == "__main__": # Permet de tester le script directement en ligne de commande : python modules/eduscol_scraper.py maj_base_education()