
    Xhw(                        d dl mZ d dlZd dlZd dlZd dlmZ d dlmZm	Z	m
Z
mZ d dlmZ ddlmZ ddlmZ d	d
lmZ d	dlmZmZ d	dlmZ 	 d dlmZ  ee      5  d dlmZ d dlZ ddd        ejB                  d      Z" e#       Z$dejJ                  fdZ& G d d      Z' G d d      Z(y# e$ r	 d dlmZ Y fw xY w# 1 sw Y   XxY w)    )suppressN)Future)AnyCallableOptionalUnion)	Awaitable   )Curl)CurlCffiWarning   )Cookies)	HTTPErrorRequestException)Headers)loads)markdownifyzcharset=([\w-]+)qc                     | j                   5  | j                  j                          | j                  j	                          d| _        d d d        y # 1 sw Y   y xY w)Nr   )mutexqueueclearall_tasks_done
notify_allunfinished_tasks)r   s    Q/home/Script/Script_env/lib/python3.12/site-packages/curl_cffi/requests/models.pyclear_queuer      sE    	
 		##%  s   <AAc                   $    e Zd ZdZdededefdZy)RequestzRepresenting a sent request.urlheadersmethodc                 .    || _         || _        || _        y )N)r    r!   r"   )selfr    r!   r"   s       r   __init__zRequest.__init__'   s        N)__name__
__module____qualname____doc__strr   r%    r&   r   r   r   $   s    &C ' 3 r&   r   c                   2   e Zd ZdZddee   dee   fdZede	fd       Z
ede	fd       Zej                  d	e	ddfd
       Zedee	   fd       Zede	fd       Zde	fdZdede	fdZd ZddZddZd Zd ZddZddZde	fdZdefdZd Zde	fdZy)Responsea  Contains information the server sends.

    Attributes:
        url: url used in the request.
        content: response body in bytes.
        text: response body in str.
        status_code: http status code.
        reason: http response reason, such as OK, Not Found.
        ok: is status_code in [200, 400)?
        headers: response headers.
        cookies: response cookies.
        elapsed: how many seconds the request cost.
        encoding: http body encoding.
        charset: alias for encoding.
        primary_ip: primary ip of the server.
        primary_port: primary port of the server.
        local_ip: local ip used in this connection.
        local_port: local port used in this connection.
        charset_encoding: encoding specified by the Content-Type header.
        default_encoding: encoding for decoding response content if charset is not found
            in headers. Defaults to "utf-8". Can be set to a callable for automatic
            detection.
        redirect_count: how many redirects happened.
        redirect_url: the final redirected url.
        http_version: http version used.
        history: history redirections, only headers are available.
    Ncurlrequestc                 t   || _         || _        d| _        d| _        d| _        d| _        d| _        t               | _        t               | _
        d| _        d| _        d| _        d| _        d| _        d| _        d| _        d| _        d| _        g | _        i | _        d | _        d | _        d | _        d | _        y )	N r&      OKTg        utf-8r   )r/   r0   r    contentstatus_codereasonokr   r!   r   cookieselapseddefault_encodingredirect_countredirect_urlhttp_version
primary_ipprimary_portlocal_ip
local_porthistoryinfosr   stream_taskastream_taskquit_now)r$   r/   r0   s      r   r%   zResponse.__init__J   s    	yyDK!!" -/%'
,0
-115r&   returnc                     | j                   S )zAlias for encoding.)encodingr$   s    r   charsetzResponse.charsetd   s     }}r&   c                    t        | d      sp| j                  }|Wt        | j                  t              r| j                  }n0t        | j                        r| j                  | j                        }|xs d| _        | j                  S )a  
        Determines the encoding to decode byte content into text.

        The method follows a specific priority to decide the encoding:
        1. If ``.encoding`` has been explicitly set, it is used.
        2. The encoding specified by the ``charset`` parameter in the ``Content-Type``
            header.
        3. The encoding specified by the ``default_encoding`` attribute. This can either
            be a string (e.g., "utf-8") or a callable for charset autodetection.
        	_encodingr5   )hasattrcharset_encoding
isinstancer<   r+   callabler6   rO   )r$   rK   s     r   rK   zResponse.encodingi   sr     t[),,Hd33S9#44Hd334#44T\\BH%0DN~~r&   valuec                 @    t        | d      rt        d      || _        y )N_textz0Cannot set encoding after text has been accessed)rP   
ValueErrorrO   )r$   rT   s     r   rK   zResponse.encoding   s    4!OPPr&   c                     | j                   j                  d      }|r*t        j                  |      }|r|j	                  d      S dS y)z=Return the encoding, as specified by the Content-Type header.zContent-Typer   N)r!   get
CHARSET_REsearchgroup)r$   content_typecharset_matchs      r   rQ   zResponse.charset_encoding   sF     ||''7&--l;M-:=&&q)DDr&   c                     t        | d      s?| j                  sd| _        | j                  S | j                  | j                        | _        | j                  S )NrV   r2   )rP   r6   rV   _decoderL   s    r   textzResponse.text   sF    tW%<<
 zz "\\$,,7
zzr&   c                     t        j                  | j                        }|j                         }|j	                  d      }t        d| d| d      }|S )NT)html_partialz<h1>z</h1><main>z</main>)rdDocumentr6   titlesummarymd)r$   docrf   rg   
body_as_mds        r   markdownzResponse.markdown   sN    kk$,,'		++4+0$ug[	AB
r&   r6   c                     	 |j                  | j                  d      S # t        t        f$ r |j                  d      cY S w xY w)Nreplace)errorsz	utf-8-sig)decoderK   UnicodeDecodeErrorLookupError)r$   r6   s     r   r`   zResponse._decode   sA    	/>>$--	>BB"K0 	/>>+..	/s     AAc                 j    | j                   s't        d| j                   d| j                   d|       y)z2Raise an error if status code is not in [200, 400)zHTTP Error z: r   N)r9   r   r7   r8   rL   s    r   raise_for_statuszResponse.raise_for_status   s6    wwk$*:*:);2dkk]KQPTUU r&   c              #     K   d}| j                  ||      D ]_  }|||z   }|r|j                  |      n|j                         }|r%|d   r |r|d   d   |d   k(  r|j                         nd}|E d{    a || yy7 wz
        iterate streaming content line by line, separated by ``\n``.

        Copied from: https://requests.readthedocs.io/en/latest/_modules/requests/models/
        which is under the License: Apache 2.0
        N)
chunk_sizedecode_unicode)iter_contentsplit
splitlinespop)r$   rv   rw   	delimiterpendingchunkliness          r   
iter_lineszResponse.iter_lines   s      &&!. ' 
 	E "%.7EKK	*U=M=M=OE U2Y5U2Yr]eBi5O 		  	 M  s   A1B3B4Bc              #   F  K   |rt        j                  dt        d       |r
t               | j                  r| j
                  sJ d       	 | j                  j                         }t        |t              r| j
                  j                          ||t        u ry| TwzD
        iterate streaming content chunk by chunk in bytes.
        z9chunk_size is ignored, there is no way to tell curl that.r
   )
stacklevelzstream mode is not enabled.N)warningswarnr   NotImplementedErrorr   r/   rY   rR   r   reset
STREAM_ENDr$   rv   rw   r   s       r   ry   zResponse.iter_content   s      MMK
 %''zzdiiF)FF'JJNN$E %!12		! 
"K s   BB!c                 .    t        | j                  fi |S )z+return a parsed json object of the content.)r   r6   )r$   kws     r   jsonzResponse.json   s    T\\(R((r&   c                     | j                   r| j                   j                          | j                  r| j                  j                          yyz:Close the streaming connection, only valid in stream mode.N)rH   setrF   resultrL   s    r   closezResponse.close   s;     ==MM##% r&   c                  K   d}| j                  ||      2 3 d{   }|||z   }|r|j                  |      n|j                         }|r%|d   r |r|d   d   |d   k(  r|j                         nd}|D ]  }| 	 i7 d6 || yywru   )aiter_contentrz   r{   r|   )r$   rv   rw   r}   r~   r   r   lines           r   aiter_lineszResponse.aiter_lines   s      --!. . 
 	 	% "%.7EKK	*U=M=M=OE U2Y5U2Yr]eBi5O 		   
	 
 M s&   BBBBA!BB
Bc                  K   |rt        j                  dt        d       |r
t               | j                  r| j
                  sJ d       	 | j                  j                          d{   }t        |t              r| j                          d{    ||t        u r| j                          d{    y| s7 U7 /7 wr   )r   r   r   r   r   r/   rY   rR   r   acloser   r   s       r   r   zResponse.aiter_content  s      MMK
 %''zzdiiF)FF'**..**E %!12kkm## 
"kkm##K * $
 $s6   A(C*C +'CC!C4C5CCCc                 \   K   | j                  | j                          d{         S 7 w)z*
        Return a decoded string.
        N)r`   acontentrL   s    r   atextzResponse.atext%  s#      ||$--/1221s   ,*	,c                    K   g }| j                         2 3 d{   }|j                  |       7 6 dj                  |      S w)z8wait and read the streaming content in one bytes object.Nr&   )r   appendjoin)r$   chunksr   s      r   r   zResponse.acontent+  sG     --/ 	! 	!%MM% 	!/xxs   A202A2Ac                 R   K   | j                   r| j                    d{    yy7 wr   )rG   rL   s    r   r   zResponse.aclose2  s(      #### #s   '%'c                 "    d| j                    dS )Nz<Response [z]>)r7   rL   s    r   __repr__zResponse.__repr__9  s    T--.b11r&   )NN)NFN)NF)r'   r(   r)   r*   r   r   r   r%   propertyr+   rM   rK   setterrQ   ra   rk   bytesr`   rs   r   ry   r   r   r   r   r   r   r   r   r,   r&   r   r.   r.   -   s%   8Xd^ Xg=N 4    #  * __c d  
 (3-   c  # /u / /V
4:)&6<3S 3   $2# 2r&   r.   ))
contextlibr   r   rer   concurrent.futuresr   typingr   r   r   r   collections.abcr	   r/   r   utilsr   r:   r   
exceptionsr   r   r!   r   orjsonr   ImportErrorr   r   rh   readabilityrd   compilerZ   objectr   Queuer   r   r.   r,   r&   r   <module>r      s      	  % 1 1 %  #  3  k - RZZ+,
X
5;;  M2 M27   s   B+ B<+B98B9<C