o
    i<R                     @   s   d dl Zd dlZd dlZd dlZd dlZddlmZ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mZ d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SearchInfoExtractor   )ExtractorErrordetermine_extfloat_or_noneformat_fieldint_or_nonestr_or_nonetraverse_objtry_getunescapeHTMLunified_timestampurl_or_noneurlencode_postdataz8https://prod-api-v2.production.rokfin.com/api/v2/public/c                   @   s  e Zd ZdZdZdZi Zdddddd	d
dddddeedddddddddddddddeeddgdddi dd d!dd"d#d$dd%d&d'd(d)d*d+d,d-dd.d/d0d1d2d0d3d4d5d4d6d7d8d7d9eedgd:dd;i dd<d!dd"d=d$dd'd>d)d?d+d@d-dAd2dd9edBed3dCd8dDdEddFgd%dGdHdIddJi ddKd!dd"dLd$dMd'dNd)dOd+dPd-dAd2d0d9edBed3dQd5dRd6dSd8dSdEdgdgZdTdU Z	dVdW Z
dXdY ZdZd[ Zd\d] Zi i fd^d_Zd`S )aRokfinIEz@https?://(?:www\.)?rokfin\.com/(?P<id>(?P<type>post|stream)/\d+)rokfinzHhttps://secure.rokfin.com/auth/realms/rokfin-web/protocol/openid-connectzOhttps://www.rokfin.com/post/57548/Mitt-Romneys-Crazy-Solution-To-Climate-Changez
post/57548mp4z.Mitt Romney's Crazy Solution To Climate Changez*re:https://img\.production\.rokfin\.com/.+20211023itaz
Jimmy Dore65429z#https://rokfin.com/TheJimmyDoreShowpublicnot_live   )idexttitle	thumbnailupload_date	timestampchannel
channel_idchannel_urlavailabilitylive_statusdislike_count
like_countduration)url	info_dictzJhttps://rokfin.com/post/223/Julian-Assange-Arrested-Streaming-In-Real-Timezpost/223z/Julian Assange Arrested: Streaming In Real Time20190412id8\zRon Placone10zhttps://rokfin.com/RonPlaconezFreeThinkingMedia^zRealProgressives^)r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   tagszrhttps://www.rokfin.com/stream/10543/Its-A-Crazy-Mess-Regional-Director-Blows-Whistle-On-Pfizers-Vaccine-Trial-Datar   zstream/10543r   r   zR"It's A Crazy Mess" Regional Director Blows Whistle On Pfizer's Vaccine Trial Datar   descriptionz$md5:324ce2d3e3b62e659506409e458b9d8er    TLAVagabondr!   53856r"   zhttps://rokfin.com/TLAVagabondr#   is_liveFwas_liveTr$   r   iwarelease_timestamprelease_date20211102r   r%   )r&   r,   zihttps://rokfin.com/post/126703/Brave-New-World--Aldous-Huxley-DEEPDIVE--Chpts-13--Quite-Frankly--Jay-Dyerzpost/126703zQBrave New World - Aldous Huxley DEEPDIVE!  (Chpts 1-3) - Quite Frankly & Jay DyerzJay Dyer186881zhttps://rokfin.com/jaydyerpremium_onlyr&   id20230307r,   z	OpenMind^z$md5:cb04e32e68326c9b2b251b297bacff35r'   i  zJhttps://rokfin.com/stream/31332/The-Grayzone-live-on-Nordstream-blame-gamezstream/31332z*The Grayzone live on Nordstream blame gamez#re:https://image\.v\.rokfin\.com/.+zMax Blumenthal248902z https://rokfin.com/MaxBlumenthalidg  A20230310c              	   C   s~  |  |dd\}}| t | |}t|d}|dr#dn|r'dn|dkr-dnd	}t|d
dtd}|dv rNt| j	dt|ddtd|d dd d}|rUd
|igng i }}	t
|dkrm| j||d|dkd\}}	|st|ddr~| jdddd n|r| jdtj|d |dd t|dd }
|pt|d!d"pt|d#}i d|d$|d%|	d&tt|d&d'd(tt|d)d*tt|d*d+d,tt|d,d-d.t|d/d0t|d1d2tt|d3d4d5tt|d6d7d8|
rtd9|
 nd d:|d;|d	kr|nd d<t|d=tdd>|d?| jtt|ddddddd@dA|dBkr;| |iS d iS )CNr   typescheduledAt	stoppedAtr1   is_upcomingstreamr0   r   r(   )content
contentUrlexpected_type)Nz	fake.m3u8z%https?://[^/]+/([^/]+)/storyboard.vtttimelineUrl)r?   rC   defaultz#https://stream.v.rokfin.com/%s.m3u8m3u8F)fatallivepremiumPlanpremiumz-This video is only available to premium usersTcookies)methodz!Stream is offline; scheduled for z%Y-%m-%d %H:%M:%S)video_idexpected)	createdByusername)creatorrP   postedAtMillii  creationDateTimeformats	subtitlesr   r?   contentTitler'   )r?   r'   r   )r?   thumbnailUrl1r-   )r?   contentDescriptionr&   	likeCountr%   dislikeCountr    )rO   name)rQ   r\   r!   )rO   r   )rQ   r   r"   https://rokfin.com/r   r2   r,   )r,   .r   r$   r#   )needs_premium
is_privateneeds_subscription
needs_authis_unlisted__post_extractorpost)_match_valid_urlgroup!_download_json_using_access_token_API_BASE_URLr   getr   r   r	   _search_regexr   #_extract_m3u8_formats_and_subtitlesraise_login_requiredraise_no_formatsdtdatetimefromtimestampstrftimer   r   r
   _availabilityboolextract_comments)selfr(   rM   
video_typemetadata	scheduledr$   	video_urlrT   rU   uploaderr    r{   K/home/ubuntu/.local/lib/python3.10/site-packages/yt_dlp/extractor/rokfin.py_real_extract   s   
	
zRokfinIE._real_extractc                 c   s   d }t  D ]}| jt d|dd   d| d|d|d  t|d d dd	p+i }|d
p2g D ]-}t|dt|d|d|ddt|dt|dt|ddV  q3t|dpid }|d}|d
r|s|r||kr d S |dur d S qd S )Nzcomment?postId=   z&page=&size=50z!Downloading viewer comments page r    of %sFnoterG   r?   commentr\   	commentIduserIdrootnumLikesnumDislikespostedAt)textauthorr   	author_idparentr&   r%   r   
totalPageslast)		itertoolscount_download_jsonrh   r	   ri   r   r
   r   )ru   rM   pages_totalpage_nraw_commentsr   is_lastr{   r{   r|   _get_comments   s8   

zRokfinIE._get_commentsc                 C   s   | j | j dd ddd}t| jd|dd}| j |d dd	d
t||dddd}|  s?td|p3dr;tdddtd| j	| j dd dddddddddd}| j
| j dd d d!ttjtj|jjdd" dd#dd$d%| _d S )&Nzw/auth?client_id=web&redirect_uri=https%3A%2F%2Frokfin.com%2Ffeed&response_mode=fragment&response_type=code&scope=openidzloading login pagezerror loading login page)r   errnotezr<form\s+[^>]+action\s*=\s*"(https://secure\.rokfin\.com/auth/realms/rokfin-web/login-actions/authenticate\?[^"]+)"zAuthentication URL)r\   z
logging inF  off )rP   password
rememberMecredentialId)r   rG   expected_statusdataz((?i)(invalid\s+username\s+or\s+password)zinvalid username/passwordTrN   zLogin failedz/authzgranting user authorizationz%user authorization rejected by Rokfinwebnonez(https://rokfin.com/silent-check-sso.htmlfragmentcodeopenid)	client_idpromptredirect_uriresponse_moderesponse_typescope)r   r   query/tokenzgetting access credentialsz error getting access credentialsr   authorization_code)r   r   
grant_typer   r   r   r   )_download_webpage
_AUTH_BASEr   rj   r   _authentication_activeresearchr   _request_webpager   urllibparseparse_qs	urldefragr(   r   ri   _access_mgmt_tokens)ru   rP   r   
login_pageauthentication_point_url	resp_bodyurlhr{   r{   r|   _perform_login   sJ   

 zRokfinIE._perform_loginc                 C   s   h dt | | j  S )N>   KEYCLOAK_SESSIONKEYCLOAK_IDENTITYKEYCLOAK_SESSION_LEGACYKEYCLOAK_IDENTITY_LEGACY)set_get_cookiesr   ru   r{   r{   r|   r      s   zRokfinIE._authentication_activec                 C   s   t | jdd S )Nc                 S   s   d | d | d gS )N 
token_typeaccess_token)join)xr{   r{   r|   <lambda>  s    z*RokfinIE._get_auth_token.<locals>.<lambda>)r   r   r   r{   r{   r|   _get_auth_token  s   zRokfinIE._get_auth_tokenc           	   
   C   s   d|vsJ |  }|  }| jd}|r||d< | j||||dd\}}|r1|jdks1|d u r7| ||S | j| j d|ddt	d|dd	d
| _|  |d< |d d u r^t
ddd| j||||dS )Nauthorizationrefresh_tokeni  )headersr   r   r   zDUser authorization expired or canceled by Rokfin. Re-authorizing ...zFailed to re-authorizer   )r   r   r   r   zUser authorization lostTr   )r   r   )copyr   r   ri   _download_webpage_handlestatus_parse_jsonr   r   r   r   )	ru   url_or_requestrM   r   r   
auth_tokenr   json_stringr   r{   r{   r|   rg     s0   

z*RokfinIE._download_json_using_access_tokenN)__name__
__module____qualname__
_VALID_URL_NETRC_MACHINEr   r   int_TESTSr}   r   r   r   r   rg   r{   r{   r{   r|   r      s8   	
	
	
e9&r   c                   @   s$   e Zd ZddddddZdd ZdS )RokfinPlaylistBaseIErd   r>   stackvideoaudior>   dead_streamr   c                 c   s    | dpg D ]7}| j | d}|dkr| dn| d}|r%|s&q| jd| d| | d| tt|dd	V  qd S )
Nr?   	mediaTyperd   r   mediaIdr]   /rV   )rM   video_title)ri   _TYPES
url_resultr   r   )ru   rw   r?   
media_typerM   r{   r{   r|   _get_video_data)  s   
z$RokfinPlaylistBaseIE._get_video_dataN)r   r   r   r   r   r{   r{   r{   r|   r      s    r   c                   @   s2   e Zd ZdZdZdZddddidgZd	d
 ZdS )RokfinStackIEzrokfin:stackzRokfin Stacksz2https?://(?:www\.)?rokfin\.com/stack/(?P<id>[^/]+)zShttps://www.rokfin.com/stack/271/Tulsi-Gabbard-Portsmouth-Townhall-FULL--Feb-9-2020   r   271r(   playlist_countr)   c              	   C   s.   |  |}| | | t d| ||S )Nzstack/)	_match_idplaylist_resultr   r   rh   )ru   r(   list_idr{   r{   r|   r}   @  s   
zRokfinStackIE._real_extractN)r   r   r   IE_NAMEIE_DESCr   r   r}   r{   r{   r{   r|   r   4  s    r   c                   @   s`   e Zd ZdZdZdZdddddd	d
gZdddddddZdd Zdd Z	dd Z
dd ZdS )RokfinChannelIEzrokfin:channelzRokfin ChannelszXhttps?://(?:www\.)?rokfin\.com/(?!((feed/?)|(discover/?)|(channels/?))$)(?P<id>[^/]+)/?$z https://rokfin.com/TheConvoCouchd   z	12071-newzTheConvoCouch - Newz$md5:bb622b1bca100209b91cd685f7847f06)r   r   r-   )r(   playlist_mincountr)   poststopr   r   r>   r   )newr   videospodcastsstreamsstacksc                 C   s   |    d S N)_validate_extractor_argsr   r{   r{   r|   _real_initialize]  s   z RokfinChannelIE._real_initializec                 C   sP   |  dd }|d ur$t|dks|d | jvr&tdd| j ddd S d S )Ntabr   r   z,Invalid extractor-arg "tab". Must be one of z, Tr   )_configuration_arglen_TABSr   r   )ru   requested_tabsr{   r{   r|   r  `  s   "z(RokfinChannelIE._validate_extractor_argsc           	   
   c   s    d }t dD ]]}|dv rt d| d| d| d}nt d| d| d| }| j||d	|d
  t|d d d}| |E d H  t|dpMd }|d}|sb|r^||kre d S |dure d S qd S )Nr   )r   r   user/r   z?page=r   zpost/search/z&size=50&creator=z Downloading video metadata page r   r   )r   r   r   F)r   r   rh   r   r	   r   r
   ri   )	ru   r!   channel_namer  r   r   data_urlrw   r   r{   r{   r|   _entriese  s&   
zRokfinChannelIE._entriesc              	   C   s~   |  |}| t d| |}|d }| jddgdd }| | ||| j| | d| | d|  t|	d	S )
Nr  r   r  r   rD   r   -z - r-   )
r   r   rh   r  r   r  r	  r   r   ri   )ru   r(   r  channel_infor!   r  r{   r{   r|   r}   v  s   
(zRokfinChannelIE._real_extractN)r   r   r   r   r   r   r   r	  r  r  r  r}   r{   r{   r{   r|   r   F  s,    	r   c                   @   sf   e Zd ZdZdZdZddddddZdd	d
d
ddgZdZdZ	dd Z
dd Zdd ZdddZdS )RokfinSearchIEzrokfin:searchzRokfin Search
rkfnsearch))r   rawrd   )
content_idr  r>   )r  r   r   zrkfnsearch5:"zelenko"r~   z	"zelenko")r   r   r   Nc                 C   s4   | j j|  ddd\| _| _| js|   d S d S )NauthNNrD   )cacheloadie_key_db_url_db_access_key_get_db_access_credentialsr   r{   r{   r|   r    s   zRokfinSearchIE._real_initializec           	   
   c   s    d }t dD ]V}| j||d|ddd| t|d d d}t|dtd	}|d
p.g D ]&}| jt|dd\}}t||td	}|rU|rU| jd| d| dV  q/|d
s^ d S qd S )Nr   r   )sizecurrent)r   pagezDownloading page z of ~%s)r   r   )metar   total_pagesrA   results)content_typer  r  r]   r   )r(   )	r   r   _run_search_queryr	   r   r
   ri   r   r   )	ru   r   r"  page_numbersearch_resultsresultvideo_id_keyrv   rM   r{   r{   r|   _search_results  s$   
zRokfinSearchIE._search_resultsc                 K   sh   t | }tdD ]&}| j| j|f||dkd| jid|}|r'|  S | d | | qd S )Nr   r   r   )r   rG   r   zUpdating access credentials)	jsondumpsencoderanger   r  r  write_debugr  )ru   rM   r   kwargsattemptr'  r{   r{   r|   r%    s   
z RokfinSearchIE._run_search_queryc              
   C   s   d d d}| j d|ddd}td|D ]K}| j d| |dd	d
}|tdd|  d|p3d t| s=qt|d  d| _	d|d  | _
| j|  d| j	| j
f  d S td)N)
SEARCH_KEYENDPOINT_BASEzhttps://rokfin.com/discoverr   zDownloading home page)r   r   z/<script\b[^>]*\ssrc\s*=\s*"(/static/js/[^">]+)"zhttps://rokfin.comzDownloading JavaScript fileFr   zREACT_APP_(|z)\s*:\s*"([^"]+)"r   r3  z,/api/as/v1/engines/rokfin-search/search.jsonzBearer r2  r  z$Unable to extract access credentials)r   r   findallupdater   keysallvaluesr   r  r  r  storer  r   )ru   rM   	auth_datanotfound_err_pagejs_file_path
js_contentr{   r{   r|   r    s$   
z)RokfinSearchIE._get_db_access_credentialsr  )r   r   r   r   r   _SEARCH_KEYr   r   r  r  r  r*  r%  r  r{   r{   r{   r|   r    s,    r  )ro   rn   r   r+  r   urllib.parser   commonr   r   utilsr   r   r   r	   r
   r   r   r   r   r   r   r   rh   r   r   r   r   r  r{   r{   r{   r|   <module>   s    8  ;