Skip to content

Commit

Permalink
Experimental code to add cookie & crumb to requests
Browse files Browse the repository at this point in the history
  • Loading branch information
ValueRaider committed Aug 7, 2023
1 parent e1f94ed commit 48545a1
Showing 1 changed file with 51 additions and 1 deletion.
52 changes: 51 additions & 1 deletion yfinance/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ def wrapped(*args, **kwargs):
class TickerData:
"""
Have one place to retrieve data from Yahoo API in order to ease caching and speed up operations
Credit for code for cookie & crumb goes to StackOverflow:
https://stackoverflow.com/questions/76065035/yahoo-finance-v7-api-now-requiring-cookies-python
"""
user_agent_headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
Expand All @@ -47,11 +50,58 @@ def __init__(self, ticker: str, session=None):
self.ticker = ticker
self._session = session or requests

def get(self, url, user_agent_headers=None, params=None, proxy=None, timeout=30):
self._cookie, self._crumb = None, None

def _get_cookie(self, proxy=None, timeout=30):
if self._cookie is not None:
return self._cookie

# response = self.get('https://fc.yahoo.com')
# To avoid infinite recursion, do NOT use self.get()
response = self._session.get(
url='https://fc.yahoo.com',
headers=self.user_agent_headers,
proxies=proxy,
timeout=timeout)

if not response.cookies:
raise Exception("Failed to obtain Yahoo auth cookie.")
self._cookie = list(response.cookies)[0]
return self._cookie

def _get_crumb(self, proxy=None, timeout=30):
if self._crumb is not None:
return self._crumb
cookie = self._get_cookie()

crumb_response = self._session.get(
url="https://query1.finance.yahoo.com/v1/test/getcrumb",
headers=self.user_agent_headers,
cookies={cookie.name: cookie.value},
proxies=proxy,
timeout=timeout)

self._crumb = crumb_response.text
return self._crumb


def get(self, url, user_agent_headers=None, params=None, cookies=None, proxy=None, timeout=30):
proxy = self._get_proxy(proxy)

# Add cookie & crumb
if cookies is None:
cookie = self._get_cookie()
cookies = {cookie.name: cookie.value}
if params is None:
params = {}
if 'crumb' not in params:
params['crumb'] = self._get_crumb()

response = self._session.get(
# response = requests.get(
url=url,
params=params,
cookies=cookies,
proxies=proxy,
timeout=timeout,
headers=user_agent_headers or self.user_agent_headers)
Expand Down

0 comments on commit 48545a1

Please sign in to comment.