U
    g1H                     @   s   d dl Z d dlZd dlZd dlZddlmZ ddlmZ ddl	m
Z
mZmZmZmZmZmZ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G dd deZG dd deZdS )    N   )InfoExtractor   )	HTTPError)
ExtractorErrorOnDemandPagedListage_restricted
clean_htmlint_or_nonetraverse_objtry_getunescapeHTMLunsmuggle_urlurlencode_postdatac                   @   sT   e Zd ZdZdddZdZdd Zedd	 Zd
d Z	dd Z
dd ZdddZdS )DailymotionBaseInfoExtractorNzapplication/jsonzhttps://www.dailymotion.com)zContent-TypeZOrigindailymotionc                 C   s
   |  dS )Nzhttps://www.dailymotion.com/)Z_get_cookies)self r   @/tmp/pip-unpacked-wheel-q5ljy6pj/yt_dlp/extractor/dailymotion.py_get_dailymotion_cookies   s    z5DailymotionBaseInfoExtractor._get_dailymotion_cookiesc                 C   s   |  |}|r|jS d S N)getvalue)cookiesnamecookier   r   r   _get_cookie_value!   s    
z.DailymotionBaseInfoExtractor._get_cookie_valuec                 C   s   |  d|| d S )Nzwww.dailymotion.com)Z_set_cookie)r   r   r   r   r   r   _set_dailymotion_cookie'   s    z4DailymotionBaseInfoExtractor._set_dailymotion_cookiec                 C   sL   |   }| |d}|r |dkntd| d| _| d| jrBdnd d S )Nffon   	age_limitoff)r   r   r   	get_param_FAMILY_FILTERr   )r   r   r   r   r   r   _real_initialize*   s    z-DailymotionBaseInfoExtractor._real_initializec              
   C   s   |   }| |dp| |d}|r(|S ddd}|  \}}|rV|d||d nd|d	< z| jd
d dt|dd }W n` tk
r } zBt|jt	r|jj
dkrt| |jj  |d dd W 5 d }~X Y nX | |rdnd| |S )NZaccess_tokenZclient_tokenZf1a362d288c1b98099c7Z(eea605b96e01c796ff369935357eca920c5da4c5)Z	client_idZclient_secretpassword)
grant_typer&   usernameZclient_credentialsr'   z/https://graphql.api.dailymotion.com/oauth/tokenzDownloading Access Token)datai  Zerror_descriptionTexpected)r   r   Z_get_login_infoupdate_download_jsonr   r   
isinstancecauser   statusZ_parse_jsonresponsereaddecoder   )r   xidr   tokenr)   r(   r&   er   r   r   
_get_token0   sJ    
  z'DailymotionBaseInfoExtractor._get_tokenc                 C   s   | j ds"d| | | j d< | jd||tdd|||rDd| nd|f i | j d}|d	 | }|st|d
 d d dd|S )NAuthorizationBearer $https://graphql.api.dailymotion.com/queryz"{
  %s(xid: "%s"%s) {
    %s
  }
}z,  r)   headersr)   errorsr   messageTr*   )_HEADERSr   r7   r-   jsondumpsencoder   )r   Zobject_typer4   Zobject_fieldsnoteZfilter_extrarespobjr   r   r   	_call_apiP   s$        z&DailymotionBaseInfoExtractor._call_api)N)__name__
__module____qualname__r$   rA   Z_NETRC_MACHINEr   staticmethodr   r   r%   r7   rH   r   r   r   r   r      s   
 r   c                       s@  e Zd ZdZdZdgZdddddd	d
dddddeedddddgddddddddddddd d!deed"gd#ddd$d%d&dd'd(d)d*d+d,d-d.ded/d0d1d2d3d4dd5d6d7d8d9id:d;d<d=d>dd?d@dAdBd0d1dCd9dDdEd9dDdFd9dDdGd9dDdHd9dDdId9dDdJd9dDdKd9dDdLd9dDgZdMZdNZ	e
 fdOdPZdQdR Z  ZS )SDailymotionIEa  (?ix)
                    https?://
                        (?:
                            (?:(?:www|touch|geo)\.)?dailymotion\.[a-z]{2,3}/(?:(?:(?:(?:embed|swf|\#)/)|player(?:/\w+)?\.html\?)?video|swf)|
                            (?:www\.)?lequipe\.fr/video
                        )
                        [/=](?P<id>[^/?_&]+)(?:.+?\bplaylist=(?P<playlist_id>x[0-9a-z]+))?
                    r   z<(?:(?:embed|iframe)[^>]+?src=|input[^>]+id=[\'"]dmcloudUrlEmissionSelect[\'"][^>]+value=)(["\'])(?P<url>(?:https?:)?//(?:www\.)?dailymotion\.com/(?:embed|swf)/video/.+?)\1zphttp://www.dailymotion.com/video/x5kesuj_office-christmas-party-review-jason-bateman-olivia-munn-t-j-miller_newsZ 074b95bdee76b9e3654137aee9c79dfeZx5kesujmp4uJ   Office Christmas Party Review –  Jason Bateman, Olivia Munn, T.J. MillerzGOffice Christmas Party Review - Jason Bateman, Olivia Munn, T.J. Miller   iUOYZ20170501ZDeadlineZx1xm8rir   Z	hollywoodZcelebZ	celebrityZmoviesz
red carpetz:re:https://(?:s[12]\.)dmcdn\.net/v/K456B1aXqIx58LKWQ/x1080)idexttitledescriptionduration	timestampupload_dateuploaderuploader_idr!   
view_count
like_counttags	thumbnail)urlmd5	info_dictz?https://geo.dailymotion.com/player.html?video=x89eyek&mute=trueZ e2f9717c6604773f963f069ca53a07f8Zx89eyeku    En quête d'esprit du 27/03/2022z$md5:66542b9f4df2eb23f314fc097488e553i
  iV@bZ20220327ZCNEWSZx24vthZen_quete_d_espritz:re:https://(?:s[12]\.)dmcdn\.net/v/Tncwi1YNg_RUl7ueu/x1080zphttps://www.dailymotion.com/video/x2iuewm_steam-machine-models-pricing-listed-on-steam-store-ign-news_videogamesZ 2137c41a8e78554bb09225b8eb322406Zx2iuewmz>Steam Machine Models, Pricing Listed on Steam Store - IGN Newsz/Several come bundled with the Steam Controller.zre:^https?:.*\.(?:jpg|png)$J   iTZ20150306ZIGNZxijv66)rP   rQ   rR   rS   r\   rT   rU   rV   rW   rX   r!   rY   z
video gone)r]   r^   r_   skipzFhttp://www.dailymotion.com/video/x149uew_katy-perry-roar-official_musizRoar (Official)ZUSUV71301934z
Katy PerryZ20130905)rR   rP   rQ   rW   rV   Zskip_downloadTz(VEVO is only available in some countries)r]   r_   paramsra   znhttp://www.dailymotion.com/video/xyh2zz_leanna-decker-cyber-girl-of-the-year-desires-nude-playboy-plus_redbandZ 0d667a7b9cebecc3c89ee93099c4159dZxyh2zzzBLeanna Decker - Cyber Girl Of The Year Desires Nude [Playboy Plus]ZHotWaves1012r    )rP   rQ   rR   rW   r!   z'http://www.dailymotion.com/video/xhza0o)r]   Zonly_matchingzqhttp://www.dailymotion.com/video/x20su5f_the-power-of-nightmares-1-the-rise-of-the-politics-of-fear-bbc-2004_newsz,http://www.dailymotion.com/swf/video/x3n92nfzLhttp://www.dailymotion.com/swf/x3ss1m_funny-magic-trick-barry-and-stuart_funz$https://www.lequipe.fr/video/x791memz0https://www.lequipe.fr/video/k7MtHciueyTcrFtFKA2z7https://www.dailymotion.com/video/x3z49k?playlist=xv4bwzGhttps://geo.dailymotion.com/player/x86gw.html?video=k46oCapRs4iikoz9DWyzhttps://geo.dailymotion.com/player/xakln.html?video=x8mjju4&customConfig%5BcustomParams%5D=%2Ffr-fr%2Ftennis%2Fwimbledon-mens-singles%2Farticles-videoFzIdescription
      geoblockedCountries {
        allowed
      }
      xidc                 #   s>   t  ||E d H  td|D ]}d|d E d H  q d S )NzS(?s)DM\.player\([^,]+,\s*{.*?video[\'"]?\s*:\s*["\']?(?P<id>[0-9a-zA-Z]+).+?}\s*\);z(https://www.dailymotion.com/embed/video/rP   )super_extract_embed_urlsrefinditergroup)clsr]   webpagemobj	__class__r   r   rd      s     z!DailymotionIE._extract_embed_urlsc                    sf  t |\}}| | \}}|r@| ||r@| d| d|S | d}| d|d| j| jf d|rvd| dnd }|d }| j	d	| |d
t
|dpddid}|d}	|	r|	dp|	d }
|	ddkrt|dd t}| j|
|d t| j d|
 dd|d }
|d}g }|d  D ]\}}|D ]}|d}|d}|r<|dkrhq<|dkr|| j||d |d!d"d# nP|d$| d%}td&|}|rtt| \}}}||||d' || q<q0|D ]@}|d d(d) |d< |d*s|d+ d,rd-|d*< qi }t|d.d tpFi }| D ]$\}}d/d0 |d1g D ||< qPg }|d2i  D ] \}}|t|||d3 q|d4pi }|d5pi   fd6d}||
t|d7|t|d8pd t|d9|d:|d;p$|d:|d<r4d=nd)|d>|d?pTt|d@|dA|||dBS )CNz$http://www.dailymotion.com/playlist/ZDailymotionPlaylistZvideopasswordmediaz... on Video {
      %s
      stats {
        likes {
          total
        }
        views {
          total
        }
      }
    }
    ... on Live {
      %s
      audienceCount
      isOnAir
    }zDownloading media JSON metadatazpassword: "{}"r4   z2https://www.dailymotion.com/player/metadata/video/zDownloading metadata JSONr;   Zappzcom.dailymotion.neon)r;   errorrR   Zraw_messagecodeZDM007c                 S   s   | d d S )NZgeoblockedCountriesallowedr   xr   r   r   <lambda>      z-DailymotionIE._real_extract.<locals>.<lambda>)msgZ	countriesz said: Tr*   ZisOnAirZ	qualitiesr]   typez#application/vnd.lumberjack.manifestzapplication/x-mpegURLrN   ZhlsF)ZliveZm3u8_idfatalzhttp-)r]   	format_idz/H264-(\d+)x(\d+)(?:-(60)/)?)fpsheightwidth#r   ry   rx   z@60<   c                 S   s   | d d S )N	subtitlesr)   r   rq   r   r   r   rs   :  rt   c                 S   s   g | ]}d |iqS )r]   r   ).0Zsubtitle_urlr   r   r   
<listcomp><  s    z/DailymotionIE._real_extract.<locals>.<listcomp>urlsZposters)rz   rP   r]   ownerstatsc                    s   t t fddS )Nc                    s   |  d  d S )Nstotalr   )yrq   r   r   rs   J  rt   z?DailymotionIE._real_extract.<locals>.<lambda>.<locals>.<lambda>)r
   r   rq   r   rq   r   rs   J  rt   rS   rT   Zcreated_timeZ
screennamerP   explicitr    r[   viewZaudienceCountlike)rP   rR   rS   
thumbnailsrT   rU   rW   rX   r!   r[   rY   rZ   formatsr~   is_live)r   Z_match_valid_urlgroupsZ_yes_playlist
url_resultr#   rH   _COMMON_MEDIA_FIELDSformatr-   r   r   r   listZraise_geo_restrictedr   IE_NAMEitemsextendZ_extract_m3u8_formatsre   searchmapr
   r,   appendsplitendswithdictr	   )r   r]   Zsmuggled_dataZvideo_idplaylist_idr&   rm   r4   metadatarn   rR   Zallowed_countriesr   r   ZqualityZ
media_listmZ	media_url
media_typefr{   rz   ry   r~   Zsubtitles_dataZsubtitle_langZsubtitler   Z
poster_urlr   Z	get_countr   r   r   _real_extract   s     
  
 
 



     

zDailymotionIE._real_extract)rI   rJ   rK   
_VALID_URLr   Z_EMBED_REGEXint_TESTSZ_GEO_BYPASSr   classmethodrd   r   __classcell__r   r   rk   r   rM   b   s    prM   c                   @   s    e Zd ZdZdd Zdd ZdS )DailymotionPlaylistBaseIEd   c                 c   sp   |d7 }|  | j|d| jrdnd| j|f d| d }|d D ](}|d }| |d	 t |d
 V  qBd S )Nr   zvideos(allowExplicit: %s, first: %d, page: %d) {
      edges {
        node {
          xid
          url
        }
      }
    }falsetruezDownloading page videosedgesnoder]   r4   )rH   _OBJECT_TYPEr$   
_PAGE_SIZEr   rM   Zie_key)r   r   pager   Zedger   r   r   r   _fetch_pageb  s$     
  z%DailymotionPlaylistBaseIE._fetch_pagec                 C   s,   |  |}tt| j|| j}| ||S r   )	_match_idr   	functoolspartialr   r   playlist_result)r   r]   r   entriesr   r   r   r   t  s    
  z'DailymotionPlaylistBaseIE._real_extractN)rI   rJ   rK   r   r   r   r   r   r   r   r   _  s   r   c                   @   s6   e Zd ZdZdZdddiddgZdZed	d
 ZdS )DailymotionPlaylistIEzdailymotion:playlistzK(?:https?://)?(?:www\.)?dailymotion\.[a-z]{2,3}/playlist/(?P<id>x[0-9a-z]+)zChttp://www.dailymotion.com/playlist/xv4bw_nqtv_sport/1#video=xl8v3qrP   Zxv4bw   r]   r_   playlist_mincountZ
collectionc                 c   s>   t d|D ],}t dt|dD ]}d| V  q&qd S )Nzf<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:www\.)?dailymotion\.[a-z]{2,3}/widget/jukebox\?.+?)\1zlist\[\]=/playlist/([^/]+)/r]   z//dailymotion.com/playlist/)re   rf   findallr   rg   )rh   r]   ri   rj   pr   r   r   rd     s    z)DailymotionPlaylistIE._extract_embed_urlsN)	rI   rJ   rK   r   r   r   r   r   rd   r   r   r   r   r   |  s    r   c                   @   sH   e Zd ZdZdZdZddddddgZd	Zd
d Zdd Z	dd Z
dS )DailymotionSearchIEzdailymotion:searchzHhttps?://(?:www\.)?dailymotion\.[a-z]{2,3}/search/(?P<id>[^/?#]+)/videosr   z8http://www.dailymotion.com/search/king of turtles/videoszking of turtles)rP   rR   Z   r   zquery SEARCH_QUERY( $query: String! $page: Int $limit: Int ) { search { videos( query: $query first: $limit page: $page ) { edges { node { xid } } } } } c                 C   s   | j ds"d| | | j d< | jdd |td| jd||dd | j d}t|d	d
t	hf}|st
t|dddthfpd|S )Nr8   r9   r:   ZSEARCH_QUERYr   )limitr   r;   )ZoperationNamer;   	variablesr=   r)   r   r?   r   r@   zCould not fetch search data)rA   r   r7   r-   rB   rC   _SEARCH_QUERYrD   r   r   r   str)r   termr   rE   rF   rG   r   r   r   _call_search_api  s,       

z$DailymotionSearchIE._call_search_apic                 c   sL   |d7 }|  ||d| d| }t|dD ]}| d| t|V  q,d S )Nr   zSearching "z" page )r   r   .r   r4   z"https://www.dailymotion.com/video/)r   r   r   rM   )r   r   r   r1   r4   r   r   r   r     s    zDailymotionSearchIE._fetch_pagec                 C   s2   t j| |}| tt| j|| j	||S r   )
urllibparseunquote_plusr   r   r   r   r   r   r   )r   r]   r   r   r   r   r     s      z!DailymotionSearchIE._real_extractN)rI   rJ   rK   r   r   r   r   r   r   r   r   r   r   r   r   r     s   r   c                   @   sN   e Zd ZdZdZdddiddddd	id
dddddidddidgZdZdS )DailymotionUserIEzdailymotion:userzwhttps?://(?:www\.)?dailymotion\.[a-z]{2,3}/(?!(?:embed|swf|#|video|playlist|search)/)(?:(?:old/)?user/)?(?P<id>[^/?#]+)z%https://www.dailymotion.com/user/nqtvrP   Znqtv   r   z,http://www.dailymotion.com/user/UnderProjectZUnderProjecti  zTakes too long time)r]   r_   r   ra      r!   r   )r]   r_   r   rb   ZchannelN)rI   rJ   rK   r   r   r   r   r   r   r   r   r     s4       r   )r   rB   re   urllib.parser   commonr   Znetworking.exceptionsr   utilsr   r   r   r	   r
   r   r   r   r   r   r   rM   r   r   r   r   r   r   r   r   <module>   s   0L ~.