3
b`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 jd| }|d k	r|jdS d S )NzD<iframe[^>]+?src=(["\'])(?P<url>http://www\.ustream\.tv/embed/.+?)\1r   )researchgroup)webpagemobj r'   HC:\Users\Broad\Desktop\All-Down\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   s(    

zUstreamIE._get_stream_infoc             C   sd   x^t dD ]R}| j||||dkr,d|d  ndd}d|d d d kr
|d d d d S q
W 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   sZ  dd }g }xF|d D ]8}|d d }d||d |d  d|d d	 ig}xRt |d
 |d  D ]:}|jd||d |d  ||d t|d d  i qfW |d }|jdd }	djtd d|	t|jdgdtddt	||jd|jd|d}
|	dkr*|
j
|jdd|jdd n|
j
d|jd|jdd |j|
 qW |S )Nc             S   s   | j dt|dj 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   rP   rQ   	fragmentsvideocodecnone)vcodecacodecvbr)rX   rY   abr)rA   appendr   splitjoinfilterr   getr   r   update)r;   Zdash_stream_inforG   formatsr@   ZproviderrT   rF   content_typekindfr'   r'   r(   _parse_segmented_mp4v   s@    ""
zUstreamIE._parse_segmented_mp4c                s  t jj|}|jd|jddkrD|jdd }j|dS |jddkr|jdj|}jjd|d}jt	fd	d
|S j
d }|jd}|rtdj|f dd|d }|d }	t|jd  fdd|d j D }
|
sBj|d'd}|rB|
jj|d d ddd j|
 |jd}t|jd}t|jd }t|jd!}|jd"i jd#}|jd"i jd}d$d |jd%i j D }|	||||||||
d&
S )(Nr   r/   zembed/recordedzhttp://www.ustream.tv/recorded/Ustreamembedz-ustream\.vars\.offAirContentVideoIds=([^;]+);zcontent video IDsc                s    j d|  dS )Nzhttp://www.ustream.tv/recorded/rg   )
url_result)u)r;   r'   r(   <lambda>   s    z)UstreamIE._real_extract.<locals>.<lambda>z%https://api.ustream.tv/videos/%s.jsonerrorz%s returned error: %sT)expectedrU   r   	file_sizec                s"   g | ]\}}|r|| d qS ))r   r   r   filesizer'   ).0rR   	video_url)ro   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'   )rp   Zthumbnail_idthumbnail_urlr'   r'   r(   rr      s   	thumbnail)
r   r   r   
thumbnailsr   duration
view_countr   r   rb   )rs   r   )r"   match
_VALID_URLr$   ri   _download_webpage_parse_json_search_regexplaylist_resultmapr:   r`   r	   IE_NAMEr   itemsrB   extend_extract_m3u8_formats_sort_formatsr
   )r;   r   mdesktop_urlr%   Zcontent_video_idsr!   rl   rU   r   rb   Zhls_streamsr   r   r~   r   r   r   r}   r'   )ro   r;   r<   r(   _real_extract   sd    




	

zUstreamIE._real_extract)N)__name__
__module____qualname__r   r   _TESTSstaticmethodr)   r?   rB   rf   r   r'   r'   r'   r(   r      sJ   


.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 j|}|jd} j||} jd|}d}d| }g }xJ|r jtj|||dt	|d  d}	|j
t jd|	d	  |	d
 }qBW  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   )r4   zdata-content-id="(\d.*)"dataZnextUrlc                s   g | ]} j d | dqS )zhttp://www.ustream.tv/recorded/rg   )ri   )rp   vid)r;   r'   r(   rr     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'   )r;   r(   r     s&    

zUstreamChannelIE._real_extractN)r   r   r   r   r   _TESTr   r'   r'   r'   r(   r      s   r   )
__future__r   r8   r"   commonr   compatr   r   utilsr   r	   r
   r   r   r   r   r   r'   r'   r'   r(   <module>   s    
 b