106 lines
4.6 KiB
Python
106 lines
4.6 KiB
Python
import os, json
|
|
import asyncio
|
|
import playwright
|
|
import platform
|
|
from playwright.async_api import async_playwright
|
|
|
|
from selenium_crawler.logger_utils import setup_logger, config
|
|
from selenium_crawler.naver_integrated_crawler import NaverIntegratedCrawler
|
|
from selenium_crawler.naver_blog_crawler import ImageFilterConfig
|
|
|
|
import asyncio
|
|
|
|
async def main():
|
|
"""메인 함수"""
|
|
print("=== 네이버 지도 + 블로그 통합 크롤러 (리팩토링 버전) ===\n")
|
|
|
|
# 설정 파일에서 값 읽기
|
|
print("설정 파일에서 크롤러 설정을 로드합니다...")
|
|
|
|
# 이미지 필터 설정
|
|
image_filter_config = config.get('image_filter', {})
|
|
image_filter = ImageFilterConfig(
|
|
min_width=image_filter_config.get('min_width', 400),
|
|
min_height=image_filter_config.get('min_height', 400),
|
|
min_file_size_kb=image_filter_config.get('min_file_size_kb', 10),
|
|
max_file_size_mb=image_filter_config.get('max_file_size_mb', 10),
|
|
require_both_dimensions=image_filter_config.get('require_both_dimensions', False),
|
|
allowed_formats=set(image_filter_config.get('allowed_formats', ['.jpg', '.jpeg', '.png', '.webp']))
|
|
)
|
|
|
|
print(f"\n이미지 필터 설정:")
|
|
print(f" - 최소 크기: {image_filter.min_width}x{image_filter.min_height} px")
|
|
print(f" - 파일 크기: {image_filter.min_file_size_kb}KB ~ {image_filter.max_file_size_mb}MB")
|
|
print(f" - 허용 포맷: {', '.join(sorted(image_filter.allowed_formats))}")
|
|
print(f" - 크기 조건: {'AND' if image_filter.require_both_dimensions else 'OR'}")
|
|
|
|
# 프로젝트 디렉토리 설정
|
|
if platform.system() == 'Windows':
|
|
project_dir = config.get('paths.project_dir_windows', r'C:\CrawlingData')
|
|
else:
|
|
project_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data')
|
|
|
|
# 크롤러 초기화
|
|
crawler = NaverIntegratedCrawler(project_dir=project_dir, image_filter=image_filter)
|
|
print(f"\n프로젝트 디렉토리: {project_dir}")
|
|
|
|
try:
|
|
crawler.setup_driver()
|
|
|
|
# 검색어 입력 (설정 파일의 기본값 사용)
|
|
default_query = config.get('crawler.default_search_query', '선돌막국수')
|
|
search_query = input(f"\n검색할 장소를 입력하세요 (기본값: {default_query}): ").strip()
|
|
if not search_query:
|
|
search_query = default_query
|
|
|
|
# 블로그 수 입력 (설정 파일의 기본값 사용)
|
|
default_max_blogs = config.get('crawler.max_blogs', 1)
|
|
max_blogs_str = input(f"수집할 블로그 수를 입력하세요 (기본값: {default_max_blogs}): ").strip()
|
|
max_blogs = int(max_blogs_str) if max_blogs_str.isdigit() else default_max_blogs
|
|
|
|
# 설정 확인
|
|
print(f"\n=== 크롤링 설정 확인 ===")
|
|
print(f"검색어: {search_query}")
|
|
print(f"수집할 블로그 수: {max_blogs}")
|
|
|
|
proceed = input("\n위 설정으로 진행하시겠습니까? (Y/n): ").strip().lower()
|
|
if proceed == 'n':
|
|
print("크롤링을 취소합니다.")
|
|
return
|
|
|
|
# 통합 수집 실행
|
|
max_blogs =1
|
|
results = crawler.collect_all_info(search_query, max_blogs)
|
|
|
|
print(f"\n=== 수집 완료 ===")
|
|
print(f"장소 정보: {results['place_info'].get('name', 'N/A')}")
|
|
print(f"수집한 사진: {len(results['photo_urls'])}개")
|
|
print(f"지도 블로그 리뷰: {len(results['map_blog_reviews'])}개")
|
|
print(f"상세 블로그 정보: {results['total_blogs_visited']}개")
|
|
|
|
# 이미지 다운로드 통계 출력
|
|
if 'image_download_info' in results:
|
|
dl_info = results['image_download_info']
|
|
print(f"\n=== 이미지 다운로드 통계 ===")
|
|
print(f"총 처리: {dl_info['total_images']}개")
|
|
print(f"다운로드 성공: {dl_info['downloaded']}개")
|
|
if 'statistics' in dl_info:
|
|
stats = dl_info['statistics']
|
|
print(f" - 신규 저장: {stats['success']}개")
|
|
print(f" - 기존 파일: {stats['file_exists']}개")
|
|
print(f" - 중복 제거: {stats['duplicate']}개")
|
|
print(f" - 크기 필터링: {stats['size_filtered']}개")
|
|
|
|
except Exception as e:
|
|
print(f"오류 발생: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
|
|
finally:
|
|
input("\n엔터를 누르면 브라우저를 종료합니다...")
|
|
crawler.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|