a
    h]                     @   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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extract_attributesint_or_nonetraverse_objtry_getunescapeHTMLunsmuggle_url
update_urlurl_or_none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   Q/mnt/pikpak/tmp/myenv/lib/python3.9/site-packages/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 nb ty } zJt|jt	r|jj
dkrt| |jj  |d dd W Y d }~n
d }~0 0 | |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_token3   sF    
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   r:   r0   jsondumpsencoder   )r   Zobject_typer7   Zobject_fieldsnoteZfilter_extrarespobjr   r   r   	_call_apiS   s    
z&DailymotionBaseInfoExtractor._call_api)N)__name__
__module____qualname__r'   rD   Z_NETRC_MACHINEr   staticmethodr   r    r(   r:   rK   r   r   r   r   r      s   
 r   c                       s   e Zd ZdZdZdedd  dgZddd	d
dddddddddeeddddd	ddddddddddeeddd d!d"d	d#d$d%dd&d'd(d)ded*d+d,d-d.d/d	d0d1d2d3d+d,d4d5d6d7d5d6d8d5d6d9d5d6d:d5d6d;d5d6d<d5d6d=d5d6d>d5d6d?d5d6d@d5d6dAd5d6dBd5d6dCd5d6gZdDdEd	dFdGdHdIdJdKdLdg deedMddNdOdPidQdRdSdTd	dUedVdWedXdYdZdd[d\d]d^dd_d`d	edadbeddcddddedfdgg dhdidjdkdld	dmdndodpeeddqg ddrdsdtdidjdudvd	dwddxdyedddzd{d|d}ed~dgZdZ	dZ
e fddZdd Z  ZS )DailymotionIEa  (?ix)
                    (?:https?:)?//
                    (?:
                        dai\.ly/|
                        (?:
                            (?:(?:www|touch|geo)\.)?dailymotion\.[a-z]{2,3}|
                            (?:www\.)?lequipe\.fr
                        )/
                        (?:
                            swf/(?!video)|
                            (?:(?:crawler|embed|swf)/)?video/|
                            player(?:/[\da-z]+)?\.html\?(?:video|(?P<is_playlist>playlist))=
                        )
                    )
                    (?P<id>[^/?_&#]+)(?:[\w-]*\?playlist=(?P<playlist_id>x[0-9a-z]+))?
    r   zl(?ix)<(?:(?:embed|iframe)[^>]+?src=|input[^>]+id=[\'"]dmcloudUrlEmissionSelect[\'"][^>]+value=)["\'](?P<url>   N)zphttp://www.dailymotion.com/video/x5kesuj_office-christmas-party-review-jason-bateman-olivia-munn-t-j-miller_newsZx5kesujmp4uJ   Office Christmas Party Review –  Jason Bateman, Olivia Munn, T.J. MillerzGOffice Christmas Party Review - Jason Bateman, Olivia Munn, T.J. Miller   zcount:5z"re:https?://s[12]\.dmcdn\.net/v/.+iUOYZ20170501ZDeadlineZx1xm8rir   )idexttitledescriptiondurationtags	thumbnail	timestampupload_dateuploaderuploader_idr$   
view_count
like_count)url	info_dictz?https://geo.dailymotion.com/player.html?video=x89eyek&mute=trueZx89eyeku    En quête d'esprit du 27/03/2022z$md5:66542b9f4df2eb23f314fc097488e553i
  zcount:1iV@bZ20220327ZCNEWSZx24vthzphttps://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.J   iTZ20150306ZIGNZxijv66)rU   rV   rW   rX   rY   r[   r\   r]   r^   r_   r$   r`   z
video gone)rb   md5rc   skipznhttp://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#   )rU   rV   rW   r^   r$   z'http://www.dailymotion.com/video/xhza0oT)rb   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-videoz=https://geo.dailymotion.com/player/xf7zn.html?playlist=x7wdsjz;https://geo.dailymotion.com/player/xmyye.html?video=x93blhiz1https://www.dailymotion.com/crawler/video/x8u4owgz/https://www.dailymotion.com/embed/video/x8u4owgzhttps://dai.ly/x94cnnkzhttps://www.financialounge.com/video/2024/08/01/borse-europee-in-rosso-dopo-la-fed-a-milano-volano-mediobanca-e-tim-edizione-del-1-agosto/Zx93blhizOnAir - 01/08/24r?      iYfZ20240801ZFinancialoungeZx2vtgmm)rU   rV   rW   rX   rY   r\   r]   r^   r_   r$   rZ   r[   r`   ra   zNhttps://www.cycleworld.com/blogs/ask-kevin/ducati-continues-to-evolve-with-v4/rU   Zx7wdsj2   rb   rc   playlist_mincountzhttps://www.leparisien.fr/environnement/video-le-veloto-la-voiture-a-pedales-qui-aimerait-se-faire-une-place-sur-les-routes-09-03-2024-KCYMCPM4WFHJXMSKBUI66UNFPU.phpZx8u4owguH   À bord du « véloto », l’alternative à la voiture pour la campagnezLe ParisienZ20240309zcount:7z're:https?://www\.leparisien\.fr/.+\.jpgi*eZx32f7bua   VIDÉO. Le « véloto », la voiture à pédales qui aimerait se faire une place sur les routesg     z@)rU   rV   rX   ra   r^   r]   r`   rZ   r[   r\   r$   r_   rW   rY   zNhttps://www.metatube.com/en/videos/546765/This-frogs-decorates-Christmas-tree/Zx8vu47wZMetatubeZ20240326i^fZx2xpy74u&   Está lindas ranitas ponen su arbolito   zQue lindura)rU   rV   ra   r^   r]   r`   r[   r\   r$   r_   rW   rY   rX   rZ   zInvalid URL)rb   rc   rf   zbhttps://lcp.fr/programmes/avant-la-catastrophe-la-naissance-de-la-dictature-nazie-1933-1936-346819Zk2Y4Mjp7krAF9iCuINMzBAvant la catastrophe la naissance de la dictature nazie 1933 -1936z$md5:7b620d5e26edbe45f27bbddc1c0257c1u   LCP Assemblée nationaleZxbz33di  iKgZ20250218)rU   rV   rW   rX   r^   r_   r`   ra   r$   rY   rZ   r[   r\   r]   zMhttps://forum.ionicframework.com/t/ionic-2-jw-player-dailymotion-player/83248Zxwr14qz2Macklemore & Ryan Lewis - Thrift Shop (feat. Wanz)z$md5:47fbe168b5a6ddc4a205e20dd6c841b2   i%PZ20130114zMacklemore OfficialZx19qlwr)rU   rV   rW   r$   rX   rY   ra   rZ   r[   r\   r]   r^   r_   r`   FzIdescription
      geoblockedCountries {
        allowed
      }
      xidc           	      #   s   t  ||E d H  td|D ]}d|d V  q td|D ]}t|d}t|d}|shqB|dd}|	d	rd
| }|d }rd| }n|d }rBd| }nqBt
||dV  qBd S )NzS(?s)DM\.player\([^,]+,\s*{.*?video[\'"]?\s*:\s*["\']?(?P<id>[0-9a-zA-Z]+).+?}\s*\);z(https://www.dailymotion.com/embed/video/rU   z`(?s)<script [^>]*\bsrc=(["\'])(?:https?:)?//[\w-]+\.dailymotion\.com/player/(?:(?!\1).)+\1[^>]*>r   srcz.jsz.htmlz//zhttps:z
data-videozvideo=zdata-playlistz	playlist=r>   )super_extract_embed_urlsrefinditergroupr
   r   r   replace
startswithr   )	clsrb   webpagemobjattrsZ
player_urlvideo_idZquery_stringplaylist_id	__class__r   r   rp   V  s*    

z!DailymotionIE._extract_embed_urlsc           !         s  t |\}}| |ddd\}}}|r2|}d }| ||rR| d| d|S | d}| d|d| j| jf d	|rd
| dnd }|d }| j	d| |dt
|dpddid}	|	d}
|
r"|
dp|
d }|
ddkr
t|dd t}| j||d t| j d| dd|	d }|d}g }i }|	d  D ]\}}|D ]}|d}|d }|rT|d!krqT|d"kr| j||d#|d$d%d&\}}|| | j||d' nP|d(| d)}td*|}|rtt| \}}}||||d+ || qTqH|D ]@}|d d,d- |d< |d.s|d/ d0rd1|d.< qt|	d2d tppi }| D ]$\}}d3d4 |d5g D ||< qzt
|	d6tjhd7d d-thfd-thfd8d9f}|	d:pi }|d;pi   fd<d} ||t |d=|t|	d>pd t|	d?|d@|dpF|	d@|	dArVdBnd-|	dC| dDpvt|dE| dF|||dGS )HNrU   is_playlistr{   z$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: "{}"r7   z2https://www.dailymotion.com/player/metadata/video/zDownloading metadata JSONr>   Zappzcom.dailymotion.neonrn   errorrW   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	qualitiesrb   typez#application/vnd.lumberjack.manifestzapplication/x-mpegURLrS   ZhlsF)ZliveZm3u8_idfatal)targetzhttp-)rb   	format_idz/H264-(\d+)x(\d+)(?:-(60)/)?)fpsheightwidth#r   r   r   z@60<   c                 S   s   | d d S )N	subtitlesr,   r   r   r   r   r   r     r   c                 S   s   g | ]}d |iqS )rb   r   ).0Zsubtitle_urlr   r   r   
<listcomp>  s   z/DailymotionIE._real_extract.<locals>.<listcomp>urls)Zposters
thumbnailsc                 S   s   t |d S )Nr   )r   )_vr   r   r   r     r   r   )r   rU   rb   ownerstatsc                    s   t t fddS )Nc                    s   |  d  d S )Nstotalr   )yr   r   r   r     r   z?DailymotionIE._real_extract.<locals>.<lambda>.<locals>.<lambda>)r   r   r   r   r   r   r     r   rX   rY   Zcreated_timeZ
screennameexplicitr#   rZ   viewZaudienceCountlike)rU   rW   rX   r   rY   r\   r^   r_   r$   rZ   r`   ra   formatsr   is_live)!r   Z_match_valid_urlrs   Z_yes_playlist
url_resultr&   rK   _COMMON_MEDIA_FIELDSformatr0   r   r   r   listZraise_geo_restrictedr   IE_NAMEitemsZ#_extract_m3u8_formats_and_subtitlesextendZ_merge_subtitlesrq   searchmapr   groupsr/   appendsplitendswithdictstrr	   )!r   rb   Zsmuggled_datarz   r~   r{   r)   r   r7   metadatar   rW   Zallowed_countriesr   r   r   ZqualityZ
media_listmZ	media_url
media_typefmtsubsfr   r   r   Zsubtitles_dataZsubtitle_langZsubtitler   r   Z	get_countr   r   r   _real_extractn  s    









zDailymotionIE._real_extract)rL   rM   rN   
_VALID_URLr   Z_EMBED_REGEXint_TESTSZ_WEBPAGE_TESTSZ_GEO_BYPASSr   classmethodrp   r   __classcell__r   r   r|   r   rP   e   s  q	hrP   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noderb   r7   )rK   _OBJECT_TYPEr'   
_PAGE_SIZEr   rP   Zie_key)r   r{   pager   Zedger   r   r   r   _fetch_page  s    
z%DailymotionPlaylistBaseIE._fetch_pagec                 C   s,   |  |}tt| j|| j}| ||S r   )	_match_idr   	functoolspartialr   r   playlist_result)r   rb   r{   entriesr   r   r   r     s    
z'DailymotionPlaylistBaseIE._real_extractN)rL   rM   rN   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=xl8v3qrU   Zxv4bw   ri   Z
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/([^/]+)/rb   z//dailymotion.com/playlist/)rq   rr   findallr   rs   )rv   rb   rw   rx   pr   r   r   rp     s    z)DailymotionPlaylistIE._extract_embed_urlsN)	rL   rM   rN   r   r   r   r   r   rp   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)rU   rW   r   ri   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 )Nr;   r<   r=   ZSEARCH_QUERYr   )limitr   r>   )ZoperationNamer>   	variablesr@   r,   r   rB   r   rC   zCould not fetch search data)rD   r   r:   r0   rE   rF   _SEARCH_QUERYrG   r   r   r   r   )r   termr   rH   rI   rJ   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   r7   z"https://www.dailymotion.com/video/)r   r   r   rP   )r   r   r   r4   r7   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   rb   r   r   r   r   r   A  s    z!DailymotionSearchIE._real_extractN)rL   rM   rN   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:userzhttps?://(?:www\.)?dailymotion\.[a-z]{2,3}/(?!(?:embed|swf|#|video|playlist|search|crawler)/)(?:(?:old/)?user/)?(?P<id>[^/?#]+)z%https://www.dailymotion.com/user/nqtvrU   Znqtv   ri   z,http://www.dailymotion.com/user/UnderProjectZUnderProjecti  zTakes too long time)rb   rc   rj   rf   r$   r   )rb   rc   rj   paramsZchannelN)rL   rM   rN   r   r   r   r   r   r   r   r   r   G  s,   r   )r   rE   rq   urllib.parser   commonr   Znetworking.exceptionsr   utilsr   r   r   r	   r
   r   r   r   r   r   r   r   r   r   rP   r   r   r   r   r   r   r   r   <module>   s   <L   .