U
    g(                     @   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 ToastZ
1000022678mp4Z20200411z$md5:3957fa31d9309bf336ceb3f37ad5b7cbi^   r   zBachelors Delighti  )titleidextZupload_datedescription	timestampdurationseason_numberseriesepisode_numberrelease_yearZskip_downloadT)url	info_dictparamsz<https://www.sonyliv.com/movies/tahalka-1000050121?watch=true)r   Z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.Zsonylivc                 C   s   t t d }td}t|D ]d\}}t |dt   d dB }t|d }|dkrht|||< q |dkr d|@ dB d||< q d	|d
 tt t d  S )N  Z xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx   r   xy       -)	inttimelist	enumeraterandommathfloorstrjoin)selfeticn r3   </tmp/pip-unpacked-wheel-q5ljy6pj/yt_dlp/extractor/sonyliv.py_get_device_idC   s    zSonyLIVIE._get_device_idc                    sF  |   | jd< d| jd< | dkrJt fddrJ | jd< |   d S t|dks^| sntd	| j |   | j	d
d d| jt
|ddtj ddddd d}|d dkrt|d dd| j	dd d| jt
d|d| dddtj ddd d}|d dkr0t|d dd|d d | jd< d S )NZ	device_idzapplication/jsonzcontent-typetokenc                      s   t  S )N)r   r3   passwordr3   r4   <lambda>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 OTPZMSMINDr   z%Y-%m-%dT%H:%M:%S.%MZ   ZREGISTERORSIGNINT)mobileNumberchannelPartnerIDcountryr   ZotpSizeZ	loginTypeisMobileMandatory)noteheadersdataZ
resultCodeZKOmessage)expectedz?https://apiv2.sonyliv.com/AGL/2.0/A/ENG/WEB/IN/HR/CONFIRMOTP-V2zVerifying OTPZOTP)r?   r>   r@   ZotpZdmaIdZageConfirmationr   rA   	resultObjZaccessToken)r5   _HEADERSlowerr	   Zreport_loginlenisdigitr   _LOGIN_HINT_download_jsonjsondumpsdtdatetimenowstrftimeencodeZ_get_tfa_info)r-   usernamer8   Zotp_request_jsonZotp_verify_jsonr3   r7   r4   _perform_loginO   sZ    

   
   
zSonyLIVIE._perform_loginc              
   C   s   z$| j d| d| || jdd W S  tk
r } zt|jtr~|jjdkr~| |jj	 
 |d dkr~| j| jd d t|jtr|jjd	kr| |jj	 
 |d }|d
kr| j| jd t| W 5 d }~X Y nX d S )Nzhttps://apiv2.sonyliv.com/AGL/z/A/ENG/WEB/rC   rG   i  rE   z&Please subscribe to watch this content)methodi  zGeoblocked Country)Z	countries)rM   rH   r   
isinstancecauser   statusZ_parse_jsonresponsereaddecodeZraise_login_requiredrL   Zraise_geo_restricted_GEO_COUNTRIES)r-   versionpathvideo_idr.   rE   r3   r3   r4   	_call_apiy   s:       zSonyLIVIE._call_apic                 C   s   |  ddd | jd< d S )Nz1.4zALL/GETTOKENsecurity_token)rc   rH   )r-   r3   r3   r4   _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 | q| dd| |d d d }|d }	i }
|dg D ]4}|d}|sq|
|ddg d|i q||	||d|d pJ|d!t|d"dt|d#t|d$|d%t|d&t|d'|
d(S ))Nz1.5zIN/CONTENT/VIDEOURL/VOD/Zallow_unplayable_formatsZisEncryptedZvideoURLzx-playback-session-idz%s-%dr   ZdashF)Zmpd_idrC   fatalz.mpdz.m3u8z/DASH/z/HLS/r   Zhls)Zm3u8_idrC   rf   Zhttp_headersz1.6z
IN/DETAIL/
containersr   metadataZepisodeTitleZsubtitleZsubtitleUrlZsubtitleLanguageNameZENGr   Z	posterURLZlongDescriptionZshortDescriptionZcreationDater   seasonr   episodeNumberyear)r   r   formatsZ	thumbnailr   r   r   r   r   r   r   	subtitles)	_match_idrc   	get_paramgetZ
report_drmuuidZuuid4hexr%   Z_extract_mpd_formatsextendZ_extract_m3u8_formatsreplace
setdefaultupdateappendr   )r-   r   rb   contentZdash_urlrC   rl   frh   r   rm   subZsub_urlr3   r3   r4   _real_extract   s~    
  
           
 zSonyLIVIE._real_extractN)__name__
__module____qualname__
_VALID_URL_TESTSr_   rH   rL   Z_NETRC_MACHINEr5   rV   rc   re   r{   r3   r3   r3   r4   r      sV    "*r   c                   @   sD   e Zd ZdZddddiddddd	idg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   Z
1700000091)r   Zplaylist_mincountr   z1https://www.sonyliv.com/shows/beyhadh-1700000007/if  Z
1700000007zhttps://apiv2.sonyliv.com/AGLc                 c   sD  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 ]}t|d }t|ddthfpd}d}tdD ]}t| j | j d| |d| d| d|t|t|d ddd dddddd!d f}	|	s ql|	D ](}
t|
d }| d"| t|V  q
|d#7 }qqld S )$Nz!application/json, text/plain, */*zhttps://www.sonyliv.com)AcceptZRefererz/1.4/A/ENG/WEB/ALL/GETTOKENzDownloading security tokenrW   rG   rd   z/1.9/R/ENG/WEB/IN/DL/DETAIL/zDownloading series JSONfalse0Z49)Z	kids_safefromto)rC   queryrg   r   c                 S   s   t |d S Nr   r   _vr3   r3   r4   r9      r:   z*SonyLIVSeriesIE._entries.<locals>.<lambda>r   rh   r   ri   r   z(/1.4/R/ENG/WEB/IN/CONTENT/DETAIL/BUNDLE/zDownloading z page z JSONc   rj   Zasc)r   r   ZorderByZ	sortOrderc                 S   s   t |d S r   r   r   r3   r3   r4   r9      r:   zsonyliv:d   )rM   	_API_BASEr
   r+   	itertoolscountZ
url_resultr   )r-   show_idrC   Zseasonsri   Z	season_idrB   cursorZpage_numZepisodesZepisoderb   r3   r3   r4   _entries   s^    
      
zSonyLIVSeriesIE._entriesc                 C   s   |  |}| j| ||dS )N)Zplaylist_id)rn   Zplaylist_resultr   )r-   r   r   r3   r3   r4   r{      s    
zSonyLIVSeriesIE._real_extractN)r|   r}   r~   r   r   r   r   r{   r3   r3   r3   r4   r      s"     #r   )rQ   rP   r   rN   r)   r(   r%   rq   commonr   Znetworking.exceptionsr   utilsr   r   r   r	   Zutils.traversalr
   r   r   r3   r3   r3   r4   <module>   s    '