refactor: Ensure Camoufox setup runs only once per session and refine its installation logic.

This commit is contained in:
2025-12-29 00:37:09 +09:00
parent 56d0d57a70
commit aa5324ad2d

View File

@@ -1232,9 +1232,15 @@ class AniLifeQueueEntity(FfmpegQueueEntity):
provider_html = None provider_html = None
aldata_value = None aldata_value = None
# Camoufox 설치 확인 및 자동 설치 # Camoufox 설치 확인 및 자동 설치 (플러그인 세션 내 1회만 실행)
if not hasattr(LogicAniLife, 'camoufox_setup_done'):
LogicAniLife.camoufox_setup_done = False
def ensure_camoufox_installed(): def ensure_camoufox_installed():
"""Camoufox 및 필수 시스템 패키지(xvfb) 설치 확인 및 자동 설치""" """Camoufox 및 필수 시스템 패키지(xvfb) 설치 확인 및 자동 설치"""
if LogicAniLife.camoufox_setup_done:
return True
import importlib.util import importlib.util
import subprocess as sp import subprocess as sp
import shutil import shutil
@@ -1243,51 +1249,38 @@ class AniLifeQueueEntity(FfmpegQueueEntity):
if platform.system() == 'Linux' and shutil.which('Xvfb') is None: if platform.system() == 'Linux' and shutil.which('Xvfb') is None:
logger.info("Xvfb not found. Attempting to install system package...") logger.info("Xvfb not found. Attempting to install system package...")
try: try:
# apt-get을 사용하는 데비안/우분투 계열 가정 (Docker 환경)
# sudo 없이 시도 (Docker는 보통 root 권한)
sp.run(['apt-get', 'update', '-qq'], capture_output=True) sp.run(['apt-get', 'update', '-qq'], capture_output=True)
sp.run(['apt-get', 'install', '-y', 'xvfb', '-qq'], capture_output=True) sp.run(['apt-get', 'install', '-y', 'xvfb', '-qq'], capture_output=True)
if shutil.which('Xvfb') is not None:
logger.info("Xvfb system package installed successfully")
except Exception as e: except Exception as e:
logger.error(f"Failed to install xvfb system package: {e}") logger.error(f"Failed to install xvfb system package: {e}")
# 2. Camoufox 패키지 확인 및 업그레이드 # 2. Camoufox 패키지 확인 및 설치
# 'xvfb' 인자는 최신 버전에서 지원되므로 존재하더라도 업그레이드 시도 # 이미 설치되어 있다면 무거운 --upgrade 체크는 건너뜀
need_install = importlib.util.find_spec("camoufox") is None need_install = importlib.util.find_spec("camoufox") is None
if need_install:
logger.info("Camoufox not installed. Installing latest version...")
else:
# 이미 설치되어 있어도 최신 버전으로 업그레이드 (xvfb 지원을 위해)
logger.info("Checking for Camoufox updates to ensure Xvfb support...")
try: try:
# pip 멤버로 설치/업그레이드 (camoufox[geoip] 포함) if need_install:
cmd = [sys.executable, "-m", "pip", "install", "--upgrade", "camoufox[geoip]", "-q"] logger.info("Camoufox not installed. Installing latest version...")
pip_result = sp.run(cmd, capture_output=True, text=True, timeout=120) cmd = [sys.executable, "-m", "pip", "install", "camoufox[geoip]", "-q"]
sp.run(cmd, capture_output=True, text=True, timeout=120)
if pip_result.returncode != 0: # 브라우저 바이너리 존재 여부 확인 (기본 경로 추정)
logger.error(f"Failed to install/upgrade camoufox: {pip_result.stderr}") # 실제로는 camoufox fetch를 매번 부르기보다 라이브러리가 알아서 확인하게 두거나 1회만 실행
if need_install: return False # 새로 설치 중이었으면 중단 logger.info("Ensuring Camoufox browser binary is fetched...")
else:
logger.info("Camoufox package installed/upgraded successfully")
# Camoufox 브라우저 바이너리 다운로드
logger.info("Downloading Camoufox browser binary...")
sp.run([sys.executable, "-m", "camoufox", "fetch"], capture_output=True, text=True, timeout=300) sp.run([sys.executable, "-m", "camoufox", "fetch"], capture_output=True, text=True, timeout=300)
LogicAniLife.camoufox_setup_done = True
return True return True
except Exception as install_err: except Exception as install_err:
logger.error(f"Failed during Camoufox setup: {install_err}") logger.error(f"Failed during Camoufox setup: {install_err}")
return not need_install # 이미 설치되어 있었으면 에러나도 일단 진행 시도 return not need_install
# Camoufox를 subprocess로 실행 (스텔스 Firefox - 봇 감지 우회) # Camoufox를 subprocess로 실행
try: try:
import subprocess import subprocess
import json as json_module import json as json_module
# Camoufox 설치 확인 # 셋업 실행 (세션 내 1회만 실제 동작)
if not ensure_camoufox_installed(): if not ensure_camoufox_installed():
logger.error("Camoufox installation failed. Cannot proceed.") logger.error("Camoufox installation failed. Cannot proceed.")
return return