
    ŞiYG                       d dl m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ZddlmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z" edddedddZ#d	 Z$dd
dZ% G d de          Z&ej'        dddd3d            Z(ej'        dddd4d            Z(d5dddd6dZ(ej'        ddddd7d             Z)ej'        dd!d8d#            Z)ej'        ddddd9d%            Z)ej'        ddd&d:d'            Z)dddddddd(d)Z)ej'        dd!d8d*            Z*ej'        ddddd7d+            Z*ddddddd,d-Z*ddd.d/Z+d0 Z,d1 Z-d;d2Z.dS )<    )annotationsN   )IDENTITY
NO_DEFAULTExtractorErrorLazyListdeprecation_warningget_elements_html_by_classget_elements_html_by_attributeget_elements_by_attributeget_element_by_classget_element_html_by_attributeget_element_by_attributeget_element_html_by_idget_element_by_idget_element_html_by_classget_elements_by_class get_element_text_and_html_by_tagis_iterable_liketry_callurl_or_nonevariadicTF)defaultexpected_typeget_all	casesenseis_user_inputtraverse_stringc                  |t           urt          d           d t          t                    rfdnfdfdd fdfdt	          |d	          D ]b\  }}	|t          |          k    }
	  | |	|
d
          }||c S .# t          $ r(}|
rt          |j        |j	                  dY d}~[d}~ww xY wt           u rdnS )a  
    Safely traverse nested `dict`s and `Iterable`s

    >>> obj = [{}, {"key": "value"}]
    >>> traverse_obj(obj, (1, "key"))
    'value'

    Each of the provided `paths` is tested and the first producing a valid result will be returned.
    The next path will also be tested if the path branched but no results could be found.
    Supported values for traversal are `Mapping`, `Iterable`, `re.Match`,
    `xml.etree.ElementTree` (xpath) and `http.cookies.Morsel`.
    Unhelpful values (`{}`, `None`) are treated as the absence of a value and discarded.

    The paths will be wrapped in `variadic`, so that `'key'` is conveniently the same as `('key', )`.

    The keys in the path can be one of:
        - `None`:           Return the current object.
        - `set`:            Requires the only item in the set to be a type or function,
                            like `{type}`/`{type, type, ...}`/`{func}`. If a `type`, return only
                            values of this type. If a function, returns `func(obj)`.
        - `str`/`int`:      Return `obj[key]`. For `re.Match`, return `obj.group(key)`.
        - `slice`:          Branch out and return all values in `obj[key]`.
        - `Ellipsis`:       Branch out and return a list of all values.
        - `tuple`/`list`:   Branch out and return a list of all matching values.
                            Read as: `[traverse_obj(obj, branch) for branch in branches]`.
        - `function`:       Branch out and return values filtered by the function.
                            Read as: `[value for key, value in obj if function(key, value)]`.
                            For `Iterable`s, `key` is the index of the value.
                            For `re.Match`es, `key` is the group number (0 = full match)
                            as well as additionally any group names, if given.
        - `dict`:           Transform the current object and return a matching dict.
                            Read as: `{key: traverse_obj(obj, path) for key, path in dct.items()}`.
        - `any`-builtin:    Take the first matching object and return it, resetting branching.
        - `all`-builtin:    Take all matching objects and return them as a list, resetting branching.
        - `filter`-builtin: Return the value if it is truthy, `None` otherwise.

        `tuple`, `list`, and `dict` all support nested paths and branches.

    @params paths           Paths by which to traverse.
    @param default          Value to return if the paths do not match.
                            If the last key in the path is a `dict`, it will apply to each value inside
                            the dict instead, depth first. Try to avoid if using nested `dict` keys.
    @param expected_type    If a `type`, only accept final values of this type.
                            If any other callable, try to call the function on each result.
                            If the last key in the path is a `dict`, it will apply to each value inside
                            the dict instead, recursively. This does respect branching paths.
    @param get_all          If `False`, return the first matching result, otherwise all matching ones.
    @param casesense        If `False`, consider string dictionary keys as case insensitive.

    `traverse_string` is only meant to be used by YoutubeDL.prepare_outtmpl and is not part of the API

    @param traverse_string  Whether to traverse into objects as strings.
                            If `True`, any non-compatible object will first be
                            converted into a string and then traversed into.
                            The return value of that path will be a string instead,
                            not respecting any further branching.


    @returns                The result of the object traversal.
                            If successful, `get_all=True`, and the path branches at least once,
                            then a list of results is returned instead.
                            If no `default` is given and the last path branches, a `list` of results
                            is always returned. If a path ends on a `dict` that result will always be a `dict`.
    z=The is_user_input parameter is deprecated and no longer worksc                X    t          | t                    r|                                 n| S N)
isinstancestrcasefold)ks    [C:\Users\betacode\Desktop\descargarYoutube\venv\Lib\site-packages\yt_dlp/utils/traversal.py<lambda>ztraverse_obj.<locals>.<lambda>l   s!    As););B     c                ,    t          |           r| nd S r!   )r"   valr   s    r&   r'   ztraverse_obj.<locals>.<lambda>o   s    z#}'E'E O4 r(   c                6    t          pt          | f          S )Nargs)r   r   r*   s    r&   r'   ztraverse_obj.<locals>.<lambda>q   s    )B(# P P P r(   c                   
 d}d }0r. du s$t                     st           t                    rd}d}nB }n<t           t                    rt	          t                               }t                     dk    st          |t                    r<t          d  D                       sJ t          t                               r}nt          |f          }nt           t          t          f          r1d}t          j                            fd D                       }nF du rd}t          t          j        j                  rt%          j        j        	          t          t*          j        j                  r                                }nt3                    s$t          t4          j        j        j                  r}nt          t<          j                  r                                 }nird}tC                    }nTd}nPt                     rd}t          t          j        j                  rt%          j        j        	          t          t*          j        j                  r"                                }nt3                    s$t          t4          j        j        j                  rtG                    }nt          t<          j                  rot          j        tG          $                                g                                 R           %                                "                                          }n#rd}tG          tC                              }nd} fd
|D             }|sd&                    |          }nt           t$                    r4fd "                                D             }fd|D             pd }npt          t*          j        j                  rt          t          j        j                  rt%          j        j        	          st          j'         f          rt          j(         f          n.t	           fd"                                D             d           }nt          t<          j                  rt           tR                    srHtU          j+        tX                    5  $                               }d d d            n# 1 swxY w Y   n<t           tB                    rAt	           fd%                                "                                D             d           }nt           tR          t          f          rt3          t*          j        j-        t4          j        j        j        f          rPt           t                    }tU          j+        tX                    5            }d d d            n# 1 swxY w Y   nCrFtU          j+        tX                    5  tC                              }d d d            n# 1 swxY w Y   nt          t4          j        j        j                  rt           tB                    r .                    d          \  }}

/                    d          s
0                    d          s }d 
|/                    d          rd| }n|r|/                    d          sd| }
fd}	|r1t          tc          |	2                    |                              }n |	          }||r|n|ffS )NF.T r   c              3  @   K   | ]}t          |t                    V  d S r!   )r"   type.0items     r&   	<genexpr>z2traverse_obj.<locals>.apply_key.<locals>.<genexpr>   s,      BBd:dD11BBBBBBr(   r-   c              3  >   K   | ]} |          d          V  dS )r   Nr0   )r4   branch
apply_pathis_lastobjs     r&   r6   z2traverse_obj.<locals>.apply_key.<locals>.<genexpr>   sO       3G 3G8>

30033G 3G 3G 3G 3G 3Gr(   )keyvaluec              3  H   K   | ]\  }}t          ||f           |V  dS )r-   N)r   )r4   r%   vr<   s      r&   r6   z2traverse_obj.<locals>.apply_key.<locals>.<genexpr>   s=      KKDAqAq60J0J0JKaKKKKKKr(    c              3  >   K   | ]\  }}| |d           fV  dS )FNr0   )r4   r%   r?   _traverse_objr:   r;   s      r&   r6   z2traverse_obj.<locals>.apply_key.<locals>.<genexpr>   s<      [[tq!MM#q%AAB[[[[[[r(   c                :    i | ]\  }}|	t           u|||nS r!   )r   )r4   r%   r?   r   s      r&   
<dictcomp>z3traverse_obj.<locals>.apply_key.<locals>.<dictcomp>   s@       7;q!=G:$=$= 117$=$=$=r(   c              3  @   K   | ]\  }} |          k    |V  d S r!   r0   r4   r%   r?   r$   r<   s      r&   r6   z2traverse_obj.<locals>.apply_key.<locals>.<genexpr>   s9      LL$!Q!9K9KA9K9K9K9KLLr(   c              3  @   K   | ]\  }} |          k    |V  d S r!   r0   rF   s      r&   r6   z2traverse_obj.<locals>.apply_key.<locals>.<genexpr>   s;      [[TQQRWZHZHZqHZHZHZHZ[[r(   /@z().z./c                    | S dk    r| j         S                     d          r$t          | j         j        dd          f          S dk    r| j        S t          d          )NrI   r   r-   ztext()z#apply_specials is missing case for )attrib
startswithr   gettextSyntaxError)elementspecials    r&   apply_specialsz7traverse_obj.<locals>.apply_key.<locals>.apply_specials   s    ?"Nc>>">)%%c** M#GN$6gabbk^LLLLh&&"<'!"S"S"STTTr(   )3callabler"   slicesetnextiterlenr2   alltupler   list	itertoolschainfrom_iterablehttpcookiesMorseldictr<   r=   collectionsabcMappingvaluesr   xmletreeElementTreeElementreMatchgroupsr#   items	enumerategroup	groupdictjoin__contains__rN   int
contextlibsuppress
IndexErrorSequence
rpartitionrM   endswithmapiterfind)r<   r;   r:   	branchingresultr5   iter_objxpath_rS   rR   rB   r9   r$   r   r   r   s   ```       @r&   	apply_keyztraverse_obj.<locals>.apply_keys   s!   	;?;czzXc]]zje.D.Dz 	[FFS!! l	-S		??D3xx!||z$55|BBcBBBBBBBBc5::.. ! F!$cV444dE]++ c	-I_22 3G 3G 3G 3G 3G 3GBE3G 3G 3G G GFF CZZI#t|233 >3CG39===#{677 
!#&& *S#):O:W*X*X C**   !	Sc]] N	-I#t|233 >3CG39===#{677 99;;!#&& 
*S#):O:W*X*X 
$S>>C** $?syy{{:SZZ\\::;;MMOO))++- - ! !	$SXX..KKKKHKKKF )T"" 8	-[[[[[[syy{{[[[H   ?G     F
 [_455 1	-#t|233 >3CG39===8A TXcN^fiekElElEl ThswcV4444LLLLL#))++LLLdSS F RX&& +	-#s## cy c(44 , , YYs^^F, , , , , , , , , , , , , , , C%% c[[[[[S]]__-B-B-D-D[[[]abbc5\** #	-ko&>	@U@]%^__ +&sE22	(44 & & XF& & & & & & & & & & & & & & &  +(44 + + XXc]F+ + + + + + + + + + + + + + + SY2:;; 	-
3PS@T@T 	- #s 3 3E1g%%c** 73C3CD3I3I  $$ %#E %u//55 %$U	U 	U 	U 	U 	U  -c.#,,u2E2EFFGG',,I<&&F9<<s6   T55T9<T9	X..X25X2Y88Y<?Y<c              3     K   t          |           }t          |t                    }|t          u rd S |D ]
}d|fV  |}d|fV  d S )NFT)rX   rW   r   )iterableiteratorprevr5   s       r&   	lazy_lastztraverse_obj.<locals>.lazy_last   sl      >>Hj)):F 	 	D+DDDjr(   c           	     H   | f}d}d } t          |t          t          t          t          f                    D ]\  }}s)t          |t                    r|                                }|t          t          fv r:d}d |D             }|t          u rt          |d           f}nt          |          f}{|t          u rt          d |          }t          |          r(t          j        |                              d d            g }|D ],}	 ||	|          \  }
}||
z  }|                    |           -t"          j                            |          }|r2t          |t          t          t(          f          st+          |          }||t          |t                    fS )NFc              3  &   K   | ]}|d i fv|V  d S r!   r0   )r4   r;   s     r&   r6   z3traverse_obj.<locals>.apply_path.<locals>.<genexpr>  s0       N ND":8M8M8M8M8M8M N Nr(   )r   r#   bytesrc   rV   r"   r$   anyrZ   rW   r\   filterrT   inspect	signaturebindappendr]   r^   r_   r[   r|   )	start_objpath	test_typeobjshas_branchedr<   lastfiltered_objsnew_objsr;   r~   resultsr   r   r   	type_tests               r&   r9   z traverse_obj.<locals>.apply_path   s   |"8D3tS2I#J#JKK 	; 	;ID# %C!5!5 %llnnsCj  $ N N N N N#:: 557DD //1Df}}dD))%c]] 8!#&&++D$777H ) )%.YsC%>%>"	7	)((((?00::DD 	(ZdD%-@AA 	(y$''D\:c4#8#888r(   c                     | ||          \  }}}t          d |D                       }	r)|r'|r|                                S |rt          u rg nS d S |r|d         n|r|ri nd S )Nc              3  &   K   | ]}|d i fv|V  d S r!   r0   r3   s     r&   r6   z6traverse_obj.<locals>._traverse_obj.<locals>.<genexpr>$  s0      NNDtD":7M7M47M7M7M7MNNr(   r   )r   exhaustr   )
r;   r   allow_emptyr   r   r   is_dictr9   r   r   s
          r&   rB   z#traverse_obj.<locals>._traverse_obj"  s    )3Cy)I)I&wNNGNNNNN 	| 	 )((( @$
22rr?4$Qwqzz*Q*Q""TQr(   r   TNexpected)
r   r	   r"   r2   rp   rY   _RequiredErrorr   orig_msgr   )r;   r   r   r   r   r   r   pathsindexr   r:   r   erB   r   r9   r$   r   r   s    ```` `      @@@@@@r&   traverse_objr   &   s   F J&&[\\\BBH-&& QOOOO		PPPP	z= z= z= z= z= z= z= z= z= z=x
 
 
%9 %9 %9 %9 %9 %9 %9 %9N
R 
R 
R 
R 
R 
R 
R !** 	P 	Pt3u::%	P"]3gt<<F! " 	P 	P 	P P$QZ!*EEE4OP P P P P	P
 j((44g5s   B!!
C+CCc                     fdS )Nc                    S r!   r0   )r   r=   s    r&   r'   zvalue.<locals>.<lambda>=  s    U r(   r0   )r=   s   `r&   r=   r=   <  s    ???r(   r   c                    fd}|S )Nc                4    | t          d           | S )NzUnable to extract r   )r   )r=   r   names    r&   funczrequire.<locals>.funcA  s)    = !<d!<!<xPPPPr(   r0   )r   r   r   s   `` r&   requirer   @  s)          Kr(   c                      e Zd ZdS )r   N)__name__
__module____qualname__r0   r(   r&   r   r   J  s        Dr(   r   undlangextr   
str | Noner   return=collections.abc.Callable[[list[dict]], dict[str, list[dict]]]c                    d S r!   r0   r   s     r&   subs_list_to_dictr   N  s     NQ  NQr(   subslist[dict] | Nonedict[str, list[dict]]c                  d S r!   r0   )r   r   r   s      r&   r   r   R  s     BE  BEr(   c                 | t          j        t          ||          S t          j        t
                    }| D ]}t          |                    d                    s|                    d          s:|                    dd          }t          |t                    s|sh|}|                    d          }t          |t                    s|s|                    dd           n||d<   ||                             |           t          |          }|                                D ]} |                     d            |S )	ax  
    Convert subtitles from a traversal into a subtitle dict.
    The path should have an `all` immediately before this function.

    Arguments:
    `ext`      The default value for `ext` in the subtitle dict

    In the dict you can set the following additional items:
    `id`       The subtitle id to sort the dict into
    `quality`  The sort order for each subtitle
    Nr   urldataidr   c                2    |                      dd          pdS )Nqualityr   )pop)xs    r&   r'   z#subs_list_to_dict.<locals>.<lambda>y  s    i 3 3 8q r(   )r<   )	functoolspartialr   rd   defaultdictr\   r   rN   r   r"   r#   r   rc   rg   sort)r   r   r   r   subsub_idsub_exts          r&   r   r   V  sJ    | !23GGGG$T**F # #3775>>** 	3776?? 	t$$&#&& 	 F''%..'3'' 	! !t$$$$ E
vc""""&\\F : :		88	9999Mr(   taghtmlregexattrr#   r=   r   c                    d S r!   r0   r   r=   r   r   r   s        r&   find_elementr   ~  s    ]`]`r(   )r   clsc                    d S r!   r0   r   r   s     r&   r   r     s    +.3r(   r   c                    d S r!   r0   )r   r   r   r   s       r&   r   r     s    ORsr(   )r   r   c                    d S r!   r0   r   s      r&   r   r     s    8;r(   )r   r   r   r   r=   r   r   c                   	  s|s|s|r|s
J d            d}|rF|rD|r
J d            |r
J d            |rt           nt          }t          j        ||| p||           S |rI|r
J d             
J d            |r
J d            |rt          nt
          }t          j        ||          S |r+|rt          nt          }t          j        || p||           S t          t          |                    		 fd	S )
Nz3One of tag, id, cls or (attr AND value) is required[\w:.-]+Cannot match both attr and clszCannot match both attr and idr   escape_valuezCannot match both cls and idCannot match both cls and tagCannot use regex with clsc                0    t          |                    S r!   )r   )r   r   r   s    r&   r'   zfind_element.<locals>.<lambda>  s    8dCCEJ r(   )
r   r   r   r   r   r   r   r   ru   bool)
r   r   r   r   r=   r   r   ANY_TAGr   r   s
   `        @r&   r   r     sg   f"fffff1ffffG W W88888866666604R,,:R tUwY^U^____	 	W555555{{;{{{555555,0J((6J s+++	 W)-D%%3D rs~gPUIVVVVT

OOEJJJJJJr(   c                    d S r!   r0   r   s     r&   find_elementsr     s    ,/Cr(   c                    d S r!   r0   r   s        r&   r   r     s    ^a^ar(   )r   r   r   r=   r   r   c                   |s|r|s
J d            |r:|r8|r
J d            |rt           nt          }t          j        |||| pd|           S | r
J d            |r
J d            |rt          nt
          }t          j        ||          S )Nz*One of cls or (attr AND value) is requiredr   r   r   r   r   )r   r   r   r   r
   r   )r   r   r   r=   r   r   r   s          r&   r   r     s    P4PEPP$PPPP d d88888815T--;T tU8J{]bYbcccc333333111111)-H%%3HDT3'''r(   )startendc                      fd}|S )Nc                    | d S d}r$|                                rt                    }r-|                               r| |t                              S | |d          S )Nr   )rM   rY   r{   )s	start_idxr   r   s     r&   trimztrim_str.<locals>.trim  sw    94	 	#Q\\%(( 	#E

I 	*1::c?? 	*YCy())}r(   r0   )r   r   r   s   `` r&   trim_strr     s)          Kr(   c                J     t          j                    fd            }|S )Nc                     | i S r!   r0   )ro   r   kwargss    r&   innerzunpack.<locals>.inner  s    tU%f%%%r(   )r   wraps)r   r   r   s   `` r&   unpackr     s>    _T& & & & & & Lr(   c                >    t          | gd |D             R i |ddiS )Nc              3  <   K   | ]}d gt          |          R V  dS ).N)r   )r4   keyss     r&   r6   zget_first.<locals>.<genexpr>  s3      HH$5htnn55HHHHHHr(   r   F)r   )r;   r   r   s      r&   	get_firstr     s;    bHH%HHHbbbFbb\abbbbr(   c                d    t          | j        t          |                    D ]}||s|s|c S |S r!   )r|   rN   r   )dkey_or_keysr   skip_false_valuesr+   s        r&   dict_getr    sB    15(;//00  ??+<?JJJNr(   )r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r!   )r   r   )r   r#   r=   r#   r   r   )r   r#   )r   r#   r   r   )r   r#   )NT)/
__future__r   rd   collections.abcrv   r   http.cookiesr`   r   r]   rl   typingxml.etree.ElementTreerh   _utilsr   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r=   r   r   overloadr   r   r   r   r   r   r  r0   r(   r&   <module>r	     s   " " " " " "                          				                                                2 (tTj%S6 S6 S6 S6 S6l   "'     	 	 	 	 	^ 	 	 	 ,1T  Q  Q  Q  Q  Q  Q HMae  E  E  E  E  E  E%D % % % % % %P =AUZ ` ` ` ` `  ` #( . . . . .  . /3%u R R R R R  R #( ; ; ; ; ;  ; 4d$UZ_ K K K K K4 $) / / / / /  / >BV[ a a a a a  a 4d$URW ( ( ( ( (        c c c     r(   