o
    i4f                     @   s   d dl Z 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-TypeOrigindailymotionc                 C   s
   |  dS )Nzhttps://www.dailymotion.com/)_get_cookies)self r   P/home/ubuntu/.local/lib/python3.10/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)_set_cookie)r   r   r   r   r   r   _set_dailymotion_cookie+   s   z4DailymotionBaseInfoExtractor._set_dailymotion_cookiec                 C   sR   |   }| |d}|r|dkntd| d| _| d| jr$d d S 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}|  \}}|r+|d||d nd|d	< z| jd
d dt|dd }W n+ tyi } zt|jt	rd|jj
dkrdt| |jj  |d dd d }~ww | |rpdnd| |S )Naccess_tokenclient_tokenf1a362d288c1b98099c7(eea605b96e01c796ff369935357eca920c5da4c5)	client_idclient_secretpassword)
grant_typer2   usernameclient_credentialsr3   z/https://graphql.api.dailymotion.com/oauth/tokenzDownloading Access Token)datai  error_descriptionTexpected)r   r!   _get_login_infoupdate_download_jsonr   r   
isinstancecauser   status_parse_jsonresponsereaddecoder#   )r   xidr   tokenr6   r4   r2   er   r   r   
_get_token4   sJ   
z'DailymotionBaseInfoExtractor._get_tokenc                 C   s   | j dsd| | | j d< | jd||tdd|||r"d| nd|f i | j d}|d	 | }|sCt|d
 d d dd|S )NAuthorizationBearer $https://graphql.api.dailymotion.com/queryz"{
  %s(xid: "%s"%s) {
    %s
  }
}z,  r6   headersr6   errorsr   messageTr8   )_HEADERSr   rG   r<   jsondumpsencoder   )r   object_typerD   object_fieldsnotefilter_extrarespobjr   r   r   	_call_apiT   s   
z&DailymotionBaseInfoExtractor._call_apir   )__name__
__module____qualname__r*   rQ   _NETRC_MACHINEr   staticmethodr!   r#   r+   rG   r[   r   r   r   r   r      s    
 r   c                       s6  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edd Zddd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_news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Y20170501Deadline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=truex89eyeku    En quête d'esprit du 27/03/2022z$md5:66542b9f4df2eb23f314fc097488e553i
  zcount:1iV@b20220327CNEWSx24vthzphttps://www.dailymotion.com/video/x2iuewm_steam-machine-models-pricing-listed-on-steam-store-ign-news_videogames 2137c41a8e78554bb09225b8eb322406x2iuewmz>Steam Machine Models, Pricing Listed on Steam Store - IGN Newsz/Several come bundled with the Steam Controller.J   iT20150306IGNxijv66)rj   rk   rl   rm   rn   rp   rq   rr   rs   rt   r'   ru   z
video gone)rw   md5rx   skipznhttp://www.dailymotion.com/video/xyh2zz_leanna-decker-cyber-girl-of-the-year-desires-nude-playboy-plus_redband 0d667a7b9cebecc3c89ee93099c4159dxyh2zzzBLeanna Decker - Cyber Girl Of The Year Desires Nude [Playboy Plus]HotWaves1012r&   )rj   rk   rl   rs   r'   z'http://www.dailymotion.com/video/xhza0oT)rw   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/x93blhizOnAir - 01/08/24rL      iYf20240801Financialoungex2vtgmm)rj   rk   rl   rm   rn   rq   rr   rs   rt   r'   ro   rp   ru   rv   zNhttps://www.cycleworld.com/blogs/ask-kevin/ducati-continues-to-evolve-with-v4/rj   x7wdsj2   rw   rx   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.phpx8u4owguH   À bord du « véloto », l’alternative à la voiture pour la campagnezLe Parisien20240309zcount:7z're:https?://www\.leparisien\.fr/.+\.jpgi*ex32f7bua   VIDÉO. Le « véloto », la voiture à pédales qui aimerait se faire une place sur les routesg     z@)rj   rk   rm   rv   rs   rr   ru   ro   rp   rq   r'   rt   rl   rn   zNhttps://www.metatube.com/en/videos/546765/This-frogs-decorates-Christmas-tree/x8vu47wMetatube20240326i^fx2xpy74u&   Está lindas ranitas ponen su arbolito   zQue lindura)rj   rk   rv   rs   rr   ru   rp   rq   r'   rt   rl   rn   rm   ro   zInvalid URL)rw   rx   r   zbhttps://lcp.fr/programmes/avant-la-catastrophe-la-naissance-de-la-dictature-nazie-1933-1936-346819k2Y4Mjp7krAF9iCuINMzBAvant la catastrophe la naissance de la dictature nazie 1933 -1936z$md5:7b620d5e26edbe45f27bbddc1c0257c1u   LCP Assemblée nationalexbz33di  iKg20250218)rj   rk   rl   rm   rs   rt   ru   rv   r'   rn   ro   rp   rq   rr   zMhttps://forum.ionicframework.com/t/ionic-2-jw-player-dailymotion-player/83248xwr14qz2Macklemore & Ryan Lewis - Thrift Shop (feat. Wanz)z$md5:47fbe168b5a6ddc4a205e20dd6c841b2   i%P20130114zMacklemore Officialx19qlwr)rj   rk   rl   r'   rm   rn   rv   ro   rp   rq   rr   rs   rt   ru   FzIdescription
      geoblockedCountries {
        allowed
      }
      xidc           	      #   s    t  ||E d H  td|D ]
}d|d V  qtd|D ]E}t|d}t|d}|s5q"|dd}|	d	rEd
| }|d }rRd| }n|d }r_d| }nq"t
||dV  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/rj   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=rK   )super_extract_embed_urlsrefinditergroupr
   r   r   replace
startswithr   )	clsrw   webpagemobjattrs
player_urlvideo_idquery_stringplaylist_id	__class__r   r   r   W  s.   

z!DailymotionIE._extract_embed_urlsc                      s&   dd   fddt tddD S )zFRandomize our HTTP header fingerprint to bust the HTTP Error 403 blockc                 S   s   d tjdt| |dS )NrL   bcdfghjklmnpqrstvwxz)k)joinrandomchoicesrandint)minimummaximumr   r   r   random_letterss  s   zCDailymotionIE._generate_blockbuster_headers.<locals>.random_lettersc                    s   i | ]} d d ddqS )             r   ).0_r   r   r   
<dictcomp>w  s    z?DailymotionIE._generate_blockbuster_headers.<locals>.<dictcomp>r   r   )ranger   r   r   r   r   r   _generate_blockbuster_headerso  s   
z+DailymotionIE._generate_blockbuster_headersc                 C   s   d}d}dd|   ifddddfd	d
ddffD ]2\}}z| j||||fi |}W  n1 tyK }	 z|	j}| | d|  W Y d}	~	qd}	~	ww d|vrY| j||d d}g i |fS | j||dd|dd\}
}|
||fS )z1See https://github.com/yt-dlp/yt-dlp/issues/15526z#Unable to download m3u8 informationNz4Downloading m3u8 information with randomized headersrN   z0Retrying m3u8 download with Chrome impersonationchromeT)impersonaterequire_impersonationz1Retrying m3u8 download with Firefox impersonationfirefoxz: impersonation)r   re   hlsF)m3u8_idlivefatal)r   _download_webpager   orig_msgwrite_debugreport_warning!_parse_m3u8_formats_and_subtitles)r   	media_urlr   r   
ERROR_NOTE
last_errorrW   kwargsm3u8_docrF   formats	subtitlesr   r   r   /_extract_dailymotion_m3u8_formats_and_subtitles|  s<    


z=DailymotionIE._extract_dailymotion_m3u8_formats_and_subtitlesc           "         s  t |\}}| |ddd\}}}|r|}d }| ||r)| d| d|S | d}| d|d| j| jf d	|rDd
| dnd }|d }| j	d| |dt
|dpZddid}	|	d}
|
r|
dpl|
d }|
ddkrt|dd t}| j||d t| j d| dd|	d }|d}g }i }d }|	d  D ]]\}}|D ]V}|d}|d }|r|d!krq|d"kr| j|||d#\}}}|| | j||d$ q|d%| d&}td'|}|rtt| \}}}||||d( || qq|s|r| j|dd |D ] }|d d)d* |d< |d+s0|d, d-r0d.|d+< qt|	d/d tp<i }| D ]\}}d0d1 |d2g D ||< qAt
|	d3tjhd4d d*thfd*t hfd5d6f}|	d7pri } |d8pzi   fd9d}!||t!|d:|t|	d;pd t|	d<| d=| dp|	d=|	d>rd?nd*|	d@|!dApt|dB|!dC|||dDS )ENrj   is_playlistr   z$http://www.dailymotion.com/playlist/DailymotionPlaylistvideopasswordmediaz... on Video {
      %s
      stats {
        likes {
          total
        }
        views {
          total
        }
      }
    }
    ... on Live {
      %s
      audienceCount
      isOnAir
    }zDownloading media JSON metadatazpassword: "{}"rD   z2https://www.dailymotion.com/player/metadata/video/zDownloading metadata JSONrK   appzcom.dailymotion.neonr   errorrl   raw_messagecodeDM007c                 S      | d d S )NgeoblockedCountriesallowedr   xr   r   r   <lambda>      z-DailymotionIE._real_extract.<locals>.<lambda>)msg	countriesz said: Tr8   isOnAir	qualitiesrw   typez#application/vnd.lumberjack.manifestzapplication/x-mpegURL)r   )targetzhttp-)rw   	format_idz/H264-(\d+)x(\d+)(?:-(60)/)?)fpsheightwidth#r   r   r   z@60<   c                 S   r   )Nr   r6   r   r   r   r   r   r     r   c                 S   s   g | ]}d |iqS )rw   r   )r   subtitle_urlr   r   r   
<listcomp>  s    z/DailymotionIE._real_extract.<locals>.<listcomp>urls)posters
thumbnailsc                 S   s   t |d S )Nr   )r   )r   vr   r   r   r      r   r   )r   rj   rw   ownerstatsc                    s   t t fddS )Nc                    s   |  d  d S )Nstotalr   )yr   r   r   r     s    z?DailymotionIE._real_extract.<locals>.<lambda>.<locals>.<lambda>)r   r   r   r   r   r   r     s    rm   rn   created_time
screennameexplicitr&   ro   viewaudienceCountlike)rj   rl   rm   r   rn   rq   rs   rt   r'   ro   ru   rv   r   r   is_live)"r   _match_valid_urlr   _yes_playlist
url_resultr)   r[   _COMMON_MEDIA_FIELDSformatr<   r   r   r   listraise_geo_restrictedr   IE_NAMEitemsr   extend_merge_subtitlesr   searchmapr   groupsr;   appendraise_no_formatssplitendswithdictstrr	   )"r   rw   smuggled_datar   r   r   r2   r   rD   metadatar   rl   allowed_countriesr  r   r   expected_errorquality
media_listmr   
media_typefmtsubsfr   r   r   subtitles_datasubtitle_langsubtitler   r   	get_countr   r  r   _real_extract  s   







zDailymotionIE._real_extract)F)r\   r]   r^   
_VALID_URLr  _EMBED_REGEXint_TESTS_WEBPAGE_TESTS_GEO_BYPASSr  classmethodr   r`   r   r   r/  __classcell__r   r   r   r   ra   f   s   q	h

$ra   c                   @   s    e Zd ZdZdd Zdd ZdS )DailymotionPlaylistBaseIEd   c                 c   sr    |d7 }|  | j|d| jrdnd| j|f d| d }|d D ]}|d }| |d	 t |d
 V  q"d S )Nr   zvideos(allowExplicit: %s, first: %d, page: %d) {
      edges {
        node {
          xid
          url
        }
      }
    }falsetruezDownloading page videosedgesnoderw   rD   )r[   _OBJECT_TYPEr*   
_PAGE_SIZEr  ra   ie_key)r   r   pager<  edger>  r   r   r   _fetch_page   s"   
z%DailymotionPlaylistBaseIE._fetch_pagec                 C   s,   |  |}tt| j|| j}| ||S r   )	_match_idr   	functoolspartialrD  r@  playlist_result)r   rw   r   entriesr   r   r   r/  2  s   
z'DailymotionPlaylistBaseIE._real_extractN)r\   r]   r^   r@  rD  r/  r   r   r   r   r8    s    r8  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=xl8v3qrj   xv4bw   r   
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/([^/]+)/rw   z//dailymotion.com/playlist/)r   r   findallr   r   )r   rw   r   r   pr   r   r   r   F  s   z)DailymotionPlaylistIE._extract_embed_urlsN)	r\   r]   r^   r  r0  r3  r?  r6  r   r   r   r   r   rJ  :  s    rJ  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>[^/?#]+)/videosrL  z8http://www.dailymotion.com/search/king of turtles/videoszking of turtles)rj   rl   r   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}|sCt
t|dddthfpAd|S )NrH   rI   rJ   SEARCH_QUERYrL  )limitrB  rK   )operationNamerK   	variablesrM   r6   r  rO   r   rP   zCould not fetch search data)rQ   r   rG   r<   rR   rS   _SEARCH_QUERYrT   r   r  r   r  )r   termrB  rW   rY   rZ   r   r   r   _call_search_api^  s&   

z$DailymotionSearchIE._call_search_apic                 c   sN    |d7 }|  ||d| d| }t|dD ]}| d| t|V  qd S )Nr   zSearching "z" page )r<  r=  .r>  rD   z"https://www.dailymotion.com/video/)rW  r   r  ra   )r   rV  rB  rA   rD   r   r   r   rD  r  s   zDailymotionSearchIE._fetch_pagec                 C   s2   t j| |}| tt| j|| j	||S r   )
urllibparseunquote_plusrE  rH  r   rF  rG  rD  r@  )r   rw   rV  r   r   r   r/  x  s   z!DailymotionSearchIE._real_extractN)r\   r]   r^   r  r0  r@  r3  rU  rW  rD  r/  r   r   r   r   rP  P  s    rP  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/nqtvrj   nqtv   r   z,http://www.dailymotion.com/user/UnderProjectUnderProjecti  zTakes too long time)rw   rx   r   r   r'   r   )rw   rx   r   paramschannelN)r\   r]   r^   r  r0  r3  r?  r   r   r   r   r[  ~  s.    r[  )rF  rR   r   r   urllib.parserX  commonr   networking.exceptionsr   utilsr   r   r   r	   r
   r   r   r   r   r   r   r   r   r   ra   r8  rJ  rP  r[  r   r   r   r   <module>   s"    <L   :.