o
    i)                     @   s   d dl Zd dlZ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	 ddl
mZ ddl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dS )    N   )InfoExtractor   )	HTTPError)ExtractorErrorint_or_nonejwt_decode_hs256try_call)traverse_objc                   @   s   e Zd ZdZ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dddgZdgZi ZdZdZdd Z	dd Z
dd Zd d! Zd"d# Zd$S )%	SonyLIVIEa  (?x)
                     (?:
                        sonyliv:|
                        https?://(?:www\.)?sonyliv\.com/(?:s(?:how|port)s/[^/]+|movies|clip|trailer|music-videos)/[^/?#&]+-
                    )
                    (?P<id>\d+)
                  zehttps://www.sonyliv.com/shows/bachelors-delight-1700000113/achaari-cheese-toast-1000022678?watch=truezAchaari Cheese Toast
1000022678mp420200411z$md5:3957fa31d9309bf336ceb3f37ad5b7cbi^   r   zBachelors Delighti  )titleidextupload_datedescription	timestampdurationseason_numberseriesepisode_numberrelease_yearskip_downloadT)url	info_dictparamsz<https://www.sonyliv.com/movies/tahalka-1000050121?watch=true)r   only_matchingz1https://www.sonyliv.com/clip/jigarbaaz-1000098925zHhttps://www.sonyliv.com/trailer/sandwiched-forever-1000100286?watch=truezhttps://www.sonyliv.com/sports/india-tour-of-australia-2020-21-1700000286/cricket-hls-day-3-1st-test-aus-vs-ind-19-dec-2020-1000100959?watch=truezHhttps://www.sonyliv.com/music-videos/yeh-un-dinon-ki-baat-hai-1000018779INz|Use "--username <mobile_number>" to login using OTP or "--username token --password <auth_token>" to login using auth token.sonylivc                 C   s   t t d }td}t|D ]2\}}t |dt   d dB }t|d }|dkr4t|||< q|dkrBd|@ dB d||< qd	|d
 tt t d  S )N   xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx   r   xy       -)	inttimelist	enumeraterandommathfloorstrjoin)selfeticn r:   L/home/ubuntu/.local/lib/python3.10/site-packages/yt_dlp/extractor/sonyliv.py_get_device_idC   s   "zSonyLIVIE._get_device_idc                    sD  |   | jd< d| jd< | dkr%t fddr% | jd< |   d S t|dks/| s7td	| j |   | j	d
d d| jt
|ddtj ddddd d}|d dkrgt|d dd| j	dd d| jt
d|d| dddtj ddd d}|d dkrt|d dd|d d | jd< d S )N	device_idzapplication/jsonzcontent-typetokenc                      s   t  S )N)r   r:   passwordr:   r;   <lambda>S   s    z*SonyLIVIE._perform_login.<locals>.<lambda>authorization
   zInvalid username/password; z>https://apiv2.sonyliv.com/AGL/1.6/A/ENG/WEB/IN/HR/CREATEOTP-V2zSending OTPMSMINDr    z%Y-%m-%dT%H:%M:%S.%MZ   REGISTERORSIGNINT)mobileNumberchannelPartnerIDcountryr   otpSize	loginTypeisMobileMandatory)noteheadersdata
resultCodeKOmessage)expectedz?https://apiv2.sonyliv.com/AGL/2.0/A/ENG/WEB/IN/HR/CONFIRMOTP-V2zVerifying OTPOTP)rH   rG   rI   otpdmaIdageConfirmationr   rL   	resultObjaccessToken)r<   _HEADERSlowerr	   report_loginlenisdigitr   _LOGIN_HINT_download_jsonjsondumpsdtdatetimenowstrftimeencode_get_tfa_info)r4   usernamer@   otp_request_jsonotp_verify_jsonr:   r?   r;   _perform_loginO   sR   

	zSonyLIVIE._perform_loginc              
   C   s   z| j d| d| || jdd W S  tyl } zNt|jtr>|jjdkr>| |jj	 
 |d dkr>| j| jd d t|jtrg|jjd	krg| |jj	 
 |d }|d
krc| j| jd t| d }~ww )Nzhttps://apiv2.sonyliv.com/AGL/z/A/ENG/WEB/rN   rX   i  rR   z&Please subscribe to watch this content)methodi  zGeoblocked Country)	countries)r`   rZ   r   
isinstancecauser   status_parse_jsonresponsereaddecoderaise_login_requiredr_   raise_geo_restricted_GEO_COUNTRIES)r4   versionpathvideo_idr5   rR   r:   r:   r;   	_call_apiy   s8   zSonyLIVIE._call_apic                 C   s   |  ddd | jd< d S )Nz1.4zALL/GETTOKENsecurity_token)r}   rZ   )r4   r:   r:   r;   _initialize_pre_login   s   zSonyLIVIE._initialize_pre_loginc                 C   s  |  |}| dd| |}| ds|dr| | |d }ddt jt d f i}| j	||d	|d
d}|
| j|dddd|dd|d
d |D ]}|di | qR| dd| |d d d }|d }	i }
|dg D ]}|d}|sqy|
|ddg d|i qy||	||d|d p|d!t|d"dt|d#t|d$|d%t|d&t|d'|
d(S ))Nz1.5zIN/CONTENT/VIDEOURL/VOD/allow_unplayable_formatsisEncryptedvideoURLzx-playback-session-idz%s-%dr"   dashF)mpd_idrN   fatalz.mpdz.m3u8z/DASH/z/HLS/r   hls)m3u8_idrN   r   http_headersz1.6z
IN/DETAIL/
containersr   metadataepisodeTitlesubtitlesubtitleUrlsubtitleLanguageNameENGr   	posterURLlongDescriptionshortDescriptioncreationDater   seasonr   episodeNumberyear)r   r   formats	thumbnailr   r   r   r   r   r   r   	subtitles)	_match_idr}   	get_paramget
report_drmuuiduuid4hexr,   _extract_mpd_formatsextend_extract_m3u8_formatsreplace
setdefaultupdateappendr   )r4   r   r|   contentdash_urlrN   r   fr   r   r   subsub_urlr:   r:   r;   _real_extract   sb   






zSonyLIVIE._real_extractN)__name__
__module____qualname__
_VALID_URL_TESTSry   rZ   r_   _NETRC_MACHINEr<   rl   r}   r   r   r:   r:   r:   r;   r      sV    "*r   c                   @   sH   e Zd ZdZddddiddddd	idgZd
ZdZdd Zdd ZdS )SonyLIVSeriesIEzIhttps?://(?:www\.)?sonyliv\.com/shows/[^/?#&]+-(?P<id>\d{10})/?(?:$|[?#])z0https://www.sonyliv.com/shows/adaalat-1700000091i  r   
1700000091)r   playlist_mincountr   z1https://www.sonyliv.com/shows/beyhadh-1700000007/if  
1700000007zhttps://apiv2.sonyliv.com/AGL)ascdescc                 c   sR   ddd}| j | j d|d|dd |d< t| j | j d	| |d
|ddddddddddd f}|dkr=t|}|D ]g}t|d }t|ddthfpQd}d}tdD ]L}	t| j | j d| |d| d|	 d|t|t|d d|d dddddd!d f}
|
s n|
D ]}t|d }| d"| t|V  q|d#7 }qYq?d S )$Nz!application/json, text/plain, */*zhttps://www.sonyliv.com)AcceptRefererz/1.4/A/ENG/WEB/ALL/GETTOKENzDownloading security tokenrm   rX   r~   z/1.9/R/ENG/WEB/IN/DL/DETAIL/zDownloading series JSONfalse049)	kids_safefromto)rN   queryr   r   c                 S      t |d S Nr   r   _vr:   r:   r;   rA          z*SonyLIVSeriesIE._entries.<locals>.<lambda>r   r   r   r   r   r   z(/1.4/R/ENG/WEB/IN/CONTENT/DETAIL/BUNDLE/zDownloading z page z JSONc   r   )r   r   orderBy	sortOrderc                 S   r   r   r   r   r:   r:   r;   rA      r   zsonyliv:d   )	r`   	_API_BASEr
   reversedr2   	itertoolscount
url_resultr   )r4   show_id
sort_orderrN   seasonsr   	season_idrM   cursorpage_numepisodesepisoder|   r:   r:   r;   _entries   s\   

zSonyLIVSeriesIE._entriesc                 C   s^   |  |}| d| jd gd }|| jvr$td| dd| j | j| |||dS )Nr   r   zInvalid sort order "z". Allowed values are: z, )playlist_id)r   _configuration_arg_SORT_ORDERS
ValueErrorr3   playlist_resultr   )r4   r   r   r   r:   r:   r;   r      s   

zSonyLIVSeriesIE._real_extractN)	r   r   r   r   r   r   r   r   r   r:   r:   r:   r;   r      s"    &r   )rd   rc   r   ra   r0   r/   r,   r   commonr   networking.exceptionsr   utilsr   r   r   r	   utils.traversalr
   r   r   r:   r:   r:   r;   <module>   s     '