Spaces:
Sleeping
Sleeping
File size: 1,333 Bytes
833b2a0 9bcd8b3 c6a3fb3 6651350 833b2a0 6651350 833b2a0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
from typing import Any, Optional
from smolagents.tools import Tool
class GetNewsTool(Tool):
name = "Get_News"
description = "Fetches the latest NYTimes RSS feed and returns the headlines and URLs of the most recent 5 stories."
inputs = {} # This tool does not require any inputs.
output_type = "string"
def __init__(self, feed_url: str = "https://rss.nytimes.com/services/xml/rss/nyt/HomePage.xml", max_results: int = 5, **kwargs):
super().__init__()
self.feed_url = feed_url
self.max_results = max_results
try:
import feedparser
except ImportError as e:
raise ImportError(
"You must install package `feedparser` to run this tool: for instance run `pip install feedparser`."
) from e
self.feedparser = feedparser
def forward(self) -> str:
feed = self.feedparser.parse(self.feed_url)
if not feed.entries:
raise Exception("No entries found in the feed.")
latest_entries = feed.entries[:self.max_results]
# Format each entry with its headline as a clickable link pointing to the URL.
result = "## NYTimes Latest Headlines and URLs\n\n" + "\n".join(
f"- [{entry.title}]({entry.link})" for entry in latest_entries
)
return result
|