baidu_web/main.py

115 lines
4.3 KiB
Python

import os
import sys
from PySide6.QtWidgets import QApplication
from PySide6.QtCore import QThread, Qt
from mainProcessor import MainProcessor
from logger_module import setup_logger
from mainWindow import MainWindow
import logging
def get_base_path():
"""
실행 파일이 위치한 경로를 반환.
- PyInstaller로 패키징된 경우와 개발 환경 모두 지원.
"""
if getattr(sys, 'frozen', False): # PyInstaller로 패키징된 실행 환경
return os.path.dirname(sys.executable) # 실행 파일 위치
return os.path.dirname(os.path.abspath(__file__)) # 개발 환경
def main():
# 로그 설정
base_path = get_base_path()
log_file = os.path.join(base_path, 'SellFree_Soucer.log')
logger = setup_logger(log_file=log_file, log_level=logging.DEBUG)
# 폴더 및 파일 경로 설정
excel_folder = os.path.join(base_path, 'xls') # xls 폴더
img_folder = os.path.join(base_path, 'img') # img 폴더
db_path = os.path.join(base_path, 'search_products.db') # 데이터베이스 파일
# 폴더가 없으면 생성
os.makedirs(excel_folder, exist_ok=True)
os.makedirs(img_folder, exist_ok=True)
# 프로그램 시작 시 이미지 폴더와 데이터베이스 파일 정리
print("Clearing image folder and database file...")
# 이미지 폴더 파일 삭제
clear_folder(img_folder)
# 데이터베이스 파일 삭제
if os.path.exists(db_path):
os.remove(db_path)
print(f"Deleted database file: {db_path}")
# PySide6 앱 초기화
app = QApplication(sys.argv)
main_window = MainWindow()
# MainProcessor와 QThread 설정
processor_thread = QThread()
processor = MainProcessor(base_path, excel_folder, db_path, img_folder, main_window, logger)
processor.moveToThread(processor_thread)
# ProductViewer 생성
product_viewer = processor.resultViewer # MainProcessor에 의해 생성된 ProductViewer
# 메인 윈도우 종료 시 ProductViewer 닫기
main_window.closing_signal.connect(product_viewer.close)
# 버튼 이벤트 연결
main_window.start_signal.connect(processor_thread.start) # 스레드 시작
main_window.start_signal.connect(lambda: main_window.log_message("Start Search Product"))
main_window.start_signal.connect(main_window.disable_start_button) # 시작 버튼 비활성화 연결
main_window.start_signal.connect(processor.process_all_products)
processor.finished_signal.connect(processor_thread.quit)
processor.finished_signal.connect(lambda: main_window.log_message("Processing completed!"))
processor.finished_signal.connect(main_window.enable_results_button)
processor.finished_signal.connect(main_window.enable_export_button) # 엑셀추출 버튼 활성화
main_window.show_results_signal.connect(lambda: main_window.log_message("show_results_signal emitted"))
main_window.show_results_signal.connect(processor.show_results)
main_window.show_results_signal.connect(processor.show_results, Qt.DirectConnection)
main_window.export_to_xls_signal.connect(processor.export_to_xls, Qt.DirectConnection)
# 스레드 작업 연결
processor.update_progress_signal.connect(main_window.update_progress)
processor.log_message_signal.connect(main_window.log_message)
# GUI 실행
main_window.show()
sys.exit(app.exec())
print("Clearing Excel folder...")
# 프로그램 종료 시 엑셀 폴더 파일 삭제
clear_folder(excel_folder)
def clear_folder(folder_path, exclude_files=None):
"""
지정된 폴더 내부의 모든 파일을 삭제합니다.
특정 파일을 제외할 수 있습니다.
"""
try:
exclude_files = exclude_files or [] # 제외할 파일 리스트 (기본값: 빈 리스트)
if os.path.exists(folder_path):
for file_name in os.listdir(folder_path):
file_path = os.path.join(folder_path, file_name)
if os.path.isfile(file_path) and file_name not in exclude_files:
os.remove(file_path)
print(f"Deleted: {file_path}")
else:
print(f"Folder does not exist: {folder_path}")
except Exception as e:
print(f"Error clearing folder {folder_path}: {e}")
if __name__ == "__main__":
main()