import os
import re

target_dir = '/Applications/XAMPP/xamppfiles/htdocs/aaa'
file_count = 0
image_count = 0

def process_img_tag(match):
    global image_count
    img_tag_original = match.group(0)
    
    if 'image.pollinations.ai' not in img_tag_original:
        return img_tag_original
        
    # extract seed from pollinations url
    seed_match = re.search(r'seed=(\d+)', img_tag_original)
    if seed_match:
        seed = seed_match.group(1)
    else:
        # fallback seed
        import random
        seed = str(random.randint(1000, 999999))
        
    new_src = f'https://picsum.photos/seed/{seed}/1000/600'
    
    # replace the src
    new_tag = re.sub(
        r'src=[\"\']https?://image\.pollinations\.ai/[^\"\']+[\"\']', 
        f'src="{new_src}"', 
        img_tag_original
    )
    
    if new_tag != img_tag_original:
        image_count += 1
        return new_tag
        
    return img_tag_original

for root, dirs, files in os.walk(target_dir):
    for f in files:
        if f.endswith('.php') or f.endswith('.html'):
            path = os.path.join(root, f)
            
            with open(path, 'r', encoding='utf-8', errors='ignore') as file:
                content = file.read()
            
            new_content = re.sub(r'<img[^>]+>', process_img_tag, content)
            
            if new_content != content:
                with open(path, 'w', encoding='utf-8') as write_file:
                    write_file.write(new_content)
                file_count += 1

print(f"Updated {image_count} images in {file_count} files across the website.")
