Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
|
2 |
import gradio as gr
|
3 |
import requests
|
4 |
from datetime import datetime, timedelta
|
@@ -31,6 +30,7 @@ def get_news(keyword, article_count, country):
|
|
31 |
base_url = "https://newsapi.org/v2/top-headlines"
|
32 |
params = {
|
33 |
'apiKey': API_KEY,
|
|
|
34 |
'country': country_code,
|
35 |
'pageSize': article_count
|
36 |
}
|
@@ -50,34 +50,32 @@ def get_news(keyword, article_count, country):
|
|
50 |
debug_info += f"API Response Headers: {json.dumps(dict(response.headers), indent=2)}\n\n"
|
51 |
debug_info += f"API Response Body: {json.dumps(news_data, indent=2)}\n\n"
|
52 |
|
53 |
-
articles = news_data['articles']
|
54 |
-
|
55 |
-
# 키워드로 기사 필터링
|
56 |
-
if country_code != 'all':
|
57 |
-
articles = [article for article in articles if keyword.lower() in article['title'].lower() or keyword.lower() in article.get('description', '').lower()]
|
58 |
-
|
59 |
except requests.RequestException as e:
|
60 |
return f"<p style='color: red;'>Error fetching news: {str(e)}</p><pre>{debug_info}</pre>"
|
61 |
|
62 |
if news_data['status'] != 'ok':
|
63 |
return f"<p style='color: red;'>API Error: {news_data.get('message', 'Unknown error occurred')}</p><pre>{debug_info}</pre>"
|
|
|
|
|
64 |
|
65 |
if not articles:
|
66 |
return (f"<p>No recent news found for the keyword '<strong>{keyword}</strong>' in {country} within the last 48 hours.<br>"
|
67 |
f"Try a different keyword or check back later.</p><pre>{debug_info}</pre>")
|
68 |
|
69 |
html_output = f"<h2>News results for '{keyword}' in {country}</h2>"
|
70 |
-
for article in articles
|
71 |
title = article['title']
|
72 |
link = article['url']
|
73 |
pub_date = datetime.strptime(article['publishedAt'], "%Y-%m-%dT%H:%M:%SZ")
|
74 |
source = article.get('source', {}).get('name', 'Unknown Source')
|
|
|
75 |
|
76 |
html_output += f"""
|
77 |
<div style='margin-bottom: 20px; padding: 10px; border: 1px solid #ddd; border-radius: 5px;'>
|
78 |
<h3><a href='{link}' target='_blank' style='text-decoration: none; color: #1a0dab;'>{title}</a></h3>
|
79 |
<p style='color: #006621;'>{source}</p>
|
80 |
<p style='color: #545454;'>{pub_date.strftime('%Y-%m-%d %H:%M:%S')}</p>
|
|
|
81 |
</div>
|
82 |
"""
|
83 |
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
from datetime import datetime, timedelta
|
|
|
30 |
base_url = "https://newsapi.org/v2/top-headlines"
|
31 |
params = {
|
32 |
'apiKey': API_KEY,
|
33 |
+
'q': keyword,
|
34 |
'country': country_code,
|
35 |
'pageSize': article_count
|
36 |
}
|
|
|
50 |
debug_info += f"API Response Headers: {json.dumps(dict(response.headers), indent=2)}\n\n"
|
51 |
debug_info += f"API Response Body: {json.dumps(news_data, indent=2)}\n\n"
|
52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
except requests.RequestException as e:
|
54 |
return f"<p style='color: red;'>Error fetching news: {str(e)}</p><pre>{debug_info}</pre>"
|
55 |
|
56 |
if news_data['status'] != 'ok':
|
57 |
return f"<p style='color: red;'>API Error: {news_data.get('message', 'Unknown error occurred')}</p><pre>{debug_info}</pre>"
|
58 |
+
|
59 |
+
articles = news_data['articles']
|
60 |
|
61 |
if not articles:
|
62 |
return (f"<p>No recent news found for the keyword '<strong>{keyword}</strong>' in {country} within the last 48 hours.<br>"
|
63 |
f"Try a different keyword or check back later.</p><pre>{debug_info}</pre>")
|
64 |
|
65 |
html_output = f"<h2>News results for '{keyword}' in {country}</h2>"
|
66 |
+
for article in articles:
|
67 |
title = article['title']
|
68 |
link = article['url']
|
69 |
pub_date = datetime.strptime(article['publishedAt'], "%Y-%m-%dT%H:%M:%SZ")
|
70 |
source = article.get('source', {}).get('name', 'Unknown Source')
|
71 |
+
description = article.get('description', 'No description available')
|
72 |
|
73 |
html_output += f"""
|
74 |
<div style='margin-bottom: 20px; padding: 10px; border: 1px solid #ddd; border-radius: 5px;'>
|
75 |
<h3><a href='{link}' target='_blank' style='text-decoration: none; color: #1a0dab;'>{title}</a></h3>
|
76 |
<p style='color: #006621;'>{source}</p>
|
77 |
<p style='color: #545454;'>{pub_date.strftime('%Y-%m-%d %H:%M:%S')}</p>
|
78 |
+
<p>{description}</p>
|
79 |
</div>
|
80 |
"""
|
81 |
|