Spaces:
Running
Running
import gradio as gr | |
import requests | |
from datetime import datetime, timedelta | |
import json | |
# NewsAPI key (์ด๊ฒ์ ์ค์ API ํค๋ก ๋์ฒดํด์ผ ํฉ๋๋ค) | |
API_KEY = "37d83e266422487b8b2e4cb6e1ff0aa6" | |
def get_news(keyword): | |
base_url = "https://newsapi.org/v2/everything" | |
# 48์๊ฐ ์ ์ ๋ ์ง๋ฅผ ISO ํ์์ผ๋ก ์ป๊ธฐ | |
two_days_ago = (datetime.utcnow() - timedelta(hours=48)).isoformat() | |
params = { | |
'apiKey': API_KEY, | |
'q': keyword, | |
'from': two_days_ago, | |
'language': 'en', | |
'sortBy': 'publishedAt' | |
} | |
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)}\n\nDebug Info:\n{debug_info}" | |
if news_data['status'] != 'ok': | |
return f"API Error: {news_data.get('message', 'Unknown error occurred')}\n\nDebug Info:\n{debug_info}" | |
articles = news_data['articles'] | |
if not articles: | |
return (f"No recent news found for the keyword '{keyword}' within the last 48 hours.\n" | |
f"Try a different keyword or check back later.\n\nDebug Info:\n{debug_info}") | |
filtered_news = [] | |
for article in articles[:10]: # ์ต๋ 10๊ฐ์ ๊ธฐ์ฌ๋ง ํ์ | |
title = article['title'] | |
link = article['url'] | |
pub_date = datetime.strptime(article['publishedAt'], "%Y-%m-%dT%H:%M:%SZ") | |
filtered_news.append(f"Title: {title}\nLink: {link}\nDate: {pub_date}\n") | |
result = "\n".join(filtered_news) | |
return f"{result}\n\nDebug Info:\n{debug_info}" | |
iface = gr.Interface( | |
fn=get_news, | |
inputs=[ | |
gr.Textbox(label="Enter keyword") | |
], | |
outputs="text", | |
title="News Search (Debug Version)", | |
description="Search for news articles from the last 48 hours using NewsAPI. This version includes debug information." | |
) | |
iface.launch() |