#!/usr/bin/env python3
import requests
from concurrent.futures import ThreadPoolExecutor, as_completed

MAX_THREADS = 30
TIMEOUT = 15
USERNAME = "8w9fsybw"
PASSWORD = "1375"
HEADERS = {
    "User-Agent": (
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
        "AppleWebKit/537.36 (KHTML, like Gecko) "
        "Chrome/140.0.0.0 Safari/537.36"
    )
}

FILE_PATH = "storage-files/raw-links.txt"

def needs_auth(url):
    return "download.php" in url and "server=" in url

def resolve_url(url):
    try:
        r = requests.get(
            url,
            allow_redirects=True,
            stream=True,
            timeout=TIMEOUT,
            headers=HEADERS
        )
        if r.status_code in (200, 206, 301, 302):
            final_url = r.url
            r.close()
            return final_url
        r.close()
    except Exception:
        pass

    if needs_auth(url):
        try:
            r = requests.get(
                url,
                allow_redirects=True,
                stream=True,
                timeout=TIMEOUT,
                headers=HEADERS,
                auth=(USERNAME, PASSWORD)
            )
            if r.status_code in (200, 206, 301, 302):
                final_url = r.url
                r.close()
                return final_url
            r.close()
        except Exception:
            pass

    return url

def process_links_in_place():
    # First: Remove exact duplicates at the beginning
    with open(FILE_PATH, "r", encoding="utf-8") as f:
        lines = [line.rstrip("\n") for line in f]

    unique_lines = []
    seen = set()
    for line in lines:
        if line not in seen:
            seen.add(line)
            unique_lines.append(line)

    with open(FILE_PATH, "w", encoding="utf-8") as f:
        for line in unique_lines:
            f.write(line + "\n")

    # Main resolving loop - max 4 iterations
    iteration = 0
    while iteration < 2:
        iteration += 1
        print(f"\n--- Iteration {iteration} started ---")

        with open(FILE_PATH, "r", encoding="utf-8") as f:
            lines = [line.rstrip("\n") for line in f]

        to_process = [i for i, line in enumerate(lines) if needs_auth(line.strip())]

        if not to_process:
            print("No unresolved links with server= found. Process completed!")
            break

        print(f"{len(to_process)} links need processing...")

        resolved_count = 0

        with ThreadPoolExecutor(max_workers=MAX_THREADS) as executor:
            futures = {executor.submit(resolve_url, lines[i]): i for i in to_process}

            for future in as_completed(futures):
                idx = futures[future]
                original = lines[idx]
                try:
                    new_url = future.result()
                    if new_url != original:
                        lines[idx] = new_url
                        resolved_count += 1
                        print(f"[{resolved_count}/{len(to_process)}] Replaced line {idx+1}")
                    else:
                        print(f"[{resolved_count}/{len(to_process)}] No change line {idx+1}")
                except Exception as e:
                    print(f"Error on line {idx+1}: {e}")

        with open(FILE_PATH, "w", encoding="utf-8") as f:
            for line in lines:
                f.write(line + "\n")

        print(f"Iteration {iteration} completed. {resolved_count} links replaced.")

    # After 4 iterations (or earlier break): remove remaining links that still need auth
    with open(FILE_PATH, "r", encoding="utf-8") as f:
        lines = [line.rstrip("\n") for line in f]

    filtered_lines = [line for line in lines if not needs_auth(line.strip())]

    with open(FILE_PATH, "w", encoding="utf-8") as f:
        for line in filtered_lines:
            f.write(line + "\n")

    # Final duplicate removal
    with open(FILE_PATH, "r", encoding="utf-8") as f:
        lines = [line.rstrip("\n") for line in f]

    unique_lines = []
    seen = set()
    for line in lines:
        if line not in seen:
            seen.add(line)
            unique_lines.append(line)

    with open(FILE_PATH, "w", encoding="utf-8") as f:
        for line in unique_lines:
            f.write(line + "\n")

    print("finished")

if __name__ == "__main__":
    process_links_in_place()