Add DuckDuckGo and Google News wrappers; refactor CryptoPanic and NewsAPI

- Implemented DuckDuckGoWrapper for news retrieval using DuckDuckGo tools.
- Added GoogleNewsWrapper for accessing Google News RSS feed.
- Refactored CryptoPanicWrapper to unify get_top_headlines and get_latest_news methods.
- Updated NewsApiWrapper to simplify top headlines retrieval.
- Added tests for DuckDuckGo and Google News wrappers.
- Enhanced documentation for CryptoPanicWrapper and NewsApiWrapper.
- Created base module for social media integrations.
This commit is contained in:
2025-09-30 12:24:43 +02:00
parent 40fb400a9c
commit dfe3b4ad90
14 changed files with 274 additions and 24 deletions

View File

@@ -1,18 +1,32 @@
from app.utils.wrapper_handler import WrapperHandler
from .base import NewsWrapper, Article
from .news_api import NewsApiWrapper
from .gnews_api import GnewsWrapper
from .gnews_api import GoogleNewsWrapper
from .cryptopanic_api import CryptoPanicWrapper
from .duckduckgo import DuckDuckGoWrapper
__all__ = ["NewsApiWrapper", "GnewsWrapper", "CryptoPanicWrapper"]
__all__ = ["NewsApiWrapper", "GoogleNewsWrapper", "CryptoPanicWrapper", "DuckDuckGoWrapper"]
class NewsAPIs(NewsWrapper):
"""
A wrapper class that aggregates multiple news API wrappers and tries them in order until one succeeds.
This class uses the WrapperHandler to manage multiple NewsWrapper instances.
It includes, and tries, the following news API wrappers in this order:
- GnewsWrapper
- DuckDuckGoWrapper
- NewsApiWrapper
- CryptoPanicWrapper
It provides methods to get top headlines and latest news by delegating the calls to the first successful wrapper.
If all wrappers fail, it raises an exception.
"""
def __init__(self):
wrappers = [GnewsWrapper, NewsApiWrapper, CryptoPanicWrapper]
wrappers = [GoogleNewsWrapper, DuckDuckGoWrapper, NewsApiWrapper, CryptoPanicWrapper]
self.wrapper_handler: WrapperHandler[NewsWrapper] = WrapperHandler.build_wrappers(wrappers)
def get_top_headlines(self, query: str, total: int = 100) -> list[Article]:
return self.wrapper_handler.try_call(lambda w: w.get_top_headlines(query, total))
def get_top_headlines(self, total: int = 100) -> list[Article]:
return self.wrapper_handler.try_call(lambda w: w.get_top_headlines(total))
def get_latest_news(self, query: str, total: int = 100) -> list[Article]:
return self.wrapper_handler.try_call(lambda w: w.get_latest_news(query, total))