-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a07c461
commit b6e4360
Showing
6 changed files
with
82 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from phi.tools import Toolkit | ||
|
||
try: | ||
from newspaper import Article | ||
except ImportError: | ||
raise ImportError("`newspaper3k` not installed.") | ||
|
||
|
||
class NewspaperToolkit(Toolkit): | ||
def __init__( | ||
self, | ||
get_article_text: bool = True, | ||
): | ||
super().__init__(name="newspaper_toolkit") | ||
|
||
if get_article_text: | ||
self.register(self.get_article_text) | ||
|
||
def get_article_text(self, url: str) -> str: | ||
"""Get the text of an article from a URL. | ||
Args: | ||
url (str): The URL of the article. | ||
Returns: | ||
str: The text of the article. | ||
""" | ||
|
||
try: | ||
article = Article(url) | ||
article.download() | ||
article.parse() | ||
return article.text | ||
except Exception as e: | ||
return f"Error getting article text from {url}: {e}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters