diff --git a/data/gnu_autoupload.py b/data/gnu_autoupload.py index 1d22914..44ea6f3 100644 --- a/data/gnu_autoupload.py +++ b/data/gnu_autoupload.py @@ -4,7 +4,7 @@ gnu_autoupload.py 기능: 1. Selenium을 이용해 날씨 정보를 캡처 (weather_capture.py 호출) -2. FTP를 이용해 이미지 업로드 (썸네일 포함) +2. FTP를 이용해 이미지 업로드 3. 그누보드 DB에 게시글 및 첨부파일 정보 자동 등록 """ @@ -21,25 +21,27 @@ import ftputil from config import DB_CONFIG, FTP_CONFIG, MAIN + # --------------------------- -# 이미지 캡처 관련 함수 +# 이미지 캡처 함수 # --------------------------- def capture_image(script_path, output_path, max_attempts=5): for attempt in range(max_attempts): - print(f"[{datetime.now().strftime('%H:%M:%S')}] \uac00\uc0ac\uac1c \uc2dc\ub3c4 {attempt + 1}/{max_attempts}") + print(f"[{datetime.now().strftime('%H:%M:%S')}] 이미지 캡처 시도 {attempt + 1}/{max_attempts}") try: subprocess.run(['python3', script_path], check=True) except subprocess.CalledProcessError as e: - print(f"[\uc624\ub958] weather_capture.py \uc2e4\ud589 \uc2e4\ud328: {e}") + print(f"[오류] weather_capture.py 실행 실패: {e}") if os.path.isfile(output_path): - print(f"[\uc131\uacf5] \uc774\ubbf8\uc9c0 \uce90\ud504 \uc644\ub8cc: {output_path}") + print(f"[성공] 이미지가 정상적으로 캡처되었습니다: {output_path}") return True time.sleep(2) - print(f"[\uc2e4\ud328] {max_attempts}\ud68c \uc2dc\ub3c4 \ud6c4\uc5d0\ub3c4 \uc774\ubbf8\uc9c0\uac00 \uc0dd\uc131\ub418\uc9c0 \uc54a\uc558\uc2b5\ub2c8\ub2e4.") + print(f"[실패] {max_attempts}회 시도 후에도 이미지가 생성되지 않았습니다.") return False + # --------------------------- -# 파일 업로드 및 처리 관련 +# 파일 관련 유틸 함수 # --------------------------- def file_type(ext): return { @@ -49,30 +51,36 @@ def file_type(ext): 'wbmp': '15', 'xbm': '16' }.get(ext.lower(), '0') + def get_filename(filename): ms = datetime.now().microsecond encoded_name = filename.encode('utf-8') return f'{ms}_{hashlib.sha1(encoded_name).hexdigest()}' + def file_upload(filename, bf_file): try: with ftputil.FTPHost(FTP_CONFIG['HOST'], FTP_CONFIG['USER'], FTP_CONFIG['PASS']) as fh: fh.chdir(FTP_CONFIG['UPLOAD_DIR']) fh.upload(filename, bf_file) - print(f"[\uc5c5\ub85c\ub4dc \uc644\ub8cc] {filename} → {bf_file}") + print(f"[업로드 완료] '{filename}' → '{bf_file}' 로 FTP 업로드 완료") return True except Exception as e: - print(f"[FTP \uc624\ub958] {type(e).__name__}: {e}") + print(f"[FTP 오류] {type(e).__name__}: {e}") return False + # --------------------------- # 게시글 작성 함수 # --------------------------- def write_board(board, subject, content, mb_id, nickname, ca_name=None, file_list=None): try: conn = pymysql.connect( - host=DB_CONFIG['HOST'], user=DB_CONFIG['USER'], db=DB_CONFIG['DBNAME'], - password=DB_CONFIG['PASS'], charset='utf8' + host=DB_CONFIG['HOST'], + user=DB_CONFIG['USER'], + db=DB_CONFIG['DBNAME'], + password=DB_CONFIG['PASS'], + charset='utf8' ) curs = conn.cursor() now = datetime.now().strftime('%Y-%m-%d %H:%M:%S') @@ -126,18 +134,19 @@ def write_board(board, subject, content, mb_id, nickname, ca_name=None, file_lis size, width, height, img_type, now)) file_count += 1 else: - print(f"[\uacbd\uace0] \ud30c\uc77c \uc5c5\ub85c\ub4dc \uc2e4\ud328: {file}") + print(f"[경고] 파일 업로드 실패: {file}") curs.execute(f"UPDATE g5_write_{board} SET wr_file = %s WHERE wr_id = %s", (file_count, wr_id)) conn.commit() - print("[\uc644\ub8cc] \uac8c\uc2dc\uae00 \uc791\uc131 \ubc0f \ud30c\uc77c \uc5c5\ub85c\ub4dc \uc644\ub8cc") + print("[성공] 게시글과 첨부파일 등록 완료") except Exception as e: conn.rollback() - print(f"[DB \uc624\ub958] {type(e).__name__}: {e}") + print(f"[DB 오류] {type(e).__name__}: {e}") finally: conn.close() + # --------------------------- # 메인 실행 함수 # --------------------------- @@ -147,17 +156,13 @@ def main(): capture_script = os.path.join(script_dir, 'weather_capture.py') weather_filename = f'weather_capture_{today}.png' weather_file = os.path.join(script_dir, weather_filename) - thumb_file = os.path.join(script_dir, 'thumb.jpg') - if not os.path.isfile(thumb_file): - print(f"[\uc624\ub958] \uc368\uba58\ub0b4\uc77c \ud30c\uc77c\uc774 \uc874\uc7ac\ud558\uc9c0 \uc54a\uc74c: {thumb_file}") - return if not capture_image(capture_script, weather_file): return FTP_CONFIG['UPLOAD_DIR'] = f"/www/data/file/{MAIN['board']}/" - MAIN['subject'] = f"[{datetime.now().strftime('%Y-%m-%d')}] \ub0a0\uc528 \uc815\ubcf4" + MAIN['subject'] = f"[{datetime.now().strftime('%Y-%m-%d')}] 날씨 정보" MAIN['file1'] = thumb_file MAIN['file2'] = weather_file @@ -176,12 +181,10 @@ def main(): try: if os.path.isfile(weather_file): os.remove(weather_file) - print(f"[\uc0ad\uc81c \uc644\ub8cc] {weather_file}") + print(f"[정리 완료] 캡처 이미지 삭제됨: {weather_file}") except Exception as e: - print(f"[\uc0ad\uc81c \uc624\ub958] {type(e).__name__}: {e}") + print(f"[삭제 오류] {type(e).__name__}: {e}") + -# --------------------------- -# 스크립트 실행 진입점 -# --------------------------- if __name__ == "__main__": main()