o
    i2                     @   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/)OriginRefererz1https://twitcasting.tv/ivetesangalo/movie/2357609 745243cad58c4681dc752490f7540d7f2357609mp4zLive #2357609ivetesangaloz4Twitter Oficial da cantora brasileira Ivete Sangalo.zre:^https?://.*\.jpg$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/36897403689740zLive playing something #3689740mttbernardiniz$md5:1dc7efa2f1ab932fcd119265cebeec6920120211i(6Oi  abc)r+   videopassword)r,   r.   r/   z2https://twitcasting.tv/loft_heaven/movie/685979292	685979292uv   【無料配信】南波一海のhear/here “ナタリー望月哲さんに聞く編集と「渋谷系狂騒曲」”loft_heavenz$md5:3a0c7b53019df987ce545c935538bacf20210604i`i4  )
r!   r"   r#   r$   r%   r'   r(   r&   r)   r*   c                 C   sB   z|  ||W S  ty   Y nw t|d d d }|  ||S )N)_parse_jsonr   base64	b64decode)selfdmpvideo_iddecoded r@   P/home/ubuntu/.local/lib/python3.10/site-packages/yt_dlp/extractor/twitcasting.py_parse_data_movie_playlistY   s   z(TwitCastingIE._parse_data_movie_playlistc              
      s4   | \}|\}d}d }|r5td|idd}j||ddidd\}|j|krI|rIj|j|ddid	dt	d
rUt
dddttdpejddgdd}tfddt}t|dpz}ttdpg d}	t|dd pt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#}|ra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 ]\}}| t!||d/| d0||d1j"d2 q	t#rTtg d3}t|d4d-tjhd5d fD ]\}}| t!||d6| d0||d7d8d9 q:|s[$  |d:d;}n1|sht
d<t%|d=krj&|d> d0j"d?}|j"d@}ndA fdBdCt'|D dD}dEi |S )FNr4   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)expected
movietitlezog:titleztwitter:title)fatalc                    s      jd| dd dd S )Nz!data-movie-playlist=\'([^\']+?)\'zmovie playlistdefault2)rB   _search_regexx)r<   r>   r@   rA   <lambda>   s    z-TwitCastingIE._real_extract.<locals>.<lambda>)r   thumbnailUrl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yr@   r@   rA   	<genexpr>       z@TwitCastingIE._real_extract.<locals>.<lambda>.<locals>.<genexpr>i  )sumrO   r@   r@   rA   rQ      s    ztw-player-duration-time)zTotal\s*:\s*Views\s*([\d,]+)u    総視聴者\s*:\s*([\d,]+)\s*</viewsz)data-toggle="true"[^>]+datetime="([^"]+)"datetimec                 3   s    | ]
}d |  v V  qdS )zdata-Nr@   )rU   rP   )webpager@   rA   rW      rX   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,   )grouprL   )rN   )rP   data_movie_url)r<   r@   rA   find_dmu   s   z-TwitCastingIE._real_extract.<locals>.find_dmu).sourcer,   z'https://twitcasting.tv/streamserver.phpzDownloading live infoclientpc_web)targetmodeplayer)queryword)lowmediumhighztc-hlsstreamsc                 S      t |d S Nr   r   _vr@   r@   rA   rQ          zhls-r   m3u8)r,   	format_idr"   qualityprotocolhttp_headers)basemobilesourcemainllfmp4c                 S   rm   rn   ro   rp   r@   r@   rA   rQ      rs   zws-iwebsocket_frag)r,   ru   r"   rv   source_preferencerw   )ra   )formats_format_sort_fieldszFailed to get m3u8 playlistr   r   )rF   )r   rx   multi_videoc                    s0   g | ]\}} d | |ddj d qS )-r   rt   )r!   r,   r"   rw   rx   )_M3U8_HEADERS)rU   numm3u8_url)	base_dictr<   r>   r@   rA   
<listcomp>   s    	z/TwitCastingIE._real_extract.<locals>.<listcomp>)_typeentriesr!   )(_match_valid_urlgroups_download_webpage_handle	get_paramr   _hidden_inputsr,   _download_webpageresearchr   r   r   _html_search_metar   listr   _og_search_thumbnailr   r
   r   rN   r   any_download_jsonhashlibr-   encode	hexdigestr   dictitemsappendr   r   r   raise_login_requiredlen_extract_m3u8_formats	enumerate)r<   r,   r$   urlhvideo_passwordrequest_datar#   video_js_datar&   r%   r)   r*   r(   r]   r`   	m3u8_urlsstream_datapassword_paramsr   qqrv   r   re   ws_urlinfodictr@   )r   r<   r>   r\   rA   _real_extractc   s  


	
	zTwitCastingIE._real_extractN)	__name__
__module____qualname__
_VALID_URLr   int_TESTSrB   r   r@   r@   r@   rA   r      sn    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,   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 r-t|d
| d| d|d}|pB| jd|ddd}| jd|dd dd}|rQ|sVt|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 status    F)rE   rJ   r]   )r>   r   z/show/zDownloading live historyzL(?s)(<span\s*class="tw-movie-thumbnail2-badge"\s*data-status="live">\s*LIVE)zis live?rK   zM(?s)<a\s+class="tw-movie-thumbnail2"\s+href="/[^/"]+/movie/(?P<video_id>\d+)"zcurrent live IDr>   )rL   r^   z/movie/)
	_match_id	to_screenr   r   boolr   r   rN   
url_resultr   )r<   r,   r$   r]   r\   current_liver@   r@   rA   r     s8   


zTwitCastingLiveIE._real_extractN)r   r   r   r   r   r   r@   r@   r@   rA   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/natsuiromatsuriznatsuiromatsuri - Live History)r!   r#      )r,   r.   playlist_mincountz'https://twitcasting.tv/noriyukicas/showTr   c              	   c   s    d| d }}t dD ]A}| j||ddid| d}td|}|D ]}| t||d	V  q&| jd
t	||f |dd d}t||}|sO d S qd S )Nr   z/showr   filter	watchablezDownloading page )rg   rG   zH(?s)<a\s+class="tw-movie-thumbnail2"\s+href="(?P<url>/[^/"]+/movie/\d+)"r,   z<a href="(/%s/show/%d-\d+)[?"]znext urlrK   )
	itertoolscountr   r   finditerr   r   r^   rN   escape)r<   r$   base_urlnext_urlpage_numr\   matchesmobjr@   r@   rA   _entries-  s(   
zTwitCastingUserIE._entriesc                 C   s$   |  |}| | ||| dS )Nz - Live History)r   playlist_resultr   )r<   r,   r$   r@   r@   rA   r   >  s   
zTwitCastingUserIE._real_extractN)r   r   r   r   r   r   r   r@   r@   r@   rA   r     s    r   )r:   r   r   r   commonr   dependenciesr   utilsr   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   utils.traversalr   r   r   r   r@   r@   r@   rA   <module>   s    D ^&