
    g,2                     x    d Z ddlmZmZ ddlmZmZmZmZ ddl	m
Z
mZ ddlmZ  G d de      Z G d d	e      Zy
)zFThis module provides a query interface for media streams and captions.    )MappingSequence)CallableListOptionalUnion)CaptionStream)
deprecatedc                      e Zd ZdZd Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d!dZdee   dd fdZde	dd fd	Z
d"d
Zd"dZdedee   fdZde	dee   fdZdee   fdZdee   fdZd#de	dee   fdZd$dedd fdZdee   fdZd Z ed      d%dee	   defd       Z ed      dee   fd       Zdeeef   fdZdefdZde	fd Z y)&StreamQueryz3Interface for querying the available media streams.c                 n    || _         |D ci c]  }t        |j                        | c}| _        yc c}w )zConstruct a :class:`StreamQuery <StreamQuery>`.

        param list fmt_streams:
            list of :class:`Stream <Stream>` instances.
        N)fmt_streamsintitag
itag_index)selfr   ss      U/var/www/it7/html/youtubeDownloader/venv/lib/python3.12/site-packages/pytube/query.py__init__zStreamQuery.__init__   s-     '3>?a3qvv;>??s   2Nc                 P  	
 g }srkt        t              st        t              r|j                  fd       n5t        t              st        t              r|j                  fd       r|j                  fd       r|j                  fd       r|j                  fd       sr|j                  fd       s	r|j                  	fd       
r|j                  
fd       r|j                  fd	       |r|j                  d
        |r|j                  d        |r|j                  d        |r|j                  d        |r|j	                  |       |j                  fd       | j                  |      S )a  Apply the given filtering criterion.

        :param fps:
            (optional) The frames per second.
        :type fps:
            int or None

        :param resolution:
            (optional) Alias to ``res``.
        :type res:
            str or None

        :param res:
            (optional) The video resolution.
        :type resolution:
            str or None

        :param mime_type:
            (optional) Two-part identifier for file formats and format contents
            composed of a "type", a "subtype".
        :type mime_type:
            str or None

        :param type:
            (optional) Type part of the ``mime_type`` (e.g.: audio, video).
        :type type:
            str or None

        :param subtype:
            (optional) Sub-type part of the ``mime_type`` (e.g.: mp4, mov).
        :type subtype:
            str or None

        :param file_extension:
            (optional) Alias to ``sub_type``.
        :type file_extension:
            str or None

        :param abr:
            (optional) Average bitrate (ABR) refers to the average amount of
            data transferred per unit of time (e.g.: 64kbps, 192kbps).
        :type abr:
            str or None

        :param bitrate:
            (optional) Alias to ``abr``.
        :type bitrate:
            str or None

        :param video_codec:
            (optional) Video compression format.
        :type video_codec:
            str or None

        :param audio_codec:
            (optional) Audio compression format.
        :type audio_codec:
            str or None

        :param bool progressive:
            Excludes adaptive streams (one file contains both audio and video
            tracks).

        :param bool adaptive:
            Excludes progressive streams (audio and video are on separate
            tracks).

        :param bool is_dash:
            Include/exclude dash streams.

        :param bool only_audio:
            Excludes streams with video tracks.

        :param bool only_video:
            Excludes streams with audio tracks.

        :param custom_filter_functions:
            (optional) Interface for defining complex filters without
            subclassing.
        :type custom_filter_functions:
            list or None

        c                 *    | j                   xs k(  S N
resolutionr   resr   s    r   <lambda>z$StreamQuery.filter.<locals>.<lambda>   s    #:K)L     c                 (    | j                   xs v S r   r   r   s    r   r   z$StreamQuery.filter.<locals>.<lambda>   s    #:K)L r   c                 "    | j                   k(  S r   )fps)r   r"   s    r   r   z$StreamQuery.filter.<locals>.<lambda>   s    QUUc\ r   c                 "    | j                   k(  S r   )	mime_type)r   r$   s    r   r   z$StreamQuery.filter.<locals>.<lambda>   s    Q[[I%= r   c                 "    | j                   k(  S r   )type)r   r&   s    r   r   z$StreamQuery.filter.<locals>.<lambda>   s    QVVt^ r   c                 *    | j                   xs k(  S r   )subtype)r   file_extensionr(   s    r   r   z$StreamQuery.filter.<locals>.<lambda>   s    QYY73Ln%M r   c                 *    | j                   xs k(  S r   )abr)r   r+   bitrates    r   r   z$StreamQuery.filter.<locals>.<lambda>   s    QUUs~g%> r   c                 "    | j                   k(  S r   )video_codec)r   r.   s    r   r   z$StreamQuery.filter.<locals>.<lambda>       Q]]k%A r   c                 "    | j                   k(  S r   )audio_codec)r   r1   s    r   r   z$StreamQuery.filter.<locals>.<lambda>   r/   r   c                 8    | j                   xr | j                   S r   )includes_audio_trackincludes_video_trackr   s    r   r   z$StreamQuery.filter.<locals>.<lambda>       **I13I3I/I r   c                 8    | j                   xr | j                   S r   )r4   r3   r5   s    r   r   z$StreamQuery.filter.<locals>.<lambda>   r6   r   c                     | j                   S r   )is_progressiver5   s    r   r   z$StreamQuery.filter.<locals>.<lambda>   s    Q%5%5 r   c                     | j                   S r   )is_adaptiver5   s    r   r   z$StreamQuery.filter.<locals>.<lambda>   s
    Q]] r   c                 "    | j                   k(  S r   )is_dash)r   r=   s    r   r   z$StreamQuery.filter.<locals>.<lambda>   s    QYY'%9 r   )
isinstancestrappendlistextend_filter)r   r"   r   r   r$   r&   r(   r)   r+   r,   r.   r1   
only_audio
only_videoprogressiveadaptiver=   custom_filter_functionsfilterss    ```````````    `  r   filterzStreamQuery.filter   sC   N *#s#z*c'BLMC&*Z*FLMNN12NN=>NN34nNNMN'NN>?NNABNNABNN NN NN56NN23"NN23NN9:||G$$r   rI   returnc                 h    | j                   }|D ]  }t        ||      } t        t        |            S r   )r   rJ   r   rA   )r   rI   r   filter_lambdas       r   rC   zStreamQuery._filter   s8    &&$ 	=M <K	=4,--r   attribute_namec                    | j                   D cg c]  }t        |      | }}|r7t        t        |d         t              r	 t	        t        |fd            S t	        t        |fd            S c c}w # t        $ r Y )w xY w)zApply a sort order. Filters out stream the do not have the attribute.

        :param str attribute_name:
            The name of the attribute to sort by.
        r   c                 |    t        dj                  t        t        j                  t        |                         S )N )r   joinrJ   r?   isdigitgetattrr   rN   s    r   r   z&StreamQuery.order_by.<locals>.<lambda>   s-    cGG &s{{GA~4N O' r   )keyc                     t        |       S r   )rT   rU   s    r   r   z&StreamQuery.order_by.<locals>.<lambda>   s    >0J r   )r   rT   r>   r?   r   sorted
ValueError)r   rN   r   has_attributes    `  r   order_byzStreamQuery.order_by   s     %%
q.)5 
 
 ZM!$n5s

"%	 	 =&JK
 	
3
,  s   A9A> >	B
	B
c                 8    t        | j                  ddd         S )z_Sort streams in descending order.

        :rtype: :class:`StreamQuery <StreamQuery>`

        N)r   r   r   s    r   desczStreamQuery.desc   s     4++DbD122r   c                     | S )z^Sort streams in ascending order.

        :rtype: :class:`StreamQuery <StreamQuery>`

         r^   s    r   asczStreamQuery.asc   s	     r   r   c                 J    | j                   j                  t        |            S )a6  Get the corresponding :class:`Stream <Stream>` for a given itag.

        :param int itag:
            YouTube format identifier code.
        :rtype: :class:`Stream <Stream>` or None
        :returns:
            The :class:`Stream <Stream>` matching the given itag or None if
            not found.

        )r   getr   )r   r   s     r   get_by_itagzStreamQuery.get_by_itag   s     ""3t9--r   r   c                 F    | j                  dd|      j                         S )a  Get the corresponding :class:`Stream <Stream>` for a given resolution.

        Stream must be a progressive mp4.

        :param str resolution:
            Video resolution i.e. "720p", "480p", "360p", "240p", "144p"
        :rtype: :class:`Stream <Stream>` or None
        :returns:
            The :class:`Stream <Stream>` matching the given itag or None if
            not found.

        Tmp4)rF   r(   r   )rJ   first)r   r   s     r   get_by_resolutionzStreamQuery.get_by_resolution   s(     {{e
  

%'	r   c                 b    | j                  dd      j                  d      j                         S )zGet lowest resolution stream that is a progressive mp4.

        :rtype: :class:`Stream <Stream>` or None
        :returns:
            The :class:`Stream <Stream>` matching the given itag or None if
            not found.

        Trg   )rF   r(   r   )rJ   r[   rh   r^   s    r   get_lowest_resolutionz!StreamQuery.get_lowest_resolution  s*     KKD%K8Xl#UW	
r   c                 `    | j                  d      j                  d      j                         S )zGet highest resolution stream that is a progressive video.

        :rtype: :class:`Stream <Stream>` or None
        :returns:
            The :class:`Stream <Stream>` matching the given itag or None if
            not found.

        T)rF   r   rJ   r[   lastr^   s    r   get_highest_resolutionz"StreamQuery.get_highest_resolution  s)     {{t{,55lCHHJJr   r(   c                 b    | j                  d|      j                  d      j                         S )a9  Get highest bitrate audio stream for given codec (defaults to mp4)

        :param str subtype:
            Audio subtype, defaults to mp4
        :rtype: :class:`Stream <Stream>` or None
        :returns:
            The :class:`Stream <Stream>` matching the given itag or None if
            not found.
        T)rD   r(   r+   rm   )r   r(   s     r   get_audio_onlyzStreamQuery.get_audio_only%  s)     KK4K9Xe_TV	
r   is_otfc                 .    | j                  fdg      S )a  Filter stream by OTF, useful if some streams have 404 URLs

        :param bool is_otf: Set to False to retrieve only non-OTF streams
        :rtype: :class:`StreamQuery <StreamQuery>`
        :returns: A StreamQuery object with otf filtered streams
        c                 "    | j                   k(  S r   )rr   )r   rr   s    r   r   z!StreamQuery.otf.<locals>.<lambda><  s    qxx6'9 r   )rC   )r   rr   s    `r   otfzStreamQuery.otf5  s     ||9:;;r   c                 @    	 | j                   d   S # t        $ r Y yw xY w)zGet the first :class:`Stream <Stream>` in the results.

        :rtype: :class:`Stream <Stream>` or None
        :returns:
            the first result of this query or None if the result doesn't
            contain any streams.

        r   Nr   
IndexErrorr^   s    r   rh   zStreamQuery.first>  s*    	##A&& 		    	c                 @    	 | j                   d   S # t        $ r Y yw xY w)zGet the last :class:`Stream <Stream>` in the results.

        :rtype: :class:`Stream <Stream>` or None
        :returns:
            Return the last result of this query or None if the result
            doesn't contain any streams.

        r]   Nrw   r^   s    r   rn   zStreamQuery.lastL  s*    	##B'' 		ry   z.Get the size of this list directly using len()valuec                 R    |r| j                   j                  |      S t        |       S )zAGet the count of items in the list.

        :rtype: int
        )r   countlen)r   r{   s     r   r}   zStreamQuery.countZ  s(     ##))%004yr   z6This object can be treated as a list, all() is uselessc                     | j                   S zXGet all the results represented by this query as a list.

        :rtype: list

        r   r^   s    r   allzStreamQuery.alle  s     r   ic                      | j                   |   S r   r   r   r   s     r   __getitem__zStreamQuery.__getitem__n  s    ""r   c                 ,    t        | j                        S r   )r~   r   r^   s    r   __len__zStreamQuery.__len__q  s    4##$$r   c                     | j                    S r   r   r^   s    r   __repr__zStreamQuery.__repr__t  s    ""#$r   )NNNNNNNNNNNNNNNNN)rK   r   )rg   )Fr   )!__name__
__module____qualname____doc__r   rJ   r   r   rC   r?   r[   r_   rb   r   r   r
   re   ri   rk   ro   rq   boolru   rh   rn   r   r}   r   r   slicer   r   r   ra   r   r   r   r   	   s   =@  $%]%~.tH~ .- .!
s !
} !
F3. .(8 .C HV4D "
x'7 
	K(8 	K
c 
hv6F 
 <$ <= <x'  @A8C= C  B HI T&\   J #U5#:. #% %%# %r   r   c                       e Zd ZdZdee   fdZ ed      dede	e   fd       Z
 ed      dee   fd	       Zd
efdZdefdZd ZdefdZy)CaptionQueryz.Interface for querying the available captions.captionsc                 N    |D ci c]  }|j                   | c}| _        yc c}w )zConstruct a :class:`Caption <Caption>`.

        param list captions:
            list of :class:`Caption <Caption>` instances.

        N)codelang_code_index)r   r   cs      r   r   zCaptionQuery.__init__{  s"     4<<a	<<s   "z?This object can be treated as a dictionary, i.e. captions['en']	lang_coderK   c                 8    | j                   j                  |      S )a[  Get the :class:`Caption <Caption>` for a given ``lang_code``.

        :param str lang_code:
            The code that identifies the caption language.
        :rtype: :class:`Caption <Caption>` or None
        :returns:
            The :class:`Caption <Caption>` matching the given ``lang_code`` or
            None if it does not exist.
        )r   rd   )r   r   s     r   get_by_language_codez!CaptionQuery.get_by_language_code  s     ##''	22r   z*This object can be treated as a dictionaryc                 H    t        | j                  j                               S r   )rA   r   valuesr^   s    r   r   zCaptionQuery.all  s     D((//122r   r   c                      | j                   |   S r   r   r   s     r   r   zCaptionQuery.__getitem__  s    ##A&&r   c                 ,    t        | j                        S r   )r~   r   r^   s    r   r   zCaptionQuery.__len__  s    4''((r   c                 H    t        | j                  j                               S r   )iterr   r   r^   s    r   __iter__zCaptionQuery.__iter__  s    D((//122r   c                     | j                    S r   r   r^   s    r   r   zCaptionQuery.__repr__  s    &&'(r   N)r   r   r   r   r   r	   r   r   r?   r   r   r   r   r   r   r   r   ra   r   r   r   r   x  s    8=g = I33	'	33 <=3T'] 3 >3'S ') )3)# )r   r   N)r   collections.abcr   r   typingr   r   r   r   pytuber	   r
   pytube.helpersr   r   r   ra   r   r   <module>r      s2    L - 2 2 " %l%( l%^0)7 0)r   