JUNGU commited on
Commit
6a7ef8f
·
verified ·
1 Parent(s): fe51087

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +41 -10
src/streamlit_app.py CHANGED
@@ -96,19 +96,41 @@ menu = st.sidebar.radio(
96
  ["뉴스 기사 크롤링", "기사 분석하기", "새 기사 생성하기", "뉴스 기사 예약하기"]
97
  )
98
 
 
 
 
 
 
 
 
 
 
 
 
99
  # 저장된 기사를 불러오는 함수
100
  def load_saved_articles():
101
- os.makedirs(TMP_DIR, exist_ok=True) # /tmp 디렉토리 생성 보장
102
- if os.path.exists(SAVED_ARTICLES_PATH):
103
- with open(SAVED_ARTICLES_PATH, 'r', encoding='utf-8') as f:
104
- return json.load(f)
 
 
 
 
105
  return []
106
 
107
  # 기사를 저장하는 함수
108
  def save_articles(articles):
109
- os.makedirs(TMP_DIR, exist_ok=True) # /tmp 디렉토리 생성 보장
110
- with open(SAVED_ARTICLES_PATH, 'w', encoding='utf-8') as f:
111
- json.dump(articles, f, ensure_ascii=False, indent=2)
 
 
 
 
 
 
 
112
 
113
  @st.cache_data
114
  def crawl_naver_news(keyword, num_articles=5):
@@ -387,12 +409,21 @@ def perform_news_task(task_type, keyword, num_articles, file_prefix):
387
  time.sleep(0.5) # 서버 부하 방지
388
 
389
  # 결과 저장
390
- os.makedirs(SCHEDULED_NEWS_DIR, exist_ok=True) # 예약 뉴스 저장 디렉토리 생성
 
 
 
391
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
392
  filename = os.path.join(SCHEDULED_NEWS_DIR, f"{file_prefix}_{task_type}_{timestamp}.json")
393
 
394
- with open(filename, 'w', encoding='utf-8') as f:
395
- json.dump(articles, f, ensure_ascii=False, indent=2)
 
 
 
 
 
 
396
 
397
  global_scheduler_state.last_run = datetime.now()
398
  print(f"{datetime.now()} - {task_type} 뉴스 기사 수집 완료: {keyword}")
 
96
  ["뉴스 기사 크롤링", "기사 분석하기", "새 기사 생성하기", "뉴스 기사 예약하기"]
97
  )
98
 
99
+ # 디렉토리 생성 함수
100
+ def ensure_directory(directory):
101
+ try:
102
+ os.makedirs(directory, mode=0o777, exist_ok=True)
103
+ # 디렉토리 권한 설정
104
+ os.chmod(directory, 0o777)
105
+ except Exception as e:
106
+ st.error(f"디렉토리 생성 중 오류 발생: {str(e)}")
107
+ return False
108
+ return True
109
+
110
  # 저장된 기사를 불러오는 함수
111
  def load_saved_articles():
112
+ try:
113
+ ensure_directory(TMP_DIR)
114
+ if os.path.exists(SAVED_ARTICLES_PATH):
115
+ with open(SAVED_ARTICLES_PATH, 'r', encoding='utf-8') as f:
116
+ return json.load(f)
117
+ except Exception as e:
118
+ st.error(f"기사 로드 중 오류 발생: {str(e)}")
119
+ return []
120
  return []
121
 
122
  # 기사를 저장하는 함수
123
  def save_articles(articles):
124
+ try:
125
+ ensure_directory(TMP_DIR)
126
+ with open(SAVED_ARTICLES_PATH, 'w', encoding='utf-8') as f:
127
+ json.dump(articles, f, ensure_ascii=False, indent=2)
128
+ # 파일 권한 설정
129
+ os.chmod(SAVED_ARTICLES_PATH, 0o666)
130
+ except Exception as e:
131
+ st.error(f"기사 저장 중 오류 발생: {str(e)}")
132
+ return False
133
+ return True
134
 
135
  @st.cache_data
136
  def crawl_naver_news(keyword, num_articles=5):
 
409
  time.sleep(0.5) # 서버 부하 방지
410
 
411
  # 결과 저장
412
+ if not ensure_directory(SCHEDULED_NEWS_DIR):
413
+ print(f"스케줄된 뉴스 디렉토리 생성 실패")
414
+ return
415
+
416
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
417
  filename = os.path.join(SCHEDULED_NEWS_DIR, f"{file_prefix}_{task_type}_{timestamp}.json")
418
 
419
+ try:
420
+ with open(filename, 'w', encoding='utf-8') as f:
421
+ json.dump(articles, f, ensure_ascii=False, indent=2)
422
+ # 파일 권한 설정
423
+ os.chmod(filename, 0o666)
424
+ except Exception as e:
425
+ print(f"파일 저장 중 오류 발생: {e}")
426
+ return
427
 
428
  global_scheduler_state.last_run = datetime.now()
429
  print(f"{datetime.now()} - {task_type} 뉴스 기사 수집 완료: {keyword}")