Skip to content

Commit

Permalink
updated base.py ticker to accommodate mutual funds and ETFs to show a…
Browse files Browse the repository at this point in the history
…nnualNetExpenseRatio in ticker.info
  • Loading branch information
ChNgineer committed Dec 9, 2023
1 parent 1d3ef4f commit 4e7344a
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Change Log
===========

0.2.33
------
Cookie fixes:
- fix backup strategy #1759
- fix Ticker(ISIN) #1760

0.2.32
------
Add cookie & crumb to requests #1657
Expand Down
15 changes: 15 additions & 0 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Code of Conduct

## Submitting a new issue

* Search through existing Issues and Discussions, in case your issue already exists and a solution is being developed.
* Ensure you read & follow the template form.
* Consider you may be the best person to investigate and fix.

## Contributing to an existing Issue

* Read the entire thread.
* Ensure your comment is contributing something new/useful. Remember you can simply react to other comments.
* Be concise:
- use the formatting options
- if replying to a big comment, instead of quoting it, link to it
2 changes: 1 addition & 1 deletion meta.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% set name = "yfinance" %}
{% set version = "0.2.32" %}
{% set version = "0.2.33" %}

package:
name: "{{ name|lower }}"
Expand Down
14 changes: 10 additions & 4 deletions yfinance/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ def __init__(self, ticker, session=None, proxy=None):

self._data: YfData = YfData(session=session)

self._analysis = Analysis(self._data, ticker)
self._holders = Holders(self._data, ticker)
self._quote = Quote(self._data, ticker)
self._fundamentals = Fundamentals(self._data, ticker)
self._analysis = Analysis(self._data, self.ticker)
self._holders = Holders(self._data, self.ticker)
self._quote = Quote(self._data, self.ticker)
self._fundamentals = Fundamentals(self._data, self.ticker)

self._fast_info = None

Expand Down Expand Up @@ -1741,6 +1741,12 @@ def get_mutualfund_holders(self, proxy=None, as_dict=False):
def get_info(self, proxy=None) -> dict:
self._quote.proxy = proxy or self.proxy
data = self._quote.info
try:
holders = self.get_institutional_holders()
expenseRatio = holders[holders[0] == 'Expense Ratio (net)'][1].values[0]
data['annualNetExpenseRatio'] = expenseRatio
except Exception:
pass
return data

def get_fast_info(self, proxy=None):
Expand Down
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
2 changes: 1 addition & 1 deletion yfinance/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = "0.2.32"
version = "0.2.33"

0 comments on commit 4e7344a

Please sign in to comment.