Skip to content

Commit

Permalink
Merge pull request #1759 from ranaroussi/hotfix/cookie-fallback-strategy
Browse files Browse the repository at this point in the history
Fix '_set_cookie_strategy'
  • Loading branch information
ValueRaider authored Dec 6, 2023
2 parents 1cfeddf + 1ab476b commit ca2040f
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions yfinance/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ def _set_cookie_strategy(self, strategy, have_lock=False):
self._cookie_lock.acquire()

try:
self._cookie_strategy = strategy
if self._cookie_strategy == 'csrf':
utils.get_yf_logger().debug(f'toggling cookie strategy {self._cookie_strategy} -> basic')
self._session.cookies.clear()
Expand Down Expand Up @@ -203,6 +202,7 @@ def _get_crumb_basic(self, proxy=None, timeout=30):
crumb_response = self._session.get(**get_args)
self._crumb = crumb_response.text
if self._crumb is None or '<html>' in self._crumb:
utils.get_yf_logger().debug("Didn't receive crumb")
return None

utils.get_yf_logger().debug(f"crumb = '{self._crumb}'")
Expand All @@ -215,7 +215,7 @@ def _get_cookie_and_crumb_basic(self, proxy, timeout):
return cookie, crumb

def _get_cookie_csrf(self, proxy, timeout):
if utils.reuse_cookie and self._cookie is not None:
if self._cookie is not None:
utils.get_yf_logger().debug('reusing cookie')
return True

Expand All @@ -239,6 +239,7 @@ def _get_cookie_csrf(self, proxy, timeout):
soup = BeautifulSoup(response.content, 'html.parser')
csrfTokenInput = soup.find('input', attrs={'name': 'csrfToken'})
if csrfTokenInput is None:
utils.get_yf_logger().debug('Failed to find "csrfToken" in response')
return False
csrfToken = csrfTokenInput['value']
utils.get_yf_logger().debug(f'csrfToken = {csrfToken}')
Expand Down Expand Up @@ -299,6 +300,7 @@ def _get_crumb_csrf(self, proxy=None, timeout=30):
self._crumb = r.text

if self._crumb is None or '<html>' in self._crumb or self._crumb == '':
utils.get_yf_logger().debug("Didn't receive crumb")
return None

utils.get_yf_logger().debug(f"crumb = '{self._crumb}'")
Expand Down Expand Up @@ -363,6 +365,7 @@ def get(self, url, user_agent_headers=None, params=None, proxy=None, timeout=30)
'headers': user_agent_headers or self.user_agent_headers
}
response = self._session.get(**request_args)
utils.get_yf_logger().debug(f'response code={response.status_code}')
if response.status_code >= 400:
# Retry with other cookie strategy
if strategy == 'basic':
Expand All @@ -374,6 +377,7 @@ def get(self, url, user_agent_headers=None, params=None, proxy=None, timeout=30)
if strategy == 'basic':
request_args['cookies'] = {cookie.name: cookie.value}
response = self._session.get(**request_args)
utils.get_yf_logger().debug(f'response code={response.status_code}')

return response

Expand Down

0 comments on commit ca2040f

Please sign in to comment.