U
    g&                     @   s  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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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  ddlm!Z!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+ e,eZ-e,eZ.G dd dZ/dd Z0d3ddZ1dd Z2dd Z3dd  Z4d!d" Z5d#d$ Z6d%d& Z7d'd( Z8d)d* Z9G d+d, d,eZ:e:Z;G d-d. d.ej<j=Z>d/d0 Z?d1d2 Z@dS )4zGNo longer used and new code should not use. Exists only for API compat.    N   )Popendecode_base_npreferredencodingtraverse_obj   )certifi
websockets)make_ssl_context)HTTPHandler)escape_rfc3986)normalize_url)random_user_agentstd_headers)YoutubeDLCookieJar)
PUTRequest)SUPPORTED_ENCODINGSHEADRequest)ProxyHandler)RedirectHandler)make_socks_conn_classupdate_Request)	HTTPErrornetwork_exceptionsc                   @   sV   e Zd ZdZdZdddZdd Zdd	 Zd
d Zdd Z	e
dd Ze
dd ZdS )WebSocketsWrapperz2Wraps websockets module to use in non-async scopesNTc                 K   sV   t  | _tj|f|d td| jtdd|| _|r@|   t	| j
d d d  d S )Ninf)extra_headersZping_intervalZclose_timeoutloopZping_timeout)asyncioZnew_event_loopr   r
   connectfloatconn	__enter__atexitregister__exit__)selfurlheadersr    Z	ws_kwargs r*   8/tmp/pip-unpacked-wheel-q5ljy6pj/yt_dlp/utils/_legacy.py__init__)   s    
   zWebSocketsWrapper.__init__c                 C   s    | j s| | j | j| _ | S N)poolrun_with_loopr"   
__aenter__r   )r'   r*   r*   r+   r#   3   s    zWebSocketsWrapper.__enter__c                 G   s   |  | jj| | j d S r-   )r/   r.   sendr   r'   argsr*   r*   r+   r1   8   s    zWebSocketsWrapper.sendc                 G   s   |  | jj| | jS r-   )r/   r.   recvr   r2   r*   r*   r+   r4   ;   s    zWebSocketsWrapper.recvc              	   C   s<   z| | j|||| j W S | j   | | j  X d S r-   )r   close_cancel_all_tasksr/   r"   	__aexit__)r'   typevalue	tracebackr*   r*   r+   r&   >   s    
zWebSocketsWrapper.__exit__c              
   C   sT   t | std| z|| W S ||  t|drN||  X d S )Nza coroutine was expected, got shutdown_default_executor)r   iscoroutine
ValueErrorrun_until_completeZshutdown_asyncgenshasattrr;   )mainr   r*   r*   r+   r/   G   s    

zWebSocketsWrapper.run_with_loopc                 C   sv   t | }|sd S |D ]}|  q| t j|| dd |D ]0}| rNq@| d k	r@| d| |d q@d S )NT)r   Zreturn_exceptionsz1unhandled exception during asyncio.run() shutdown)message	exceptiontask)r   Z	all_taskscancelr>   ZgatherZ	cancelledrB   Zcall_exception_handler)r   Z	to_cancelrC   r*   r*   r+   r6   S   s"    

z#WebSocketsWrapper._cancel_all_tasks)NT)__name__
__module____qualname____doc__r.   r,   r#   r1   r4   r&   staticmethodr/   r6   r*   r*   r*   r+   r   %   s   

	
r   c                 C   s$   ddl m} || |}|| |S )Nr   )load_plugins)ZpluginsrJ   update)namesuffix	namespacerJ   retr*   r*   r+   rJ   l   s    

rJ   Tc                 C   s   t | ||dddS )NT)	casesenseZis_user_inputZtraverse_stringr   )ZdictnkeysrP   r*   r*   r+   traverse_dicts   s    rR   c                 C   s   t | |dS )N)table)r   )r9   digitsr*   r*   r+   decode_basew   s    rU   c                   C   s   t   S )z$ Returns the platform name as a str )platformr*   r*   r*   r+   platform_name{   s    rW   c                  C   s:   t jdkr"t  d dkr"t } nt  } | d kr6d} | S )Nwin32r      zutf-8)sysrV   getwindowsversionr   getfilesystemencoding)encodingr*   r*   r+   get_subprocess_encoding   s    r^   c                    s  | dd  }| d d dks,|dd dkr4t ddddd	  fd
d}g }|r||d d }|dd  }|d d }|dd  }|d | }||d  }|dd  }||||d qP|d d }||d d }||dd }	d}
|D ]}|d dkr|
|d 7 }
q|
s t dtt|
}|d g fdd}t|	D ]}|d  }|| }g }| tD ]`}|d| |  }| | }d}d}|dkr||d }|dkr|| }|dkr|| d@ }n|dkr|| d@ }n|dkr&||| d?  d@ }n|dkr|}|}d}|dkr`|dkr`|| d }|| | }t|| }t|| }t|| }||kr||kr|| d@ }n$||kr|| d@ }n|| d@ }|| qqP||	fS )N   s   PNG

   s   IHDRzNot a valid PNG file.z>Bz>Hz>I)r   r   r`   c                    s   t  t|  | d S )Nr   )structunpacklen)x)int_mapr*   r+   <lambda>       zdecode_png.<locals>.<lambda>)r8   lengthdatar   ri   rg   r8   s   IDATzUnable to read PNG data.   c                    s   |  }|  } | | S r-   r*   )idxrd   y)pixelsstrider*   r+   
_get_pixel   s    zdecode_png.<locals>._get_pixelr   r      )OSErrorappend	bytearrayzlib
decompressrangeabs)Zpng_dataheaderZunpack_integerchunksrh   Z
chunk_typeZ
chunk_dataZihdrwidthheightZidatchunkZdecompressed_dataro   rl   Zbase_posZfilter_typeZcurrent_rowrd   colorZbasexleftZupabcppaZpbZpcr*   )re   rm   rn   r+   
decode_png   s     








r   c                  C   s(   dD ]} | t jjkrt jj|  qd S )N)socksZsocks4Zsocks4aZsocks5)urllibparseuses_netlocrr   )schemer*   r*   r+   register_socks_protocols   s    r   c                 C   s(   | }d|kr$dd |  D }|d= |S )NzYoutubedl-no-compressionc                 S   s"   i | ]\}}|  d kr||qS )zaccept-encoding)lower).0kvr*   r*   r+   
<dictcomp>  s       z,handle_youtubedl_headers.<locals>.<dictcomp>)items)r)   Zfiltered_headersr*   r*   r+   handle_youtubedl_headers  s
    r   c                 C   s   t | tjjr|  S | S d S r-   )
isinstancer   requestRequestget_full_url)reqr*   r*   r+   request_to_url  s    r   c                 O   sh   ddl m}m} |t|| \} }|d k	rTt|dkr@|d n
|di }||d< tjj| f||S )Nr   )extract_basic_authsanitize_urlr   r)   Authorization)	utilsr   r   
escape_urlrc   
setdefaultr   r   r   )r(   r3   kwargsr   r   auth_headerr)   r*   r*   r+   sanitized_Request  s     r   c                       s   e Zd Z fddZ  ZS )YoutubeDLHandlerc                    s   || _ t j|| d S r-   )_paramssuperr,   )r'   paramsr3   r   	__class__r*   r+   r,     s    zYoutubeDLHandler.__init__)rE   rF   rG   r,   __classcell__r*   r*   r   r+   r     s   r   c                   @   s,   e Zd ZdddZdd ZejjjZ	eZ
dS )YoutubeDLCookieProcessorNc                 C   s   t jj| | d S r-   )r   r   HTTPCookieProcessorr,   )r'   	cookiejarr*   r*   r+   r,   %  s    z!YoutubeDLCookieProcessor.__init__c                 C   s   t jj| ||S r-   )r   r   r   http_response)r'   r   responser*   r*   r+   r   (  s    z&YoutubeDLCookieProcessor.http_response)N)rE   rF   rG   r,   r   r   r   r   http_requesthttps_requesthttps_responser*   r*   r*   r+   r   $  s   

r   c                 K   sP   t | fdt| d | d| d| d| dd| dg kd	i|S )
NcontextZnocheckcertificateclient_certificateclient_certificate_keyclient_certificate_passwordZlegacyserverconnectz
no-certifiZcompat_opts)verifyr   r   r   Zlegacy_supportZuse_certifi)YoutubeDLHTTPSHandlerr   get)r   r   r*   r*   r+   make_HTTPS_handler/  s    

r   c                 O   s   t j| f||S r-   )r   Zcommunicate_or_kill)r   r3   r   r*   r*   r+   process_communicate_or_kill:  s    r   )T)ArH   r   r$   rV   ra   rZ   urllib.errorr   urllib.parseurllib.requestrt   _utilsr   r   r   Z	traversalr   Zdependenciesr	   r
   Znetworking._helperr   Znetworking._urllibr   Z
networkingr   r   r   r   r   cookiesr   r   r   r   r   ZPerRequestProxyHandlerr   ZYoutubeDLRedirectHandlerr   r   Znetworking.exceptionsr   r   boolZhas_certifiZhas_websocketsr   rJ   rR   rU   rW   r^   r   r   r   r   r   r   r   r   r   r   r   r   r*   r*   r*   r+   <module>   sT   G
i	
	