ADO2VMCrawler/selenium_crawler/config_manager.py

148 lines
6.2 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
설정 관리 도구
config.json 파일을 쉽게 편집할 수 있는 도구
"""
import json
from pathlib import Path
from logger_utils import config
def display_config():
"""현재 설정 표시"""
print("\n=== 현재 설정 ===")
current_config = config.get_all()
print(json.dumps(current_config, indent=2, ensure_ascii=False))
def edit_crawler_settings():
"""크롤러 설정 편집"""
print("\n=== 크롤러 설정 편집 ===")
# 현재 값 표시
max_blogs = config.get('crawler.max_blogs', 5)
default_query = config.get('crawler.default_search_query', '선돌막국수')
print(f"현재 설정:")
print(f" - 최대 블로그 수: {max_blogs}")
print(f" - 기본 검색어: {default_query}")
# 새 값 입력
new_max_blogs = input(f"\n새로운 최대 블로그 수 (현재: {max_blogs}, Enter로 유지): ").strip()
if new_max_blogs.isdigit():
config.update('crawler.max_blogs', int(new_max_blogs))
print(f"✓ 최대 블로그 수를 {new_max_blogs}로 변경했습니다.")
new_default_query = input(f"새로운 기본 검색어 (현재: {default_query}, Enter로 유지): ").strip()
if new_default_query:
config.update('crawler.default_search_query', new_default_query)
print(f"✓ 기본 검색어를 '{new_default_query}'로 변경했습니다.")
def edit_image_filter_settings():
"""이미지 필터 설정 편집"""
print("\n=== 이미지 필터 설정 편집 ===")
# 현재 값 표시
image_filter = config.get('image_filter', {})
print(f"현재 설정:")
print(f" - 최소 가로: {image_filter.get('min_width', 400)}px")
print(f" - 최소 세로: {image_filter.get('min_height', 400)}px")
print(f" - 최소 파일 크기: {image_filter.get('min_file_size_kb', 10)}KB")
print(f" - 최대 파일 크기: {image_filter.get('max_file_size_mb', 10)}MB")
print(f" - 크기 조건: {'AND' if image_filter.get('require_both_dimensions', False) else 'OR'}")
print(f" - 허용 포맷: {', '.join(image_filter.get('allowed_formats', []))}")
# 새 값 입력
new_min_width = input(f"\n새로운 최소 가로 (현재: {image_filter.get('min_width', 400)}, Enter로 유지): ").strip()
if new_min_width.isdigit():
config.update('image_filter.min_width', int(new_min_width))
print(f"✓ 최소 가로를 {new_min_width}px로 변경했습니다.")
new_min_height = input(f"새로운 최소 세로 (현재: {image_filter.get('min_height', 400)}, Enter로 유지): ").strip()
if new_min_height.isdigit():
config.update('image_filter.min_height', int(new_min_height))
print(f"✓ 최소 세로를 {new_min_height}px로 변경했습니다.")
new_min_size = input(f"새로운 최소 파일 크기 KB (현재: {image_filter.get('min_file_size_kb', 10)}, Enter로 유지): ").strip()
if new_min_size.isdigit():
config.update('image_filter.min_file_size_kb', int(new_min_size))
print(f"✓ 최소 파일 크기를 {new_min_size}KB로 변경했습니다.")
new_max_size = input(f"새로운 최대 파일 크기 MB (현재: {image_filter.get('max_file_size_mb', 10)}, Enter로 유지): ").strip()
if new_max_size.isdigit():
config.update('image_filter.max_file_size_mb', int(new_max_size))
print(f"✓ 최대 파일 크기를 {new_max_size}MB로 변경했습니다.")
both_dimensions = input("가로와 세로 모두 조건 만족 필요? (y/N): ").strip().lower()
if both_dimensions in ['y', 'n']:
config.update('image_filter.require_both_dimensions', both_dimensions == 'y')
print(f"✓ 크기 조건을 {'AND' if both_dimensions == 'y' else 'OR'}로 변경했습니다.")
def edit_webdriver_settings():
"""웹드라이버 설정 편집"""
print("\n=== 웹드라이버 설정 편집 ===")
# 현재 값 표시
webdriver = config.get('webdriver', {})
print(f"현재 설정:")
print(f" - Headless 모드: {webdriver.get('headless', False)}")
print(f" - 창 크기: {webdriver.get('window_size', '1920,1080')}")
print(f" - 페이지 로드 타임아웃: {webdriver.get('page_load_timeout', 30)}")
print(f" - Implicit Wait: {webdriver.get('implicit_wait', 10)}")
# Headless 모드
headless = input("\nHeadless 모드 사용? (y/N): ").strip().lower()
if headless in ['y', 'n']:
config.update('webdriver.headless', headless == 'y')
print(f"✓ Headless 모드를 {'활성화' if headless == 'y' else '비활성화'}했습니다.")
# 타임아웃 설정
new_timeout = input(f"새로운 페이지 로드 타임아웃 초 (현재: {webdriver.get('page_load_timeout', 30)}, Enter로 유지): ").strip()
if new_timeout.isdigit():
config.update('webdriver.page_load_timeout', int(new_timeout))
print(f"✓ 페이지 로드 타임아웃을 {new_timeout}초로 변경했습니다.")
def main():
"""메인 함수"""
print("=== 네이버 크롤러 설정 관리 도구 ===")
while True:
print("\n메뉴:")
print("1. 현재 설정 보기")
print("2. 크롤러 설정 편집")
print("3. 이미지 필터 설정 편집")
print("4. 웹드라이버 설정 편집")
print("5. 설정 저장 및 종료")
print("0. 저장하지 않고 종료")
choice = input("\n선택: ").strip()
if choice == '1':
display_config()
elif choice == '2':
edit_crawler_settings()
elif choice == '3':
edit_image_filter_settings()
elif choice == '4':
edit_webdriver_settings()
elif choice == '5':
config.save_config()
print("\n설정이 저장되었습니다.")
break
elif choice == '0':
print("\n설정을 저장하지 않고 종료합니다.")
break
else:
print("\n잘못된 선택입니다.")
if __name__ == "__main__":
main()