o
    i(                     @   sl   d dl Z d dlZd dlZddlmZ ddl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dS )
    N   )InfoExtractor   )ExtractorErrorencode_data_urifloat_or_noneint_or_nonejoin_nonemptymimetype2extstr_or_nonec                   @   s   e Zd ZdZdZdg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iddddddd dd!d"d#d$d%d&id'd(d&d)gZd3d+d,Zd-d. Zd/d0 Z	d1d2 Z
d*S )4	UstreamIEzfhttps?://(?:www\.)?(?:ustream\.tv|video\.ibm\.com)/(?P<type>recorded|embed|embed/recorded)/(?P<id>\d+)ustreamz_<iframe[^>]+?src=(["\'])(?P<url>https?://(?:www\.)?(?:ustream\.tv|video\.ibm\.com)/embed/.+?)\1z'http://www.ustream.tv/recorded/20274954 088f151799e8f572f84eb62f17d73e5c20274954flvz4Young Americans for Liberty February 7, 2012 2:28 AMiz0O20120207	yaliberty6780869)idexttitledescription	timestampupload_dateuploaderuploader_id)urlmd5	info_dictzfhttp://www.ustream.tv/embed/recorded/59307601?ub=ff0000&lc=ff0000&oc=ffffff&uc=ffffff&v=3&wmode=direct 5a2abf40babeac9812ed20ae12d34e1059307601z"-CG11- Canada Games Figure Skatingsportscanadatv)r   r   r   r   zNThis Pro Broadcaster has chosen to remove this video from the ustream.tv site.)r   r   r   skipz$http://www.ustream.tv/embed/10299409r   10299409   )r   r   playlist_countz'http://www.ustream.tv/recorded/9134326391343263mp4z)GitHub Universe - General Session - Day 120160914iZWwa0dnskeqkr38977840)r   r   r   r   r   r   r   r   skip_downloadT)r   r   paramszVhttps://video.ibm.com/embed/recorded/128240221?&autoplay=true&controls=true&volume=100)r   only_matchingNc           
      C   s   dd }dd }|sd}| j d|d d| d	|d
| d|d |d ||d d||d d|d ||ddd}|d d d d }|d d d d }	| j d| d|	 |d| dS )Nc                 S   s   t | dd  S )Nr   )hex)n r0   L/home/ubuntu/.local/lib/python3.10/site-packages/yt_dlp/extractor/ustream.py
num_to_hexI   s   z.UstreamIE._get_stream_info.<locals>.num_to_hexc                 S   s   t t| S N)random	randrangeint)xr0   r0   r1   <lambda>L   s    z,UstreamIE._get_stream_info.<locals>.<lambda> zhttp://rg    חAz-1-z*-recorded-lp-live.ums.ustream.tv/1/ustreamzDownloading connection infoviewerr   r   :z_rpin.g  4&kCrecorded)typeappId
appVersionrsidrpinreferrermediaapplication)notequeryargshostconnectionIdzhttp://z/1/ustream?connectionId=zDownloading stream inforE   )_download_json)
selfr   video_id
app_id_ver
extra_noter2   rnd	conn_inforH   connection_idr0   r0   r1   _get_stream_infoH   s.   zUstreamIE._get_stream_infoc                 C   sh   t dD ]-}| j||||dkrd|d  dndd}d|d d	 d v r1|d d	 d d   S qg S )
Nr$   r   z (try r   )r9   )rO   streamrG   )rangerS   )rL   r   rM   rN   trial_countstream_infor0   r0   r1   _get_streamse   s   zUstreamIE._get_streamsc                 C   sB  dd }g }|d D ]}|d d }d||d |d  d|d d	 ig}t |d
 |d  D ]}|d||d |d  ||d t|d d  i q/|d }|dd }	td|	t|ddtddt||d|d|d}
|	dkr|
	|dd|dd n|
	d|d|dd ||
 q
|S )Nc                 S   s   |  dt|d d|S )N%r   )replacestr)templateidx
chunk_hashr0   r0   r1   resolve_dash_templatep   s   z=UstreamIE._parse_segmented_mp4.<locals>.resolve_dash_templatestreams	providersr   r   initUrlhashes0videoLength	chunkTime
segmentUrl
   contentType/dashbitratehttp_dash_segmentss   <MPD/>ztext/xmlheightwidth)	format_idprotocolr   r   ro   rp   	fragmentsvideocodecnone)vcodecacodecvbr)rw   rx   abr)
rV   appendr\   splitr	   r   getr   r
   update)rL   dash_stream_infor`   formatsrU   providerrs   r^   content_typekindfr0   r0   r1   _parse_segmented_mp4o   sP   
zUstreamIE._parse_segmented_mp4c                    s   |}|d|ddkr |dd }|dS |ddkrJ|d|}d|d}fd	d
|D S d d}|d}|rgt	j
 d| dd|d }|d }	t|d  fdd|d  D }
|
sj|dd}|r|
j|d d ddd 	 |d}t|d}t|d }t|d!}|d"i d#}|d"i d}d$d |d%i  D }|	||||||||
d&
S )'Nr   r=   zembed/recordedhttp://www.ustream.tv/recorded/Ustreamembedz-ustream\.vars\.offAirContentVideoIds=([^;]+);zcontent video IDsc                 3   s     | ]}  d | dV  qdS )r   r   N
url_result).0urL   r0   r1   	<genexpr>   s    z*UstreamIE._real_extract.<locals>.<genexpr>zhttps://api.ustream.tv/videos/z.jsonerrorz returned error: T)expectedrt   r   	file_sizec                    s"   g | ]\}}|r|| d qS ))r   r   r   filesizer0   )r   rq   	video_url)r   rM   r0   r1   
<listcomp>   s    
z+UstreamIE._real_extract.<locals>.<listcomp>
media_urls)   r   )rN   r   r   r'   hls)r   m3u8_idr   
created_atlengthviewsownerusernamec                 S   s   g | ]	\}}||d qS ))r   r   r0   )r   thumbnail_idthumbnail_urlr0   r0   r1   r      s
    
	thumbnail)
r   r   r   
thumbnailsr   duration
view_countr   r   r   )_match_valid_urlgroupr   _download_webpage_parse_json_search_regexplaylist_resultrK   r}   r   IE_NAMEr   itemsrY   extend_extract_m3u8_formatsr   )rL   r   mdesktop_urlwebpagecontent_video_idsr,   r   rt   r   r   hls_streamsr   r   r   r   r   r   r   r0   )r   rL   rM   r1   _real_extract   sx   






zUstreamIE._real_extractr3   )__name__
__module____qualname__
_VALID_URLr   _EMBED_REGEX_TESTSrS   rY   r   r   r0   r0   r0   r1   r      sf    
3
.r   c                   @   s,   e Zd ZdZdZdddiddZdd	 Zd
S )UstreamChannelIEz3https?://(?:www\.)?ustream\.tv/channel/(?P<slug>.+)zustream:channelz*http://www.ustream.tv/channel/channeljapanr   10874166   )r   r   playlist_mincountc                    s     |}|d} ||} d|}d}d| d}g }|rH jtj|||dt|d  dd	}	|	t
d
|	d  |	d }|s" fdd|D }
d|||
dS )Nslugzustream:channel_idzhttp://www.ustream.tvz/ajax/socialstream/videos/z/1.jsonz%Downloading video information (next: r   rT   rJ   zdata-content-id="(\d.*)"datanextUrlc                    s   g | ]
}  d | dqS )r   r   r   )r   vidr   r0   r1   r     s    z2UstreamChannelIE._real_extract.<locals>.<listcomp>playlist)_typer   
display_identries)r   r   r   _html_search_metarK   urllibparseurljoinlenr   refindall)rL   r   r   r   r   
channel_idBASEnext_url	video_idsreplyr   r0   r   r1   r      s.   


zUstreamChannelIE._real_extractN)r   r   r   r   r   _TESTr   r0   r0   r0   r1   r      s    r   )r4   r   urllib.parser   commonr   utilsr   r   r   r   r	   r
   r   r   r   r0   r0   r0   r1   <module>   s    $ ]