import re
import glob
import random

html_files = glob.glob('/Applications/XAMPP/xamppfiles/htdocs/bus.online/*.html')

keywords = ["bus", "travel", "road", "comfort", "seat", "wifi", "power", "money", "baggage", "eco", "green", "safe", "service", "route", "map", "clock", "fast"]

def replacement(match):
    kw = random.choice(keywords)
    r = random.randint(100, 999)
    # the matched string is the whole div tag including attributes, then the content, then closing tag
    # we want to replace the whole thing or just inject the style.
    # match.group(1) is the attributes if any, match.group(2) is the emoji.
    attr = match.group(1)
    if 'style="' in attr:
        # just append to style
        new_attr = attr.replace('style="', f'style="background: url(\'https://loremflickr.com/100/100/{kw}?random={r}\') center/cover; color: transparent; ')
    else:
        new_attr = f'{attr} style="background: url(\'https://loremflickr.com/100/100/{kw}?random={r}\') center/cover; color: transparent;"'
    
    return f'<div class="info-card-icon"{new_attr}></div>'

for filepath in html_files:
    with open(filepath, 'r') as f:
        content = f.read()
    
    # regex to match <div class="info-card-icon" ... > emoji </div>
    # match.group(1) will capture any extra attributes like style="..."
    new_content = re.sub(r'<div class="info-card-icon"([^>]*)>.*?</div>', replacement, content, flags=re.DOTALL)
    
    with open(filepath, 'w') as f:
        f.write(new_content)

print("Updated all info-card-icons in all HTML files.")
