o
    i,                     @   s   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 G dd deZG d	d
 d
eZG dd deZdS )    N   )InfoExtractor   )Request)ExtractorErrorUserNotLive
js_to_jsontraverse_objupdate_url_queryurlencode_postdataurljoinc                	   @   s|   e Zd ZdZdZdZddddddd	d
diddddddddddddddddddddddgZdd  Zd!d" Zd#S )$FC2IEzB(?:https?://video\.fc2\.com/(?:[^/]+/)*content/|fc2:)(?P<id>[^/]+)fc2z0http://video.fc2.com/en/content/20121103kUan1KHs a6ebe8ebe0396518689d963774a54eb720121103kUan1KHszBoxing again with Puffmp4zre:https?://.+\.jpe?g)idtitleext	thumbnailskip_downloadm3u8)urlmd5	info_dictparamsz.https://video.fc2.com/content/20121209FP73fxDx 066bdb9b3a56a97f49cbf0d0b8a75a1f20121209FP73fxDxz1Farewelling The Wiggles Live in Sydney Dec 8 2012zbSaying goodbye to the Wiggles at their Celebration Concert in Sydney, and what a concert that was!)r   r   r   r   descriptionr   r   r   z1http://video.fc2.com/en/content/20150125cEva0hDn/20150125cEva0hDn)r   r   zytdl@yt-dl.orgz(snip))usernamepasswordzrequires actual password)r   r   r   skipz2http://video.fc2.com/en/a/content/20130926eZpARwsFT)r   only_matchingc                 C   s   |   \}}|d u s|d u rdS ||ddd}t|}td|}| j|d ddd}d	|vr4| d
 dS td}| j|d ddd dS )NFvideoz Login )emailr"   doneSubmitzAhttps://secure.id.fc2.com/index.php?mode=login&switch_language=enz
Logging inzUnable to log in)noteerrnotezmode=redirect&login=donez*unable to log in: bad username or passwordz+http://id.fc2.com/?mode=redirect&login=donezLogin redirectzLogin redirect failedT)_get_login_infor   r   _download_webpagereport_warning)selfr!   r"   login_form_strs
login_datarequestlogin_resultslogin_redir r4   H/home/ubuntu/.local/lib/python3.10/site-packages/yt_dlp/extractor/fc2.py_login<   s*   
zFC2IE._loginc           	      C   s   |  |}|   d }|ds| ||}| j  |   d\}}}|d ur=| jd|ddd}| |}| j|d d}| j	d| d	|d
d}t
|d}|sTtdtd|}|||d|ddkrfdnd||dS )Nfc2:)NNN)z*<h2\s+class="videoCnt_title">([^<]+?)</h2>z;\s+href="[^"]+"\s*title="([^"]+?)"\s*rel="nofollow">\s*<imgz9\s+href="[^"]+"\s*title="([^"]+?)"\s*rel="nofollow">\s*\1r   Ffatal)defaultz+https://video.fc2.com/api/v3/videoplaylist/z
?sh=1&fs=0zDownloading info pager)   )playlistnqzUnable to extract video URLzhttps://video.fc2.com/r   typer   m3u8_nativehttps)r   r   r   r   protocolr   r   )	_match_idr6   
startswithr,   	cookiejarclear_session_cookies_html_search_regex_og_search_thumbnail_og_search_description_download_jsonr	   r   r   get)	r.   r   video_idwebpager   r   r   vidplaylistvid_urlr4   r4   r5   _real_extractY   s@   






zFC2IE._real_extractN)	__name__
__module____qualname__
_VALID_URLIE_NAME_NETRC_MACHINE_TESTSr6   rO   r4   r4   r4   r5   r      sL    'r   c                   @   s2   e Zd ZdZdZddddddd	d
Zdd ZdS )
FC2EmbedIEz1https?://video\.fc2\.com/flv2\.swf\?(?P<query>.+)z	fc2:embedu'  http://video.fc2.com/flv2.swf?t=201404182936758512407645&i=20130316kwishtfitaknmcgd76kjd864hso93htfjcnaogz629mcgfs6rbfk0hsycma7shkf85937cbchfygd74&i=201403223kCqB3Ez&d=2625&sj=11&lang=ja&rel=1&from=11&cmt=1&tk=TlRBM09EQTNNekU9&tl=プリズン･ブレイク%20S1-01%20マイケル%20【吹替】 b8aae5334cb691bdb1193a88a6ab5d5a201403223kCqB3Ezflvu;   プリズン･ブレイク S1-01 マイケル 【吹替】zre:^https?://.*\.jpg$)r   r   r   r   r   c              
   C   s   |  |}tj|d}|d d }|dd| gd }|dd gd }d }|rGd|d	|d d
 |d
d |d |d |f}dt	 d| ||dS )Nqueryitlz
FC2 video r   sjz.http://video{}-thumbnail.fc2.com/up/pic/{}.jpg/      url_transparentr7   )_typeie_keyr   r   r   )
_match_valid_urlurllibparseparse_qsgrouprJ   formatjoinr   rf   )r.   r   mobjr[   rK   r   r_   r   r4   r4   r5   rO      s    
,zFC2EmbedIE._real_extractN)rP   rQ   rR   rS   rT   _TESTrO   r4   r4   r4   r5   rW      s    rW   c                   @   s6   e Zd ZdZdZdddddddd	d
gZdd ZdS )	FC2LiveIEz#https?://live\.fc2\.com/(?P<id>\d+)zfc2:livezhttps://live.fc2.com/57892267/57892267u   どこまで・・・u   あつあげzre:https?://.+fc2.+)r   r   uploaderuploader_idr   
livestream)r   r   r#   c                 C   sj  |  |}| d| d|}| ddd | jd|tddd|ddd	}| jd
|dt|dd|d d d dddddddid}t|dthfrTd|vrTt|d| dd|d  t|d d|d i}d }| j	t
|ddid |d!d"}| d# 	 | }	|	sq}| j|	|d%d&}
|
rt|
tsq}|
d'd(krnq~|d) 	 | }	|	sq| j|	|d%d&}
|
rt|
tsq|
d'd*kr|
d+d,kr| d- |
}n| d.|	d d/ t|	d/krd0nd q|std1g }|d2  D ]3\}}t|tsq|D ]&}|dd3kr&d4|dv r&|| j|d |d5|d$d|d6d7 qq|D ]}|d8|d9 q+| jd:|d;d%d&}|sU| j|d<d%d&}|rUtd=d|}d }|rp| jd>|d?d d@dA}|rpt|rp|\}}| jdB|dCd%d&p{d }|rtdDdE|}| t ||}||pt|dF| jdG|dHd%d&pt|dI||pt|d'|t|dJd$dKS )LNzhttps://live.fc2.com/r`   zlive.fc2.comzjs-player_size1z&https://live.fc2.com/api/memberApi.php)channelprofileuserstreamidzRequesting member info)datar)   z-https://live.fc2.com/api/getControlServer.phpzDownloading ControlServer dataplay rz   channel_dataversionz
2.1.0
 [1]pcbrowser_hls)
channel_idmodeorzchannel_versionclient_versionclient_type
client_appipv6zX-Requested-WithXMLHttpRequest)r)   rz   headersstatuscontrol_token)rK   l_ortknorz_rawr   Originzhttps://live.fc2.com)r   z(Fetching HLS playlist info via WebSocketr;   zSending HLS server requestTFr8   nameconnect_completez4{"name":"get_hls_information","arguments":{},"id":1}
_response_r   r   GoodbyezServer said: {}{}d   z...z/Unable to fetch HLS playlist info via WebSocket	argumentsr   master_playlistr   )r   Referer)r   m3u8_idliver   fc2_live)rA   ws)zog:titleztwitter:titlez
live titlez
html titlez
\s+-\s+.+$z^(.+?)\s*\[(.+?)\]$ztitle and uploader)r   r   )r:   rk   z2(?s)liveInfoView\s*:\s*({.+?}),\s*premiumStateViewz	user infoz\$\(.+?\)[^,]+,z"",r   )zog:descriptionztwitter:descriptionzlive descriptioninfothumb)r   r   r   formatsrr   rs   r   is_live)!rB   r,   _set_cookierI   r   r	   intr   r
   _request_webpager   write_debugrecv_parse_json
isinstancedictrJ   sendrl   lenr   itemslistextend_extract_m3u8_formatsupdate_html_search_meta_html_extract_titleresub_search_regexallr   )r.   r   rK   rL   
member_apicontrol_serverws_urlplaylist_datar   r   rz   r   r   	playlistsplfmtr   rr   matchlive_info_viewr4   r4   r5   rO      s   
	




*
 	zFC2LiveIE._real_extractN)rP   rQ   rR   rS   rT   rV   rO   r4   r4   r4   r5   rp      s    rp   )r   urllib.parserh   commonr   
networkingr   utilsr   r   r   r	   r
   r   r   r   rW   rp   r4   r4   r4   r5   <module>   s    $p&