from ns_shopping import ShoppingLensScraper import json import os, sys from pywinauto import Application, findwindows, timings from pywinauto.controls.hwndwrapper import HwndWrapper import logging def get_base_dir(): """ 실행 환경에 따라 base_dir을 설정하는 메서드. cx_Freeze로 패키징된 경우 실행 파일의 경로, 일반 Python 환경일 경우 __file__을 기준으로 설정. """ if getattr(sys, 'frozen', False): # 패키징된 경우 base_dir = os.path.dirname(sys.executable) internal_dir = os.path.join(base_dir, '_internal') # _internal 디렉토리 포함 if os.path.exists(internal_dir): # _internal 디렉토리가 존재하면 base_dir로 설정 return internal_dir else: # 일반 Python 실행 환경 base_dir = os.path.dirname(os.path.abspath(__file__)) return base_dir def start_whale_browser(): base_path = get_base_dir() whale_exe_path = os.path.join(base_path, "browsers", "whale", "whale.exe") user_data_dir = os.path.join(base_path, "browsers", "whale", "user_data") cache_dir = os.path.join(base_path, "browsers", "whale", "cache") whale_app = Application(backend="uia").start( f'"{whale_exe_path}" --incognito --user-data-dir="{user_data_dir}" --disk-cache-dir="{cache_dir}"' ) # 창이 완전히 생성될 때까지 대기 whale_window = find_whale_window(whale_app) if whale_window: print("웨일 시크릿 모드로 시작 완료.") else: print("웨일 창을 찾을 수 없습니다.") return whale_window def find_whale_window(whale_app): try: # 최대 10초 동안 '새 시크릿 탭 - Whale' 창이 나타나기를 기다림 timings.wait_until(10, 0.5, lambda: any(window.name == '새 시크릿 탭 - Whale' for window in findwindows.find_elements())) windows = findwindows.find_elements() for window in windows: if window.name == '새 시크릿 탭 - Whale': whale_pid = window.process_id whale_app = Application(backend="uia").connect(process=whale_pid) whale_window = whale_app.top_window() # 위치 및 크기 조절 hwnd_wrapper = HwndWrapper(whale_window.handle) hwnd_wrapper.move_window(x=1, y=1, width=1280, height=720) whale_window.set_focus() print("웨일 창을 성공적으로 찾았습니다.") return whale_window print("'새 시크릿 탭 - Whale' 창을 찾을 수 없습니다.") except Exception as e: print(f"웨일 창 탐색 중 오류 발생: {e}") return None if __name__ == "__main__": # base_path = get_base_dir() # whale_exe_path = os.path.join(base_path, "browsers", "whale", "whale.exe") # user_data_dir = os.path.join(base_path, "browsers", "whale", "user_data") # cache_dir = os.path.join(base_path, "browsers", "whale", "cache") scraper = ShoppingLensScraper() whale_window = start_whale_browser() # 테스트 이미지 URL image_url = "https://file.percenty.co.kr/public/652bed8e865b1f32ea62bf1f/products/675100fb29f74e392b42d31b/b8b35f6e-69ee-4350-80f0-244f92d0421f.jpg" # image_url='https://file.percenty.co.kr/public/652bed8e865b1f32ea62bf1f/products/67494597e4e72565ac3b286e/8b3562ff-83d2-4deb-a419-e24fe4536674.jpg' results = scraper.search(whale_window, image_url) # 결과 출력 # print(json.dumps(results, ensure_ascii=False, indent=2))