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

Fix for #1076 #1109

Merged
merged 2 commits into from Oct 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 16 additions & 26 deletions yfinance/base.py
Expand Up @@ -29,6 +29,8 @@
import numpy as _np
import re as _re

from pytz import UnknownTimeZoneError

try:
from urllib.parse import quote as urlencode
except ImportError:
Expand Down Expand Up @@ -381,48 +383,36 @@ def _get_ticker_tz(self):

tz = utils.tz_cache.lookup(self.ticker)

if tz is not None:
invalid_value = not isinstance(tz, str)
if not invalid_value:
try:
_tz.timezone(tz)
except:
invalid_value = True

if invalid_value:
# Clear from cache and force re-fetch
utils.tz_cache.store(self.ticker, None)
tz = None
if tz and not utils.is_valid_timezone(tz):
# Clear from cache and force re-fetch
utils.tz_cache.store(self.ticker, None)
tz = None

if tz is None:
if not 'exchangeTimezoneName' in self.info:
try:
tz = self.info["exchangeTimezoneName"]
except KeyError:
return None
tz = self.info["exchangeTimezoneName"]
if not isinstance(tz, str):
tz = None
else:
try:
_tz.timezone(tz)
except:
tz = None
if tz is not None:

if utils.is_valid_timezone(tz):
# info fetch is relatively slow so cache timezone
utils.tz_cache.store(self.ticker, tz)
else:
tz = None

self._tz = tz
return tz


def _get_info(self, proxy=None):
# setup proxy in requests format
if proxy is not None:
if isinstance(proxy, dict) and "https" in proxy:
proxy = proxy["https"]
proxy = {"https": proxy}

if (self._info is None) or (self._sustainability is None) or (self._recommendations is None):
## Need to fetch
pass
else:
if None not in (self._info, self._sustainability, self._recommendations):
# No need to fetch
return

ticker_url = "{}/{}".format(self._scrape_url, self.ticker)
Expand Down
9 changes: 9 additions & 0 deletions yfinance/utils.py
Expand Up @@ -37,6 +37,8 @@

from threading import Lock

from pytz import UnknownTimeZoneError

try:
import ujson as _json
except ImportError:
Expand Down Expand Up @@ -442,6 +444,13 @@ def fix_Yahoo_dst_issue(df, interval):
df.index += _pd.TimedeltaIndex(dst_error_hours, 'h')
return df

def is_valid_timezone(tz: str) -> bool:
try:
_tz.timezone(tz)
except UnknownTimeZoneError:
return False
return True


class ProgressBar:
def __init__(self, iterations, text='completed'):
Expand Down