|
7 | 7 | from django.utils import timezone as django_timezone
|
8 | 8 | from django.core.files.base import ContentFile
|
9 | 9 | from .models import RSSFeed, RSSItem
|
| 10 | +from .utils_trans import translate_rssitem |
10 | 11 |
|
11 | 12 | def crawl_all_rss_feeds():
|
12 | 13 | """모든 활성화된 RSS 피드를 크롤링합니다."""
|
@@ -206,3 +207,55 @@ def crawl_rss_item_content():
|
206 | 207 | "item_id": pending_item.id,
|
207 | 208 | "error": error_msg
|
208 | 209 | }
|
| 210 | + |
| 211 | + |
| 212 | +@shared_task |
| 213 | +def translate_pending_rss_item(): |
| 214 | + """크롤링이 완료되고 번역 대기 상태인 RSS 아이템을 번역하는 태스크 (10분마다 실행)""" |
| 215 | + logfire.info("Starting RSS item translation") |
| 216 | + |
| 217 | + # 크롤링이 완료되고 번역 대기 상태이며 translated_contents가 없는 아이템 1개 가져오기 |
| 218 | + pending_item = RSSItem.objects.filter( |
| 219 | + crawling_status='completed', |
| 220 | + translate_status='pending' |
| 221 | + ).exclude( |
| 222 | + translated_contents__isnull=False |
| 223 | + ).order_by('-crawled_at', '-created_at').first() |
| 224 | + |
| 225 | + if not pending_item: |
| 226 | + logfire.info("No pending RSS items to translate") |
| 227 | + return {"status": "no_items", "message": "No pending items to translate"} |
| 228 | + |
| 229 | + logfire.info(f"Translating RSS item: {pending_item.title} ({pending_item.link})") |
| 230 | + |
| 231 | + try: |
| 232 | + # 번역 실행 |
| 233 | + translated_content = translate_rssitem(pending_item.id) |
| 234 | + |
| 235 | + # 번역 상태를 완료로 변경 |
| 236 | + pending_item.translate_status = 'completed' |
| 237 | + pending_item.translate_error_message = '' # 성공 시 에러 메시지 초기화 |
| 238 | + pending_item.save(update_fields=['translate_status', 'translate_error_message']) |
| 239 | + |
| 240 | + logfire.info(f"Successfully translated RSS item: {pending_item.title}") |
| 241 | + |
| 242 | + return { |
| 243 | + "status": "success", |
| 244 | + "item_id": pending_item.id, |
| 245 | + "item_title": pending_item.title, |
| 246 | + "translated_content_id": translated_content.id |
| 247 | + } |
| 248 | + |
| 249 | + except Exception as e: |
| 250 | + error_msg = f"Error translating RSS item {pending_item.id}: {str(e)}" |
| 251 | + pending_item.translate_status = 'failed' |
| 252 | + pending_item.translate_error_message = error_msg |
| 253 | + pending_item.save(update_fields=['translate_status', 'translate_error_message']) |
| 254 | + |
| 255 | + logfire.error(error_msg) |
| 256 | + |
| 257 | + return { |
| 258 | + "status": "failed", |
| 259 | + "item_id": pending_item.id, |
| 260 | + "error": error_msg |
| 261 | + } |
0 commit comments