o
    i                     @   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	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,dd Z-d<ddZ.dd Z/dd Z0dd Z1dd  Z2d!d" Z3d#d$ Z4d%d& Z5d'd( Z6G d)d* d*eZ7e7Z8G d+d, d,ej9j:Z;d-d. Z<d/d0 Z=d=d2d3Z>d=d4d5Z?d6d7 Z@d8d9 ZAd:d; ZBdS )>zGNo 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                 C   s$   ddl m} || |}|| |S )Nr   )load_plugins)pluginsr   update)namesuffix	namespacer   ret r"   H/home/ubuntu/.local/lib/python3.10/site-packages/yt_dlp/utils/_legacy.pyr   #   s   

r   Tc                 C   s   t | ||dddS )NT)	casesenseis_user_inputtraverse_stringr   )dictnkeysr$   r"   r"   r#   traverse_dict*      r)   c                 C   s   t | |dS )N)table)r   )valuedigitsr"   r"   r#   decode_base.   s   r.   c                   C   s   t   S )z$ Returns the platform name as a str )platformr"   r"   r"   r#   platform_name2   s   r0   c                  C   s:   t jdkrt  d dkrt } nt  } | d u rd} | S )Nwin32r      zutf-8)sysr/   getwindowsversionr   getfilesystemencoding)encodingr"   r"   r#   get_subprocess_encoding7   s   r7   c                    s  | dd  }| d d dks|dd dkrt ddddd	  fd
d}g }|ra||d d }|dd  }|d d }|dd  }|d | }||d  }|dd  }||||d |s*|d d }||d d }||dd }	d}
|D ]}|d dkr|
|d 7 }
q{|
st 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@ }ns|dkr|| d@ }nh|dkr||| d?  d@ }nX|dkre|}|}d}|dkr*|dkr*|| d }|| | }t|| }t|| }t|| }||krS||krS|| d@ }n||kr_|| d@ }n|| d@ }|| qq||	fS )N   s   PNG

   s   IHDRzNot a valid PNG file.z>Bz>Hz>I)r   r   r9   c                    s   t  t|  | d S )Nr   )structunpacklen)x)int_mapr"   r#   <lambda>N   s    zdecode_png.<locals>.<lambda>)typelengthdatar   rB       r@   s   IDATzUnable to read PNG data.   c                    s   |  }|  } | | S Nr"   )idxr=   y)pixelsstrider"   r#   
_get_pixelw   s   zdecode_png.<locals>._get_pixelr   r      )OSErrorappend	bytearrayzlib
decompressrangeabs)png_dataheaderunpack_integerchunksrA   
chunk_type
chunk_dataihdrwidthheightidatchunkdecompressed_datarJ   rG   base_posfilter_typecurrent_rowr=   colorbasexleftupabcppapbpcr"   )r>   rH   rI   r#   
decode_pngF   s    




(rm   c                  C   s(   dD ]} | t jjvrt jj|  qd S )N)sockssocks4socks4asocks5)urllibparseuses_netlocrM   )schemer"   r"   r#   register_socks_protocols   s
   rv   c                 C   s(   | }d|v 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)headersfiltered_headersr"   r"   r#   handle_youtubedl_headers   s
   r   c                 C   s   t | tjjr|  S | S rE   )
isinstancerr   requestRequestget_full_url)reqr"   r"   r#   request_to_url   s   r   c                 O   sn   ddl m}m} |t|| \} }|d ur*t|dkr |d n|di }||d< tjj| g|R i |S )Nr   )extract_basic_authsanitize_urlr   r}   Authorization)	utilsr   r   
escape_urlr<   
setdefaultrr   r   r   )urlargs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|i | d S rE   )_paramssuper__init__)selfparamsr   r   	__class__r"   r#   r      s   zYoutubeDLHandler.__init__)__name__
__module____qualname__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 rE   )rr   r   HTTPCookieProcessorr   )r   	cookiejarr"   r"   r#   r      s   z!YoutubeDLCookieProcessor.__init__c                 C   s   t jj| ||S rE   )rr   r   r   http_response)r   r   responser"   r"   r#   r      r*   z&YoutubeDLCookieProcessor.http_responserE   )r   r   r   r   r   rr   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 vd	i|S )
Ncontextnocheckcertificateclient_certificateclient_certificate_keyclient_certificate_passwordlegacyserverconnectz
no-certificompat_opts)verifyr   r   r   legacy_supportuse_certifi)YoutubeDLHTTPSHandlerr   get)r   r   r"   r"   r#   make_HTTPS_handler   s   

r   c                 O   s   t j| g|R i |S rE   )r   communicate_or_kill)ri   r   r   r"   r"   r#   process_communicate_or_kill   s   r   Fc                 C   s   t | tsJ | S rE   )r   str)sfor_subprocessr"   r"   r#   encodeFilename   s   r   c                 C      | S rE   r"   )rg   r   r"   r"   r#   decodeFilename      r   c                 C   r   rE   r"   )rg   r"   r"   r#   decodeArgument   r   r   c                 C   s4   | d u r| S t | tr| t } t | tsJ | S rE   )r   bytesdecoder   r   )optvalr"   r"   r#   decodeOption  s   
r   c                 C   s   t | S rE   )r   )errr"   r"   r#   error_to_compat_str  s   r   )T)F)C__doc__r/   r:   r3   urllib.errorrr   urllib.parseurllib.requestrO   _utilsr   r   r   	traversalr   dependenciesr	   r
   networking._helperr   networking._urllibr   
networkingr   r   r   r   r   cookiesr   r   r   r   r   PerRequestProxyHandlerr   YoutubeDLRedirectHandlerr   r   networking.exceptionsr   r   boolhas_certifihas_websocketsr   r)   r.   r0   r7   rm   rv   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r"   r"   r"   r#   <module>   sZ    
i	
	


