a
    h2                     @   s   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mZmZ ddlmZ G dd	 d	eZG d
d deZG dd deZdS )    N   )InfoExtractor   )
websockets)ExtractorErrorUserNotLive
clean_htmlfloat_or_noneget_element_by_classget_element_by_idparse_duration	qualities
str_to_inttry_getunified_timestampupdate_url_queryurl_or_noneurlencode_postdataurljoin)traverse_objc                   @   s   e Zd ZdZdddZddddd	d
ddddded
ddiddddddddddded
dddddd dd!d"d#d$d%dd&ed'
ddidgZd(d) Zd*d+ Zd,S )-TwitCastingIEz^https?://(?:[^/?#]+\.)?twitcasting\.tv/(?P<uploader_id>[^/?#]+)/(?:movie|twplayer)/(?P<id>\d+)https://twitcasting.tvhttps://twitcasting.tv/)OriginZRefererz1https://twitcasting.tv/ivetesangalo/movie/2357609Z 745243cad58c4681dc752490f7540d7fZ2357609mp4zLive #2357609Zivetesangaloz4Twitter Oficial da cantora brasileira Ivete Sangalo.zre:^https?://.*\.jpg$Z20110822i8QN    )
idexttitleuploader_iddescription	thumbnailupload_date	timestampduration
view_countskip_downloadT)urlmd5	info_dictparamsz2https://twitcasting.tv/mttbernardini/movie/3689740Z3689740zLive playing something #3689740Zmttbernardiniz$md5:1dc7efa2f1ab932fcd119265cebeec69Z20120211i(6Oi  abc)r&   videopassword)r'   r)   r*   z2https://twitcasting.tv/loft_heaven/movie/685979292Z	685979292uv   【無料配信】南波一海のhear/here “ナタリー望月哲さんに聞く編集と「渋谷系狂騒曲」”Zloft_heavenz$md5:3a0c7b53019df987ce545c935538bacfZ20210604i`i4  )
r   r   r   r   r    r"   r#   r!   r$   r%   c                 C   sB   z|  ||W S  ty    Y n0 t|d d d }|  ||S )N)Z_parse_jsonr   base64	b64decode)selfZdmpvideo_iddecoded r3   Q/mnt/pikpak/tmp/myenv/lib/python3.9/site-packages/yt_dlp/extractor/twitcasting.py_parse_data_movie_playlistY   s    z(TwitCastingIE._parse_data_movie_playlistc              
      s>   | \}|\}d}d }|rjtd|idd}j||ddidd\}|j|kr|rj|j|ddid	dt	d
rt
dddttdpʈjddgdd}tfddt}t|dp}ttdpg d}	t|dd p6tttd}
tddd }tddd }tfdddD }||	||||
||d  fd!d"}t|tpt|d#}|rʈjd$d%|d&d'd(d)}|rd*t|  ind }g }tg d+}t|d,d-tjhd.d fD ]4\}}| t!||d/| d0||d1j"d2 qt#rtg d3}t|d4d-tjhd5d fD ]2\}}| t!||d6| d0||d7d8d9 q||s$  |d:d;}nd|st
d<nTt%|d=krj&|d> d0j"d?}|j"d@}n dA fdBdCt'|D dD}dEi |S )FNr,   passwordzutf-8)encodingr   r   zTrying video password)dataheadersnotezRetrying authenticationz7<form\s+method="POST">\s*<input\s+[^>]+?name="password"zFThis video is protected by a password, use the --video-password optionT)expectedZ
movietitlezog:titleztwitter:title)fatalc                    s      jd| dd dd S )Nz!data-movie-playlist=\'([^\']+?)\'zmovie playlistdefault2)r5   _search_regexx)r0   r1   r3   r4   <lambda>   s   z-TwitCastingIE._real_extract.<locals>.<lambda>)r   ZthumbnailUrlZauthorcomment)r    zog:descriptionztwitter:descriptionc                 S   s   t dd | D d S )Nc                 s   s   | ]}t |d V  qdS )r$   N)r	   get).0yr3   r3   r4   	<genexpr>       z@TwitCastingIE._real_extract.<locals>.<lambda>.<locals>.<genexpr>i  )sumrA   r3   r3   r4   rC      rH   ztw-player-duration-time)zTotal\s*:\s*Views\s*([\d,]+)u    総視聴者\s*:\s*([\d,]+)\s*</Zviewsz)data-toggle="true"[^>]+datetime="([^"]+)"datetimec                 3   s   | ]}d |  v V  qdS )zdata-Nr3   )rE   rB   )webpager3   r4   rG      rH   z.TwitCastingIE._real_extract.<locals>.<genexpr>)zis-onlive="true"zlive-type="live"zstatus="online")r   r    r!   r#   r   r$   r%   is_livec                    s"    j d| ddd d}|r|gS d S )Nz-data-movie-url=(["\'])(?P<url>(?:(?!\1).)+)\1zm3u8 urlr'   )groupr>   )r@   )rB   Zdata_movie_url)r0   r3   r4   find_dmu   s    z-TwitCastingIE._real_extract.<locals>.find_dmu).sourcer'   z'https://twitcasting.tv/streamserver.phpzDownloading live infoclientZpc_web)targetmodeZplayer)queryword)lowZmediumhighztc-hlsZstreamsc                 S   s   t |d S Nr   r   _vr3   r3   r4   rC      rH   zhls-r   m3u8)r'   	format_idr   qualityprotocolhttp_headers)baseZmobilesourcemainZllfmp4c                 S   s   t |d S rW   rX   rY   r3   r3   r4   rC      rH   zws-iZwebsocket_frag)r'   r]   r   r^   Zsource_preferencer_   )rO   )formatsZ_format_sort_fieldszFailed to get m3u8 playlistr   r   )r9   )rc   r`   Zmulti_videoc                    s0   g | ](\}} d | |ddj d qS )-r   r\   )r   r'   r   r_   r`   )_M3U8_HEADERS)rE   numm3u8_url)	base_dictr0   r1   r3   r4   
<listcomp>   s   	z/TwitCastingIE._real_extract.<locals>.<listcomp>)_typeentriesr   )(Z_match_valid_urlgroupsZ_download_webpage_handle	get_paramr   Z_hidden_inputsr'   _download_webpageresearchr   r   r   Z_html_search_metar   listr   Z_og_search_thumbnailr   r
   r   r@   r   any_download_jsonhashlibr(   encode	hexdigestr   dictitemsappendr   re   r   Zraise_login_requiredlenZ_extract_m3u8_formats	enumerate)r0   r'   r   ZurlhZvideo_passwordZrequest_datar   Zvideo_js_datar!   r    r$   r%   r#   rL   rN   Z	m3u8_urlsZstream_dataZpassword_paramsrc   Zqqr^   rg   rR   Zws_urlZinfodictr3   )rh   r0   r1   rK   r4   _real_extractc   s   


	

	zTwitCastingIE._real_extractN)	__name__
__module____qualname__
_VALID_URLre   int_TESTSr5   r|   r3   r3   r3   r4   r      sl   7
r   c                   @   s,   e Zd ZdZddddddgZdd	 Zd
S )TwitCastingLiveIEzBhttps?://(?:[^/?#]+\.)?twitcasting\.tv/(?P<id>[^/?#]+)/?(?:[#?]|$)z#https://twitcasting.tv/ivetesangaloTr'   Zonly_matchingz#https://twitcasting.tv/c:unusedliver   )r'   expected_exceptionc                 C   s   |  |}| d| d| d t| jd| |ddddd	thf}|du rZt|d
| d| d|d}|p| jd|ddd}| jd|dd dd}|r|st|d
| d| d| t	S )NzDownloading live video of user z. Pass "https://twitcasting.tv/z/show" to download the historyz.https://frontendapi.twitcasting.tv/watch/user/zChecking live statusrH   F)r8   r<   rL   )r1   r   z/show/zDownloading live historyzL(?s)(<span\s*class="tw-movie-thumbnail2-badge"\s*data-status="live">\s*LIVE)zis live?r=   zM(?s)<a\s+class="tw-movie-thumbnail2"\s+href="/[^/"]+/movie/(?P<video_id>\d+)"zcurrent live IDr1   )r>   rM   z/movie/)
	_match_idZ	to_screenr   rs   boolr   rn   r@   
url_resultr   )r0   r'   r   rL   rK   Zcurrent_liver3   r3   r4   r|     s8    


zTwitCastingLiveIE._real_extractN)r}   r~   r   r   r   r|   r3   r3   r3   r4   r      s   r   c                   @   s<   e Zd ZdZdddddddd	d
gZdd Zdd ZdS )TwitCastingUserIEzShttps?://(?:[^/?#]+\.)?twitcasting\.tv/(?P<id>[^/?#]+)/(?:show|archive)/?(?:[#?]|$)z/https://twitcasting.tv/natsuiromatsuri/archive/Znatsuiromatsuriznatsuiromatsuri - Live History)r   r      )r'   r)   Zplaylist_mincountz'https://twitcasting.tv/noriyukicas/showTr   c              	   c   s   d| d }}t dD ]}| j||ddid| d}td|}|D ]}| t||d	V  qJ| jd
t	||f |dd d}t||}|s d S qd S )Nr   z/showr   filterZ	watchablezDownloading page )rS   r:   zH(?s)<a\s+class="tw-movie-thumbnail2"\s+href="(?P<url>/[^/"]+/movie/\d+)"r'   z<a href="(/%s/show/%d-\d+)[?"]znext urlr=   )
	itertoolscountrn   ro   finditerr   r   rM   r@   escape)r0   r   base_urlZnext_urlZpage_numrK   matchesZmobjr3   r3   r4   _entries-  s"    
zTwitCastingUserIE._entriesc                 C   s$   |  |}| | ||| dS )Nz - Live History)r   Zplaylist_resultr   )r0   r'   r   r3   r3   r4   r|   >  s    
zTwitCastingUserIE._real_extractN)r}   r~   r   r   r   r   r|   r3   r3   r3   r4   r     s   r   )r.   rt   r   ro   commonr   Zdependenciesr   utilsr   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   Zutils.traversalr   r   r   r   r3   r3   r3   r4   <module>   s   D ^&