Skip to content

Commit 68b7c16

Browse files
authored
Merge pull request #2487 from ranaroussi/feature/deprecate-requests-cache
Deprecate using requests_cache etc, only curl_cffi works
2 parents 81d8737 + 78ad990 commit 68b7c16

File tree

3 files changed

+13
-51
lines changed

3 files changed

+13
-51
lines changed

doc/source/advanced/caching.rst

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,6 @@
11
Caching
22
=======
33

4-
Smarter Scraping
5-
----------------
6-
7-
Install the `nospam` package to cache API calls and reduce spam to Yahoo:
8-
9-
.. code-block:: bash
10-
11-
pip install yfinance[nospam]
12-
13-
To use a custom `requests` session, pass a `session=` argument to
14-
the Ticker constructor. This allows for caching calls to the API as well as a custom way to modify requests via the `User-agent` header.
15-
16-
.. code-block:: python
17-
18-
import requests_cache
19-
session = requests_cache.CachedSession('yfinance.cache')
20-
session.headers['User-agent'] = 'my-program/1.0'
21-
ticker = yf.Ticker('MSFT', session=session)
22-
23-
# The scraped response will be stored in the cache
24-
ticker.actions
25-
26-
27-
Combine `requests_cache` with rate-limiting to avoid triggering Yahoo's rate-limiter/blocker that can corrupt data.
28-
29-
.. code-block:: python
30-
31-
from requests import Session
32-
from requests_cache import CacheMixin, SQLiteCache
33-
from requests_ratelimiter import LimiterMixin, MemoryQueueBucket
34-
from pyrate_limiter import Duration, RequestRate, Limiter
35-
class CachedLimiterSession(CacheMixin, LimiterMixin, Session):
36-
pass
37-
38-
session = CachedLimiterSession(
39-
limiter=Limiter(RequestRate(2, Duration.SECOND*5)), # max 2 requests per 5 seconds
40-
bucket_class=MemoryQueueBucket,
41-
backend=SQLiteCache("yfinance.cache"),
42-
)
43-
44-
454
Persistent Cache
465
----------------
476

yfinance/data.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from . import utils, cache
1111
import threading
1212

13-
from .exceptions import YFRateLimitError
13+
from .exceptions import YFRateLimitError, YFDataException
1414

1515
cache_maxsize = 64
1616

@@ -83,13 +83,9 @@ def __init__(self, session=None, proxy=None):
8383
def _set_session(self, session):
8484
if session is None:
8585
return
86-
with self._cookie_lock:
87-
self._session = session
88-
if self._proxy is not None:
89-
self._session.proxies = self._proxy
9086

9187
try:
92-
self._session.cache
88+
session.cache
9389
except AttributeError:
9490
# Not caching
9591
self._session_is_caching = False
@@ -98,8 +94,16 @@ def _set_session(self, session):
9894
# Can't simply use a non-caching session to fetch cookie & crumb,
9995
# because then the caching-session won't have cookie.
10096
self._session_is_caching = True
101-
from requests_cache import DO_NOT_CACHE
102-
self._expire_after = DO_NOT_CACHE
97+
# But since switch to curl_cffi, can't use requests_cache with it.
98+
raise YFDataException("request_cache sessions don't work with curl_cffi, which is necessary now for Yahoo API. Solution: stop setting session, let YF handle.")
99+
100+
if not isinstance(session, requests.session.Session):
101+
raise YFDataException(f"Yahoo API requires curl_cffi session not {type(session)}. Solution: stop setting session, let YF handle.")
102+
103+
with self._cookie_lock:
104+
self._session = session
105+
if self._proxy is not None:
106+
self._session.proxies = self._proxy
103107

104108
def _set_proxy(self, proxy=None):
105109
with self._cookie_lock:

yfinance/pricing_pb2.py

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)