Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,3 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
import gradio as gr
|
| 4 |
import requests
|
| 5 |
from datetime import datetime, timedelta
|
|
@@ -11,7 +9,6 @@ API_KEY = "37d83e266422487b8b2e4cb6e1ff0aa6"
|
|
| 11 |
def get_news(keyword):
|
| 12 |
base_url = "https://newsapi.org/v2/everything"
|
| 13 |
|
| 14 |
-
# 48์๊ฐ ์ ์ ๋ ์ง๋ฅผ ISO ํ์์ผ๋ก ์ป๊ธฐ
|
| 15 |
two_days_ago = (datetime.utcnow() - timedelta(hours=48)).isoformat()
|
| 16 |
|
| 17 |
params = {
|
|
@@ -22,48 +19,48 @@ def get_news(keyword):
|
|
| 22 |
'sortBy': 'publishedAt'
|
| 23 |
}
|
| 24 |
|
| 25 |
-
debug_info = f"API Request URL: {base_url}\n"
|
| 26 |
-
debug_info += f"Parameters: {json.dumps(params, indent=2)}\n\n"
|
| 27 |
-
|
| 28 |
try:
|
| 29 |
response = requests.get(base_url, params=params, timeout=10)
|
| 30 |
response.raise_for_status()
|
| 31 |
news_data = response.json()
|
| 32 |
-
|
| 33 |
-
debug_info += f"API Response Status: {response.status_code}\n"
|
| 34 |
-
debug_info += f"API Response Headers: {json.dumps(dict(response.headers), indent=2)}\n\n"
|
| 35 |
-
debug_info += f"API Response Body: {json.dumps(news_data, indent=2)}\n\n"
|
| 36 |
-
|
| 37 |
except requests.RequestException as e:
|
| 38 |
-
return f"Error fetching news: {str(e)}
|
| 39 |
|
| 40 |
if news_data['status'] != 'ok':
|
| 41 |
-
return f"API Error: {news_data.get('message', 'Unknown error occurred')}
|
| 42 |
|
| 43 |
articles = news_data['articles']
|
| 44 |
|
| 45 |
if not articles:
|
| 46 |
-
return (f"No recent news found for the keyword '{keyword}' within the last 48 hours
|
| 47 |
-
f"Try a different keyword or check back later
|
| 48 |
|
| 49 |
-
|
| 50 |
for article in articles[:10]: # ์ต๋ 10๊ฐ์ ๊ธฐ์ฌ๋ง ํ์
|
| 51 |
title = article['title']
|
| 52 |
link = article['url']
|
| 53 |
pub_date = datetime.strptime(article['publishedAt'], "%Y-%m-%dT%H:%M:%SZ")
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
|
| 57 |
-
return f"{result}\n\nDebug Info:\n{debug_info}"
|
| 58 |
|
| 59 |
iface = gr.Interface(
|
| 60 |
fn=get_news,
|
| 61 |
inputs=[
|
| 62 |
gr.Textbox(label="Enter keyword")
|
| 63 |
],
|
| 64 |
-
outputs=
|
| 65 |
-
title="News Search
|
| 66 |
-
description="Search for news articles from the last 48 hours using NewsAPI.
|
|
|
|
| 67 |
)
|
| 68 |
|
| 69 |
iface.launch()
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
from datetime import datetime, timedelta
|
|
|
|
| 9 |
def get_news(keyword):
|
| 10 |
base_url = "https://newsapi.org/v2/everything"
|
| 11 |
|
|
|
|
| 12 |
two_days_ago = (datetime.utcnow() - timedelta(hours=48)).isoformat()
|
| 13 |
|
| 14 |
params = {
|
|
|
|
| 19 |
'sortBy': 'publishedAt'
|
| 20 |
}
|
| 21 |
|
|
|
|
|
|
|
|
|
|
| 22 |
try:
|
| 23 |
response = requests.get(base_url, params=params, timeout=10)
|
| 24 |
response.raise_for_status()
|
| 25 |
news_data = response.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
except requests.RequestException as e:
|
| 27 |
+
return f"<p style='color: red;'>Error fetching news: {str(e)}</p>"
|
| 28 |
|
| 29 |
if news_data['status'] != 'ok':
|
| 30 |
+
return f"<p style='color: red;'>API Error: {news_data.get('message', 'Unknown error occurred')}</p>"
|
| 31 |
|
| 32 |
articles = news_data['articles']
|
| 33 |
|
| 34 |
if not articles:
|
| 35 |
+
return (f"<p>No recent news found for the keyword '<strong>{keyword}</strong>' within the last 48 hours.<br>"
|
| 36 |
+
f"Try a different keyword or check back later.</p>")
|
| 37 |
|
| 38 |
+
html_output = f"<h2>News results for '{keyword}'</h2>"
|
| 39 |
for article in articles[:10]: # ์ต๋ 10๊ฐ์ ๊ธฐ์ฌ๋ง ํ์
|
| 40 |
title = article['title']
|
| 41 |
link = article['url']
|
| 42 |
pub_date = datetime.strptime(article['publishedAt'], "%Y-%m-%dT%H:%M:%SZ")
|
| 43 |
+
source = article.get('source', {}).get('name', 'Unknown Source')
|
| 44 |
+
|
| 45 |
+
html_output += f"""
|
| 46 |
+
<div style='margin-bottom: 20px; padding: 10px; border: 1px solid #ddd; border-radius: 5px;'>
|
| 47 |
+
<h3><a href='{link}' target='_blank' style='text-decoration: none; color: #1a0dab;'>{title}</a></h3>
|
| 48 |
+
<p style='color: #006621;'>{source}</p>
|
| 49 |
+
<p style='color: #545454;'>{pub_date.strftime('%Y-%m-%d %H:%M:%S')}</p>
|
| 50 |
+
</div>
|
| 51 |
+
"""
|
| 52 |
|
| 53 |
+
return html_output
|
|
|
|
| 54 |
|
| 55 |
iface = gr.Interface(
|
| 56 |
fn=get_news,
|
| 57 |
inputs=[
|
| 58 |
gr.Textbox(label="Enter keyword")
|
| 59 |
],
|
| 60 |
+
outputs=gr.HTML(),
|
| 61 |
+
title="Visual News Search",
|
| 62 |
+
description="Search for news articles from the last 48 hours using NewsAPI.",
|
| 63 |
+
theme=gr.themes.Soft()
|
| 64 |
)
|
| 65 |
|
| 66 |
iface.launch()
|