U
    gM)                     @   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 G dd deZG d	d
 d
eZG dd deZdS )    N   )InfoExtractor   )Request)ExtractorError
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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>[^/]+)Zfc2z0http://video.fc2.com/en/content/20121103kUan1KHsZ a6ebe8ebe0396518689d963774a54eb7Z20121103kUan1KHsflvzBoxing again with Puff)idexttitleurlmd5	info_dictz1http://video.fc2.com/en/content/20150125cEva0hDn/Z20150125cEva0hDnmp4)r   r   zytdl@yt-dl.orgz(snip))usernamepasswordzrequires actual password)r   r   paramsskipz2http://video.fc2.com/en/a/content/20130926eZpARwsFT)r   Zonly_matchingc                 C   s   |   \}}|d ks|d kr dS ||ddd}t|}td|}| j|d ddd}d	|krh| d
 dS td}| j|d ddd dS )NFvideoz Login )emailr   doneZSubmitzAhttps://secure.id.fc2.com/index.php?mode=login&switch_language=enz
Logging inzUnable to log in)noteZ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)Z_get_login_infor
   r   _download_webpageZreport_warning)selfr   r   Zlogin_form_strsZ
login_datarequestZlogin_resultsZlogin_redir r!   8/tmp/pip-unpacked-wheel-q5ljy6pj/yt_dlp/extractor/fc2.py_login,   s2     
   zFC2IE._loginc           	      C   s   |  |}|   d }|ds>| ||}| j  |   d\}}}|d k	rz| jd|ddd}| |}| j|d d}| j	d| d	|d
d}t
|d}|stdtd|}|||d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   )ZplaylistZnqzUnable to extract video URLzhttps://video.fc2.com/r   Zm3u8_native)r   r   r   r   protocoldescription	thumbnail)	_match_idr#   
startswithr   	cookiejarclear_session_cookiesZ_html_search_regexZ_og_search_thumbnailZ_og_search_description_download_jsonr   r   r   )	r   r   video_idwebpager   r+   r*   ZvidplaylistZvid_urlr!   r!   r"   _real_extractI   sD    



 

 

zFC2IE._real_extractN)	__name__
__module____qualname__
_VALID_URLIE_NAMEZ_NETRC_MACHINE_TESTSr#   r3   r!   r!   r!   r"   r      s2   	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【吹替】Z b8aae5334cb691bdb1193a88a6ab5d5aZ201403223kCqB3Ezr   u;   プリズン･ブレイク 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 }|rd|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/      Zurl_transparentr$   )_typeie_keyr   r   r+   )
Z_match_valid_urlurllibparseparse_qsgroupgetformatjoinr   rE   )r   r   Zmobjr;   r1   r   r?   r+   r!   r!   r"   r3      s"    
 *zFC2EmbedIE._real_extractN)r4   r5   r6   r7   r8   Z_TESTr3   r!   r!   r!   r"   r:   q   s   r:   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/Z57892267u   どこまで・・・u   あつあげzre:https?://.+fc2.+)r   r   uploaderuploader_idr+   Z
livestream)r   r   r   c                 C   sd  |  |}| 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}| dd|d  t|d d|d i}d }| jt|ddid|dd }| d! |	 }	|	sq| j
|	|d"d#}
|
rt|
tsq|
d$d%krؐqq|d& |	 }	|	s8q&| j
|	|d"d#}
|
r&t|
ts^q&|
d$d'kr|
d(d)kr| d* |
}q| d+|	d d, t|	d,krd-nd q&|std.g }|d/  D ]l\}}t|tsq|D ]L}|d0d1krd2|dkr|| j|d |d3|d4d|d5d6 qq|D ]}|d7|d8 qP| jd9|d:d"d#}|s| j|d;d"d#}|rtd<d|}d }|r| jd=|d>d d?d@}|rt|r|\}}| jdA|dBd"d#pd }|rtdCdD|}| 
t||}||p&t|dE| jdF|dGd"d#pBt|dH||pRt|d$|t|dId4dJS )KNzhttps://live.fc2.com/r@   zlive.fc2.comzjs-player_size1z&https://live.fc2.com/api/memberApi.php)ZchannelZprofileuserZstreamidzRequesting member info)datar   z-https://live.fc2.com/api/getControlServer.phpzDownloading ControlServer dataZplay rR   Zchannel_dataversionz
2.1.0
 [1]ZpcZbrowser_hls)Z
channel_idmodeZorzZchannel_versionclient_versionZclient_typeZ
client_appZipv6zX-Requested-WithZXMLHttpRequest)r   rR   headersZl_ortknZorz_rawr   Zcontrol_tokenOriginzhttps://live.fc2.com)rW   z(Fetching HLS playlist info via WebSocketr(   zSending HLS server requestFr%   nameZconnect_completez4{"name":"get_hls_information","arguments":{},"id":1}Z
_response_r   r   ZGoodbyezServer said: {}{}d   z...z/Unable to fetch HLS playlist info via WebSocket	argumentsstatusr   Zmaster_playlistr   T)rX   ZReferer)r   Zm3u8_idZliverW   Zfc2_live)r)   ws)zog:titleztwitter:titlez
live titlez
html titlez
\s+-\s+.+$z^(.+?)\s*\[(.+?)\]$ztitle and uploader)r   r   )r'   rI   z2(?s)liveInfoView\s*:\s*({.+?}),\s*premiumStateViewz	user infoz\$\(.+?\)[^,]+,z"",r   )zog:descriptionztwitter:descriptionzlive descriptioninfoZthumb)r   r   r*   formatsrN   rO   r+   Zis_live)r,   r   Z_set_cookier0   r
   r	   Z_request_webpager   Zwrite_debugrecvZ_parse_json
isinstancedictrJ   sendrK   lenr   itemslistextendZ_extract_m3u8_formatsupdateZ_html_search_metaZ_html_extract_titleresubZ_search_regexallr   r   )r   r   r1   r2   Z
member_apiZcontrol_serverZws_urlZplaylist_datar]   r`   rR   r_   rY   Z	playlistsplfmtr   rN   matchZlive_info_viewr!   r!   r"   r3      s    
    	  

 
0       zFC2LiveIE._real_extractN)r4   r5   r6   r7   r8   r9   r3   r!   r!   r!   r"   rM      s   rM   )ri   urllib.parserF   commonr   Z
networkingr   utilsr   r   r   r	   r
   r   r   r:   rM   r!   r!   r!   r"   <module>   s    
a&