Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -22,9 +22,8 @@ def news_aggregator(keyword: str, max_results: int = 5) -> str:
|
|
| 22 |
"""
|
| 23 |
try:
|
| 24 |
|
| 25 |
-
api_key =
|
| 26 |
-
|
| 27 |
-
return "NewsAPI key is missing. Please set the NEWSAPI_KEY environment variable."
|
| 28 |
url = f"https://newsapi.org/v2/everything?q={keyword}&pageSize={max_results}&apiKey={api_key}"
|
| 29 |
|
| 30 |
# Make the API request
|
|
@@ -34,13 +33,13 @@ def news_aggregator(keyword: str, max_results: int = 5) -> str:
|
|
| 34 |
# Parse the JSON response
|
| 35 |
data = response.json()
|
| 36 |
|
| 37 |
-
#
|
| 38 |
articles = data.get("articles", [])
|
| 39 |
if not articles:
|
| 40 |
-
return "No news articles found for the
|
| 41 |
|
| 42 |
# Format the results
|
| 43 |
-
result = "Here are the latest news articles:\n"
|
| 44 |
for i, article in enumerate(articles, 1):
|
| 45 |
title = article.get("title", "No title")
|
| 46 |
url = article.get("url", "#")
|
|
@@ -50,6 +49,8 @@ def news_aggregator(keyword: str, max_results: int = 5) -> str:
|
|
| 50 |
|
| 51 |
except requests.exceptions.RequestException as e:
|
| 52 |
return f"Error fetching news articles: {str(e)}"
|
|
|
|
|
|
|
| 53 |
|
| 54 |
|
| 55 |
final_answer = FinalAnswerTool()
|
|
|
|
| 22 |
"""
|
| 23 |
try:
|
| 24 |
|
| 25 |
+
api_key = 'd43565ac0e674145a22c5f0de4dcd69c'
|
| 26 |
+
# Construct the API URL
|
|
|
|
| 27 |
url = f"https://newsapi.org/v2/everything?q={keyword}&pageSize={max_results}&apiKey={api_key}"
|
| 28 |
|
| 29 |
# Make the API request
|
|
|
|
| 33 |
# Parse the JSON response
|
| 34 |
data = response.json()
|
| 35 |
|
| 36 |
+
# Check if the response contains articles
|
| 37 |
articles = data.get("articles", [])
|
| 38 |
if not articles:
|
| 39 |
+
return f"No news articles found for the keyword '{keyword}'."
|
| 40 |
|
| 41 |
# Format the results
|
| 42 |
+
result = f"Here are the latest news articles about '{keyword}':\n"
|
| 43 |
for i, article in enumerate(articles, 1):
|
| 44 |
title = article.get("title", "No title")
|
| 45 |
url = article.get("url", "#")
|
|
|
|
| 49 |
|
| 50 |
except requests.exceptions.RequestException as e:
|
| 51 |
return f"Error fetching news articles: {str(e)}"
|
| 52 |
+
except Exception as e:
|
| 53 |
+
return f"An unexpected error occurred: {str(e)}"
|
| 54 |
|
| 55 |
|
| 56 |
final_answer = FinalAnswerTool()
|