U
    g+E                     @   s   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 ddlmZ G dd	 d	eZG d
d de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OnDemandPagedListUserNotLivedetermine_extfilter_dictint_or_none
orderedSetunified_timestampurl_or_noneurlencode_postdataurljoin)traverse_objc                   @   s"   e Zd ZdZdd ZdddZdS )AfreecaTVBaseIEZ	afreecatvc                 C   s   dd||dddd}| j dd dt|d	}d
ddddddddddddd}t|d}|dkr||d}td| j d| ddd S )NloginjsonfalseZ	oLoginRet )ZszWorkZszTypeZszUidZ
szPasswordZisSaveIdZszScriptVarZszActionz0https://login.sooplive.co.kr/app/LoginAction.phpz
Logging indatazMYour account has been suspended due to a violation of our terms and policies.z:https://member.sooplive.co.kr/app/user_delete_progress.phpz8https://login.sooplive.co.kr/membership/changeMember.phpzHello! Soop here.
The username you have entered belongs to 
 an account that requires a legal guardian's consent. 
If you wish to use our services without restriction, 
please make sure to go through the necessary verification process.z5https://member.sooplive.co.kr/app/pop_login_block.phpz5https://login.sooplive.co.kr/afreeca/second_login.phpz3https://member.sooplive.co.kr/app/user_security.phpzCThe username does not exist or you have entered the wrong password.z4You have entered your username/password incorrectly.z>You cannot use your Global Soop account to access Korean Soop.zSorry for the inconvenience. 
Your account has been blocked due to an unauthorized access. 
Please contact our Help Center for assistance.z:You have failed to log in. Please contact our Help Center.)iiiir   iiiRESULTr   zYou have failed to log in.zUnable to login: z said: Texpected)_download_jsonr   r   getr   IE_NAME)selfusernamepasswordZ
login_formresponseZ_ERRORSresulterror r*   >/tmp/pip-unpacked-wheel-q5ljy6pj/yt_dlp/extractor/afreecatv.py_perform_login   sF    
  zAfreecaTVBaseIE._perform_loginNc              	   C   s(   |  td| |||ddid|ddS )Nzhttps://api.m.sooplive.co.kr/Z
legacy_sslT)r   headersquery
extensionszDownloading API JSONzUnable to download API JSON)r!   r   )r$   ZendpointZ
display_idr   r-   r.   r*   r*   r+   	_call_api>   s       zAfreecaTVBaseIE._call_api)NNN)__name__
__module____qualname__Z_NETRC_MACHINEr,   r0   r*   r*   r*   r+   r      s   &r   c                   @   s   e Zd ZdZdZdZ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ddddddd d!d"d#ddid$d%d&dd'd(dd'd)dd'gZd*d+ Zd,S )-AfreecaTVIEZsoopzsooplive.co.krzdhttps?://vod\.(?:sooplive\.co\.kr|afreecatv\.com)/(?:PLAYER/STATION|player)/(?P<id>\d+)/?(?:$|[?#&])z*https://vod.sooplive.co.kr/player/96753363Z20230108_9FF5BEE1_244432674_1mp4Z
rlantnghksu   페이즈으iX*  z)re:https?://videoimg\.sooplive\.co/.kr/.+Z20230108iJcu   젠지 페이즈)	idextuploader_iduploaderduration	thumbnailupload_date	timestamptitleZskip_downloadT)url	info_dictparamsz1http://vod.sooplive.co.kr/PLAYER/STATION/20515605Z!20170411_BE689A0E_190960999_1_2_hu   혼자사는여자집z0re:https?://(?:video|st)img\.sooplive\.co\.kr/.+u   ♥이슬이Zdasl8121Z20170411i	X   )	r6   r7   r>   r;   r9   r8   r<   r=   r:   z*https://vod.sooplive.co.kr/player/97267690Z20180327_27901457_202289533_1u   [생]빨개요♥ (part 1)u
   [SA]서아ZbjdyrksuZ20180327i  )r6   r7   r>   r;   r9   r8   r<   r:   The VOD does not exist)r?   r@   rA   skipz*https://vod.sooplive.co.kr/player/70395877r?   Zonly_matchingz+https://vod.sooplive.co.kr/player/104647403z*https://vod.sooplive.co.kr/player/81669846c                 C   s(  |  |}| jd|d|it|dddd }t|dthf}|dkrTtd	d
dn|dkrhtdd
dt|dthfdthfdthfdtjt	ddhfdt
hfd}g }tt|ddd fddD ]\}}|d }	t|	dkr| j|	|ddd| d d!}
n|	d"d#g}
|||d$p(| d%| |dp6d& d'| d(|
d)t|d*tjt	ddhfd+thfd, qt|d-thfd.kr|s| jd/d0d1 | d2| d0  |st|d3thfr| jd4d0d1 t|dkr|d5 d|diS t|d6d7d8|d9< | j||fd:d
i|S );Nzstation/video/a/viewReferer
   )ZnTitleNoZ	nApiLevel)r-   r   r   codeirC   Tr   izThis VOD is privater>   writer_nickZbj_idZtotal_file_duration  Zscalethumb)r>   r9   r8   r:   r;   filesc                 S   s   t |d S )Nfile)r   _vr*   r*   r+   <lambda>       z+AfreecaTVIE._real_extract.<locals>.<lambda>r   startrN   Zm3u8r5   hlszDownloading part z m3u8 information)m3u8_idnotehttp)r?   Z	format_idfile_info_keyrP   ZUntitledz (part ))r6   r>   formatsr:   Z
file_start)r:   r=   Zadult_statusZnotLoginz5Only users older than 19 are able to watch this videor&   methodzIn accordance with local laws and regulations, underage users are restricted from watching adult content. Only content suitable for all ages will be downloaded. Zsub_upload_typez This VOD is for subscribers onlyr   ).r=   F)get_allr=   Zmulti_video)	_match_idr0   r   r   intr   str	functoolspartialr   r   	enumerater	   _extract_m3u8_formatsappendr"   r   raise_login_requiredreport_warningZ_login_hintlenplaylist_result)r$   r?   video_idr   Z
error_codeZcommon_infoentriesZfile_numZfile_elementZfile_urlr\   r*   r*   r+   _real_extract   s    
      
  zAfreecaTVIE._real_extractN)r1   r2   r3   r#   IE_DESC
_VALID_URL_TESTSrn   r*   r*   r*   r+   r4   F   sv      @r4   c                   @   s>   e Zd ZdZdZdZdddiddgZd	d
 Zedd Z	dS )AfreecaTVCatchStoryIEzsoop:catchstoryzsooplive.co.kr catch storyzOhttps?://vod\.(?:sooplive\.co\.kr|afreecatv\.com)/player/(?P<id>\d+)/catchstoryz3https://vod.sooplive.co.kr/player/103247/catchstoryr6   Z103247r   r?   r@   Zplaylist_countc                 C   s8   |  |}| jd|d|id|dd}| | ||S )Nzcatchstory/a/viewrF   r   )ZaStoryListIdxZ	nStoryIdx)r-   r.   )r`   r0   rk   _entries)r$   r?   rl   r   r*   r*   r+   rn      s    
  z#AfreecaTVCatchStoryIE._real_extractc                 c   s~   t | ddd ddd dddthfddd	thfddd
tjtddhfdthfdthfdthfdthfdthfdfE d H  d S )Nr   c                 S   s   |d dkS )NZ
story_typeZcatchr*   rO   r*   r*   r+   rR      rS   z0AfreecaTVCatchStoryIE._entries.<locals>.<lambda>Z
catch_listc                 S   s   |d d d S )NrM   r   rN   r*   rO   r*   r*   r+   rR      rS   rM   r   rZ   rN   r:   rJ   rK   r>   rI   Z	writer_idrL   Zwrite_timestamp)r6   r?   r:   r>   r9   r8   r;   r=   )r   rb   r   rc   rd   r   r   r*   r*   r+   rt      s      zAfreecaTVCatchStoryIE._entriesN)
r1   r2   r3   r#   ro   rp   rq   rn   staticmethodrt   r*   r*   r*   r+   rr      s    rr   c                	   @   st   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gZdZddddgZdddddgZ	dd Z
dd ZdS ) AfreecaTVLiveIEz	soop:livezsooplive.co.kr livestreamszThttps?://play\.(?:sooplive\.co\.kr|afreecatv\.com)/(?P<id>[^/?#]+)(?:/(?P<bno>\d+))?z-https://play.sooplive.co.kr/pyh3646/237852185Z	237852185r5   u,   【 우루과이 오늘은 무슨일이? 】u   박진우[JINU]Zpyh3646iaT)r6   r7   r>   r9   r8   r=   is_livezLivestream has ended)r?   r@   rD   rE   z#https://play.sooplive.co.kr/pyh3646z7https://live.sooplive.co.kr/afreeca/player_live_api.phpZgcp_cdnZgs_cdn_pc_appZgs_cdn_mobile_webZgs_cdn_pc_webZgs_cdnZgs_cdn_chromecastZ	azure_cdnZaws_cfZkt_cdnc                    s  | dpd}tt|dtht fddf j} d|}t|ddD ]\}}t jt	|d	|d
| dd| dd|| ddddt
hf}	z$ j|	|ddd|iddidW   S  tk
r }
 z:|t|krނ  |
jp|
j d| dt| d W 5 d }
~
X Y qLX qLd S )NZRMDz)https://livestream-manager.sooplive.co.krZCDNc                    s
   | j kS )N)	_BAD_CDNSrO   r$   r*   r+   rR   "  rS   z2AfreecaTVLiveIE._extract_formats.<locals>.<lambda>Zcdnr   rT   zbroad_stream_assign.htmlDownloading z stream infozUnable to download Fz-common-master-hls)return_typeZ	broad_key)fatalr.   Zview_urlr5   rV   aidrF   zhttps://play.sooplive.co.kr/)rW   r.   r-   z. Retrying... (attempt z of r[   )r"   r   r   rb   all_WORKING_CDNSZ_configuration_argre   r!   r   r   rf   r   rj   ri   causemsg)r$   channel_infobroadcast_nor}   Zstream_base_urlZdefault_cdn_idsZcdn_idsattemptZcdn_idZm3u8_urler*   ry   r+   _extract_formats  sD     
 
     z AfreecaTVLiveIE._extract_formatsc                 C   s  |  |dd\}}t| j| j|td|iddthfp>i }|dpL|}|dpZ|}|s|d}|d	kr~t|d
n|dkr| j	ddd t
d| d}|ddkr|d krt
dddt| j| j|ddtt|ddd|dddthfpi }|d}|sV|d}|d	kr6t
dddn|dkrN| j	ddd t
d| |||}	t| jd |d!d"d#|id$d%thpi }
||d&p|
d'|d(p|
d)|t|
d*|	dd+|id,S )-Nr6   bnobidr   ZCHANNELZBJIDZBNOr   r   )rl   r   z.This channel is streaming for subscribers onlyr&   r]   z"Unable to extract broadcast numberZvideopasswordZBPWDYzKThis livestream is protected by a password, use the --video-password optionTr   z#Downloading access token for streamz*Unable to download access token for streamcommonr}   Zmaster)r   Zstream_typetypeZqualitypwdZAIDzThis livestream has endedz'This livestream is for subscribers onlyzUnable to extract access tokenz4https://st.sooplive.co.kr/api/get_station_status.phpzDownloading channel metadataz#Unable to download channel metadataZszBjIdF)r.   r|   ZTITLEZstation_titleZBJNICKZstation_nameZbroad_startrF   )r6   r>   r9   r8   r=   r\   rw   Zhttp_headers)_match_valid_urlgroupr   r!   _LIVE_API_URLr   dictr"   r   rh   r   	get_paramr
   r   r   )r$   r?   Zbroadcaster_idr   r   r(   r&   Z
token_infor}   r\   Zstation_infor*   r*   r+   rn   9  s      

 
   	



   zAfreecaTVLiveIE._real_extractN)r1   r2   r3   r#   ro   rp   rq   r   r   rx   r   rn   r*   r*   r*   r+   rv      sF   	rv   c                   @   st   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dddd	gZdZdd Zdd ZdS )AfreecaTVUserIEz	soop:userz^https?://ch\.(?:sooplive\.co\.kr|afreecatv\.com)/(?P<id>[^/?#]+)/vods/?(?P<slug_type>[^/?#]+)?z.https://ch.sooplive.co.kr/ryuryu24/vods/reviewZplaylistZryuryu24zryuryu24 - review)_typer6   r>      rs   z3https://ch.sooplive.co.kr/parang1995/vods/highlightZ
parang1995zparang1995 - highlighti  z'https://ch.sooplive.co.kr/ryuryu24/vodszryuryu24 - all   z3https://ch.sooplive.co.kr/ryuryu24/vods/balloonclipzryuryu24 - balloonclipr   <   c                 c   sn   |d7 }| j d| d| ||| jddd| d| d}|d	 D ]$}| d
|d  dt|d V  qDd S )Nr   z!https://chapi.sooplive.co.kr/api/z/vods/Zreg_date)pageZper_pageZorderbyrz   z video page )r.   rX   r   z"https://vod.sooplive.co.kr/player/Ztitle_no/)r!   	_PER_PAGEZ
url_resultr4   )r$   user_id	user_typer   infoitemr*   r*   r+   _fetch_page  s      zAfreecaTVUserIE._fetch_pagec                 C   sN   |  |dd\}}|pd}tt| j||| j}| ||| d| S )Nr6   Z	slug_typer~   z - )r   r   r   rc   rd   r   r   rk   )r$   r?   r   r   rm   r*   r*   r+   rn     s    zAfreecaTVUserIE._real_extractN)	r1   r2   r3   r#   rp   rq   r   r   rn   r*   r*   r*   r+   r   u  sD   			!	r   )rc   r   r   Z
networkingr   utilsr   r   r   r	   r
   r   r   r   r   r   r   Zutils.traversalr   r   r4   rr   rv   r   r*   r*   r*   r+   <module>   s   41 %