Raise error if rate limited

This commit is contained in:
Dhruvan Gnanadhandayuthapani
2024-12-22 11:12:19 +01:00
parent 64b4726c4e
commit af7806640f
3 changed files with 15 additions and 1 deletions

View File

@@ -10,6 +10,8 @@ from frozendict import frozendict
from . import utils, cache
import threading
from .exceptions import YFRateLimitError
cache_maxsize = 64
@@ -390,6 +392,10 @@ class YfData(metaclass=SingletonMeta):
response = request_method(**request_args)
utils.get_yf_logger().debug(f'response code={response.status_code}')
# Raise exception if rate limited
if response.status_code == 429:
raise YFRateLimitError()
return response
@lru_cache_freezeargs

View File

@@ -45,3 +45,8 @@ class YFInvalidPeriodError(YFException):
self.invalid_period = invalid_period
self.valid_ranges = valid_ranges
super().__init__(f"{self.ticker}: Period '{invalid_period}' is invalid, must be one of {valid_ranges}")
class YFRateLimitError(YFException):
def __init__(self):
super().__init__("Too Many Requests. Rate limited. Try after a while.")

View File

@@ -9,7 +9,7 @@ import bisect
from yfinance import shared, utils
from yfinance.const import _BASE_URL_, _PRICE_COLNAMES_
from yfinance.exceptions import YFInvalidPeriodError, YFPricesMissingError, YFTzMissingError
from yfinance.exceptions import YFInvalidPeriodError, YFPricesMissingError, YFTzMissingError, YFRateLimitError
class PriceHistory:
def __init__(self, data, ticker, tz, session=None, proxy=None):
@@ -184,6 +184,9 @@ class PriceHistory:
"the issue. Thank you for your patience.")
data = data.json()
# Special case for rate limits
except YFRateLimitError:
raise
except Exception:
if raise_errors:
raise