U
    y^N)                     @   s|   d dl mZ d dlZd dlZddlmZ ddlmZmZ ddl	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 )    )unicode_literalsN   )InfoExtractor   )
compat_strcompat_urlparse)encode_data_uriExtractorErrorint_or_nonefloat_or_nonemimetype2extstr_or_nonec                   @   s   e Zd 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iddddddddd d!d"d#d$d%id&gZed'd( Zd2d*d+Zd,d- Z	d.d/ Z
d0d1 Zd)S )3	UstreamIEzRhttps?://(?:www\.)?ustream\.tv/(?P<type>recorded|embed|embed/recorded)/(?P<id>\d+)ustreamz'http://www.ustream.tv/recorded/20274954Z 088f151799e8f572f84eb62f17d73e5cZ20274954flvz4Young Americans for Liberty February 7, 2012 2:28 AMiz0OZ20120207Z	yalibertyZ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=directZ 5a2abf40babeac9812ed20ae12d34e10Z59307601z"-CG11- Canada Games Figure SkatingZ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   Z10299409   )r   r   playlist_countz'http://www.ustream.tv/recorded/91343263Z91343263mp4z)GitHub Universe - General Session - Day 1Z20160914iZWZwa0dnskeqkrZ38977840)r   r   r   r   r   r   r   r   skip_downloadT)r   r   paramsc                 C   s"   t d| }|d k	r|dS d S )NzD<iframe[^>]+?src=(["\'])(?P<url>http://www\.ustream\.tv/embed/.+?)\1r   )researchgroup)webpagemobj r'   OC:\Users\TaGoRe\Desktop\beinconnect-dl\binaries\youtube_dl\extractor\ustream.py_extract_urlH   s     zUstreamIE._extract_urlNc           
      C   s   dd }t j}|sd}| jd|d|f |d| d|d |d	 d
||d||df d|d ||ddd}|d d d d }|d d d d }	| jd||	f |d| dS )Nc                 S   s   t | dd  S )Nr   )hex)nr'   r'   r(   
num_to_hexP   s    z.UstreamIE._get_stream_info.<locals>.num_to_hex z9http://r%d-1-%s-recorded-lp-live.ums.ustream.tv/1/ustreamg    חAzDownloading connection infoviewerr   r   z%s:%sz_rpin.%dg  4&kCZrecorded)typeappIdZ
appVersionZrsidZrpinreferrermediaapplication)notequeryargshostZconnectionIdz#http://%s/1/ustream?connectionId=%szDownloading stream infor4   )random	randrange_download_json)
selfr   video_id
app_id_ver
extra_noter,   ZrndZ	conn_infor7   Zconnection_idr'   r'   r(   _get_stream_infoO   s2     

 zUstreamIE._get_stream_infoc                 C   sd   t dD ]V}| j||||dkr*d|d  ndd}d|d d d kr|d d d d   S qg S )	Nr   r   z	 (try %d)r   r-   )r?   streamr6   )ranger@   )r<   r   r=   r>   Ztrial_countZstream_infor'   r'   r(   _get_streamsl   s      zUstreamIE._get_streamsc                 C   sP  dd }g }|d D ]4}|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 }	dtd d|	t|dg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   )replacer   )templateidxZ
chunk_hashr'   r'   r(   resolve_dash_templatew   s    z=UstreamIE._parse_segmented_mp4.<locals>.resolve_dash_templatestreamsZ	providersr   r   ZinitUrlZhashes0ZvideoLengthZ	chunkTimeZ
segmentUrl
   ZcontentType/-dashbitratehttp_dash_segmentss   <MPD/>ztext/xmlheightwidth)	format_idprotocolr   r   rQ   rR   	fragmentsvideocodecnone)vcodecacodecvbr)rY   rZ   abr)rB   appendr   splitjoinfilterr   getr   r   update)r<   Zdash_stream_inforH   formatsrA   ZproviderrU   rG   content_typekindfr'   r'   r(   _parse_segmented_mp4v   s^       
    

zUstreamIE._parse_segmented_mp4c                    s  t j|}|d|ddkrD|dd }|dS |ddkr|d|}d|d}t	fd	d
|S 
d }|d}|rtdj|f dd|d }|d }	t|d  fdd|d  D }
|
sBj|dd}|rB|
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                    s     d|  dS )Nrh   ri   
url_result)ur<   r'   r(   <lambda>       z)UstreamIE._real_extract.<locals>.<lambda>z%https://api.ustream.tv/videos/%s.jsonerrorz%s returned error: %sT)expectedrV   r   	file_sizec                    s"   g | ]\}}|r|| d qS ))r   r   r   filesizer'   ).0rS   	video_url)rt   r=   r'   r(   
<listcomp>   s    z+UstreamIE._real_extract.<locals>.<listcomp>Z
media_urls)   r   )r>   r   r   r   hls)r   m3u8_idr   
created_atlengthviewsownerusernamec                 S   s   g | ]\}}||d qS ))r   r   r'   )ru   Zthumbnail_idthumbnail_urlr'   r'   r(   rw      s   	thumbnail)
r   r   r   
thumbnailsr   duration
view_countr   r   rc   )r"   match
_VALID_URLr$   rl   _download_webpage_parse_json_search_regexplaylist_resultmapr;   ra   r	   IE_NAMEr   itemsrC   extend_extract_m3u8_formats_sort_formatsr
   )r<   r   mdesktop_urlr%   Zcontent_video_idsr!   rq   rV   r   rc   Zhls_streamsr   r   r   r   r   r   r   r'   )rt   r<   r=   r(   _real_extract   s    


  
 

   


zUstreamIE._real_extract)N)__name__
__module____qualname__r   r   _TESTSstaticmethodr)   r@   rC   rg   r   r'   r'   r'   r(   r      sd     0


.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   Z10874166   )r   r   playlist_mincountc                    s   t  j|}|d} ||} d|}d}d| }g }|r jt|||dt	|d  d}	|
t d|	d	  |	d
 }q@ fdd|D }
d|||
dS )Nslugzustream:channel_idzhttp://www.ustream.tvz#/ajax/socialstream/videos/%s/1.jsonz(Downloading video information (next: %d)r   r8   zdata-content-id="(\d.*)"dataZnextUrlc                    s   g | ]}  d | dqS )rh   ri   rk   )ru   vidrn   r'   r(   rw     s   z2UstreamChannelIE._real_extract.<locals>.<listcomp>playlist)_typer   
display_identries)r"   r   r   r$   r   _html_search_metar;   r   urljoinlenr   findall)r<   r   r   r   r%   
channel_idZBASEnext_urlZ	video_idsZreplyr   r'   rn   r(   r     s.    

 

zUstreamChannelIE._real_extractN)r   r   r   r   r   _TESTr   r'   r'   r'   r(   r      s    r   )
__future__r   r9   r"   commonr   compatr   r   utilsr   r	   r
   r   r   r   r   r   r'   r'   r'   r(   <module>   s    
 b