|
30 | 30 | import pandas as pd |
31 | 31 | import requests |
32 | 32 |
|
33 | | -from . import utils, cache, Search |
| 33 | +from . import utils, cache |
34 | 34 | from .data import YfData |
35 | 35 | from .exceptions import YFEarningsDateMissing |
36 | 36 | from .scrapers.analysis import Analysis |
@@ -534,19 +534,45 @@ def get_isin(self, proxy=None) -> Optional[str]: |
534 | 534 | self._isin = data.split(search_str)[1].split('"')[0].split('|')[0] |
535 | 535 | return self._isin |
536 | 536 |
|
537 | | - def get_news(self, proxy=None) -> list: |
| 537 | + def get_news(self, count=10, tab="news", proxy=None) -> list: |
| 538 | + """Allowed options for tab: "news", "all", "press releases""" |
538 | 539 | if self._news: |
539 | 540 | return self._news |
540 | 541 |
|
541 | | - search = Search( |
542 | | - query=self.ticker, |
543 | | - news_count=10, |
544 | | - session=self.session, |
545 | | - proxy=proxy, |
546 | | - raise_errors=True |
547 | | - ) |
548 | | - self._news = search.news |
| 542 | + logger = utils.get_yf_logger() |
| 543 | + |
| 544 | + tab_queryrefs = { |
| 545 | + "all": "newsAll", |
| 546 | + "news": "latestNews", |
| 547 | + "press releases": "pressRelease", |
| 548 | + } |
| 549 | + |
| 550 | + query_ref = tab_queryrefs.get(tab.lower()) |
| 551 | + if not query_ref: |
| 552 | + raise ValueError(f"Invalid tab name '{tab}'. Choose from: {', '.join(tab_queryrefs.keys())}") |
| 553 | + |
| 554 | + url = f"{_ROOT_URL_}/xhr/ncp?queryRef={query_ref}&serviceKey=ncp_fin" |
| 555 | + payload = { |
| 556 | + "serviceConfig": { |
| 557 | + "snippetCount": count, |
| 558 | + "s": [self.ticker] |
| 559 | + } |
| 560 | + } |
| 561 | + |
| 562 | + data = self._data.post(url, body=payload, proxy=proxy) |
| 563 | + if data is None or "Will be right back" in data.text: |
| 564 | + raise RuntimeError("*** YAHOO! FINANCE IS CURRENTLY DOWN! ***\n" |
| 565 | + "Our engineers are working quickly to resolve " |
| 566 | + "the issue. Thank you for your patience.") |
| 567 | + try: |
| 568 | + data = data.json() |
| 569 | + except _json.JSONDecodeError: |
| 570 | + logger.error(f"{self.ticker}: Failed to retrieve the news and received faulty response instead.") |
| 571 | + data = {} |
| 572 | + |
| 573 | + news = data.get("data", {}).get("tickerStream", {}).get("stream", []) |
549 | 574 |
|
| 575 | + self._news = [article for article in news if not article.get('ad', [])] |
550 | 576 | return self._news |
551 | 577 |
|
552 | 578 | @utils.log_indent_decorator |
|
0 commit comments