.env를 crontab에서 인식하지 못하는 문제 수정
This commit is contained in:
65
app/weather_capture.py
Normal file
65
app/weather_capture.py
Normal file
@ -0,0 +1,65 @@
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
from config import TODAY
|
||||
from selenium_manager import SeleniumManager
|
||||
|
||||
# 로깅 설정
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
||||
)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
WEATHER_URL = 'https://www.weather.go.kr/w/weather/forecast/short-term.do#dong/4148026200/37.73208578534846/126.79463099866948'
|
||||
OUTPUT_DIR = '/data'
|
||||
OUTPUT_FILENAME = f'weather_capture_{TODAY}.png'
|
||||
|
||||
def capture_weather():
|
||||
"""기상청 날씨 정보 캡처"""
|
||||
|
||||
# 저장 경로 설정
|
||||
output_path = os.path.join(OUTPUT_DIR, OUTPUT_FILENAME)
|
||||
|
||||
# 저장 디렉토리 생성 (없으면)
|
||||
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
||||
|
||||
manager = SeleniumManager()
|
||||
|
||||
try:
|
||||
with manager.managed_driver():
|
||||
logger.info(f"URL 접속: {WEATHER_URL}")
|
||||
manager.driver.get(WEATHER_URL)
|
||||
|
||||
# 첫 번째 탭 클릭
|
||||
logger.info("첫 번째 탭 클릭 시도...")
|
||||
if not manager.click_with_retry(manager.WEATHER_SELECTORS['tab_button']):
|
||||
logger.error("첫 번째 탭 클릭 실패")
|
||||
return False
|
||||
|
||||
# 두 번째 항목 클릭
|
||||
logger.info("두 번째 항목 클릭 시도...")
|
||||
if not manager.click_with_retry(manager.WEATHER_SELECTORS['list_button']):
|
||||
logger.error("두 번째 항목 클릭 실패")
|
||||
return False
|
||||
|
||||
# 페이지 반영 대기
|
||||
time.sleep(2)
|
||||
|
||||
# 스크린샷 저장
|
||||
logger.info(f"스크린샷 저장 시도: {output_path}")
|
||||
if manager.take_element_screenshot(manager.WEATHER_SELECTORS['target_element'], output_path):
|
||||
logger.info(f"[캡처 완료] 저장 위치: {output_path}")
|
||||
return True
|
||||
else:
|
||||
logger.error("스크린샷 저장 실패")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"[오류] 날씨 캡처 중 오류 발생: {type(e).__name__}: {e}", exc_info=True)
|
||||
return False
|
||||
|
||||
if __name__ == '__main__':
|
||||
success = capture_weather()
|
||||
sys.exit(0 if success else 1)
|
||||
Reference in New Issue
Block a user