o
    iF                     @   s   d dl Z 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 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filter_dictint_or_noneparse_qsstr_or_nonetraverse_objunified_timestampurl_or_nonec                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )NiconicoChannelPlusBaseIEhttps://nicochannel.jpc                 K   s   | j d| fd|i|S )Nz"https://nfc-api.nicochannel.jp/fc/video_id)_download_json)selfpathitem_idkwargs r   X/home/ubuntu/.local/lib/python3.10/site-packages/yt_dlp/extractor/niconicochannelplus.py	_call_api   s   z#NiconicoChannelPlusBaseIE._call_apic                    sX   j dd  dddd d }t| fdd	d
fdd}|s*td  ddd|S )Nzcontent_providers/channelsz	channels/zFetching channel listzUnable to fetch channel list)r   noteerrnotedatacontent_providersc                    s   |d j  d  kS )Ndomain/)_WEBPAGE_BASE_URL)_vchannel_namer   r   r   <lambda>   s    zANiconicoChannelPlusBaseIE._find_fanclub_site_id.<locals>.<lambda>idFget_allzChannel z does not existT)expected)r   r   r   )r   r#   fanclub_list_json
fanclub_idr   r"   r   _find_fanclub_site_id   s    
z/NiconicoChannelPlusBaseIE._find_fanclub_site_idc                 C   s4   t | jd| dd| ddddddthfpi S )	Nfanclub_sites/z/page_base_infozFetching channel base infoz!Unable to fetch channel base infoF)r   r   r   fatalr   fanclub_site)r   r   dictr   fanclub_site_idr   r   r   _get_channel_base_info%   s   
z0NiconicoChannelPlusBaseIE._get_channel_base_infoc              
   C   sB   t | jd| dd| dddtdddd	d
thfp i S )Nr,   z
/user_infozFetching channel user infoz!Unable to fetch channel user infoFnullascii)r   r   r   r-   r   r   r.   )r   r   jsondumpsencoder/   r0   r   r   r   _get_channel_user_info+   s   
z0NiconicoChannelPlusBaseIE._get_channel_user_infoN)__name__
__module____qualname__r   r   r+   r2   r8   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eeg ddddiddddddddddddeeg dd ddidgZd!d" Zd#d$ Z	d%d& Z
d'S )(NiconicoChannelPlusIENiconicoChannelPlusu$   ニコニコチャンネルプラスzLhttps?://nicochannel\.jp/(?P<channel>[\w.-]+)/(?:video|live)/(?P<code>sm\w+)z<https://nicochannel.jp/kaorin/video/smsDd8EdFLcVZk9yyAhD6H7HsmsDd8EdFLcVZk9yyAhD6H7Hu*   前田佳織里はニコ生がしたい！mp4u$   前田佳織里の世界攻略計画kaorinzhttps://nicochannel.jp/kaorinnot_livezIhttps://nicochannel.jp/public_html/contents/video_pages/74/thumbnail_pathu|   ２０２１年１１月に放送された
「前田佳織里はニコ生がしたい！」アーカイブになります。i+ai  20220105)r%   titleextchannel
channel_idchannel_urllive_status	thumbnaildescription	timestampdurationcomment_count
view_counttagsupload_dateskip_downloadT)url	info_dictparamsz=https://nicochannel.jp/testman/video/smDXbcrtyPNxLx9jc4BW69VesmDXbcrtyPNxLx9jc4BW69Veztest oshirou-   本番チャンネルプラステストマンtestmanzhttps://nicochannel.jp/testman   was_liveifRciQ 20221021)r%   rC   rD   rE   rF   rG   	age_limitrH   rK   rL   rM   rN   rO   rP   c                 C   s`  |  |dd\}}| |}| jd| |ddidddd	 d
 }| ||\}}|d}g }	|dkrJ|r?d| d}
nd}
| j|
d|d n| j|d d j|d|d}	||	d| 	|d|| j
 d| t| |d|t|d	t|dthfdthfdthfdthfdd thfd!d"thfd!d#thfd$d%d&thfd'd(| j|t|d)d*iS )+NcoderE   video_pages/fc_use_devicer3   zFetching video page infozUnable to fetch video page info)r   headersr   r   r   
video_pagelive_scheduled_start_atis_upcomingzThis live event will begin at z UTCzThis event has not started yetT)r(   r   video_streamauthenticated_url)
session_id)m3u8_urlr   )tbrvcodecacodecfanclub_site_namer   )content_providerrZ   )	r%   formats_format_sort_fieldsrE   rF   rG   rZ   rH   release_timestamprC   thumbnail_urlrJ   released_atactive_video_filenamelengthvideo_aggregate_infonumber_of_commentstotal_views
video_tags.tag)rC   rI   rJ   rK   rL   rM   rN   rO   __post_extractor)video_comment_settingcomment_group_id)content_codery   )_match_valid_urlgroupr+   r   _get_live_status_and_session_idgetraise_no_formats_extract_m3u8_formatsformatr2   r   r   r8   r   strr   r   extract_comments)r   rR   rz   rF   r1   	data_jsonrH   rd   release_timestamp_strrk   msgr   r   r   _real_extracte   s`   





z#NiconicoChannelPlusIE._real_extractc                 c   s    | d}|s
d S | j d| d|dddd d }| jd	|d
dddidt| ddgd p1ddt||ddd}t|dD ]%}t|dthfdt	hfdt	hfdthfdt
hfddd hfdd d!V  qEd S )"Nz	/commentsr\   z/comments_user_tokenzGetting comment tokenzUnable to get comment token)r   r   r   access_tokenz,https://comm-api.sheeta.com/messages.historyzFetching commentszUnable to fetch commentsContent-Typeapplication/jsonascmax_comments r   x   )sort_directionlimit)tokengroup_idr4   )r   r   r   r^   queryr   .nickname	sender_idr%   message)
updated_atsent_at
created_atc                 S   s   | dkS )Nz-1r   )xr   r   r   r$      s    z5NiconicoChannelPlusIE._get_comments.<locals>.<lambda>)author	author_idr%   textrK   author_is_uploaderFr&   )r   r   r   _configuration_argr5   r6   r7   r   r   r
   r   )r   rz   ry   r   comment_access_tokencomment_listcommentr   r   r   _get_comments   sJ   

z#NiconicoChannelPlusIE._get_commentsc           
      C   s  | d}| d}i }|dkr|rd}nEd}nB|dkrP| ds#dS |s(d	}n2d}d
di}t|d}t|d}| d| d| d |rH|sOtd|ddn
td| |dd| | d| d|  | jd| d| dt|dddddd d!d"d# d$ }	||	fS )%Ntypelive_finished_atvodrX   rA   livelive_started_at)ra   r   is_livebroadcast_typedvr)videoallow_dvr_flg)r   convert_to_vod_flgzallow_dvr_flg = z, convert_to_vod_flg = .z/Live was ended, there is no video for download.T)r   r(   zUnknown type: Fz: video_type=z, live_status=r\   z/session_idsz/sessionr4   r   r3   r   )r   r]   originzGetting session idzUnable to get session id)r   r   r^   r   r   r   rd   )r~   r   write_debugr   r   r5   r6   r7   )
r   rz   r   
video_typer   payloadrH   video_allow_dvr_flgvideo_convert_to_vod_flgrd   r   r   r   r}      sL   





z5NiconicoChannelPlusIE._get_live_status_and_session_idN)r9   r:   r;   IE_NAMEIE_DESC
_VALID_URLint_TESTSr   r   r}   r   r   r   r   r<   3   s^    .4"r<   c                   @   s   e Zd ZdZdd ZdS ) NiconicoChannelPlusChannelBaseIE   c              	   c   s|    | j ||i ||d | jdddid|d  dd|d  dd}t|d	D ]}| | j d
| d| tV  q)d S )Nr   )pageper_pager]   r3   zGetting channel info (page )z!Unable to get channel info (page )r   r^   r   r   )r   video_pageslist.rz   r   z/video/)r   
_PAGE_SIZEr   
url_resultr   r<   )r   r   r   r#   r   r   responserz   r   r   r   _fetch_paged_channel_video_list   s"   
z@NiconicoChannelPlusChannelBaseIE._fetch_paged_channel_video_listN)r9   r:   r;   r   r   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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d ZdS )"NiconicoChannelPlusChannelVideosIEz"NiconicoChannelPlus:channel:videosug   ニコニコチャンネルプラス - チャンネル - 動画リスト. nicochannel.jp/channel/videosz=https?://nicochannel\.jp/(?P<id>[a-z\d\._-]+)/videos(?:\?.*)?z%https://nicochannel.jp/testman/videosztestman-videosu4   本番チャンネルプラステストマン-videosr%   rC   rW   rR   rS   playlist_mincountz'https://nicochannel.jp/testtarou/videosztesttarou-videosu.   チャンネルプラステスト太郎-videosr   z'https://nicochannel.jp/testjirou/videosztestjirou-videosu.   チャンネルプラステスト二郎-videosr   zEhttps://nicochannel.jp/testman/videos?tag=%E6%A4%9C%E8%A8%BC%E7%94%A8   z/https://nicochannel.jp/testman/videos?vodType=1z7https://nicochannel.jp/testman/videos?sort=-released_atzOhttps://nicochannel.jp/testman/videos?tag=%E6%A4%9C%E8%A8%BC%E7%94%A8&vodType=1zWhttps://nicochannel.jp/testman/videos?tag=%E6%A4%9C%E8%A8%BC%E7%94%A8&sort=-released_atzAhttps://nicochannel.jp/testman/videos?vodType=1&sort=-released_atzahttps://nicochannel.jp/testman/videos?tag=%E6%A4%9C%E8%A8%BC%E7%94%A8&vodType=1&sort=-released_atc                 C   s   |  |}| |}| |d}t|}| jtt| j	d| dt
t|dt|dddt|dd	dd
|| d| j| d| ddS )u  
        API parameters:
            sort:
                -released_at         公開日が新しい順 (newest to oldest)
                 released_at         公開日が古い順 (oldest to newest)
                -number_of_vod_views 再生数が多い順 (most play count)
                 number_of_vod_views コメントが多い順 (most comments)
            vod_type (is "vodType" in "url"):
                0 すべて (all)
                1 会員限定 (members only)
                2 一部無料 (partially free)
                3 レンタル (rental)
                4 生放送アーカイブ (live archives)
                5 アップロード動画 (uploaded videos)
        ri   r,   z/video_pages)rv   r   )sortr   z-released_at)default)vodTyper   0)rv   r   vod_typez/videosz-videosplaylist_idplaylist_title)	_match_idr+   r2   r~   r	   playlist_resultr   	functoolspartialr   r   r   r   )r   rR   rF   r1   r#   qsr   r   r   r   S  s&   



z0NiconicoChannelPlusChannelVideosIE._real_extractNr9   r:   r;   r   r   r   r   r   r   r   r   r   r      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d	gZdd ZdS )!NiconicoChannelPlusChannelLivesIEz!NiconicoChannelPlus:channel:livesui   ニコニコチャンネルプラス - チャンネル - ライブリスト. nicochannel.jp/channel/livesz3https?://nicochannel\.jp/(?P<id>[a-z\d\._-]+)/livesz$https://nicochannel.jp/testman/livesztestman-livesu3   本番チャンネルプラステストマン-livesr   rW   r   z&https://nicochannel.jp/testtarou/livesztesttarou-livesu-   チャンネルプラステスト太郎-livesr   z&https://nicochannel.jp/testjirou/livesztestjirou-livesu-   チャンネルプラステスト二郎-livesr   c              
   C   sj   |  |}| |}| |d}| jtt| jd| dddi|| d| j	| d| ddS )	u  
        API parameters:
            live_type:
                1 放送中 (on air)
                2 放送予定 (scheduled live streams, oldest to newest)
                3 過去の放送 - すべて (all ended live streams, newest to oldest)
                4 過去の放送 - 生放送アーカイブ (all archives for live streams, oldest to newest)
            We use "4" instead of "3" because some recently ended live streams could not be downloaded.
        ri   r,   z/live_pages	live_type   z/livesz-livesr   )
r   r+   r2   r~   r   r   r   r   r   r   )r   rR   rF   r1   r#   r   r   r   r     s   


z/NiconicoChannelPlusChannelLivesIE._real_extractNr   r   r   r   r   r   w  s0    r   )r   r5   commonr   utilsr   r   r   r   r	   r
   r   r   r   r   r<   r   r   r   r   r   r   r   <module>   s    ,! 7z