fix: /idea 타임아웃 — 리다이렉트/크롤링 제거하고 RSS만 파싱

NAS→Google 뉴스 리다이렉트 추적이 매우 느려서 Telegram 타임아웃 발생.
RSS 피드 파싱만으로 제목/설명 수집, URL 변환은 글 작성 시점에 처리.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
JOUNGWOOK KWON
2026-03-30 15:36:01 +09:00
parent c836c720da
commit f25a95440a

View File

@@ -783,12 +783,11 @@ async def cmd_idea(update: Update, context: ContextTypes.DEFAULT_TYPE):
def _search_and_build_topic(keyword: str, corner: str = '') -> dict: def _search_and_build_topic(keyword: str, corner: str = '') -> dict:
"""키워드로 Google 뉴스 검색 → 관련 기사 수집 → topic_data 생성""" """키워드로 Google 뉴스 검색 → 관련 기사 수집 → topic_data 생성 (빠른 버전)"""
import requests
import feedparser import feedparser
from urllib.parse import quote from urllib.parse import quote
# Google 뉴스 RSS로 검색 # Google 뉴스 RSS로 검색 (리다이렉트 추적 없이 빠르게)
search_url = f"https://news.google.com/rss/search?q={quote(keyword)}&hl=ko&gl=KR&ceid=KR:ko" search_url = f"https://news.google.com/rss/search?q={quote(keyword)}&hl=ko&gl=KR&ceid=KR:ko"
sources = [] sources = []
best_description = '' best_description = ''
@@ -800,35 +799,18 @@ def _search_and_build_topic(keyword: str, corner: str = '') -> dict:
title = entry.get('title', '') title = entry.get('title', '')
link = entry.get('link', '') link = entry.get('link', '')
pub_date = entry.get('published', '') pub_date = entry.get('published', '')
# RSS description에서 설명 추출
desc = entry.get('summary', '') or entry.get('description', '')
if desc and not best_description:
# HTML 태그 제거
import re as _re
best_description = _re.sub(r'<[^>]+>', '', desc).strip()[:300]
# Google 뉴스 RSS 제목에서 "- 매체명" 분리
sources.append({ sources.append({
'url': link, 'url': link,
'title': title, 'title': title,
'date': pub_date, 'date': pub_date,
}) })
# 첫 번째 기사만 리다이렉트 추적 + 크롤링 (속도 최적화)
if sources and 'news.google.com' in sources[0]['url']:
try:
resp = requests.head(sources[0]['url'], timeout=5, allow_redirects=True,
headers={'User-Agent': 'Mozilla/5.0'})
if resp.url and 'news.google.com' not in resp.url:
sources[0]['url'] = resp.url
# og:description, og:image 크롤링
from bs4 import BeautifulSoup
page = requests.get(resp.url, timeout=5,
headers={'User-Agent': 'Mozilla/5.0'})
if page.status_code == 200:
soup = BeautifulSoup(page.text, 'lxml')
og_desc = soup.find('meta', property='og:description')
if og_desc and og_desc.get('content'):
best_description = og_desc['content'].strip()[:300]
og_img = soup.find('meta', property='og:image')
if og_img and og_img.get('content', '').startswith('http'):
best_image = og_img['content']
except Exception:
pass
except Exception: except Exception:
pass pass