
    VGhYG                       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jN                  dddd%d       Z(ejN                  dddd&d       Z(d'dddd(dZ(ejN                  ddddd)d       Z)ejN                  ddd*d       Z)ejN                  ddddd+d       Z)ejN                  dddd,d       Z)dddddddddZ)ejN                  ddd*d       Z*ejN                  ddddd)d       Z*ddddddddZ*ddd d!Z+d" Z,d# Z-d-d$Z.y).    )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 ]%  \  }}	|t        |      k(  }
	  | |	|
d
      }||c S ' t         u rdS S # t        $ r.}|
r"t        |j                  |j                        dY d}~fd}~ww xY w)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                F    t        | t              r| j                         S | S N)
isinstancestrcasefold)ks    _/var/www/it7/html/youtubeDownloader/venv/lib/python3.12/site-packages/yt_dlp/utils/traversal.py<lambda>ztraverse_obj.<locals>.<lambda>l   s    As);      c                $    t        |       r| S d S r!   )r"   valr   s    r&   r'   ztraverse_obj.<locals>.<lambda>o   s    z#}'E 4 r(   c                0    t        xs t        | f      S )Nargs)r   r   r*   s    r&   r'   ztraverse_obj.<locals>.<lambda>q   s    )B(# P r(   c                ,    d}d }(r& du st               st         t              rd}d}n }nt         t              rpt	        t                     }t               dkD  st        |t              r.t        d  D              sJ t        t                     r<}n8t        |f      }n(t         t        t        f      r.d}t        j                  j                  fd D              }n du rd}t        t        j                   j"                        r"t%        j&                  j(                  	      t        t*        j,                  j.                        rj1                         }nbt3              s.t        t4        j6                  j8                  j:                        r}n%t        t<        j>                        rjA                         }nrd}tC              }nd}nt               rtd}t        t        j                   j"                        r"t%        j&                  j(                  	      t        t*        j,                  j.                        rjE                         }nt3              s.t        t4        j6                  j8                  j:                        rtG              }nt        t<        j>                        r\t        j                  tG        jI                         gjA                               jK                         jE                               }nrd}tG        tC                    }nd} fd
|D        }|swdjM                  |      }ndt         t$              rEfd jE                         D        }|D ci c]  \  }}|tN        ur|||n c}}xs d }nt        t*        j,                  j.                        rt        t        j                   j"                        r"t%        j&                  j(                  	      st        jP                   f      rt        jR                   f      n#t	         fdjE                         D        d       }nMt        t<        j>                        rt         tT              sr5tW        jX                  tZ              5  jI                         }d d d        nt         tB              rt	         fdjK                         jE                         D        d       }nt         tT        t        f      rt3        t*        j,                  j\                  t4        j6                  j8                  j:                  f      r9t         t              }tW        jX                  tZ              5      }d d d        nrtW        jX                  tZ              5  tC                  }d d d        nt        t4        j6                  j8                  j:                        rt         tB              r j_                  d      \  }	}
ja                  d      sjc                  d      s }	d |	ja                  d      rd|	 }	n|	r|	ja                  d      sd|	 }	fd}|	r%t        te        |jg                  |	                  }n |      }||r|fS |ffS c c}}w # 1 sw Y   xY w# 1 sw Y   %xY w# 1 sw Y   1xY w)NF.T r   c              3  <   K   | ]  }t        |t                y wr!   )r"   type.0items     r&   	<genexpr>z2traverse_obj.<locals>.apply_key.<locals>.<genexpr>   s     Bd:dD1Bs   r-   c              3  8   K   | ]  } |      d      yw)r   Nr0   )r4   branch
apply_pathis_lastobjs     r&   r6   z2traverse_obj.<locals>.apply_key.<locals>.<genexpr>   s&      3G8>
3033Gs   )keyvaluec              3  H   K   | ]  \  }}t        ||f       s|  yw)r-   N)r   )r4   r%   vr<   s      r&   r6   z2traverse_obj.<locals>.apply_key.<locals>.<genexpr>   s"     KDAqAq60JaKs   "" c              3  >   K   | ]  \  }}| |d       f  yw)FNr0   )r4   r%   r?   _traverse_objr:   r;   s      r&   r6   z2traverse_obj.<locals>.apply_key.<locals>.<genexpr>   s%     [tq!M#q%AB[s   c              3  @   K   | ]  \  }} |      k(  s|  y wr!   r0   r4   r%   r?   r$   r<   s      r&   r6   z2traverse_obj.<locals>.apply_key.<locals>.<genexpr>   s      L$!Q!9KAL   c              3  @   K   | ]  \  }} |      k(  s|  y wr!   r0   rD   s      r&   r6   z2traverse_obj.<locals>.apply_key.<locals>.<genexpr>   s"     [TQQRWZHZq[rE   /@z().z./c                    | S dk(  r| j                   S j                  d      r%t        | j                   j                  dd  f      S dk(  r| j                  S t        d      )NrH   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   sp    ?"Nc>">>)%%c*#GNN$6$6gabk^LLh&"<<'!$G{"STTr(   )4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joinr   __contains__rM   int
contextlibsuppress
IndexErrorSequence
rpartitionrL   endswithmapiterfind)r<   r;   r:   	branchingresultr5   iter_objr%   r?   xpath_rR   rQ   rB   r9   r$   r   r   r   s   ```         @r&   	apply_keyztraverse_obj.<locals>.apply_keys   sh   	;?czXc]je.D 	[FS!S	?D3x!|z$5BcBBBBc5:. F!$cV4dE]+I__22 3GBE3G GF CZI#t||2233CGG399=#{667!#&*S#)):O:O:W:W*XC* !	Sc]I#t||2233CGG399=#{66799;!#&*S#)):O:O:W:W*X$S>C*$??syy{:SZZ\:;MMO))+- !!	$SX.KHKFT"[syy{[H?G7;q!=G:$= 172   
 [__445#t||2233CGG399=8AXcN^N^fiekElhswwcV4L#))+LdS  RXX&#s#y((4 , YYs^F, , C%[S]]_-B-B-D[]abc5\*koo&>&>		@U@U@]@]%^_&sE2	((4 & XF& & ((4 + Xc]F+ + SYY22::;
3PS@T #s 3E1g%%c*73C3CD3I $E7u//5UG	U c.#,,u2EFG',I&<<F9<<q, ,& &+ +s*   .[,[2[>.\
2[;>\
\c              3  ~   K   t        |       }t        |t              }|t        u ry |D ]
  }d|f |} d|f y w)NFT)rW   rV   r   )iterableiteratorprevr5   s       r&   	lazy_lastztraverse_obj.<locals>.lazy_last   sP     >Hj): 	D+D	 Djs   ;=c           	        | f}d}d } t        |t        t        t        t        f            D ]  \  }}s t        |t              r|j                         }|t        t        fv r.d}d |D        }|t        u rt        |d       f}nt        |      f}d|t        u rt        d |      }yt        |      r%t        j                  |      j                  d d        g }|D ]%  }	 ||	|      \  }
}||
z  }|j!                  |       ' t"        j$                  j'                  |      } |r't        |t        t        t(        f      st+        |      }||t        |t              fS )NFc              3  .   K   | ]  }|d i fvs
|  y wr!   r0   )r4   r;   s     r&   r6   z3traverse_obj.<locals>.apply_path.<locals>.<genexpr>  s      ND":8M N   )r   r#   bytesrb   rU   r"   r$   anyrY   rV   r[   filterrS   inspect	signaturebindappendr\   r]   r^   rZ   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   sT   |"8D3tS2I#JK 	;ID#C!5llnsCj $ N N#: 57D /1Df}dD)%c]!!#&++D$7H )%.sC%>"	7	)()
 ??00:D7	;: ZdD%-@Ay$'D\:c4#888r(   c                     | ||      \  }}}t        d |D              }	r#|r!|r|j                         S |rt        u rg S S y |r|d   S |r|ri S d S )Nc              3  .   K   | ]  }|d i fvs
|  y wr!   r0   r3   s     r&   r6   z6traverse_obj.<locals>._traverse_obj.<locals>.<genexpr>$  s     NDtD":7M4Nr   r   )r   exhaustr   )
r;   r   allow_emptyr   r   r   is_dictr9   r   r   s
          r&   rB   z#traverse_obj.<locals>._traverse_obj"  so    )3Cy)I&wNGNN|(($
2r??$wqzQ"QTQr(   r   TNexpected)
r   r	   r"   r2   ro   rX   _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&[\BH-&O	P	z= z=x
%9N
R !* 	Pt3u:%	P"3gt<F! "		P j(45g5  	P$QZZ!**E4O 	Ps   :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                ,    | t        d       | S )NzUnable to extract r   )r   )r=   r   names    r&   funczrequire.<locals>.funcA  s"    = #5dV!<xPPr(   r0   )r   r   r   s   `` r&   requirer   @  s     Kr(   c                      e Zd Zy)r   N)__name__
__module____qualname__r0   r(   r&   r   r   J  s    r(   r   undlangextc                     y r!   r0   r   s     r&   subs_list_to_dictr   N  s	     NQr(   c                   y r!   r0   )subsr   r   s      r&   r   r   R  s	     BEr(   c              ,   | t        j                  t        ||      S t        j                  t
              }| D ]  }t        |j                  d            s|j                  d      s/|j                  dd      }t        |t              s|sT|}|j                  d      }t        |t              s|s|j                  dd       n||d<   ||   j                  |        t        |      }|j                         D ]  } | j                  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                .    | j                  dd      xs dS )Nqualityr   )pop)xs    r&   r'   z#subs_list_to_dict.<locals>.<lambda>y  s    i 3 8q r(   )r<   )	functoolspartialr   rc   defaultdictr[   r   rM   r   r"   r#   r   rb   rf   sort)r   r   r   r~   subsub_idsub_exts          r&   r   r   V  s     |  !23GG$$T*F #3775>*3776?t$&#&F''%.'3't$ E
vc"# &\F :		8	9: Mr(   taghtmlregexc                     y r!   r0   attrr=   r   r   r   s        r&   find_elementr   ~  s    ]`r(   )r   c                     y r!   r0   clsr   s     r&   r   r     s    +.r(   c                     y r!   r0   )r   r   r   r   s       r&   r   r     s    ORr(   )r   r   c                     y 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}|rA|r?|rJ d       |rJ d       |rt         nt        }t        j                  ||| xs ||       S |r?|rJ d        J d       |rJ d       |rt        nt
        }t        j                  ||      S |r,|rt        nt        }t        j                  || xs ||       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                "    t        |          S r!   )r   )r   r   r   s    r&   r'   zfind_element.<locals>.<lambda>  s    8dCEJ r(   )
r   r   r   r   r   r   r   r   rt   bool)
r   r   r   r   r=   r   r   ANY_TAGr   r   s
   `        @r&   r   r     s    "f1ff/G888w666v04,:R  tUwY^U^__	555v{;;;{555y,0(6J  s++	)-%3D  rs~gPUIVVT
OEJJr(   c                     y r!   r0   r   s     r&   find_elementsr     s    ,/r(   c                     y r!   r0   r   s        r&   r   r     s    ^ar(   )r   r   r   r=   r   r   c                    |s|r|sJ d       |r8|r6|rJ d       |rt         nt        }t        j                  |||| xs 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    4EP$PP"888w15-;T  tU8J{]bYbcc33371119)-%3HDT3''r(   )startendc                      fd}|S )Nc                    | y d}r| j                        rt              }r | j                        r| |t                S | |d  S )Nr   )rL   rX   rz   )s	start_idxr   r   s     r&   trimztrim_str.<locals>.trim  sS    9	Q\\%(E
I1::c?YCy))}r(   r0   )r   r   r   s   `` r&   trim_strr     s     Kr(   c                F     t        j                          fd       }|S )Nc                     | i S r!   r0   )rn   r   kwargss    r&   innerzunpack.<locals>.inner  s    U%f%%r(   )r   wraps)r   r   r   s   `` r&   unpackr     s%    __T& & Lr(   c                6    t        | gd |D        i |ddiS )Nc              3  :   K   | ]  }d gt        |        yw).N)r   )r4   keyss     r&   r6   zget_first.<locals>.<genexpr>  s     H$5htn5Hs   r   F)r   )r;   r   r   s      r&   	get_firstr     s#    bH%HbFb\abbr(   c                d    t        | j                  t        |            D ]  }||s|r|c S  |S r!   )r{   rM   r   )dkey_or_keysr   skip_false_valuesr+   s        r&   dict_getr     s7    155(;/0 ?+<J Nr(   )r   
str | Noner   r   returnz=collections.abc.Callable[[list[dict]], dict[str, list[dict]]])r   list[dict] | Noner   r   r   r   r   zdict[str, list[dict]]r!   )r   r   )r   r#   r=   r#   r   r   )r   r#   )r   r#   r   r   )r   r#   )NT)/
__future__r   rc   collections.abcru   r   http.cookiesr_   r   r\   rk   typingxml.etree.ElementTreerg   _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6l "' 	^ 	 ,1T  Q  Q HMae  E  E%D %P =AUZ `  ` #( .  . /3%u R  R #( ;  ; 4d$UZ_ K4 $) /  / >BV[ a  a 4d$URW (  cr(   