Spaces:
Sleeping
Sleeping
Update app.py
Browse files21 unmodified lines2223242526272829303 unmodified lines343536373839404142434445463 unmodified lines50515253545521 unmodified lines"""try:api_key = os.getenv("NEWSAPI_KEY") # Fetch the API key from environment variablesif not api_key:return "NewsAPI key is missing. Please set the NEWSAPI_KEY environment variable."url = f"https://newsapi.org/v2/everything?q={keyword}&pageSize={max_results}&apiKey={api_key}"# Make the API request3 unmodified lines# Parse the JSON responsedata = response.json()# Extract relevant information (titles and URLs)articles = data.get("articles", [])if not articles:return "No news articles found for the given keyword."# Format the resultsresult = "Here are the latest news articles:\n"for i, article in enumerate(articles, 1):title = article.get("title", "No title")url = article.get("url", "#")3 unmodified linesexcept requests.exceptions.RequestException as e:return f"Error fetching news articles: {str(e)}"final_answer = FinalAnswerTool()21 unmodified lines22232425262728293 unmodified lines333435363738394041424344453 unmodified lines495051525354555621 unmodified lines"""try:api_key = 'd43565ac0e674145a22c5f0de4dcd69c'# Construct the API URLurl = f"https://newsapi.org/v2/everything?q={keyword}&pageSize={max_results}&apiKey={api_key}"# Make the API request3 unmodified lines# Parse the JSON responsedata = response.json()# Check if the response contains articlesarticles = data.get("articles", [])if not articles:return f"No news articles found for the keyword '{keyword}'."# Format the resultsresult = f"Here are the latest news articles about '{keyword}':\n"for i, article in enumerate(articles, 1):title = article.get("title", "No title")url = article.get("url", "#")3 unmodified linesexcept requests.exceptions.RequestException as e:return f"Error fetching news articles: {str(e)}"except Exception as e:return f"An unexpected error occurred: {str(e)}"final_answer = FinalAnswerTool()
app.py+7-6