U
    MfK                     @   s   d dl 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   )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S )AfreecaTVBaseIE	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szActionz/https://login.afreecatv.com/app/LoginAction.phpz
Logging indatazMYour account has been suspended due to a violation of our terms and policies.z9https://member.afreecatv.com/app/user_delete_progress.phpz7https://login.afreecatv.com/membership/changeMember.phpzHello! AfreecaTV 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.z4https://member.afreecatv.com/app/pop_login_block.phpz4https://login.afreecatv.com/afreeca/second_login.phpz2https://member.afreecatv.com/app/user_security.phpzCThe username does not exist or you have entered the wrong password.z4You have entered your username/password incorrectly.zHYou cannot use your Global AfreecaTV account to access Korean AfreecaTV.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-zk9bgjdq/yt_dlp/extractor/afreecatv.py_perform_login   sF    
  zAfreecaTVBaseIE._perform_loginN)__name__
__module____qualname__Z_NETRC_MACHINEr,   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ddd	d
d
ddddddddddddddddgd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d0d1d2d&d3d4d5dd6d7d8d9d:d;d<d=	d%d&id'd>d&d3d?d&d3d@d&d3g	ZdAdB ZdCS )DAfreecaTVIEr   zafreecatv.coma
  (?x)
                    https?://
                        (?:
                            (?:(?:live|afbbs|www)\.)?afreeca(?:tv)?\.com(?::\d+)?
                            (?:
                                /app/(?:index|read_ucc_bbs)\.cgi|
                                /player/[Pp]layer\.(?:swf|html)
                            )\?.*?\bnTitleNo=|
                            vod\.afreecatv\.com/(PLAYER/STATION|player)/
                        )
                        (?P<id>\d+)/?(?:$|[?#&])
                    zhttp://live.afreecatv.com:8079/app/index.cgi?szType=read_ucc_bbs&szBjId=dailyapril&nStationNo=16711924&nBbsNo=18605867&nTitleNo=36164052&szSkin=Z f72c89fe7ecc14c1b5ce506c4996046eZ36164052mp4u.   데일리 에이프릴 요정들의 시상식!z.re:^https?://(?:video|st)img.afreecatv.com/.*$Z
dailyaprilZ20160503)idexttitle	thumbnailuploaderuploader_idupload_datezVideo is gone)urlmd5	info_dictskipz|http://afbbs.afreecatv.com:8080/app/read_ucc_bbs.cgi?nStationNo=16711924&nTitleNo=36153164&szBjId=dailyapril&nBbsNo=18605867Z36153164u5   BJ유트루와 함께하는 '팅커벨 메이크업!')r2   r4   r5   r6   r7   r   Z d8b7c174568da61d774ef0203159bf97Z
36153164_1Z20160502)r2   r3   r4   r8   )r:   r;   Z 58f2ce7f6044e34439ab2d50612ab02bZ
36153164_2)r9   r;   playlist_countplaylistr<   z0http://vod.afreecatv.com/PLAYER/STATION/20515605Z!20170411_BE689A0E_190960999_1_2_hu   혼자사는여자집u   ♥이슬이Zdasl8121Z20170411i	X   )	r2   r3   r4   r5   r6   r7   r8   	timestampdurationZskip_downloadT)r9   r;   paramsz)https://vod.afreecatv.com/player/97267690Z20180327_27901457_202289533_1u   [생]빨개요♥ (part 1)u
   [SA]서아ZbjdyrksuZ20180327i  )r2   r3   r4   r5   r6   r7   r8   rA   The VOD does not exist)r9   r;   rB   r<   zxhttp://www.afreecatv.com/player/Player.swf?szType=szBjId=djleegoon&nStationNo=11273158&nBbsNo=13161095&nTitleNo=36327652r9   Zonly_matchingz)https://vod.afreecatv.com/player/96753363Z20230108_9FF5BEE1_244432674_1Z
rlantnghksu   페이즈으iX*  z're:https?://videoimg\.afreecatv\.com/.+Z20230108iJcu   젠지 페이즈)	r2   r3   r7   r6   rA   r5   r8   r@   r4   z)https://vod.afreecatv.com/player/70395877z*https://vod.afreecatv.com/player/104647403z)https://vod.afreecatv.com/player/81669846c                 C   s,  |  |}| jd|d|it|ddddd }t|dthf}|d	krVtd
ddn|dkrjt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p:d& 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 );Nz0https://api.m.afreecatv.com/station/video/a/viewReferer
   )ZnTitleNoZ	nApiLevelT)headersr   impersonater   codeirC   r   izThis VOD is privater4   writer_nickZbj_idZtotal_file_duration  Zscalethumb)r4   r6   r7   rA   r5   filesc                 S   s   t |d S )Nfile)r   _vr*   r*   r+   <lambda>       z+AfreecaTVIE._real_extract.<locals>.<lambda>r   startrO   Zm3u8r1   hlszDownloading part z m3u8 information)m3u8_idnotehttp)r9   Z	format_idfile_info_keyrQ   ZUntitledz (part ))r2   r4   formatsrA   Z
file_start)rA   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_idr!   r   r   intr   str	functoolspartialr
   r   	enumerater   _extract_m3u8_formatsappendr"   r   raise_login_requiredreport_warningZ_login_hintlenplaylist_result)r$   r9   video_idr   Z
error_codeZcommon_infoentriesZfile_numZfile_elementZfile_urlr]   r*   r*   r+   _real_extract   s    
      
  zAfreecaTVIE._real_extractN)r-   r.   r/   r#   IE_DESC
_VALID_URL_TESTSro   r*   r*   r*   r+   r0   >   s   		   lr0   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afreecatv:catchstoryzafreecatv.com catch storyz:https?://vod\.afreecatv\.com/player/(?P<id>\d+)/catchstoryz2https://vod.afreecatv.com/player/103247/catchstoryr2   Z103247r   r9   r;   r=   c                 C   s:   |  |}| jd|d|id|ddd}| | ||S )Nz-https://api.m.afreecatv.com/catchstory/a/viewrE   r   )ZaStoryListIdxZ	nStoryIdxT)rG   queryrH   )ra   r!   rl   _entries)r$   r9   rm   r   r*   r*   r+   ro     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*   rP   r*   r*   r+   rS     rT   z0AfreecaTVCatchStoryIE._entries.<locals>.<lambda>Z
catch_listc                 S   s   |d d d S )NrN   r   rO   r*   rP   r*   r*   r+   rS     rT   rN   r   r[   rO   rA   rK   rL   r4   rJ   Z	writer_idrM   Zwrite_timestamp)r2   r9   rA   r4   r6   r7   r5   r@   )r   rc   r   rd   re   r
   r   r*   r*   r+   rv     s      zAfreecaTVCatchStoryIE._entriesN)
r-   r.   r/   r#   rp   rq   rr   ro   staticmethodrv   r*   r*   r*   r+   rs      s    rs   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afreecatv:livezafreecatv.com livestreamszBhttps?://play\.afreeca(?:tv)?\.com/(?P<id>[^/]+)(?:/(?P<bno>\d+))?z,https://play.afreecatv.com/pyh3646/237852185Z	237852185r1   u,   【 우루과이 오늘은 무슨일이? 】u   박진우[JINU]Zpyh3646iaT)r2   r3   r4   r6   r7   r@   is_livezLivestream has ended)r9   r;   r<   rD   z"https://play.afreecatv.com/pyh3646z6https://live.afreecatv.com/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.afreecatv.comZCDNc                    s
   | j kS )N)	_BAD_CDNSrP   r$   r*   r+   rS   Q  rT   z2AfreecaTVLiveIE._extract_formats.<locals>.<lambda>Zcdnr   rU   zbroad_stream_assign.htmlDownloading z stream infozUnable to download Fz-common-master-hls)return_typeZ	broad_key)fatalru   Zview_urlr1   rW   aidrE   zhttps://play.afreecatv.com/)rX   ru   rG   z. Retrying... (attempt z of r\   )r"   r   r   rc   all_WORKING_CDNSZ_configuration_argrf   r!   r   r   rg   r   rk   rj   causemsg)r$   channel_infobroadcast_nor   Zstream_base_urlZdefault_cdn_idsZcdn_idsattemptZcdn_idZm3u8_urler*   r{   r+   _extract_formatsL  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|}|sjt|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pi }|d}|s"|d}|dkrt
dddn|dkr| jddd t
d| |||}	t| jd|dd d!|id"d#thpVi }
||d$pn|
d%|d&p|
d'|t|
d(|	dd)|id*S )+Nr2   bnobidr   ZCHANNELZBJIDZBNO)rm   Z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AIDr   r   zThis livestream has endedr   z'This livestream is for subscribers onlyr&   r^   zUnable to extract access tokenz3https://st.afreecatv.com/api/get_station_status.phpzDownloading channel metadataz#Unable to download channel metadataZszBjIdF)ru   r~   ZTITLEZstation_titleZBJNICKZstation_nameZbroad_startrE   )r2   r4   r6   r7   r@   r]   ry   Zhttp_headers)_match_valid_urlgroupr   r!   _LIVE_API_URLr   dictr"   r   	get_paramr   r	   ri   r   r   )r$   r9   Zbroadcaster_idr   r   r&   Z
token_infor   r(   r]   Zstation_infor*   r*   r+   ro   h  s      


   	



   zAfreecaTVLiveIE._real_extractN)r-   r.   r/   r#   rp   rq   rr   r   r   rz   r   ro   r*   r*   r*   r+   rx   %  sF   	rx   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afreecatv:userzJhttps?://bj\.afreeca(?:tv)?\.com/(?P<id>[^/]+)/vods/?(?P<slug_type>[^/]+)?z-https://bj.afreecatv.com/ryuryu24/vods/reviewr>   Zryuryu24zryuryu24 - review)_typer2   r4      rt   z2https://bj.afreecatv.com/parang1995/vods/highlightZ
parang1995zparang1995 - highlighti  z&https://bj.afreecatv.com/ryuryu24/vodszryuryu24 - all   z2https://bj.afreecatv.com/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://bjapi.afreecatv.com/api/z/vods/Zreg_date)pageZper_pageZorderbyr|   z video page )ru   rY   r   z!https://vod.afreecatv.com/player/Ztitle_no/)r!   	_PER_PAGEZ
url_resultr0   )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 )Nr2   Z	slug_typer   z - )r   r   r   rd   re   r   r   rl   )r$   r9   r   r   rn   r*   r*   r+   ro     s    zAfreecaTVUserIE._real_extractN)	r-   r.   r/   r#   rq   rr   r   r   ro   r*   r*   r*   r+   r     sD   			!	r   )rd   r   r   utilsr   r   r   r   r	   r
   r   r   r   r   r   Zutils.traversalr   r   r0   rs   rx   r   r*   r*   r*   r+   <module>   s   4* C%y