]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/sonyliv.py
[extractor/youtube] Improve nsig function name extraction
[yt-dlp.git] / yt_dlp / extractor / sonyliv.py
1 import datetime
2 import json
3 import math
4 import random
5 import time
6 import uuid
7
8 from .common import InfoExtractor
9 from ..compat import compat_HTTPError
10 from ..utils import (
11 ExtractorError,
12 int_or_none,
13 jwt_decode_hs256,
14 try_call,
15 try_get,
16 )
17
18
19 class SonyLIVIE(InfoExtractor):
20 _VALID_URL = r'''(?x)
21 (?:
22 sonyliv:|
23 https?://(?:www\.)?sonyliv\.com/(?:s(?:how|port)s/[^/]+|movies|clip|trailer|music-videos)/[^/?#&]+-
24 )
25 (?P<id>\d+)
26 '''
27 _TESTS = [{
28 'url': 'https://www.sonyliv.com/shows/bachelors-delight-1700000113/achaari-cheese-toast-1000022678?watch=true',
29 'info_dict': {
30 'title': 'Achaari Cheese Toast',
31 'id': '1000022678',
32 'ext': 'mp4',
33 'upload_date': '20200411',
34 'description': 'md5:3957fa31d9309bf336ceb3f37ad5b7cb',
35 'timestamp': 1586632091,
36 'duration': 185,
37 'season_number': 1,
38 'series': 'Bachelors Delight',
39 'episode_number': 1,
40 'release_year': 2016,
41 },
42 'params': {
43 'skip_download': True,
44 },
45 }, {
46 'url': 'https://www.sonyliv.com/movies/tahalka-1000050121?watch=true',
47 'only_matching': True,
48 }, {
49 'url': 'https://www.sonyliv.com/clip/jigarbaaz-1000098925',
50 'only_matching': True,
51 }, {
52 'url': 'https://www.sonyliv.com/trailer/sandwiched-forever-1000100286?watch=true',
53 'only_matching': True,
54 }, {
55 'url': 'https://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=true',
56 'only_matching': True,
57 }, {
58 'url': 'https://www.sonyliv.com/music-videos/yeh-un-dinon-ki-baat-hai-1000018779',
59 'only_matching': True,
60 }]
61 _GEO_COUNTRIES = ['IN']
62 _HEADERS = {}
63 _LOGIN_HINT = 'Use "--username <mobile_number>" to login using OTP or "--username token --password <auth_token>" to login using auth token.'
64 _NETRC_MACHINE = 'sonyliv'
65
66 def _get_device_id(self):
67 e = int(time.time() * 1000)
68 t = list('xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx')
69 for i, c in enumerate(t):
70 n = int((e + 16 * random.random()) % 16) | 0
71 e = math.floor(e / 16)
72 if c == 'x':
73 t[i] = str(n)
74 elif c == 'y':
75 t[i] = '{:x}'.format(3 & n | 8)
76 return ''.join(t) + '-' + str(int(time.time() * 1000))
77
78 def _perform_login(self, username, password):
79 self._HEADERS['device_id'] = self._get_device_id()
80 self._HEADERS['content-type'] = 'application/json'
81
82 if username.lower() == 'token' and try_call(lambda: jwt_decode_hs256(password)):
83 self._HEADERS['authorization'] = password
84 self.report_login()
85 return
86 elif len(username) != 10 or not username.isdigit():
87 raise ExtractorError(f'Invalid username/password; {self._LOGIN_HINT}')
88
89 self.report_login()
90 otp_request_json = self._download_json(
91 'https://apiv2.sonyliv.com/AGL/1.6/A/ENG/WEB/IN/HR/CREATEOTP-V2',
92 None, note='Sending OTP', headers=self._HEADERS, data=json.dumps({
93 'mobileNumber': username,
94 'channelPartnerID': 'MSMIND',
95 'country': 'IN',
96 'timestamp': datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%MZ'),
97 'otpSize': 6,
98 'loginType': 'REGISTERORSIGNIN',
99 'isMobileMandatory': True,
100 }).encode())
101 if otp_request_json['resultCode'] == 'KO':
102 raise ExtractorError(otp_request_json['message'], expected=True)
103
104 otp_verify_json = self._download_json(
105 'https://apiv2.sonyliv.com/AGL/2.0/A/ENG/WEB/IN/HR/CONFIRMOTP-V2',
106 None, note='Verifying OTP', headers=self._HEADERS, data=json.dumps({
107 'channelPartnerID': 'MSMIND',
108 'mobileNumber': username,
109 'country': 'IN',
110 'otp': self._get_tfa_info('OTP'),
111 'dmaId': 'IN',
112 'ageConfirmation': True,
113 'timestamp': datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%MZ'),
114 'isMobileMandatory': True,
115 }).encode())
116 if otp_verify_json['resultCode'] == 'KO':
117 raise ExtractorError(otp_request_json['message'], expected=True)
118 self._HEADERS['authorization'] = otp_verify_json['resultObj']['accessToken']
119
120 def _call_api(self, version, path, video_id):
121 try:
122 return self._download_json(
123 'https://apiv2.sonyliv.com/AGL/%s/A/ENG/WEB/%s' % (version, path),
124 video_id, headers=self._HEADERS)['resultObj']
125 except ExtractorError as e:
126 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 406 and self._parse_json(
127 e.cause.read().decode(), video_id)['message'] == 'Please subscribe to watch this content':
128 self.raise_login_required(self._LOGIN_HINT, method=None)
129 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
130 message = self._parse_json(
131 e.cause.read().decode(), video_id)['message']
132 if message == 'Geoblocked Country':
133 self.raise_geo_restricted(countries=self._GEO_COUNTRIES)
134 raise ExtractorError(message)
135 raise
136
137 def _initialize_pre_login(self):
138 self._HEADERS['security_token'] = self._call_api('1.4', 'ALL/GETTOKEN', None)
139
140 def _real_extract(self, url):
141 video_id = self._match_id(url)
142 content = self._call_api(
143 '1.5', 'IN/CONTENT/VIDEOURL/VOD/' + video_id, video_id)
144 if not self.get_param('allow_unplayable_formats') and content.get('isEncrypted'):
145 self.report_drm(video_id)
146 dash_url = content['videoURL']
147 headers = {
148 'x-playback-session-id': '%s-%d' % (uuid.uuid4().hex, time.time() * 1000)
149 }
150 formats = self._extract_mpd_formats(
151 dash_url, video_id, mpd_id='dash', headers=headers, fatal=False)
152 formats.extend(self._extract_m3u8_formats(
153 dash_url.replace('.mpd', '.m3u8').replace('/DASH/', '/HLS/'),
154 video_id, 'mp4', m3u8_id='hls', headers=headers, fatal=False))
155 for f in formats:
156 f.setdefault('http_headers', {}).update(headers)
157
158 metadata = self._call_api(
159 '1.6', 'IN/DETAIL/' + video_id, video_id)['containers'][0]['metadata']
160 title = metadata['episodeTitle']
161 subtitles = {}
162 for sub in content.get('subtitle', []):
163 sub_url = sub.get('subtitleUrl')
164 if not sub_url:
165 continue
166 subtitles.setdefault(sub.get('subtitleLanguageName', 'ENG'), []).append({
167 'url': sub_url,
168 })
169 return {
170 'id': video_id,
171 'title': title,
172 'formats': formats,
173 'thumbnail': content.get('posterURL'),
174 'description': metadata.get('longDescription') or metadata.get('shortDescription'),
175 'timestamp': int_or_none(metadata.get('creationDate'), 1000),
176 'duration': int_or_none(metadata.get('duration')),
177 'season_number': int_or_none(metadata.get('season')),
178 'series': metadata.get('title'),
179 'episode_number': int_or_none(metadata.get('episodeNumber')),
180 'release_year': int_or_none(metadata.get('year')),
181 'subtitles': subtitles,
182 }
183
184
185 class SonyLIVSeriesIE(InfoExtractor):
186 _VALID_URL = r'https?://(?:www\.)?sonyliv\.com/shows/[^/?#&]+-(?P<id>\d{10})$'
187 _TESTS = [{
188 'url': 'https://www.sonyliv.com/shows/adaalat-1700000091',
189 'playlist_mincount': 456,
190 'info_dict': {
191 'id': '1700000091',
192 },
193 }]
194 _API_SHOW_URL = "https://apiv2.sonyliv.com/AGL/1.9/R/ENG/WEB/IN/DL/DETAIL/{}?kids_safe=false&from=0&to=49"
195 _API_EPISODES_URL = "https://apiv2.sonyliv.com/AGL/1.4/R/ENG/WEB/IN/CONTENT/DETAIL/BUNDLE/{}?from=0&to=1000&orderBy=episodeNumber&sortOrder=asc"
196 _API_SECURITY_URL = 'https://apiv2.sonyliv.com/AGL/1.4/A/ENG/WEB/ALL/GETTOKEN'
197
198 def _entries(self, show_id):
199 headers = {
200 'Accept': 'application/json, text/plain, */*',
201 'Referer': 'https://www.sonyliv.com',
202 }
203 headers['security_token'] = self._download_json(
204 self._API_SECURITY_URL, video_id=show_id, headers=headers,
205 note='Downloading security token')['resultObj']
206 seasons = try_get(
207 self._download_json(self._API_SHOW_URL.format(show_id), video_id=show_id, headers=headers),
208 lambda x: x['resultObj']['containers'][0]['containers'], list)
209 for season in seasons or []:
210 season_id = season['id']
211 episodes = try_get(
212 self._download_json(self._API_EPISODES_URL.format(season_id), video_id=season_id, headers=headers),
213 lambda x: x['resultObj']['containers'][0]['containers'], list)
214 for episode in episodes or []:
215 video_id = episode.get('id')
216 yield self.url_result('sonyliv:%s' % video_id, ie=SonyLIVIE.ie_key(), video_id=video_id)
217
218 def _real_extract(self, url):
219 show_id = self._match_id(url)
220 return self.playlist_result(self._entries(show_id), playlist_id=show_id)