
    yf)(                         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	e	      Z G d
 de	      Zy)    N   )InfoExtractor   )	HTTPError)ExtractorErrorint_or_nonejwt_decode_hs256try_call)traverse_objc                       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 Z	d Z
d Zd Zd Zy) 	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                    t        t        j                         dz        }t        d      }t        |      D ]z  \  }}t        |dt	        j                         z  z   dz        dz  }t        j                  |dz        }|dk(  rt        |      ||<   [|dk(  sadj                  d|z  d	z        ||<   | d
j                  |      dz   t        t        t        j                         dz              z   S )N   xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx   r   xyz{:x}       -)
inttimelist	enumeraterandommathfloorstrformatjoin)selfeticns         :/usr/lib/python3/dist-packages/yt_dlp/extractor/sonyliv.py_get_device_idzSonyLIVIE._get_device_idC   s    		d"#34aL 	0DAqQfmmo--34q8A

1r6"ACx1v!c}}QUQY/!	0 wwqzC#c$))+*<&=">>>    c                    | j                         | j                  d<   d| j                  d<   |j                         dk(  r.t        fd      r | j                  d<   | j	                          y t        |      dk7  s|j                         st        d| j                         | j	                          | j                  d	d d
| j                  t        j                  |ddt        j                  j                         j                  d      dddd      j!                               }|d   dk(  rt        |d   d      | j                  dd d| j                  t        j                  d|d| j#                  d      ddt        j                  j                         j                  d      dd      j!                               }|d   dk(  rt        |d   d      |d   d   | j                  d<   y )N	device_idzapplication/jsonzcontent-typetokenc                      t               S )N)r	   )passwords   r>   <lambda>z*SonyLIVIE._perform_login.<locals>.<lambda>S   s    <LX<V r@   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)rM   rL   rN   otpdmaIdageConfirmationr   rQ   	resultObjaccessToken)r?   _HEADERSlowerr
   report_loginlenisdigitr   _LOGIN_HINT_download_jsonjsondumpsdtdatetimenowstrftimeencode_get_tfa_info)r8   usernamerE   otp_request_jsonotp_verify_jsons     `  r>   _perform_loginzSonyLIVIE._perform_loginO   s   %)%8%8%:k"(:n%>>w&84V+W-5DMM/*]b (8(8(: #>t?O?O>P!QRR..L}dmm$** ($,[[__.778OP/%)N C vx / 
 L)T1 !1)!<tLL--MDJJ$, ())%0#'[[__.778OP%)	P 	E vx .  <(D0 !1)!<tLL)8)Em)To&r@   c                    	 | j                  d|d||| j                        d   S # t        $ rB}t        |j                  t
              r~|j                  j                  dk(  re| j                  |j                  j                  j                         j                         |      d   dk(  r| j                  | j                  d        t        |j                  t
              r|j                  j                  d	k(  rq| j                  |j                  j                  j                         j                         |      d   }|d
k(  r| j                  | j                         t        |       d }~ww xY w)Nzhttps://apiv2.sonyliv.com/AGL/z/A/ENG/WEB/rS   r]   i  rW   z&Please subscribe to watch this content)methodi  zGeoblocked Country)	countries)re   r_   r   
isinstancecauser   status_parse_jsonresponsereaddecoderaise_login_requiredrd   raise_geo_restricted_GEO_COUNTRIES)r8   versionpathvideo_idr9   rW   s         r>   	_call_apizSonyLIVIE._call_apiy   sJ   	&&CJDQ$-- ' 11<> >  
	!''9-!''..C2GDL\L\GG$$))+224hM@@IMKNvMw))$*:*:4)H!''9-!''..C2G**GG$$))+224h@@IK22--8K8K-L$W--
	s   &) 
E5D=E00E5c                 D    | j                  ddd       | j                  d<   y )Nz1.4zALL/GETTOKENsecurity_token)r   r_   )r8   s    r>   _initialize_pre_loginzSonyLIVIE._initialize_pre_login   s    *...PT*U&'r@   c                    | j                  |      }| j                  dd|z   |      }| j                  d      s"|j                  d      r| j	                  |       |d   }ddt        j                         j                  t        j                         dz  fz  i}| j                  ||d	|d
      }|j                  | j                  |j                  dd      j                  dd      |dd|d
             |D ]#  }|j                  di       j                  |       % | j                  dd|z   |      d   d   d   }|d   }	i }
|j                  dg       D ]I  }|j                  d      }|s|
j                  |j                  dd      g       j                  d|i       K ||	||j                  d      |j                  d       xs |j                  d!      t!        |j                  d"      d      t!        |j                  d#            t!        |j                  d$            |j                  d%      t!        |j                  d&            t!        |j                  d'            |
d(S ))Nz1.5zIN/CONTENT/VIDEOURL/VOD/allow_unplayable_formatsisEncryptedvideoURLzx-playback-session-idz%s-%dr%   dashF)mpd_idrS   fatalz.mpdz.m3u8z/DASH/z/HLS/r   hls)m3u8_idrS   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   )r8   r   r   contentdash_urlrS   r   fr   r   r   subsub_urls                r>   _real_extractzSonyLIVIE._real_extract   sf   >>#&..-8(D~~89gkk->XOOH%:&#W

0@0@$))+PTBT/U%U
 ++hvwe , Mt11VW-55hHeUG5 2 J 	K  	=ALL,33G<	= >><(*H66BDDEGGQS(	;;z2. 	Cggm,G  )?!GLSSwU 		  [1#<<(9:^hllK]>^$X\\.%A4H#HLL$<=(h)?@ll7+)(,,*GH'V(<="
 	
r@   N)__name__
__module____qualname__
_VALID_URL_TESTSr   r_   rd   _NETRC_MACHINEr?   rq   r   r   r    r@   r>   r   r      s    J w+%A#) 
 T
& N C Z c Z=!FD VNH QKN
?(UT"V*
r@   r   c                   @    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 Zy)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/AGLc              #     K   ddd}| j                  | j                   d|d|      d   |d<   t        | j                  | j                   d	| |d
|dddd      ddddd f      }|D ]  }t        |d         }t        |ddt        hf      xs d}d}t	        j
                  d      D ]  }t        | j                  | j                   d| |d| d| d|t        |      t        |dz         ddd      ddddd  f      }	|	s |	D ],  }
t        |
d         }| j                  d!| t        |       . |d"z  }  y w)#Nz!application/json, text/plain, */*zhttps://www.sonyliv.com)AcceptRefererz/1.4/A/ENG/WEB/ALL/GETTOKENzDownloading security tokenrs   r]   r   z/1.9/R/ENG/WEB/IN/DL/DETAIL/zDownloading series JSONfalse049)	kids_safefromto)rS   queryr   r   c                     t        |d         S Nr   r   _vs     r>   rF   z*SonyLIVSeriesIE._entries.<locals>.<lambda>   s    +VWX\V]J^ r@   r   r   r   r   r   z(/1.4/R/ENG/WEB/IN/CONTENT/DETAIL/BUNDLE/zDownloading z page z JSONc   r   asc)r   r   orderBy	sortOrderc                     t        |d         S r   r   r   s     r>   rF   z*SonyLIVSeriesIE._entries.<locals>.<lambda>   s    R]^_`d^eRf r@   zsonyliv:d   )re   	_API_BASEr   r5   	itertoolscount
url_resultr   )r8   show_idrS   seasonsr   	season_idrR   cursorpage_numepisodesepisoder   s               r>   _entrieszSonyLIVSeriesIE._entries   s    90
 %)$7$7~~9:G(' %8 %;;F%H ! t22~~:7)Dg%w$?  3   lA|=^_a  	FF4L)IWse(DEQDF%OOA. '(;(;~~&&NykZdV6(5ISZ #F!&2+.#2%*	c )< ) %lA|Efgi  ' VG"74=1H//HXJ*?HUUV #		s   EEc                 h    | j                  |      }| j                  | j                  |      |      S )N)playlist_id)r   playlist_resultr   )r8   r   r   s      r>   r   zSonyLIVSeriesIE._real_extract   s/    ..%##DMM'$:#PPr@   N)r   r   r   r   r   r   r   r   r   r@   r>   r   r      sN    ]JA ,
 C ,
F 0I!FQr@   r   )ri   rh   r   rf   r3   r2   r/   r   commonr   networking.exceptionsr   utilsr   r   r	   r
   utils.traversalr   r   r   r   r@   r>   <module>r      sL           ! -  +c
 c
L6Qm 6Qr@   