import gradio as gr import requests from datetime import datetime, timedelta import pycountry # NewsAPI key (이것을 실제 API 키로 대체해야 합니다) API_KEY = "37d83e266422487b8b2e4cb6e1ff0aa6" import gradio as gr import requests from datetime import datetime, timedelta import pycountry import json # NewsAPI key (이것을 실제 API 키로 대체해야 합니다) API_KEY = "YOUR_NEWSAPI_KEY_HERE" # 국가 코드 리스트 COUNTRY_CODES = "ae ar at au be bg br ca ch cn co cu cz de eg fr gb gr hk hu id ie il in it jp kr lt lv ma mx my ng nl no nz ph pl pt ro rs ru sa se sg si sk th tr tw ua us ve za".split() # 국가 코드와 이름 매핑 COUNTRIES = {'all': 'All Countries'} for code in COUNTRY_CODES: try: country = pycountry.countries.get(alpha_2=code.upper()) COUNTRIES[code] = country.name except AttributeError: COUNTRIES[code] = code.upper() def get_news(keyword, article_count, country): base_url = "https://newsapi.org/v2/top-headlines" if country != 'all' else "https://newsapi.org/v2/everything" two_days_ago = (datetime.utcnow() - timedelta(hours=48)).isoformat() params = { 'apiKey': API_KEY, 'q': keyword, 'from': two_days_ago, 'language': 'en', 'sortBy': 'publishedAt', 'pageSize': article_count } if country != 'all': params['country'] = country debug_info = f"API Request URL: {base_url}\n" debug_info += f"Parameters: {json.dumps(params, indent=2)}\n\n" try: response = requests.get(base_url, params=params, timeout=10) response.raise_for_status() news_data = response.json() debug_info += f"API Response Status: {response.status_code}\n" debug_info += f"API Response Headers: {json.dumps(dict(response.headers), indent=2)}\n\n" debug_info += f"API Response Body: {json.dumps(news_data, indent=2)}\n\n" except requests.RequestException as e: return f"
Error fetching news: {str(e)}
{debug_info}" if news_data['status'] != 'ok': return f"
API Error: {news_data.get('message', 'Unknown error occurred')}
{debug_info}" articles = news_data['articles'] if not articles: return (f"
No recent news found for the keyword '{keyword}' within the last 48 hours.
"
f"Try a different keyword or check back later.
{debug_info}") html_output = f"
{debug_info}