import time
import random
import undetected_chromedriver as uc


def get_driver():
    options = uc.ChromeOptions()
    options.add_argument("--disable-blink-features=AutomationControlled")
    options.add_argument("--disable-gpu")
    options.add_argument("--disable-dev-shm-usage")
    options.add_argument("--disable-infobars")
    options.add_argument("--disable-extensions")
    options.add_argument("--no-sandbox")
    options.add_argument("--start-maximized")

    return uc.Chrome(options=options)


def safe_get(driver, url, attempts=5):
    for attempt in range(1, attempts + 1):
        try:
            driver.get(url)
            return True
        except Exception as e:
            print(f"[safe_get] Ошибка: {e}. Попытка {attempt}/{attempts}")
            time.sleep(random.uniform(1.5, 3))

    print(f"[safe_get] Не удалось загрузить: {url}")
    return False
