U
    g$                     @   sl   d dl mZ ddlmZ ddl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S )   )InfoExtractor   )	HTTPError)ExtractorErrorfloat_or_nonec                	   @   s|   e Zd ZdZdddddddd	d
ddddddd	ddiddddddddddddddddgZdd Zdd ZdS )RedBullTVIEzhttps?://(?:www\.)?redbull(?:\.tv|\.com(?:/[^/]+)?(?:/tv)?)(?:/events/[^/]+)?/(?:videos?|live|(?:film|episode)s)/(?P<id>AP-\w+)z-https://www.redbull.tv/video/AP-1Q6XCDTAN1W11Z fb0445b98aa4394e504b413d98031d1fzAP-1Q6XCDTAN1W11mp4zABC of... WRC - ABC of... S1E6z$md5:5c7ed8f4015c8492ecf64b6ab31e7d31g\(@)idexttitledescriptiondurationurlmd5	info_dictz-https://www.redbull.tv/video/AP-1PMHKJFCW1W11zAP-1PMHKJFCW1W11Grime - Hashtags S2E4$md5:5546aa612958c08a98faaad4abce484di  Zskip_downloadT)r   r   paramszhttps://www.redbull.com/int-en/tv/video/AP-1UWHCAR9S1W11/rob-meets-sam-gaze?playlist=playlists::3f81040a-2f31-4832-8e2e-545b1d39d173r   Zonly_matchingz5https://www.redbull.com/us-en/videos/AP-1YM9QCYE52111zKhttps://www.redbull.com/us-en/events/AP-1XV2K61Q51W11/live/AP-1XUJ86FDH1W11z5https://www.redbull.com/int-en/films/AP-1ZSMAW8FH2111z8https://www.redbull.com/int-en/episodes/AP-1TQWK7XE11W11c              
   C   s  | j d|ddddd}|ddkr<td	| j|d
 |d }z| j d| |dd|id}W np tk
r } zRt|jtr|jjdkr| 	|jj
  |d }t| j d| dd W 5 d }~X Y nX |d  }| jd| d| d|dddd\}}	|dg D ]N}
|
dr|
d}|d r|	d g d!| d|
 |d d" q|d#}|r|d$| 7 }|||d%p|d&t|d'd(d)||	d*S )+Nz!https://api.redbull.tv/v3/sessionzDownloading access tokenZpersonal_computerhttp)categoryZ	os_family)notequerycodeerrorz{} said: {}messagetokenz#https://api.redbull.tv/v3/products/zDownloading video informationAuthorization)r   headersi  z said: T)expectedr   zhttps://dms.redbull.tv/v3//z/playlist.m3u8r   Zm3u8_nativeZhls)Zentry_protocolZm3u8_id	resourcesZclosed_caption__r   enzhttps://resources.redbull.tv/)r   r
   
subheadingz - long_descriptionZshort_descriptionr   i  )Zscale)r	   r   r   r   formats	subtitles)_download_jsongetr   formatZIE_NAME
isinstancecauser   statusZ_parse_jsonresponsereaddecodestripZ#_extract_m3u8_formats_and_subtitles
startswithsplit
setdefaultappendr   )selfvideo_idsessionr   videoeerror_messager   r'   r(   resourceZsplitted_resourcer%    r>   >/tmp/pip-unpacked-wheel-q5ljy6pj/yt_dlp/extractor/redbulltv.pyextract_info4   sv       
    



zRedBullTVIE.extract_infoc                 C   s   |  |}| |S )N)	_match_idr@   )r7   r   r8   r>   r>   r?   _real_extractj   s    
zRedBullTVIE._real_extractN)__name__
__module____qualname__
_VALID_URL_TESTSr@   rB   r>   r>   r>   r?   r   	   sN    )6r   c                   @   s(   e Zd ZdZdddgZdZdd ZdS )	RedBullEmbedIEzhttps?://(?:www\.)?redbull\.com/embed/(?P<id>rrn:content:[^:]+:[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}:[a-z]{2}-[A-Z]{2,3})zdhttps://www.redbull.com/embed/rrn:content:episode-videos:f3021f4f-3ed4-51ac-915a-11987126e405:en-INTTr   zA... on %s {
      videoEssence {
        attributes
      }
    }c              
   C   sZ   |  |}| jd|ddddd|| jd | jd f id	d
 d d d d }| |S )Nz?https://edge-graphql.crepo-production.redbullaws.com/v1/graphqlzapplication/jsonZ e90a1ff11335423998b100c929ecc866)AcceptzAPI-KEYr   zG{
  resource(id: "%s", enforceGeoBlocking: false) {
    %s
    %s
  }
}Z	LiveVideoZVideoResource)r   r   datar=   ZvideoEssence
attributesZassetId)rA   r)   _VIDEO_ESSENSE_TMPLr@   )r7   r   rrn_idZasset_idr>   r>   r?   rB   |   s0    
 zRedBullEmbedIE._real_extractN)rC   rD   rE   rF   rG   rL   rB   r>   r>   r>   r?   rH   o   s   rH   c                   @   s4   e Zd ZdZdddddddddgZdd Zd	S )
RedBullTVRrnContentIEzhttps?://(?:www\.)?redbull\.com/(?P<region>[a-z]{2,3})-(?P<lang>[a-z]{2})/tv/(?:video|live|film)/(?P<id>rrn:content:[^:]+:[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12})zhttps://www.redbull.com/int-en/tv/video/rrn:content:live-videos:e3e6feb4-e95f-50b7-962a-c70f8fd13c73/mens-dh-finals-fort-williamTr   zhttps://www.redbull.com/int-en/tv/video/rrn:content:videos:a36a0f36-ff1b-5db8-a69d-ee11a14bf48b/tn-ts-style?playlist=rrn:content:event-profiles:83f05926-5de8-5389-b5e4-9bb312d715e8:extraszghttps://www.redbull.com/int-en/tv/film/rrn:content:films:d1f4d00e-4c04-5d19-b510-a805ffa2ab83/follow-mec                 C   sB   |  | \}}}|d| d|  7 }| d| t |S )N:-https://www.redbull.com/embed/)_match_valid_urlgroupsupper
url_resultrH   ie_key)r7   r   regionlangrM   r>   r>   r?   rB      s     z#RedBullTVRrnContentIE._real_extractN)rC   rD   rE   rF   rG   rB   r>   r>   r>   r?   rN      s   rN   c                	   @   s~   e Zd ZdZdddddddd	d
dddddddddddddddddgZddddgZdddddddgZdd ZdS ) 	RedBullIEzhttps?://(?:www\.)?redbull\.com/(?P<region>[a-z]{2,3})-(?P<lang>[a-z]{2})/(?P<type>(?:episode|film|(?:(?:recap|trailer)-)?video)s|live)/(?!AP-|rrn:content:)(?P<id>[^/?#&]+)z>https://www.redbull.com/int-en/episodes/grime-hashtags-s02-e04Z db8271a7200d40053a1809ed0dd574ffzAA-1MT8DQWA91W14r   r   r   )r	   r
   r   r   r   zFhttps://www.redbull.com/int-en/films/kilimanjaro-mountain-of-greatnessTr   zjhttps://www.redbull.com/int-en/recap-videos/uci-mountain-bike-world-cup-2017-mens-xco-finals-from-vallnordz>https://www.redbull.com/int-en/trailer-videos/kings-of-contentzQhttps://www.redbull.com/int-en/videos/tnts-style-red-bull-dance-your-style-s1-e12z?https://www.redbull.com/int-en/live/mens-dh-finals-fort-williamzAhttps://www.redbull.com/gb-en/live/fia-wrc-saturday-recap-estoniader$   esfrarZboZcarZclcoZmxpec                    s   |  | \} }}|dkr$d}n|dkr0d}| g}|dkrj|| jkrV|d  | jkrj|d d fd	d
|D }| jd| |||dddd d }| d| t	
 |S )NZepisodeszepisode-videosZlivezlive-videosintZLATINT>c                    s   g | ]}  d | qS )rP   r>   ).0regrX   r>   r?   
<listcomp>   s     z+RedBullIE._real_extract.<locals>.<listcomp>z3https://www.redbull.com/v3/api/graphql/v1/v3/query/zv1:hero)zfilter[type]zfilter[uriSlug]Z	rb3Schema)r   rJ   r	   rQ   )rR   rS   rT   _LAT_FALLBACK_MAPr6   _INT_FALLBACK_LISTjoinr)   rU   rH   rV   )r7   r   rW   Zfilter_typeZ
display_idZregionslocalerM   r>   re   r?   rB      s:    




 zRedBullIE._real_extractN)rC   rD   rE   rF   rG   rh   rg   rB   r>   r>   r>   r?   rY      s>   
rY   N)commonr   Znetworking.exceptionsr   utilsr   r   r   rH   rN   rY   r>   r>   r>   r?   <module>   s   f