]>
Commit | Line | Data |
---|---|---|
85cd69ad RA |
1 | import hashlib |
2 | import hmac | |
1cb812d3 | 3 | import re |
85cd69ad | 4 | import time |
2533f5b6 | 5 | import uuid |
7078ec64 | 6 | import json |
909191de | 7 | |
fb8e402a | 8 | from .common import InfoExtractor |
2533f5b6 S |
9 | from ..compat import ( |
10 | compat_HTTPError, | |
6923b538 | 11 | compat_str |
2533f5b6 | 12 | ) |
fb8e402a | 13 | from ..utils import ( |
fb8e402a | 14 | determine_ext, |
909191de | 15 | ExtractorError, |
fb8e402a | 16 | int_or_none, |
a1ddaa89 | 17 | join_nonempty, |
2533f5b6 | 18 | str_or_none, |
05e7c184 | 19 | try_get, |
2533f5b6 | 20 | url_or_none, |
fb8e402a | 21 | ) |
22 | ||
23 | ||
909191de | 24 | class HotStarBaseIE(InfoExtractor): |
a1ddaa89 | 25 | _BASE_URL = 'https://www.hotstar.com' |
26 | _API_URL = 'https://api.hotstar.com' | |
85cd69ad RA |
27 | _AKAMAI_ENCRYPTION_KEY = b'\x05\xfc\x1a\x01\xca\xc9\x4b\xc4\x12\xfc\x53\x12\x07\x75\xf9\xee' |
28 | ||
fe07e2c6 | 29 | def _call_api_impl(self, path, video_id, query, st=None, cookies=None): |
9fc0de57 | 30 | st = int_or_none(st) or int(time.time()) |
85cd69ad RA |
31 | exp = st + 6000 |
32 | auth = 'st=%d~exp=%d~acl=/*' % (st, exp) | |
33 | auth += '~hmac=' + hmac.new(self._AKAMAI_ENCRYPTION_KEY, auth.encode(), hashlib.sha256).hexdigest() | |
6923b538 | 34 | |
b6a35ad8 | 35 | if cookies and cookies.get('userUP'): |
fe07e2c6 A |
36 | token = cookies.get('userUP').value |
37 | else: | |
38 | token = self._download_json( | |
a1ddaa89 | 39 | f'{self._API_URL}/um/v3/users', |
fe07e2c6 A |
40 | video_id, note='Downloading token', |
41 | data=json.dumps({"device_ids": [{"id": compat_str(uuid.uuid4()), "type": "device_id"}]}).encode('utf-8'), | |
42 | headers={ | |
43 | 'hotstarauth': auth, | |
44 | 'x-hs-platform': 'PCTV', # or 'web' | |
45 | 'Content-Type': 'application/json', | |
46 | })['user_identity'] | |
6923b538 | 47 | |
85cd69ad | 48 | response = self._download_json( |
a1ddaa89 | 49 | f'{self._API_URL}/{path}', video_id, query=query, |
50 | headers={ | |
85cd69ad | 51 | 'hotstarauth': auth, |
7078ec64 N |
52 | 'x-hs-appversion': '6.72.2', |
53 | 'x-hs-platform': 'web', | |
54 | 'x-hs-usertoken': token, | |
a1ddaa89 | 55 | }) |
6923b538 | 56 | |
7078ec64 | 57 | if response['message'] != "Playback URL's fetched successfully": |
85cd69ad | 58 | raise ExtractorError( |
7078ec64 N |
59 | response['message'], expected=True) |
60 | return response['data'] | |
909191de | 61 | |
2533f5b6 | 62 | def _call_api(self, path, video_id, query_name='contentId'): |
a1ddaa89 | 63 | return self._download_json( |
64 | f'{self._API_URL}/{path}', video_id=video_id, | |
65 | query={ | |
66 | query_name: video_id, | |
67 | 'tas': 10000, | |
68 | }, headers={ | |
69 | 'x-country-code': 'IN', | |
70 | 'x-platform-code': 'PCTV', | |
71 | }) | |
2533f5b6 | 72 | |
5d5c0f7e | 73 | def _call_api_v2(self, path, video_id, st=None, cookies=None): |
2533f5b6 | 74 | return self._call_api_impl( |
a1ddaa89 | 75 | f'{path}/content/{video_id}', video_id, st=st, cookies=cookies, query={ |
a64907d0 | 76 | 'desired-config': 'audio_channel:stereo|container:fmp4|dynamic_range:hdr|encryption:plain|ladder:tv|package:dash|resolution:fhd|subs-tag:HotstarVIP|video_codec:h265', |
fe07e2c6 | 77 | 'device-id': cookies.get('device_id').value if cookies.get('device_id') else compat_str(uuid.uuid4()), |
7078ec64 N |
78 | 'os-name': 'Windows', |
79 | 'os-version': '10', | |
2533f5b6 S |
80 | }) |
81 | ||
909191de S |
82 | |
83 | class HotStarIE(HotStarBaseIE): | |
85cd69ad | 84 | IE_NAME = 'hotstar' |
6e639032 | 85 | _VALID_URL = r'''(?x) |
a1ddaa89 | 86 | https?://(?:www\.)?hotstar\.com(?:/in)?/(?!in/) |
87 | (?: | |
88 | (?P<type>movies|sports|episode|(?P<tv>tv))/ | |
89 | (?(tv)(?:[^/?#]+/){2}|[^?#]*) | |
90 | )? | |
91 | [^/?#]+/ | |
92 | (?P<id>\d{10}) | |
93 | ''' | |
94 | ||
89d23f37 | 95 | _TESTS = [{ |
85cd69ad | 96 | 'url': 'https://www.hotstar.com/can-you-not-spread-rumours/1000076273', |
fb8e402a | 97 | 'info_dict': { |
98 | 'id': '1000076273', | |
99 | 'ext': 'mp4', | |
85cd69ad | 100 | 'title': 'Can You Not Spread Rumours?', |
fb8e402a | 101 | 'description': 'md5:c957d8868e9bc793ccb813691cc4c434', |
85cd69ad | 102 | 'timestamp': 1447248600, |
fb8e402a | 103 | 'upload_date': '20151111', |
104 | 'duration': 381, | |
a1ddaa89 | 105 | 'episode': 'Can You Not Spread Rumours?', |
fb8e402a | 106 | }, |
2533f5b6 | 107 | }, { |
2533f5b6 | 108 | 'url': 'https://www.hotstar.com/tv/ek-bhram-sarvagun-sampanna/s-2116/janhvi-targets-suman/1000234847', |
b6a35ad8 A |
109 | 'info_dict': { |
110 | 'id': '1000234847', | |
111 | 'ext': 'mp4', | |
112 | 'title': 'Janhvi Targets Suman', | |
113 | 'description': 'md5:78a85509348910bd1ca31be898c5796b', | |
114 | 'timestamp': 1556670600, | |
115 | 'upload_date': '20190501', | |
116 | 'duration': 1219, | |
117 | 'channel': 'StarPlus', | |
118 | 'channel_id': 3, | |
119 | 'series': 'Ek Bhram - Sarvagun Sampanna', | |
120 | 'season': 'Chapter 1', | |
121 | 'season_number': 1, | |
122 | 'season_id': 6771, | |
123 | 'episode': 'Janhvi Targets Suman', | |
124 | 'episode_number': 8, | |
a1ddaa89 | 125 | } |
b6a35ad8 | 126 | }, { |
a1ddaa89 | 127 | 'url': 'https://www.hotstar.com/movies/radha-gopalam/1000057157', |
128 | 'only_matching': True, | |
129 | }, { | |
130 | 'url': 'https://www.hotstar.com/in/sports/cricket/follow-the-blues-2021/recap-eng-fight-back-on-day-2/1260066104', | |
131 | 'only_matching': True, | |
132 | }, { | |
133 | 'url': 'https://www.hotstar.com/in/sports/football/most-costly-pl-transfers-ft-grealish/1260065956', | |
2533f5b6 | 134 | 'only_matching': True, |
89d23f37 | 135 | }] |
85cd69ad | 136 | _GEO_BYPASS = False |
a1ddaa89 | 137 | |
b6a35ad8 A |
138 | _TYPE = { |
139 | 'movies': 'movie', | |
140 | 'sports': 'match', | |
141 | 'episode': 'episode', | |
142 | 'tv': 'episode', | |
143 | None: 'content', | |
144 | } | |
fb8e402a | 145 | |
a1ddaa89 | 146 | _IGNORE_MAP = { |
147 | 'res': 'resolution', | |
148 | 'vcodec': 'video_codec', | |
149 | 'dr': 'dynamic_range', | |
150 | } | |
151 | ||
152 | @classmethod | |
153 | def _video_url(cls, video_id, video_type=None, *, slug='ignore_me', root=None): | |
154 | assert None in (video_type, root) | |
155 | if not root: | |
156 | root = join_nonempty(cls._BASE_URL, video_type, delim='/') | |
157 | return f'{root}/{slug}/{video_id}' | |
158 | ||
fb8e402a | 159 | def _real_extract(self, url): |
a1ddaa89 | 160 | video_id, video_type = self._match_valid_url(url).group('id', 'type') |
b6a35ad8 | 161 | video_type = self._TYPE.get(video_type, video_type) |
5d5c0f7e | 162 | cookies = self._get_cookies(url) # Cookies before any request |
0dac7cbb | 163 | |
a1ddaa89 | 164 | video_data = self._call_api(f'o/v1/{video_type}/detail', video_id)['body']['results']['item'] |
a06916d9 | 165 | if not self.get_param('allow_unplayable_formats') and video_data.get('drmProtected'): |
88acdbc2 | 166 | self.report_drm(video_id) |
167 | ||
a1ddaa89 | 168 | # See https://github.com/yt-dlp/yt-dlp/issues/396 |
169 | st = self._download_webpage_handle(f'{self._BASE_URL}/in', video_id)[1].headers.get('x-origin-date') | |
170 | ||
2533f5b6 | 171 | geo_restricted = False |
a1ddaa89 | 172 | formats, subs = [], {} |
173 | headers = {'Referer': f'{self._BASE_URL}/in'} | |
174 | ||
6923b538 | 175 | # change to v2 in the future |
5d5c0f7e | 176 | playback_sets = self._call_api_v2('play/v1/playback', video_id, st=st, cookies=cookies)['playBackSets'] |
2533f5b6 S |
177 | for playback_set in playback_sets: |
178 | if not isinstance(playback_set, dict): | |
179 | continue | |
a1ddaa89 | 180 | tags = str_or_none(playback_set.get('tagsCombination')) or '' |
181 | if any(f'{prefix}:{ignore}' in tags | |
182 | for key, prefix in self._IGNORE_MAP.items() | |
183 | for ignore in self._configuration_arg(key)): | |
184 | continue | |
185 | ||
2533f5b6 S |
186 | format_url = url_or_none(playback_set.get('playbackUrl')) |
187 | if not format_url: | |
188 | continue | |
a1ddaa89 | 189 | format_url = re.sub(r'(?<=//staragvod)(\d)', r'web\1', format_url) |
190 | dr = re.search(r'dynamic_range:(?P<dr>[a-z]+)', playback_set.get('tagsCombination')).group('dr') | |
2533f5b6 | 191 | ext = determine_ext(format_url) |
a1ddaa89 | 192 | |
6ff34542 | 193 | current_formats, current_subs = [], {} |
85cd69ad | 194 | try: |
2533f5b6 | 195 | if 'package:hls' in tags or ext == 'm3u8': |
6ff34542 | 196 | current_formats, current_subs = self._extract_m3u8_formats_and_subtitles( |
d9d30986 | 197 | format_url, video_id, 'mp4', |
d7def23d | 198 | entry_protocol='m3u8_native', |
a64907d0 | 199 | m3u8_id=f'{dr}-hls', headers=headers) |
2533f5b6 | 200 | elif 'package:dash' in tags or ext == 'mpd': |
6ff34542 | 201 | current_formats, current_subs = self._extract_mpd_formats_and_subtitles( |
a64907d0 | 202 | format_url, video_id, mpd_id=f'{dr}-dash', headers=headers) |
2533f5b6 | 203 | elif ext == 'f4m': |
a1ddaa89 | 204 | pass # XXX: produce broken files |
2533f5b6 | 205 | else: |
6ff34542 | 206 | current_formats = [{ |
2533f5b6 S |
207 | 'url': format_url, |
208 | 'width': int_or_none(playback_set.get('width')), | |
209 | 'height': int_or_none(playback_set.get('height')), | |
6ff34542 | 210 | }] |
85cd69ad RA |
211 | except ExtractorError as e: |
212 | if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403: | |
2533f5b6 S |
213 | geo_restricted = True |
214 | continue | |
a1ddaa89 | 215 | |
6ff34542 AG |
216 | if tags and 'encryption:plain' not in tags: |
217 | for f in current_formats: | |
218 | f['has_drm'] = True | |
525d9e0c AG |
219 | if tags and 'language' in tags: |
220 | lang = re.search(r'language:(?P<lang>[a-z]+)', tags).group('lang') | |
221 | for f in current_formats: | |
222 | if not f.get('langauge'): | |
223 | f['language'] = lang | |
a1ddaa89 | 224 | |
6ff34542 AG |
225 | formats.extend(current_formats) |
226 | subs = self._merge_subtitles(subs, current_subs) | |
a1ddaa89 | 227 | |
2533f5b6 | 228 | if not formats and geo_restricted: |
b7da73eb | 229 | self.raise_geo_restricted(countries=['IN'], metadata_available=True) |
fb8e402a | 230 | self._sort_formats(formats) |
d7def23d RA |
231 | for f in formats: |
232 | f.setdefault('http_headers', {}).update(headers) | |
233 | ||
fb8e402a | 234 | return { |
235 | 'id': video_id, | |
a1ddaa89 | 236 | 'title': video_data.get('title'), |
fb8e402a | 237 | 'description': video_data.get('description'), |
238 | 'duration': int_or_none(video_data.get('duration')), | |
85cd69ad | 239 | 'timestamp': int_or_none(video_data.get('broadcastDate') or video_data.get('startDate')), |
fb8e402a | 240 | 'formats': formats, |
b6a35ad8 | 241 | 'subtitles': subs, |
85cd69ad RA |
242 | 'channel': video_data.get('channelName'), |
243 | 'channel_id': video_data.get('channelId'), | |
244 | 'series': video_data.get('showName'), | |
245 | 'season': video_data.get('seasonName'), | |
246 | 'season_number': int_or_none(video_data.get('seasonNo')), | |
247 | 'season_id': video_data.get('seasonId'), | |
a1ddaa89 | 248 | 'episode': video_data.get('title'), |
85cd69ad | 249 | 'episode_number': int_or_none(video_data.get('episodeNo')), |
fb8e402a | 250 | } |
477c97f8 AV |
251 | |
252 | ||
a1ddaa89 | 253 | class HotStarPrefixIE(InfoExtractor): |
254 | """ The "hotstar:" prefix is no longer in use, but this is kept for backward compatibility """ | |
255 | IE_DESC = False | |
256 | _VALID_URL = r'hotstar:(?:(?P<type>\w+):)?(?P<id>\d+)$' | |
257 | _TESTS = [{ | |
258 | 'url': 'hotstar:1000076273', | |
259 | 'only_matching': True, | |
260 | }, { | |
261 | 'url': 'hotstar:movies:1000057157', | |
262 | 'info_dict': { | |
263 | 'id': '1000057157', | |
264 | 'ext': 'mp4', | |
265 | 'title': 'Radha Gopalam', | |
266 | 'description': 'md5:be3bc342cc120bbc95b3b0960e2b0d22', | |
267 | 'timestamp': 1140805800, | |
268 | 'upload_date': '20060224', | |
269 | 'duration': 9182, | |
270 | 'episode': 'Radha Gopalam', | |
271 | }, | |
272 | }, { | |
273 | 'url': 'hotstar:episode:1000234847', | |
274 | 'only_matching': True, | |
275 | }, { | |
276 | # contentData | |
277 | 'url': 'hotstar:sports:1260065956', | |
278 | 'only_matching': True, | |
279 | }, { | |
280 | # contentData | |
281 | 'url': 'hotstar:sports:1260066104', | |
282 | 'only_matching': True, | |
283 | }] | |
284 | ||
285 | def _real_extract(self, url): | |
286 | video_id, video_type = self._match_valid_url(url).group('id', 'type') | |
287 | return self.url_result(HotStarIE._video_url(video_id, video_type), HotStarIE, video_id) | |
288 | ||
289 | ||
909191de | 290 | class HotStarPlaylistIE(HotStarBaseIE): |
477c97f8 | 291 | IE_NAME = 'hotstar:playlist' |
85cd69ad | 292 | _VALID_URL = r'https?://(?:www\.)?hotstar\.com/tv/[^/]+/s-\w+/list/[^/]+/t-(?P<id>\w+)' |
477c97f8 | 293 | _TESTS = [{ |
85cd69ad | 294 | 'url': 'https://www.hotstar.com/tv/savdhaan-india/s-26/list/popular-clips/t-3_2_26', |
477c97f8 | 295 | 'info_dict': { |
85cd69ad | 296 | 'id': '3_2_26', |
477c97f8 | 297 | }, |
85cd69ad | 298 | 'playlist_mincount': 20, |
477c97f8 | 299 | }, { |
85cd69ad | 300 | 'url': 'https://www.hotstar.com/tv/savdhaan-india/s-26/list/extras/t-2480', |
477c97f8 AV |
301 | 'only_matching': True, |
302 | }] | |
477c97f8 AV |
303 | |
304 | def _real_extract(self, url): | |
85cd69ad RA |
305 | playlist_id = self._match_id(url) |
306 | ||
b6a35ad8 | 307 | collection = self._call_api('o/v1/tray/find', playlist_id, 'uqId')['body']['results'] |
477c97f8 | 308 | entries = [ |
a1ddaa89 | 309 | self.url_result(HotStarIE._video_url(video['contentId']), HotStarIE, video['contentId']) |
310 | for video in collection['assets']['items'] if video.get('contentId')] | |
909191de S |
311 | |
312 | return self.playlist_result(entries, playlist_id) | |
6e639032 A |
313 | |
314 | ||
315 | class HotStarSeriesIE(HotStarBaseIE): | |
316 | IE_NAME = 'hotstar:series' | |
73f035e1 | 317 | _VALID_URL = r'(?P<url>https?://(?:www\.)?hotstar\.com(?:/in)?/tv/[^/]+/(?P<id>\d+))' |
6e639032 A |
318 | _TESTS = [{ |
319 | 'url': 'https://www.hotstar.com/in/tv/radhakrishn/1260000646', | |
320 | 'info_dict': { | |
321 | 'id': '1260000646', | |
322 | }, | |
323 | 'playlist_mincount': 690, | |
324 | }, { | |
325 | 'url': 'https://www.hotstar.com/tv/dancee-/1260050431', | |
326 | 'info_dict': { | |
327 | 'id': '1260050431', | |
328 | }, | |
329 | 'playlist_mincount': 43, | |
8242bf22 A |
330 | }, { |
331 | 'url': 'https://www.hotstar.com/in/tv/mahabharat/435/', | |
332 | 'info_dict': { | |
333 | 'id': '435', | |
334 | }, | |
335 | 'playlist_mincount': 269, | |
6e639032 A |
336 | }] |
337 | ||
338 | def _real_extract(self, url): | |
81bcd43a | 339 | url, series_id = self._match_valid_url(url).groups() |
6e639032 A |
340 | headers = { |
341 | 'x-country-code': 'IN', | |
342 | 'x-platform-code': 'PCTV', | |
343 | } | |
a1ddaa89 | 344 | detail_json = self._download_json( |
345 | f'{self._API_URL}/o/v1/show/detail?contentId={series_id}', series_id, headers=headers) | |
346 | id = try_get(detail_json, lambda x: x['body']['results']['item']['id'], int) | |
347 | item_json = self._download_json( | |
348 | f'{self._API_URL}/o/v1/tray/g/1/items?etid=0&tao=0&tas=10000&eid={id}', series_id, headers=headers) | |
6e639032 | 349 | |
a1ddaa89 | 350 | return self.playlist_result([ |
351 | self.url_result(HotStarIE._video_url(video['contentId'], root=url), HotStarIE, video['contentId']) | |
352 | for video in item_json['body']['results']['items'] if video.get('contentId') | |
353 | ], series_id) |