
    g!                        d Z ddlZddlZddlZddlZddl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mZ ddlmZ  ej*                  e      Zd	Zdddej2                  fd
Zdej2                  fdZddej2                  fdZej2                  dfdZej2                  dfdZ e       d        Z e       d        Z d Z!y)z+Implements a simple wrapper around urlopen.    N)	lru_cache)parse)URLError)Requesturlopen)RegexMatchErrorMaxRetriesExceeded)regex_searchi   c                 $   ddd}|r|j                  |       |r0t        |t              s t        t        j                  |      d      }| j                         j                  d      rt        | |||      }nt        d      t        ||	      S )
NzMozilla/5.0zen-US,en)z
User-Agentzaccept-languageutf-8)encodinghttp)headersmethoddatazInvalid URL)timeout)
update
isinstancebytesjsondumpslower
startswithr   
ValueErrorr   )urlr   r   r   r   base_headersrequests          W/var/www/it7/html/youtubeDownloader/venv/lib/python3.12/site-packages/pytube/request.py_execute_requestr      s}     #0JOLG$$&D)G<D
yy{f%#|FN''7G,,    c                 d    |i }t        | ||      }|j                         j                  d      S )zSend an http GET request.

    :param str url:
        The URL to perform the GET request for.
    :param dict extra_headers:
        Extra headers to add to the request
    :rtype: str
    :returns:
        UTF-8 encoded string of response
    )r   r   r   )r   readdecode)r   extra_headersr   responses       r   getr&   (   s4     ]GLH==?!!'**r    c                     |i }|i }|j                  ddi       t        | |||      }|j                         j                  d      S )a:  Send an http POST request.

    :param str url:
        The URL to perform the POST request for.
    :param dict extra_headers:
        Extra headers to add to the request
    :param dict data:
        The data to send on the POST request
    :rtype: str
    :returns:
        UTF-8 encoded string of response
    zContent-Typezapplication/json)r   r   r   r   )r   r   r"   r#   )r   r$   r   r   r%   s        r   postr(   9   s^     | .*<=>	H ==?!!'**r    c              #     K   t        j                  |       }|j                  d|j                  d|j                  d}t        t        j                  |j                              }d|d<   |t        j                  |      z   } d}t        | ||      D ]  }| ||z  } |j                  d      }t        j                  d	      }	|D ]?  }
|	j                  |
      }|st        |j                  d
      j!                  d            }A d
}|k  r>||d<   |t        j                  |      z   } t        | ||      E d{    |d
z  }||k  r>y7 w)z{Read the response in sequence.
    :param str url: The URL to perform the GET request for.
    :rtype: Iterable[bytes]
    :///?r   sqr    )r   max_retries   
   Segment-Count: (\d+)   r   N)r   urlsplitschemenetlocpathdict	parse_qslquery	urlencodestreamsplitrecompilesearchintgroupr#   )r   r   r.   	split_urlbase_urlqueryssegment_datachunkstream_infosegment_count_patternlinematchsegment_countseq_nums                 r   
seq_streamrL   X   sQ     s#I ) 0 0)2B2BINNSH%//)//23F F4L
U__V,
,CLW+F 
 $$W-KJJ'?@ @%,,T2A 5 5g >?M@ G
]
"t00#wKHHH1 ]
"  	Is   C E#A EEEEc              #     K   t         }d}||k  rt        |t         z   |      dz
  }d| d| }d}	 |d|z   k\  r
t               	 t        | d| d| z   d|      }	 |t         k(  r6	 t        | dd dd	 z   d|      }
|
j                         d
   }t        |      }	 |j'                         }|sn|t)        |      z  }| &||k  ry# t        $ r0}	t        |	j                  t        j                        rn Y d}	~	n&d}	~	wt        j                  j                  $ r Y nw xY w|dz  }# t        t        t         f$ r}	t"        j%                  |	       Y d}	~	d}	~	ww xY ww)zyRead the response in chunks.
    :param str url: The URL to perform the GET request for.
    :rtype: Iterable[bytes]
    r   r1   zbytes=-z&range=GET)r   r   Nl   g] zContent-Length)default_range_sizeminr	   r   r   r   reasonsocketr   r   clientIncompleteReadinfor?   KeyError
IndexErrorr   loggererrorr"   len)r   r   r.   	file_size
downloadedstop_posrange_headertriesr%   erespcontent_rangerE   s                r   r:   r:      s     (IJ
y
 z$66	BQF
|1XJ7 K'(**+GJ<q
;; #"  **	 'GA3a}55 #
 !%		,< =.	 MMOE#e*$JK Y y
 d A   ahh7  ;;--  QJE3 H j*5  Q sl   AEC	 
E&5D) ,EE		D &C=8E= D ED  	E)E=EEEEc                 0    t        t        |       d         S )zFetch size in bytes of file at given URL

    :param str url: The URL to get the size of
    :returns: int: size in bytes of remote file
    content-length)r?   head)r   s    r   filesizerg      s     tCy)*++r    c                    d}t        j                  |       }|j                  d|j                  d|j                  d}t        t        j                  |j                              }d|d<   |t        j                  |      z   } t        | d      }|j                         }|t        |      z  }d}|j                  d      }d	}	|D ]  }
	 t        t        |	|
d
            } |dk(  rt        d|	      d
}||k  rB||d<   |t        j                  |      z   } |t        t!        |       d         z  }|d
z  }||k  rB|S # t        $ r Y w xY w)zFetch size in bytes of file at given URL from sequential requests

    :param str url: The URL to get the size of
    :returns: int: size in bytes of remote file
    r   r*   r+   r,   r-   rO   r   r/   r0   r1   seq_filesizere   )r   r2   r3   r4   r5   r6   r7   r8   r9   r   r"   r[   r;   r?   r
   r   rf   )r   total_filesizerA   rB   rC   r%   response_valuerJ   rF   segment_regexrH   rK   s               r   rj   rj      s`    Ns#I ) 0 0)2B2BINNSH%//)//23F F4L
U__V,
,CEH ]]_Nc.))N M &&w/K,M 	]D! DEM	 nm<< G
]
"t00#d3i(89::1 ]
"   		s   E  	EEc                     t        | d      j                         }|j                         D ci c]  \  }}|j                         | c}}S c c}}w )zFetch headers returned http GET request.

    :param str url:
        The URL to perform the GET request for.
    :rtype: dict
    :returns:
        dictionary of lowercase headers
    HEADri   )r   rV   itemsr   )r   response_headerskvs       r   rf   rf     sE     (F;@@B%5%;%;%=>TQAGGIqL>>>s   A)"__doc__http.clientr   r   loggingr<   rS   	functoolsr   urllibr   urllib.errorr   urllib.requestr   r   pytube.exceptionsr   r	   pytube.helpersr
   	getLogger__name__rY   rP   _GLOBAL_DEFAULT_TIMEOUTr   r&   r(   rL   r:   rg   rj   rf    r    r   <module>r      s    1    	    ! + A '			8	$ 
 	**-*  )G)G +" !tV5S5S +B ***^ **=@ , , 0 0f
?r    