U
    g<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                   @   s2  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ddd dd!d"d#d$dd%d&d'd(d(d)d)eedgd*dd+d,dd-dd.d/d0d1deed2d3dd4gd5d6d7dd8d9dd:d;d<d=d>d1d'eed?d@dAdAdgdBdgZdCdD Z	dEdF Z
dGdH ZdIdJ ZdKdL Zi i fdMdNZdOS )PRokfinIEz@https?://(?:www\.)?rokfin\.com/(?P<id>(?P<type>post|stream)/\d+)ZrokfinzHhttps://secure.rokfin.com/auth/realms/rokfin-web/protocol/openid-connectzOhttps://www.rokfin.com/post/57548/Mitt-Romneys-Crazy-Solution-To-Climate-Changez
post/57548Zmp4z.Mitt Romney's Crazy Solution To Climate Changez*re:https://img\.production\.rokfin\.com/.+Z20211023itaz
Jimmy DoreZ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 TimeZ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-Datazstream/10543zR"It's A Crazy Mess" Regional Director Blows Whistle On Pfizer's Vaccine Trial Dataz$md5:324ce2d3e3b62e659506409e458b9d8eZTLAVagabondZ53856zhttps://rokfin.com/TLAVagabondFTwas_liveiwaZ20211102)r   r   r   r   descriptionr   r   r   r   is_liver(   r    r   release_timestamprelease_dater   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 DyerZ186881zhttps://rokfin.com/jaydyerZpremium_onlyidZ20230307z	OpenMind^z$md5:cb04e32e68326c9b2b251b297bacff35i  )r   r   r   r   r   r   r   r   r    r!   r"   r   r   r'   r)   r#   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 BlumenthalZ248902z https://rokfin.com/MaxBlumenthalidg  AZ20230310)r   r   r   r   r   r   r   r   r    r!   r"   r   r+   r,   r   r'   c                 C   s<  |  |dd\}}| t | |}t|d}|drFdn|rNdn|dkrZdnd	}t|d
dtd}|dkrt| j	dt|ddtd|d dd d}|rd
|igng i  }}	t
|dkr| j||d|dkd\}}	|s*t|ddr | jdddd n*|r*| jdtj|d |dd t|dd }
|pZt|d!d"pZt|d#}|||	tt|d$d%tt|d&tt|d'd(tt|d)d*t|d+t|d,tt|d-d.tt|d/d0|
rtd1|
 nd ||d	kr|nd t|d2td|| jtt|ddddddd3|d4kr4| |nd d5S )6Nr   typeZscheduledAtZ	stoppedAtr(   Zis_upcomingstreamr*   r   r$   )contentZ
contentUrlZexpected_type)Nz	fake.m3u8z%https?://[^/]+/([^/]+)/storyboard.vtttimelineUrl)r/   r1   defaultz#https://stream.v.rokfin.com/%s.m3u8Zm3u8F)fatalZliveZpremiumPlanZ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)creatorr:   ZpostedAtMillii  ZcreationDateTimer   r/   ZcontentTitle)r/   r#   r   )r/   ZthumbnailUrl1r)   )r/   ZcontentDescriptionZ	likeCountZdislikeCount)r9   name)r;   r=   )r9   r   )r;   r   https://rokfin.com/)r'   .r   )Zneeds_premium
is_privateZneeds_subscriptionZ
needs_authZis_unlistedpost)r   formats	subtitlesr   r#   r   r)   r"   r!   r   r   r   r   r+   r'   r    r   Z__post_extractor)Z_match_valid_urlgroup!_download_json_using_access_token_API_BASE_URLr   getr   r   r	   _search_regexr   Z#_extract_m3u8_formats_and_subtitlesZraise_login_requiredZraise_no_formatsdtdatetimefromtimestampstrftimer   r   r
   Z_availabilityboolZextract_comments)selfr$   r7   
video_typemetadataZ	scheduledr    Z	video_urlrA   rB   Zuploaderr    rP   ;/tmp/pip-unpacked-wheel-q5ljy6pj/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	pVi }|d
pdg D ]Z}t|dt|d|d|ddt|dt|dt|ddV  qft|dpd }|d}|d
r|s|r ||krn|dk	r d S qd S )Nzcomment?postId=   z&page=&size=50z!Downloading viewer comments page r    of %sFnoter4   r/   commentr=   Z	commentIdZuserIdrootZnumLikesZnumDislikesZpostedAt)textauthorr   Z	author_idparentr"   r!   r   
totalPageslast)		itertoolscount_download_jsonrE   r	   rF   r   r
   r   )rM   r7   pages_totalpage_nZraw_commentsrX   is_lastrP   rP   rQ   _get_comments   s0     

*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|pfdrvt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)rW   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 )r:   passwordZ
rememberMeZcredentialId)rW   r4   expected_statusdataz((?i)(invalid\s+username\s+or\s+password)zinvalid username/passwordTr8   zLogin failedz/authzgranting user authorizationz%user authorization rejected by Rokfinwebnonez(https://rokfin.com/silent-check-sso.htmlfragmentcodeZopenid)	client_idpromptredirect_uriZresponse_modeZresponse_typeZscope)rW   rf   query/tokenzgetting access credentialsz error getting access credentialsr   Zauthorization_code)rq   rr   
grant_typert   rW   rf   rl   )_download_webpage
_AUTH_BASEr   rG   r   _authentication_activeresearchr   Z_request_webpagera   urllibparseparse_qs	urldefragr$   rp   rF   _access_mgmt_tokens)rM   r:   rj   Z
login_pageZauthentication_point_urlZ	resp_bodyurlhrP   rP   rQ   _perform_login   s`    
       
  
   zRokfinIE._perform_loginc                 C   s   ddddht | | j  S )NZKEYCLOAK_IDENTITYZKEYCLOAK_IDENTITY_LEGACYZKEYCLOAK_SESSIONZKEYCLOAK_SESSION_LEGACY)setZ_get_cookiesrz   rM   rP   rP   rQ   r{      s    
zRokfinIE._authentication_activec                 C   s   t | jdd S )Nc                 S   s   d | d | d gS )N 
token_typeZaccess_token)join)xrP   rP   rQ   <lambda>      z*RokfinIE._get_auth_token.<locals>.<lambda>)r   r   r   rP   rP   rQ   _get_auth_token  s    zRokfinIE._get_auth_tokenc           	   
   C   s   d|kst | }|  }| jd}|r4||d< | j||||dd\}}|rb|jdksb|d krn| ||S | j| j	 d|ddt
d|dd	d
| _|  |d< |d d krtddd| j||||dS )Nauthorizationrefresh_tokeni  )headersru   rk   rv   zDUser authorization expired or canceled by Rokfin. Re-authorizing ...zFailed to re-authorizern   )rw   r   rr   rx   zUser authorization lostTrm   )r   ru   )AssertionErrorcopyr   r   rF   Z_download_webpage_handlestatusZ_parse_jsonra   rz   r   r   )	rM   Zurl_or_requestr7   r   ru   Z
auth_tokenr   Zjson_stringr   rP   rP   rQ   rD     s<        

  z*RokfinIE._download_json_using_access_tokenN)__name__
__module____qualname__
_VALID_URLZ_NETRC_MACHINErz   r   int_TESTSrR   re   r   r{   r   rD   rP   rP   rP   rQ   r      s   e9&r   c                   @   s$   e Zd ZddddddZdd ZdS )RokfinPlaylistBaseIEr@   r.   stackvideoaudior.   Zdead_streamr   c                 c   s   | dpg D ]n}| j | d}|dkr6| dn| d}|r|sJq| jd| d| | d| tt|dd	V  qd S )
Nr/   Z	mediaTyper@   r   ZmediaIdr>   /r<   )r7   Zvideo_title)rF   _TYPES
url_resultr   r   )rM   rO   r/   
media_typer7   rP   rP   rQ   _get_video_data)  s    z$RokfinPlaylistBaseIE._get_video_dataN)r   r   r   r   r   rP   rP   rP   rQ   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   Z271r$   Zplaylist_countr%   c              	   C   s.   |  |}| | | t d| ||S )Nzstack/)	_match_idplaylist_resultr   ra   rE   )rM   r$   Zlist_idrP   rP   rQ   rR   @  s    
zRokfinStackIE._real_extractN)r   r   r   IE_NAMEIE_DESCr   r   rR   rP   rP   rP   rQ   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$   Zplaylist_mincountr%   poststopr   r   r.   r   )newr   ZvideosZpodcastsZstreamsZstacksc                 C   s   |    d S )N)_validate_extractor_argsr   rP   rP   rQ   _real_initialize]  s    z RokfinChannelIE._real_initializec                 C   sL   |  dd }|d k	rHt|dks.|d | jkrHtdd| j ddd S )Ntabr   r   z,Invalid extractor-arg "tab". Must be one of z, Trm   )_configuration_arglen_TABSr   r   )rM   Zrequested_tabsrP   rP   rQ   r   `  s    "z(RokfinChannelIE._validate_extractor_argsc           	   
   c   s   d }t dD ]}|dkr8t d| d| d| d}nt d| d| d| }| j||d	|d
  t|d d d}| |E d H  t|dpd }|d}|s|r||krn|dk	r d S qd S )Nr   )r   r   user/r   z?page=rT   zpost/search/z&size=50&creator=z Downloading video metadata page r   rU   )rW   r]   r^   F)r_   r`   rE   ra   r	   r   r
   rF   )	rM   r   channel_namer   rb   rc   Zdata_urlrO   rd   rP   rP   rQ   _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   r2   r   -z - r)   )
r   ra   rE   r   r   r   r   r   r   rF   )rM   r$   r   Zchannel_infor   r   rP   rP   rQ   rR   v  s    
  zRokfinChannelIE._real_extractN)r   r   r   r   r   r   r   r   r   r   r   rR   rP   rP   rP   rQ   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 SearchZ
rkfnsearch))r   rawr@   )Z
content_idr   r.   )r   r   r   zrkfnsearch5:"zelenko"rS   z	"zelenko")r   r   r   Nc                 C   s0   | j j|  ddd\| _| _| js,|   d S )NauthNNr2   )cacheloadie_key_db_url_db_access_key_get_db_access_credentialsr   rP   rP   rQ   r     s    zRokfinSearchIE._real_initializec           	   
   c   s   d }t dD ]}| j||d|ddd| t|d d d}t|dtd	}|d
pZg D ]L}| jt|dd\}}t||td	}|r\|r\| jd| d| dV  q\|d
s d S qd S )Nr   r   )sizecurrent)ru   pagezDownloading page z of ~%s)rl   rW   )metar   total_pagesr0   results)content_typer   r   r>   r   )r$   )	r_   r`   _run_search_queryr	   r   r
   rF   r   r   )	rM   ru   r   Zpage_numbersearch_resultsresultZvideo_id_keyrN   r7   rP   rP   rQ   _search_results  s     
zRokfinSearchIE._search_resultsc                 K   sh   t | }tdD ]L}| j| j|f||dkd| jid|}|rN|  S | d | | qd S )Nr   r   r   )rl   r4   r   zUpdating access credentials)	jsondumpsencoderangera   r   r   Zwrite_debugr   )rM   r7   rl   kwargsattemptr   rP   rP   rQ   r     s       
z RokfinSearchIE._run_search_queryc              
   C   s   d d d}| j d|ddd}td|D ]}| j d| |dd	d
}|tdd|  d|pfd t| szq(t|d  d| _	d|d  | _
| j|  d| j	| j
f  d S tdd S )N)
SEARCH_KEYENDPOINT_BASEzhttps://rokfin.com/discoverrg   zDownloading home page)rk   rW   z/<script\b[^>]*\ssrc\s*=\s*"(/static/js/[^">]+)"zhttps://rokfin.comzDownloading JavaScript fileFrV   zREACT_APP_(|z)\s*:\s*"([^"]+)"ri   r   z,/api/as/v1/engines/rokfin-search/search.jsonzBearer r   r   z$Unable to extract access credentials)ry   r|   findallupdater   keysallvaluesr   r   r   r   storer   r   )rM   r7   Z	auth_dataZnotfound_err_pageZjs_file_pathZ
js_contentrP   rP   rQ   r     s2    
       z)RokfinSearchIE._get_db_access_credentials)N)r   r   r   r   r   Z_SEARCH_KEYr   r   r   r   r   r   r   r   rP   rP   rP   rQ   r     s*   r   )rI   rH   r_   r   r|   urllib.parser~   commonr   r   utilsr   r   r   r	   r
   r   r   r   r   r   r   r   rE   r   r   r   r   r   rP   rP   rP   rQ   <module>   s   8  ;