Skip to content

Commit

Permalink
Merge pull request #2173 from dhruvan2006/feature/full-news
Browse files Browse the repository at this point in the history
Feature: Fetch upto 200 news articles
  • Loading branch information
ValueRaider authored Dec 14, 2024
2 parents 3ac8539 + 5bc3ec0 commit 59d0974
Showing 1 changed file with 36 additions and 10 deletions.
46 changes: 36 additions & 10 deletions yfinance/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import pandas as pd
import requests

from . import utils, cache, Search
from . import utils, cache
from .data import YfData
from .exceptions import YFEarningsDateMissing
from .scrapers.analysis import Analysis
Expand Down Expand Up @@ -534,19 +534,45 @@ def get_isin(self, proxy=None) -> Optional[str]:
self._isin = data.split(search_str)[1].split('"')[0].split('|')[0]
return self._isin

def get_news(self, proxy=None) -> list:
def get_news(self, count=10, tab="news", proxy=None) -> list:
"""Allowed options for tab: "news", "all", "press releases"""
if self._news:
return self._news

search = Search(
query=self.ticker,
news_count=10,
session=self.session,
proxy=proxy,
raise_errors=True
)
self._news = search.news
logger = utils.get_yf_logger()

tab_queryrefs = {
"all": "newsAll",
"news": "latestNews",
"press releases": "pressRelease",
}

query_ref = tab_queryrefs.get(tab.lower())
if not query_ref:
raise ValueError(f"Invalid tab name '{tab}'. Choose from: {', '.join(tab_queryrefs.keys())}")

url = f"{_ROOT_URL_}/xhr/ncp?queryRef={query_ref}&serviceKey=ncp_fin"
payload = {
"serviceConfig": {
"snippetCount": count,
"s": [self.ticker]
}
}

data = self._data.post(url, body=payload, proxy=proxy)
if data is None or "Will be right back" in data.text:
raise RuntimeError("*** YAHOO! FINANCE IS CURRENTLY DOWN! ***\n"
"Our engineers are working quickly to resolve "
"the issue. Thank you for your patience.")
try:
data = data.json()
except _json.JSONDecodeError:
logger.error(f"{self.ticker}: Failed to retrieve the news and received faulty response instead.")
data = {}

news = data.get("data", {}).get("tickerStream", {}).get("stream", [])

self._news = [article for article in news if not article.get('ad', [])]
return self._news

@utils.log_indent_decorator
Expand Down

0 comments on commit 59d0974

Please sign in to comment.