o
    ùT·j¢  ã                   @   sÞ   d Z ddlZddlZddlmZ ej dd¡ ¡ dv Zes;zddl	m
Z eZd	ZW n ey:   ddl
ZeZd
ZY n	w ddl
ZeZd
ZeZ
ejjZdZd
add„ Zdd„ Zdd„ Zejdd�defdd„ƒZdefdd„ZdS )u—  HTTP backend abstraction.

Prefers ``curl_cffi`` for browser TLS impersonation. Falls back to plain
``requests`` with a realistic ``User-Agent`` when ``curl_cffi`` cannot be
imported (e.g. binary not buildable on the host platform â€” see issue #2692)
or when ``YF_DISABLE_CURL_CFFI`` is set in the environment (downstream
packagers may ship without ``curl_cffi`` even if it is installed).

The fallback is best-effort: plain ``requests`` cannot replicate the
JA3/JA4 fingerprint and HTTP/2 settings that ``curl_cffi`` provides, so
Yahoo Finance may rate-limit or block this client. ``curl_cffi`` remains
the preferred backend and the default install dependency.
é    Né   )ÚutilsÚYF_DISABLE_CURL_CFFIÚ )Ú1ÚtrueÚyes)ÚrequestsTFzoMozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36c                   C   s"   t strd S dat ¡  d¡ d S )NTzÃcurl_cffi not available; falling back to requests without browser TLS impersonation. Yahoo Finance may rate-limit or block this client. Install curl_cffi (>=0.15) for the supported configuration.)ÚHAS_CURL_CFFIÚ_fallback_warnedr   Úget_yf_loggerÚwarning© r   r   úQ/home/dinkstrade/pdmp-scanner/venv/lib/python3.10/site-packages/yfinance/_http.pyÚ_warn_once_on_fallback.   s   ÿr   c                  C   s6   t rtjdd�S tƒ  t ¡ } | j tdddœ¡ | S )z0Create a default Session for the active backend.Úchrome)Úimpersonatez?text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8zen-US,en;q=0.5)z
User-AgentÚAcceptzAccept-Language)r
   Ú_backendÚSessionr   ÚheadersÚupdateÚ_FALLBACK_USER_AGENT)Úsr   r   r   Únew_session:   s   ýr   c                 C   s   | j }t|d|ƒS )zÜReturn the underlying ``http.cookiejar.CookieJar`` for either backend.

    ``curl_cffi`` exposes the jar as ``session.cookies.jar``; ``requests``
    uses ``session.cookies`` directly (it subclasses ``CookieJar``).
    Újar)ÚcookiesÚgetattr)Úsessionr   r   r   r   Ú
cookie_jarH   s   r   )ÚmaxsizeÚreturnc                  C   sl   g } zddl m} |  |¡ W n	 ty   Y nw zddlm} |  |¡ W t| ƒS  ty5   Y t| ƒS w )Nr   )r   )Úcurl_cffi.requests.sessionr   ÚappendÚImportErrorÚrequests.sessionsÚtuple)ÚclassesÚ_CurlSessionÚ_ReqSessionr   r   r   Ú_supported_session_classesR   s   ÿþþr*   c                 C   s   t ƒ }t|ƒot| |ƒS )z;True if ``obj`` is a Session from either supported backend.)r*   ÚboolÚ
isinstance)Úobjr'   r   r   r   Úis_supported_sessionb   s   r.   )Ú__doc__Ú	functoolsÚosr   r   ÚenvironÚgetÚlowerÚ_DISABLEÚ	curl_cffir	   Ú_curl_backendr   r
   r$   Ú_requests_backendÚ
exceptionsÚ	HTTPErrorr   r   r   r   r   Ú	lru_cacher&   r*   r+   r.   r   r   r   r   Ú<module>   s:    ýÿ

