Skip to content

Commit

Permalink
Merge pull request #2191 from R5dan/Update-Search
Browse files Browse the repository at this point in the history
Update Search class to allow more options
  • Loading branch information
ValueRaider authored Dec 30, 2024
2 parents a928eb4 + b94173b commit d0e5920
Showing 1 changed file with 70 additions and 9 deletions.
79 changes: 70 additions & 9 deletions yfinance/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@


class Search:
def __init__(self, query, max_results=8, news_count=8, enable_fuzzy_query=False,
def __init__(self, query, max_results=8, news_count=8, lists_count=8, include_cb=True, include_nav_links=False, include_research=False, include_cultural_assets=False, enable_fuzzy_query=False, recommended=8,
session=None, proxy=None, timeout=30, raise_errors=True):
"""
Fetches and organizes search results from Yahoo Finance, including stock quotes and news articles.
Expand All @@ -36,7 +36,13 @@ def __init__(self, query, max_results=8, news_count=8, enable_fuzzy_query=False,
query: The search query (ticker symbol or company name).
max_results: Maximum number of stock quotes to return (default 8).
news_count: Number of news articles to include (default 8).
lists_count: Number of lists to include (default 8).
include_cb: Include the company breakdown (default True).
include_nav_links: Include the navigation links (default False).
include_research: Include the research reports (default False).
include_cultural_assets: Include the cultural assets (default False).
enable_fuzzy_query: Enable fuzzy search for typos (default False).
recommended: Recommended number of results to return (default 8).
session: Custom HTTP session for requests (default None).
proxy: Proxy settings for requests (default None).
timeout: Request timeout in seconds (default 30).
Expand All @@ -51,22 +57,42 @@ def __init__(self, query, max_results=8, news_count=8, enable_fuzzy_query=False,
self.timeout = timeout
self.raise_errors = raise_errors

self.lists_count = lists_count
self.include_cb = include_cb
self.nav_links = include_nav_links
self.enable_research = include_research
self.enable_cultural_assets = include_cultural_assets
self.recommended = recommended

self._data = YfData(session=self.session)
self._logger = utils.get_yf_logger()

self._response = self._fetch_results()
self._quotes = self._response.get("quotes", [])
self._news = self._response.get("news", [])
self._response = {}
self._all = {}
self._quotes = []
self._news = []
self._lists = []
self._research = []
self._nav = []

self.search()

def _fetch_results(self):
def search(self) -> 'Search':
"""Search using the query parameters defined in the constructor."""
url = f"{_BASE_URL_}/v1/finance/search"
params = {
"q": self.query,
"quotesCount": self.max_results,
"enableFuzzyQuery": self.enable_fuzzy_query,
"newsCount": self.news_count,
"quotesQueryId": "tss_match_phrase_query",
"newsQueryId": "news_cie_vespa"
"newsQueryId": "news_cie_vespa",
"listsCount": self.lists_count,
"enableCb": self.include_cb,
"enableNavLinks": self.nav_links,
"enableResearchReports": self.enable_research,
"enableCulturalAssets": self.enable_cultural_assets,
"recommendedCount": self.recommended
}

self._logger.debug(f'{self.query}: Yahoo GET parameters: {str(dict(params))}')
Expand All @@ -82,14 +108,49 @@ def _fetch_results(self):
self._logger.error(f"{self.query}: Failed to retrieve the news and received faulty response instead.")
data = {}

return data
self._response = data
self._quotes = data.get("quotes", [])
self._news = data.get("news", [])
self._lists = data.get("lists", [])
self._research = data.get("researchReports", [])
self._nav = data.get("nav", [])

self._all = {"quotes": self._quotes, "news": self._news, "lists": self._lists, "research": self._research, "nav": self._nav}

return self


@property
def quotes(self):
def quotes(self) -> 'list':
"""Get the quotes from the search results."""
return self._quotes

@property
def news(self):
def news(self) -> 'list':
"""Get the news from the search results."""
return self._news

@property
def lists(self) -> 'list':
"""Get the lists from the search results."""
return self._lists

@property
def research(self) -> 'list':
"""Get the research reports from the search results."""
return self._research

@property
def nav(self) -> 'list':
"""Get the navigation links from the search results."""
return self._nav

@property
def all(self) -> 'dict[str,list]':
"""Get all the results from the search results: filtered down version of response."""
return self._all

@property
def response(self) -> 'dict':
"""Get the raw response from the search results."""
return self._response

0 comments on commit d0e5920

Please sign in to comment.