U
    (a*                     @   s   d dl Z d dlZd dlmZ d dl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 d dlmZmZmZ G dd	 d	eZG d
d deZG dd deZG dd deZdd ZG dd dZG dd deZG dd deZG dd deZdS )    N)	QueryDict)render_to_string)cached_property)import_string)	mark_safe)EMBED_VIDEO_BACKENDSEMBED_VIDEO_TIMEOUT!EMBED_VIDEO_YOUTUBE_DEFAULT_QUERYc                   @   s   e Zd ZdZdS )EmbedVideoExceptionz/ Parental class for all embed_video exceptions N__name__
__module____qualname____doc__ r   r   /home/adriano.carvalho/ftp/files/BrinquedotecaVirtual/brinquedotecavirtual/venv/lib/python3.8/site-packages/embed_video/backends.pyr
      s   r
   c                   @   s   e Zd ZdZdS )VideoDoesntExistExceptionz) Exception thrown if video doesn't exist Nr   r   r   r   r   r      s   r   c                   @   s   e Zd ZdZdS )UnknownBackendExceptionz6 Exception thrown if video backend is not recognized. Nr   r   r   r   r   r      s   r   c                   @   s   e Zd ZdZdS )UnknownIdExceptionzQ
    Exception thrown if backend is detected, but video ID cannot be parsed.
    Nr   r   r   r   r   r   %   s   r   c                 C   s0   t D ]"}t|}|| r||   S qtdS )a`  
    Detect the right backend for given URL.

    Goes over backends in ``settings.EMBED_VIDEO_BACKENDS``,
    calls :py:func:`~VideoBackend.is_valid` and returns backend instance.

    :param url: URL which is passed to `is_valid` methods of VideoBackends.
    :type url: str

    :return: Returns recognized VideoBackend
    :rtype: VideoBackend
    N)r   r   is_validr   )urlbackend_namebackendr   r   r   detect_backend-   s
    
r   c                   @   s   e Zd ZdZdZdZdZdZdZdZ	dZ
dZdd Zed	d
 Zedd Zedd Zedd Zedd Zedd Zejdd Zedd Zdd Zdd Zdd Zdd Zd d! Zd"d# ZdS )$VideoBackenda>  
    Base class used as parental class for backends.


    Backend variables:

    .. autosummary::

        url
        code
        thumbnail
        query
        info
        is_secure
        protocol
        template_name


    .. code-block:: python

        class MyBackend(VideoBackend):
            ...

    NTzembed_video/embed_code.html Fc                 C   s$   | j j| _|| _t| jdd| _dS )z
        First it tries to load data from cache and if it don't succeed, run
        :py:meth:`init` and then save it to cache.

        :type url: str
        TZmutableN)	__class__r   r   _urlr   default_queryqueryselfr   r   r   r   __init__   s    
zVideoBackend.__init__c                 C   s   |   S )z 
        Code of video.
        )get_coder"   r   r   r   code   s    zVideoBackend.codec                 C   s   |   S )z
        URL of video.
        )get_urlr%   r   r   r   r      s    zVideoBackend.urlc                 C   s   | j r| jrdS dS )z0
        Protocol used to generate URL.
        httpshttp)allow_https	is_securer%   r   r   r   protocol   s    zVideoBackend.protocolc                 C   s   |   S )z)
        URL of video thumbnail.
        )get_thumbnail_urlr%   r   r   r   	thumbnail   s    zVideoBackend.thumbnailc                 C   s   |   S )zV
        Additional information about video. Not implemented in all backends.
        )get_infor%   r   r   r   info   s    zVideoBackend.infoc                 C   s   | j S )zB
        String transformed to QueryDict appended to url.
        )_queryr%   r   r   r   r       s    zVideoBackend.queryc                 C   s    t |tr|n
t|dd| _dS )z.
        :type value: QueryDict | str
        Tr   N)
isinstancer   r1   )r"   valuer   r   r   r       s    c                 C   s   | j |rdS dS )z
        Class method to control if passed url is valid for current backend. By
        default it is done by :py:data:`re_detect` regex.

        :type url: str
        TF)	re_detectmatch)clsr   r   r   r   r      s    zVideoBackend.is_validc                 C   s    | j | j}|r|dS dS )zg
        Returns video code matched from given url by :py:data:`re_code`.

        :rtype: str
        r&   N)re_codesearchr   groupr"   r5   r   r   r   r$      s    zVideoBackend.get_codec                 C   s8   | j j| j| jd}|| jr*d| j  nd7 }t|S )zQ
        Returns URL folded from :py:data:`pattern_url` and parsed code.
        r&   r,   ?r   )pattern_urlformatr&   r,   r    	urlencoder   r!   r   r   r   r'      s    zVideoBackend.get_urlc                 C   s   | j j| j| jdS )
        Returns thumbnail URL folded from :py:data:`pattern_thumbnail_url` and
        parsed code.

        :rtype: str
        r;   )pattern_thumbnail_urlr>   r&   r,   r%   r   r   r   r-      s    zVideoBackend.get_thumbnail_urlc                 C   s   t | j| ||dS )z
        Returns embed code rendered from template :py:data:`template_name`.

        :type width: int | str
        :type height: int | str
        :rtype: str
        )r   widthheight)r   template_namer"   rB   rC   r   r   r   get_embed_code   s     
zVideoBackend.get_embed_codec                 C   s   t dS )z
        :rtype: dict
        N)NotImplementedErrorr%   r   r   r   r/     s    zVideoBackend.get_infoc                 C   s   |D ]}t | |||  qdS )z%
        :type options: dict
        N)setattr)r"   optionskeyr   r   r   set_options  s    zVideoBackend.set_options)r   r   r   r   r7   r4   r=   rA   r*   rD   r   r+   r#   propertyr&   r   r,   r.   r0   r    setterclassmethodr   r$   r'   r-   rF   r/   rK   r   r   r   r   r   C   s@   		








	
	r   c                       sf   e Zd ZdZedejZedejejB Z	dZ
dZeZdddd	gZd
Z fddZdd Z  ZS )YoutubeBackendz#
    Backend for YouTube URLs.
    z2^(http(s)?://)?(www\.|m\.)?youtu(\.?)be(\.com)?/.*a  youtu(\.?)be(\.com)?/  # match youtube's domains
            (\#/)? # for mobile urls
            (embed/)?  # match the embed url syntax
            (v/)?
            (watch\?v=)?  # match the youtube page url
            (ytscreeningroom\?v=)?
            (feeds/api/videos/)?
            (user\S*[^\w\-\s])?
            (?P<code>[\w\-]{11})[a-z0-9;:@?&%=+/\$_.-]*  # match and extract
        z){protocol}://www.youtube.com/embed/{code}z3{protocol}://img.youtube.com/vi/{code}/{resolution}zmaxresdefault.jpgzsddefault.jpgzhqdefault.jpgzmqdefault.jpgTc                    sf   t   }|sbt| j}t|j}d|kr<|d d }n&d|krR|d d }ntd| j|S )Nvr   Zvideo_idzCannot get ID from `{0}`)superr$   urlparser   parse_qsr    r   r>   )r"   r&   
parsed_urlZ	parsed_qsr   r   r   r$   @  s    
zYoutubeBackend.get_codec                 C   sB   | j D ]6}| jj| j| j|d}tt|jdk r|  S qdS )r@   )r&   r,   
resolutioni  N)	resolutionsrA   r>   r&   r,   intrequestsheadstatus_code)r"   rV   Ztemp_thumbnail_urlr   r   r   r-   P  s    
  
z YoutubeBackend.get_thumbnail_url)r   r   r   r   recompileIr4   Xr7   r=   rA   r	   r   rW   r+   r$   r-   __classcell__r   r   rU   r   rO     s"   

rO   c                   @   sH   e Zd ZdZedejZedejZdZ	dZ
dZdd Zd	d
 ZdS )VimeoBackendz!
    Backend for Vimeo URLs.
    z2^((http(s)?:)?//)?(www\.)?(player\.)?vimeo\.com/.*zOvimeo\.com/(video/)?(channels/(.*/)?)?((.+)/review/)?(manage/)?(?P<code>[0-9]+)z*{protocol}://player.vimeo.com/video/{code}z/{protocol}://vimeo.com/api/v2/video/{code}.jsonTc                 C   sP   z0t j| jj| j| jdtd}t|j	d W S  t
k
rJ   t Y nX d S )Nr;   )timeoutr   )rY   getpattern_infor>   r&   r,   r   jsonloadstext
ValueErrorr   )r"   responser   r   r   r/   t  s    zVimeoBackend.get_infoc                 C   s   | j dS )NZthumbnail_larger0   rc   r%   r   r   r   r-   ~  s    zVimeoBackend.get_thumbnail_urlN)r   r   r   r   r\   r]   r^   r4   r7   r=   rd   r+   r/   r-   r   r   r   r   ra   `  s   
ra   c                       s   e Zd ZdZdZedejZedejZ	edejZ
dZedd Zed	d
 Zdd Zdd Zdd Zdd Z fddZ  ZS )SoundCloudBackendz&
    Backend for SoundCloud URLs.
    z"{protocol}://soundcloud.com/oembedz-^(http(s)?://(www\.|m\.)?)?soundcloud\.com/.*z'src=".*%2F(?P<code>\d+)&show_artwork.*"zsrc="(?P<url>.*?)"Tc                 C   s   | j dS )
        :rtype: str
        rB   rj   r%   r   r   r   rB     s    zSoundCloudBackend.widthc                 C   s   | j dS )rl   rC   rj   r%   r   r   r   rC     s    zSoundCloudBackend.heightc                 C   sN   d| j d}tj| jj| jd|td}|jdkrBtd|jt	
|jS )Nre   )r>   r   )r,   )paramsrb      z&SoundCloud returned status code `{0}`.)r   rY   rc   base_urlr>   r,   r   r[   r   re   rf   rg   )r"   rm   rr   r   r   r/     s    

zSoundCloudBackend.get_infoc                 C   s   | j dS )NZthumbnail_urlrj   r%   r   r   r   r-     s    z#SoundCloudBackend.get_thumbnail_urlc                 C   s   | j | jd}|dS )Nhtmlr   )re_urlr8   r0   rc   r9   r:   r   r   r   r'     s    zSoundCloudBackend.get_urlc                 C   s   | j | jd}|dS )Nrq   r&   )r7   r8   r0   rc   r9   r:   r   r   r   r$     s    zSoundCloudBackend.get_codec                    s   t  j||dS )N)rB   rC   )rQ   rF   rE   rU   r   r   rF     s    z SoundCloudBackend.get_embed_code)r   r   r   r   ro   r\   r]   r^   r4   r7   rr   r+   r   rB   rC   r/   r-   r'   r$   rF   r`   r   r   rU   r   rk     s   

rk   )re   r\   urllib.parseparserR   rY   Zdjango.httpr   Zdjango.template.loaderr   Zdjango.utils.functionalr   Zdjango.utils.module_loadingr   Zdjango.utils.safestringr   Zembed_video.settingsr   r   r	   	Exceptionr
   r   r   r   r   r   rO   ra   rk   r   r   r   r   <module>   s&    XF"