from typing import Annotated from datetime import datetime from dateutil.relativedelta import relativedelta from .googlenews_utils import getNewsData def get_google_news_for_ticker( ticker: Annotated[str, "Stock ticker symbol"], start_date: Annotated[str, "Start date in yyyy-mm-dd format"], end_date: Annotated[str, "End date in yyyy-mm-dd format"], ) -> str: """Adapter for get_google_news that accepts standard news API parameters. Converts (ticker, start_date, end_date) to (query, curr_date, look_back_days) format expected by get_google_news. """ # Calculate look_back_days from date range start_dt = datetime.strptime(start_date, "%Y-%m-%d") end_dt = datetime.strptime(end_date, "%Y-%m-%d") look_back_days = (end_dt - start_dt).days # Use end_date as curr_date and ticker as query return get_google_news(ticker, end_date, look_back_days) def get_google_global_news( curr_date: Annotated[str, "Current date in yyyy-mm-dd format"], look_back_days: Annotated[int, "Number of days to look back"] = 7, limit: Annotated[int, "Maximum number of articles to return"] = 5, ) -> str: """Wrapper for global news that uses Google News with market/economy query.""" query = "stock+market+economy+finance" return get_google_news(query, curr_date, look_back_days) def get_google_news( query: Annotated[str, "Query to search with"], curr_date: Annotated[str, "Curr date in yyyy-mm-dd format"], look_back_days: Annotated[int, "how many days to look back"], ) -> str: query = query.replace(" ", "+") start_date = datetime.strptime(curr_date, "%Y-%m-%d") before = start_date - relativedelta(days=look_back_days) before = before.strftime("%Y-%m-%d") news_results = getNewsData(query, before, curr_date) news_str = "" for news in news_results: news_str += ( f"### {news['title']} (source: {news['source']}) \n\n{news['snippet']}\n\n" ) if len(news_results) == 0: return "" return f"## {query} Google News, from {before} to {curr_date}:\n\n{news_str}"