o
    {#i,2                     @   sl   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 deZG dd	 d	eZd
S )zFThis module provides a query interface for media streams and captions.    )MappingSequence)CallableListOptionalUnion)CaptionStream)
deprecatedc                   @   s  e Zd ZdZdd Z																	d6ddZdee dd fd	d
Zde	dd fddZ
d7ddZd7ddZdedee fddZde	dee fddZdee fddZdee fddZd8de	dee fddZd9d!edd fd"d#Zdee fd$d%Zd&d' Zed(d:d)ee	 defd*d+Zed,dee fd-d.Zd/eeef fd0d1Zdefd2d3Zde	fd4d5Z dS );StreamQueryz3Interface for querying the available media streams.c                 C   s   || _ dd |D | _dS )zConstruct a :class:`StreamQuery <StreamQuery>`.

        param list fmt_streams:
            list of :class:`Stream <Stream>` instances.
        c                 S   s   i | ]}t |j|qS  )intitag.0sr   r   S/var/www/GraceOrthoHospital/venv_grace/lib/python3.10/site-packages/pytube/query.py
<dictcomp>   s    z(StreamQuery.__init__.<locals>.<dictcomp>N)fmt_streams
itag_index)selfr   r   r   r   __init__   s   zStreamQuery.__init__Nc                    s~  g }sr/t tst tr|fdd nt ts%t tr/|fdd r:|fdd rE|fdd 
rP|
fdd 	sTr^|	fdd  sbrl| fdd rw|fd	d r|fd
d |r|dd  |r|dd  |r|dd  |r|dd  |r|| dur|fdd | |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  pkS N
resolutionr   resr   r   r   <lambda>       z$StreamQuery.filter.<locals>.<lambda>c                    s   | j  pv S r   r   r   r   r   r   r      r    c                    
   | j  kS r   fpsr   r"   r   r   r         
 c                    r!   r   	mime_typer   r%   r   r   r      r$   c                    r!   r   typer   r'   r   r   r      r$   c                    s   | j p kS r   )subtyper   )file_extensionr)   r   r   r      r    c                    r   r   )abrr   )r+   bitrater   r   r      r    c                    r!   r   video_codecr   r-   r   r   r      r$   c                    r!   r   audio_codecr   r/   r   r   r      r$   c                 S      | j o| j S r   )includes_audio_trackincludes_video_trackr   r   r   r   r         c                 S   r1   r   )r3   r2   r   r   r   r   r      r4   c                 S      | j S r   )is_progressiver   r   r   r   r          c                 S   r5   r   )is_adaptiver   r   r   r   r      r7   Nc                    r!   r   is_dashr   r9   r   r   r      r$   )
isinstancestrappendlistextend_filter)r   r#   r   r   r&   r(   r)   r*   r+   r,   r.   r0   
only_audio
only_videoprogressiveadaptiver:   custom_filter_functionsfiltersr   )r+   r0   r,   r*   r#   r:   r&   r   r   r)   r(   r.   r   filter   sJ   g

zStreamQuery.filterrF   returnc                 C   s&   | j }|D ]}t||}qtt|S r   )r   rG   r   r>   )r   rF   r   filter_lambdar   r   r   r@      s   zStreamQuery._filterattribute_namec                    sr    fdd| j D }|r-tt|d  tr-ztt| fdddW S  ty,   Y nw tt| fdddS )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.
        c                    s   g | ]}t | d ur|qS r   getattrr   rJ   r   r   
<listcomp>   s
    z(StreamQuery.order_by.<locals>.<listcomp>r   c                    s   t dttjt|  S )N )r   joinrG   r<   isdigitrL   r   rM   r   r   r      s
    z&StreamQuery.order_by.<locals>.<lambda>)keyc                    s
   t |  S r   rK   r   rM   r   r   r      r$   )r   r;   rL   r<   r   sorted
ValueError)r   rJ   has_attributer   rM   r   order_by   s&   


zStreamQuery.order_byc                 C   s   t | jddd S )z_Sort streams in descending order.

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

        N)r   r   r   r   r   r   desc   s   zStreamQuery.descc                 C   s   | S )z^Sort streams in ascending order.

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

        r   rX   r   r   r   asc   s   zStreamQuery.ascr   c                 C   s   | 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   r   r   r   get_by_itag   s   zStreamQuery.get_by_itagr   c                 C   s   | j dd|d 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)rC   r)   r   )rG   first)r   r   r   r   r   get_by_resolution   s
   zStreamQuery.get_by_resolutionc                 C   s   | j dddd 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.

        Tr]   )rC   r)   r   )rG   rV   r^   rX   r   r   r   get_lowest_resolution  s   
z!StreamQuery.get_lowest_resolutionc                 C   s   | j ddd 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)rC   r   rG   rV   lastrX   r   r   r   get_highest_resolution  s   	z"StreamQuery.get_highest_resolutionr]   r)   c                 C   s   | j d|dd 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)rA   r)   r+   ra   )r   r)   r   r   r   get_audio_only%  s   zStreamQuery.get_audio_onlyFis_otfc                    s   |   fd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                    r!   r   re   r   rf   r   r   r   <  r$   z!StreamQuery.otf.<locals>.<lambda>)r@   )r   re   r   rf   r   otf5     zStreamQuery.otfc                 C   "   z| j d W S  ty   Y dS 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   
IndexErrorrX   r   r   r   r^   >  
   	zStreamQuery.firstc                 C   ri   )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.

        rW   Nrj   rX   r   r   r   rb   L  rl   zStreamQuery.lastz.Get the size of this list directly using len()valuec                 C   s   |r| j |S t| S )zAGet the count of items in the list.

        :rtype: int
        )r   countlen)r   rm   r   r   r   rn   Z  s   zStreamQuery.countz6This object can be treated as a list, all() is uselessc                 C   r5   zXGet all the results represented by this query as a list.

        :rtype: list

        r   rX   r   r   r   alle  s   zStreamQuery.allic                 C   
   | j | S r   rq   r   rs   r   r   r   __getitem__n     
zStreamQuery.__getitem__c                 C   
   t | jS r   )ro   r   rX   r   r   r   __len__q  rw   zStreamQuery.__len__c                 C      | j  S r   rq   rX   r   r   r   __repr__t     zStreamQuery.__repr__)NNNNNNNNNNNNNNNNN)rH   r   )r]   )Fr   )!__name__
__module____qualname____doc__r   rG   r   r   r@   r<   rV   rY   rZ   r   r   r	   r\   r_   r`   rc   rd   boolrg   r^   rb   r
   rn   rr   r   slicerv   ry   r{   r   r   r   r   r   	   sR    
  
#
	
r   c                   @   s   e Zd ZdZdee fddZeddede	e fdd	Z
ed
dee fddZdefddZdefddZdd ZdefddZdS )CaptionQueryz.Interface for querying the available captions.captionsc                 C   s   dd |D | _ dS )zConstruct a :class:`Caption <Caption>`.

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

        c                 S   s   i | ]}|j |qS r   )code)r   cr   r   r   r     s    z)CaptionQuery.__init__.<locals>.<dictcomp>Nlang_code_index)r   r   r   r   r   r   {  rh   zCaptionQuery.__init__z?This object can be treated as a dictionary, i.e. captions['en']	lang_coderH   c                 C   s   | 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   r[   )r   r   r   r   r   get_by_language_code  s   z!CaptionQuery.get_by_language_codez*This object can be treated as a dictionaryc                 C      t | j S rp   )r>   r   valuesrX   r   r   r   rr     s   zCaptionQuery.allrs   c                 C   rt   r   r   ru   r   r   r   rv     rw   zCaptionQuery.__getitem__c                 C   rx   r   )ro   r   rX   r   r   r   ry     rw   zCaptionQuery.__len__c                 C   r   r   )iterr   r   rX   r   r   r   __iter__  r4   zCaptionQuery.__iter__c                 C   rz   r   r   rX   r   r   r   r{     r|   zCaptionQuery.__repr__N)r}   r~   r   r   r   r   r   r
   r<   r   r   rr   rv   r   ry   r   r{   r   r   r   r   r   x  s"    	r   N)r   collections.abcr   r   typingr   r   r   r   pytuber   r	   pytube.helpersr
   r   r   r   r   r   r   <module>   s      q