except requests.exceptions.RequestException as e:  # Catch request-related exceptions
        logging.error(f"{Fore.RED}[ERROR]{Style.RESET_ALL} Prelogin failed for {account}: {e}")
        return None, None, None  # Return None for all values on failure
    except Exception as e:  # Catch any other unexpected exceptions
        logging.error(f"{Fore.RED}[ERROR]{Style.RESET_ALL} Prelogin failed for {account}: {e}")
        return None, None, None  # Return None for all values on failure
def login(session, account, password, v1, v2):
    hashed_password = hash_password(password, v1, v2)
    url = 'https://sso.garena.com/api/login'
    params = {
        'app_id': '10100',
        'account': account,
        'password': hashed_password,
        'redirect_uri': 'https://account.garena.com/',
        'format': 'json',
        'id': str(int(time.time() * 1000))
    }
    headers = {
        'accept': 'application/json, text/plain, */*',
        'referer': 'https://sso.garena.com/universal/login',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/129.0.0.0 Safari/537.36'
    }
    try:
        response = session.get(url, headers=headers, params=params)
        response.raise_for_status()
        data = response.json()
        if 'error' in data:
            logging.error(f"Login failed for {account}: {data['error']}")
            return None
        logging.info(f"Login successful for {account}")
        return response.cookies.get('sso_key')
    except requests.RequestException as e:
        logging.error(f"Login request failed for {account}: {e}")
        return None
def initaccount(session, sso_key):
    url = 'https://account.garena.com/api/account/init'
    headers = {
        'accept': '*/*',
        'cookie': f'sso_key={sso_key}',
        'referer': 'https://account.garena.com/',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/129.0.0.0 Safari/537.36'
    }
    try:
        response = session.get(url, headers=headers)
        response.raise_for_status()
        data = response.json()
        ac_session = response.cookies.get('ac_session')
        logging.info("Processing")
        return ac_session
    except requests.RequestException as e:
        logging.error(f"Processingfailed: {e}")
        return None
def parse_account_details(data):
    user_info = data.get('user_info', {})
    binds = []
    if user_info.get('email'):
        binds.append('Email')
    if user_info.get('mobile_no'):
        binds.append('Phone')
    return {
        'username': user_info.get('username', 'N/A'),
        'email': user_info.get('email', 'N/A'),
        'mobile': user_info.get('mobile_no', 'N/A'),
        'country': user_info.get('acc_country', 'N/A'),
        'binds': binds,
        'status': 'Clean' if not binds else 'Binded'
    }
def get_fresh_cookie(session):
    cookies = session.cookies.get_dict()
    return '; '.join([f"{name}={value}" for name, value in cookies.items()])
def handle_captcha():
    captcha_url = 'https://gop.captcha.garena.com/image?key={keycap}'
    response = requests.get(captcha_url)
    if response.status_code == 200:
        captcha_image = Image.open(BytesIO(response.content))
        captcha_image.show()
        captcha_text = input("Please enter the CAPTCHA: ")
        captcha_verification_url = 'https://gop.captcha.garena.com/verify'
        verification_response = requests.post(captcha_verification_url, data={'captcha': captcha_text})
        if verification_response.status_code == 200:
            return True
        else:
            logging.error("CAPTCHA verification failed.")
            return False
    else:
        logging.error("Failed to retrieve CAPTCHA image.")
        return False