import requests
import time
from playwright.sync_api import sync_playwright, Error as PlaywrightError

proxy = "http://apiec803a20e08d2684_c_US:driS9IZ2@res.proxy-seller.com:10000"
proxies = {"http": proxy, "https": proxy}

def send_request_to_empire(url, retries=5, delay=30):

    bearer_token = "2752|LowvmtHwwAqUu8MPYULRHja65igYCMAmsMeHDFgA26f5114b"

    attempt = 0
    while attempt < retries:
        try:
            with sync_playwright() as p:
                browser = p.chromium.launch(
                    headless=True
                )

                context = browser.new_context(
                    ignore_https_errors=True,
                    extra_http_headers={
                        "Authorization": f"Bearer {bearer_token}",
                        "Accept": "application/json",
                        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36",
                    }
                )

                page = context.new_page()
                page.goto("https://empirebesttv124.cfd", timeout=0, wait_until="domcontentloaded")

                response = context.request.get(url)

                if response.status == 200:
                    try:
                        return response.json()
                    except:
                        return response.text()
                else:
                    print(f"------ attempt {attempt+1}: HTTP {response.status}, retrying in {delay}s...")
                    time.sleep(delay)
                    attempt += 1
        except PlaywrightError as e:
            print(f"----- attempt {attempt+1}: Playwright error: {e}, retrying in {delay}s...")
            time.sleep(delay)
            attempt += 1

def get_empire_new_domain():
    res = requests.get('https://empirebesttv.com', proxies=proxies)
    return res.url