53 lines
2.5 KiB
Python
53 lines
2.5 KiB
Python
"""
|
|
commands/base.py — 공통 CLI 명령어 유틸리티
|
|
|
|
브라우저 컨텍스트 초기화, 리포트 파일 로드 등 여러 명령어에서 공통으로
|
|
사용하는 기능을 제공합니다.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
from playwright.sync_api import Playwright, BrowserContext
|
|
|
|
|
|
def launch_browser_context(
|
|
playwright: Playwright, user_data_dir: str | None, channel: str | None, headless: bool
|
|
) -> BrowserContext:
|
|
"""공용 Playwright 브라우저 컨텍스트 생성"""
|
|
if user_data_dir:
|
|
return playwright.chromium.launch_persistent_context(
|
|
user_data_dir=user_data_dir,
|
|
channel=channel,
|
|
headless=headless,
|
|
args=["--disable-blink-features=AutomationControlled"],
|
|
viewport={"width": 1280, "height": 800},
|
|
)
|
|
browser = playwright.chromium.launch(channel=channel, headless=headless)
|
|
return browser.new_context(viewport={"width": 1280, "height": 800})
|
|
|
|
|
|
def load_report(path: Path) -> dict[str, Any]:
|
|
"""JSON 리포트 파일 로드"""
|
|
if not path.is_file():
|
|
raise FileNotFoundError(f"리포트 파일을 찾을 수 없습니다: {path}")
|
|
with path.open(encoding="utf-8") as f:
|
|
return json.load(f)
|
|
|
|
|
|
def add_common_arguments(parser: argparse.ArgumentParser) -> None:
|
|
"""명령행 인자에 브라우저/설정 관련 공통 옵션 추가"""
|
|
parser.add_argument("--base-url", required=True, help="기록 시스템 기본 URL")
|
|
parser.add_argument("--game-id", required=True, help="크롤링할 네이버 게임 ID (예: 20260501NCLG02026)")
|
|
parser.add_argument("--report-path", help="로컬 리포트 JSON 경로 (기본값: output/{game_id}_report.json)")
|
|
parser.add_argument("--manager-game-no", help="관리자 사이트의 게임번호 (생략 시 목록에서 검색)")
|
|
parser.add_argument("--user-data-dir", help="Chrome 사용자 프로필 경로 (로그인 유지용)")
|
|
parser.add_argument("--channel", default="chrome", help="브라우저 채널 (기본값: chrome)")
|
|
parser.add_argument("--headless", action="store_true", help="브라우저 숨김 모드 실행")
|
|
parser.add_argument("--close", action="store_true", help="작업 완료 후 브라우저 닫기")
|
|
parser.add_argument("--no-write", dest="write_events", action="store_false", help="실제 입력은 하지 않고 시뮬레이션만 수행")
|
|
parser.add_argument("--job-id", help="DB 로깅용 작업 ID (UUID)")
|