Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cookies as a workaround to fix earnings dates #2153

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions yfinance/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ def get_news(self, count=10, tab="news", proxy=None) -> list:
return self._news

@utils.log_indent_decorator
def get_earnings_dates(self, limit=12, proxy=None) -> Optional[pd.DataFrame]:
def get_earnings_dates(self, limit=12, proxy=None, add_cookies=None) -> Optional[pd.DataFrame]:
"""
Get earning dates (future and historic)

Expand All @@ -586,7 +586,8 @@ def get_earnings_dates(self, limit=12, proxy=None) -> Optional[pd.DataFrame]:
Default value 12 should return next 4 quarters and last 8 quarters.
Increase if more history is needed.
proxy: requests proxy to use.

add_cookies: additional cookies to use

Returns:
pd.DataFrame
"""
Expand All @@ -600,7 +601,7 @@ def get_earnings_dates(self, limit=12, proxy=None) -> Optional[pd.DataFrame]:
dates = None
while True:
url = f"{_ROOT_URL_}/calendar/earnings?day={date.today()}&symbol={self.ticker}&offset={page_offset}&size={page_size}"
data = self._data.cache_get(url=url, proxy=proxy).text
data = self._data.cache_get(url=url, proxy=proxy, add_cookies=add_cookies).text

if "Will be right back" in data:
raise RuntimeError("*** YAHOO! FINANCE IS CURRENTLY DOWN! ***\n"
Expand Down
14 changes: 9 additions & 5 deletions yfinance/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,15 +329,15 @@ def _get_cookie_and_crumb(self, proxy=None, timeout=30):
return cookie, crumb, strategy

@utils.log_indent_decorator
def get(self, url, user_agent_headers=None, params=None, proxy=None, timeout=30):
return self._make_request(url, request_method = self._session.get, user_agent_headers=user_agent_headers, params=params, proxy=proxy, timeout=timeout)
def get(self, url, user_agent_headers=None, params=None, proxy=None, timeout=30, add_cookies=None):
return self._make_request(url, request_method = self._session.get, user_agent_headers=user_agent_headers, params=params, proxy=proxy, timeout=timeout, add_cookies=add_cookies)

@utils.log_indent_decorator
def post(self, url, body, user_agent_headers=None, params=None, proxy=None, timeout=30):
return self._make_request(url, request_method = self._session.post, user_agent_headers=user_agent_headers, body=body, params=params, proxy=proxy, timeout=timeout)

@utils.log_indent_decorator
def _make_request(self, url, request_method, user_agent_headers=None, body=None, params=None, proxy=None, timeout=30):
def _make_request(self, url, request_method, user_agent_headers=None, body=None, params=None, proxy=None, timeout=30, add_cookies=None):
# Important: treat input arguments as immutable.

if len(url) > 200:
Expand All @@ -362,6 +362,10 @@ def _make_request(self, url, request_method, user_agent_headers=None, body=None,
cookies = {cookie.name: cookie.value}
else:
cookies = None
if cookies is None:
cookies = add_cookies
elif add_cookies is not None:
cookies.update(add_cookies)

request_args = {
'url': url,
Expand Down Expand Up @@ -394,8 +398,8 @@ def _make_request(self, url, request_method, user_agent_headers=None, body=None,

@lru_cache_freezeargs
@lru_cache(maxsize=cache_maxsize)
def cache_get(self, url, user_agent_headers=None, params=None, proxy=None, timeout=30):
return self.get(url, user_agent_headers, params, proxy, timeout)
def cache_get(self, url, user_agent_headers=None, params=None, proxy=None, timeout=30, add_cookies=None):
return self.get(url, user_agent_headers, params, proxy, timeout, add_cookies)

def _get_proxy(self, proxy):
# setup proxy in requests format
Expand Down