U
    cUh                     @   sP  d Z ddlZddlZddlZddlZddl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 dd	lmZ dd
lmZ ddlmZmZmZ ddlmZmZmZ e e!Z"eee# dddZ$eee%ee& dddZ'e%e%dddZ(e%e%e%dddZ)eee%dddZ*eeedddZ+G dd  d Z,G d!d" d"Z-dS )#z)Download files with progress indicators.
    N)IterableOptionalTuple)Response)get_download_progress_renderer)NetworkConnectionError)PyPI)Link)is_from_cache)
PipSession)HEADERSraise_for_statusresponse_chunks)format_sizeredact_auth_from_urlsplitext)respreturnc              
   C   s2   zt | jd W S  tttfk
r,   Y d S X d S )Nzcontent-length)intheaders
ValueErrorKeyError	TypeError)r    r   ^/opt/bot/vods_filmes/script/venv/lib/python3.8/site-packages/pip/_internal/network/download.py_get_http_response_size   s    r   )r   linkprogress_barr   c           	      C   s   t | }|jtjkr|j}n|j}t|}|rB| dt| d}t| rXt	
d| nt	
d| t	 tjkrxd}n*t| rd}n|sd}n|dkrd}nd}t| }|s|S t||d}||S )	Nz ()zUsing cached %szDownloading %sFTi   )Zbar_typesize)r   netlocr   file_storage_domainshow_urlurl_without_fragmentr   r   r
   loggerinfogetEffectiveLevelloggingINFOr   r   )	r   r   r   total_lengthurlZ
logged_urlZshow_progresschunksZrendererr   r   r   _prepare_download   s0    r,   )filenamer   c                 C   s   t j| S )zJ
    Sanitize the "filename" value from a Content-Disposition header.
    )ospathbasename)r-   r   r   r   sanitize_content_filenameI   s    r1   )content_dispositiondefault_filenamer   c                 C   s4   t j }| |d< |d}|r,tt|}|p2|S )z
    Parse the "filename" value from a Content-Disposition header, and
    return the default filename if the result is empty.
    content-typer-   )emailmessageMessage	get_paramr1   str)r2   r3   mr-   r   r   r   parse_content_dispositionP   s    

r;   )r   r   r   c                 C   s   |j }| jd}|r t||}t|d }|sPt| jdd}|rP||7 }|s~|j| jkr~tj	| jd }|r~||7 }|S )zoGet an ideal filename from the given HTTP response, falling back to
    the link filename if not provided.
    zcontent-disposition   r4    )
r-   r   getr;   r   	mimetypesguess_extensionr*   r.   r/   )r   r   r-   r2   extr   r   r   _get_http_response_filename_   s    
rB   )sessionr   r   c                 C   s.   |j ddd }| j|tdd}t| |S )N#r<   r   T)r   stream)r*   splitr>   r   r   )rC   r   Z
target_urlr   r   r   r   _http_get_downloadt   s    rG   c                   @   s8   e Zd ZeeddddZeeeeef dddZdS )
DownloaderNrC   r   r   c                 C   s   || _ || _d S N_session_progress_barselfrC   r   r   r   r   __init__|   s    zDownloader.__init__)r   locationr   c              
   C   s   zt | j|}W nD tk
rT } z&|jdk	s0ttd|jj|  W 5 d}~X Y nX t||}t	j
||}t||| j}t|d}|D ]}	||	 qW 5 Q R X |jdd}
||
fS )z.Download the file given by link into location.NHTTP error %s while getting %swbContent-Typer=   rG   rL   r   responseAssertionErrorr$   criticalstatus_coderB   r.   r/   joinr,   rM   openwriter   r>   )rO   r   rQ   r   er-   filepathr+   content_filechunkcontent_typer   r   r   __call__   s$      
zDownloader.__call__)	__name__
__module____qualname__r   r9   rP   r	   r   rb   r   r   r   r   rH   {   s
   rH   c                   @   sH   e Zd ZeeddddZee eeeeeeef f  dddZ	dS )BatchDownloaderNrI   c                 C   s   || _ || _d S rJ   rK   rN   r   r   r   rP      s    zBatchDownloader.__init__)linksrQ   r   c                 c   s   |D ]}zt | j|}W nD tk
r\ } z&|jdk	s8ttd|jj|  W 5 d}~X Y nX t||}t	j
||}t||| j}t|d}	|D ]}
|	|
 qW 5 Q R X |jdd}|||ffV  qdS )z0Download the files given by links into location.NrR   rS   rT   r=   rU   )rO   rg   rQ   r   r   r]   r-   r^   r+   r_   r`   ra   r   r   r   rb      s&    
zBatchDownloader.__call__)
rc   rd   re   r   r9   rP   r   r	   r   rb   r   r   r   r   rf      s   	 rf   ).__doc__email.messager5   r'   r?   r.   typingr   r   r   Zpip._vendor.requests.modelsr   Zpip._internal.cli.progress_barsr   pip._internal.exceptionsr   pip._internal.models.indexr   pip._internal.models.linkr	   pip._internal.network.cacher
   pip._internal.network.sessionr   pip._internal.network.utilsr   r   r   pip._internal.utils.miscr   r   r   	getLoggerrc   r$   r   r   r9   bytesr,   r1   r;   rB   rG   rH   rf   r   r   r   r   <module>   s6   
*