11import functools
2- import random
32from functools import lru_cache
43
54from curl_cffi import requests
1110from . import utils , cache
1211import threading
1312
14- from .const import USER_AGENTS
1513from .exceptions import YFRateLimitError
1614
1715cache_maxsize = 64
@@ -66,9 +64,6 @@ class YfData(metaclass=SingletonMeta):
6664 Have one place to retrieve data from Yahoo API in order to ease caching and speed up operations.
6765 Singleton means one session one cookie shared by all threads.
6866 """
69- user_agent_headers = {
70- 'User-Agent' : random .choice (USER_AGENTS )
71- }
7267
7368 def __init__ (self , session = None , proxy = None ):
7469 self ._crumb = None
@@ -85,8 +80,6 @@ def __init__(self, session=None, proxy=None):
8580 self ._set_session (session or requests .Session (impersonate = "chrome" ))
8681 self ._set_proxy (proxy )
8782
88- utils .get_yf_logger ().debug (f"Using User-Agent: { self .user_agent_headers ['User-Agent' ]} " )
89-
9083 def _set_session (self , session ):
9184 if session is None :
9285 return
@@ -178,7 +171,6 @@ def _get_cookie_basic(self, timeout=30):
178171 # - 'allow_redirects' copied from @psychoz971 solution - does it help USA?
179172 self ._session .get (
180173 url = 'https://fc.yahoo.com' ,
181- headers = self .user_agent_headers ,
182174 timeout = timeout ,
183175 allow_redirects = True )
184176
@@ -191,7 +183,6 @@ def _get_crumb_basic(self, timeout=30):
191183 # - 'allow_redirects' copied from @psychoz971 solution - does it help USA?
192184 get_args = {
193185 'url' : "https://query1.finance.yahoo.com/v1/test/getcrumb" ,
194- 'headers' : self .user_agent_headers ,
195186 'timeout' : timeout ,
196187 'allow_redirects' : True
197188 }
@@ -225,7 +216,6 @@ def _get_cookie_csrf(self, timeout):
225216 return True
226217
227218 base_args = {
228- 'headers' : self .user_agent_headers ,
229219 'timeout' : timeout }
230220
231221 get_args = {** base_args , 'url' : 'https://guce.yahoo.com/consent' }
@@ -297,7 +287,6 @@ def _get_crumb_csrf(self, timeout=30):
297287
298288 get_args = {
299289 'url' : 'https://query2.finance.yahoo.com/v1/test/getcrumb' ,
300- 'headers' : self .user_agent_headers ,
301290 'timeout' : timeout }
302291 if self ._session_is_caching :
303292 get_args ['expire_after' ] = self ._expire_after
@@ -337,15 +326,15 @@ def _get_cookie_and_crumb(self, timeout=30):
337326 return crumb , strategy
338327
339328 @utils .log_indent_decorator
340- def get (self , url , user_agent_headers = None , params = None , timeout = 30 ):
341- return self ._make_request (url , request_method = self ._session .get , user_agent_headers = user_agent_headers , params = params , timeout = timeout )
329+ def get (self , url , params = None , timeout = 30 ):
330+ return self ._make_request (url , request_method = self ._session .get , params = params , timeout = timeout )
342331
343332 @utils .log_indent_decorator
344- def post (self , url , body , user_agent_headers = None , params = None , timeout = 30 ):
345- return self ._make_request (url , request_method = self ._session .post , user_agent_headers = user_agent_headers , body = body , params = params , timeout = timeout )
333+ def post (self , url , body , params = None , timeout = 30 ):
334+ return self ._make_request (url , request_method = self ._session .post , body = body , params = params , timeout = timeout )
346335
347336 @utils .log_indent_decorator
348- def _make_request (self , url , request_method , user_agent_headers = None , body = None , params = None , timeout = 30 ):
337+ def _make_request (self , url , request_method , body = None , params = None , timeout = 30 ):
349338 # Important: treat input arguments as immutable.
350339
351340 if len (url ) > 200 :
@@ -368,8 +357,7 @@ def _make_request(self, url, request_method, user_agent_headers=None, body=None,
368357 request_args = {
369358 'url' : url ,
370359 'params' : {** params , ** crumbs },
371- 'timeout' : timeout ,
372- 'headers' : user_agent_headers or self .user_agent_headers
360+ 'timeout' : timeout
373361 }
374362
375363 if body :
@@ -396,11 +384,11 @@ def _make_request(self, url, request_method, user_agent_headers=None, body=None,
396384
397385 @lru_cache_freezeargs
398386 @lru_cache (maxsize = cache_maxsize )
399- def cache_get (self , url , user_agent_headers = None , params = None , timeout = 30 ):
400- return self .get (url , user_agent_headers , params , timeout )
387+ def cache_get (self , url , params = None , timeout = 30 ):
388+ return self .get (url , params , timeout )
401389
402- def get_raw_json (self , url , user_agent_headers = None , params = None , timeout = 30 ):
390+ def get_raw_json (self , url , params = None , timeout = 30 ):
403391 utils .get_yf_logger ().debug (f'get_raw_json(): { url } ' )
404- response = self .get (url , user_agent_headers = user_agent_headers , params = params , timeout = timeout )
392+ response = self .get (url , params = params , timeout = timeout )
405393 response .raise_for_status ()
406394 return response .json ()
0 commit comments