import requests
from bs4 import BeautifulSoup
import re

print("=" * 60)
print("RECHERCHE DES IMAGES DE BACKGROUND")
print("=" * 60)
print()

url = "https://www.transport-transco86.fr/"

try:
    print(f"Récupération de la page : {url}")
    response = requests.get(url, timeout=30)
    response.raise_for_status()
    
    soup = BeautifulSoup(response.content, 'html.parser')
    
    # Chercher tous les fichiers CSS
    print("\nRecherche des fichiers CSS...")
    css_files = []
    for link in soup.find_all('link', rel='stylesheet'):
        href = link.get('href')
        if href:
            css_files.append(href)
    
    print(f"Trouvé {len(css_files)} fichiers CSS")
    
    # Télécharger et analyser les CSS
    print("\nAnalyse des CSS pour trouver les backgrounds...")
    
    background_images = {}
    
    for css_url in css_files:
        if 'elementor' in css_url or 'theme' in css_url or 'custom' in css_url:
            try:
                print(f"\n  Analyse : {css_url[:80]}...")
                css_response = requests.get(css_url, timeout=10)
                css_content = css_response.text
                
                # Chercher les background-image
                patterns = [
                    r'\.mapbackground[^{]*{[^}]*background[^:]*:\s*url\(["\']?([^"\']+?)["\']?\)',
                    r'\.about-section[^{]*{[^}]*background[^:]*:\s*url\(["\']?([^"\']+?)["\']?\)',
                    r'background-image:\s*url\(["\']?([^"\']+?\.(?:jpg|jpeg|png))["\']?\)',
                ]
                
                for pattern in patterns:
                    matches = re.findall(pattern, css_content, re.IGNORECASE | re.DOTALL)
                    for match in matches:
                        if 'http' in match or match.startswith('/'):
                            background_images[match] = css_url
                            print(f"    ✅ Trouvé : {match}")
            
            except Exception as e:
                pass
    
    print()
    print("=" * 60)
    print("IMAGES DE BACKGROUND TROUVÉES :")
    print("=" * 60)
    
    if background_images:
        for img, source in background_images.items():
            print(f"\n{img}")
            print(f"  Source CSS : {source[:80]}")
    else:
        print("Aucune image de background trouvée dans les CSS")
        print()
        print("Analyse du style inline...")
        
        # Chercher dans le HTML inline
        html_content = response.text
        
        # Chercher la section mapbackground
        map_section = re.search(r'class="[^"]*mapbackground[^"]*"[^>]*style="([^"]*)"', html_content)
        if map_section:
            print(f"Style mapbackground : {map_section.group(1)}")
        
        # Chercher toutes les sections avec background
        bg_patterns = [
            r'data-settings=\'[^\']*background_image[^\']*url":"([^"]+)"',
            r'data-settings="[^"]*background_image[^"]*url\\"::\\"([^"]+)\\"',
        ]
        
        for pattern in bg_patterns:
            matches = re.findall(pattern, html_content)
            for match in matches:
                print(f"Background Elementor : {match}")

except Exception as e:
    print(f"❌ Erreur : {e}")
