2 from __future__ import unicode_literals
11 from .common import InfoExtractor
12 from ..compat import (
26 class HotStarBaseIE(InfoExtractor):
27 _AKAMAI_ENCRYPTION_KEY = b'\x05\xfc\x1a\x01\xca\xc9\x4b\xc4\x12\xfc\x53\x12\x07\x75\xf9\xee'
29 def _call_api_impl(self, path, video_id, headers, query, data=None):
32 auth = 'st=%d~exp=%d~acl=/*' % (st, exp)
33 auth += '~hmac=' + hmac.new(self._AKAMAI_ENCRYPTION_KEY, auth.encode(), hashlib.sha256).hexdigest()
34 h = {'hotstarauth': auth}
36 return self._download_json(
37 'https://api.hotstar.com/' + path,
38 video_id, headers=h, query=query, data=data)
40 def _call_api(self, path, video_id, query_name='contentId'):
41 response = self._call_api_impl(path, video_id, {
42 'x-country-code': 'IN',
43 'x-platform-code': 'JIO',
48 if response['statusCode'] != 'OK':
50 response['body']['message'], expected=True)
51 return response['body']['results']
53 def _call_api_v2(self, path, video_id, headers, query=None, data=None):
54 h = {'X-Request-Id': compat_str(uuid.uuid4())}
57 return self._call_api_impl(
58 path, video_id, h, query, data)
59 except ExtractorError as e:
60 if isinstance(e.cause, compat_HTTPError):
61 if e.cause.code == 402:
62 self.raise_login_required()
63 message = self._parse_json(e.cause.read().decode(), video_id)['message']
64 if message in ('Content not available in region', 'Country is not supported'):
65 raise self.raise_geo_restricted(message)
66 raise ExtractorError(message)
70 class HotStarIE(HotStarBaseIE):
72 _VALID_URL = r'https?://(?:www\.)?hotstar\.com/(?:.+[/-])?(?P<id>\d{10})'
75 'url': 'https://www.hotstar.com/can-you-not-spread-rumours/1000076273',
79 'title': 'Can You Not Spread Rumours?',
80 'description': 'md5:c957d8868e9bc793ccb813691cc4c434',
81 'timestamp': 1447248600,
82 'upload_date': '20151111',
87 'skip_download': True,
91 'url': 'https://www.hotstar.com/movies/radha-gopalam/1000057157',
92 'only_matching': True,
94 'url': 'http://www.hotstar.com/sports/cricket/rajitha-sizzles-on-debut-with-329/2001477583',
95 'only_matching': True,
97 'url': 'http://www.hotstar.com/1000000515',
98 'only_matching': True,
100 # only available via api v2
101 'url': 'https://www.hotstar.com/tv/ek-bhram-sarvagun-sampanna/s-2116/janhvi-targets-suman/1000234847',
102 'only_matching': True,
104 'url': 'https://www.hotstar.com/in/tv/start-music/1260005217/cooks-vs-comalis/1100039717',
105 'only_matching': True,
111 def _real_extract(self, url):
112 video_id = self._match_id(url)
114 webpage = self._download_webpage(url, video_id)
115 app_state = self._parse_json(self._search_regex(
116 r'<script>window\.APP_STATE\s*=\s*({.+?})</script>',
117 webpage, 'app state'), video_id)
120 lambda x, k=k: x['initialState']['content%s' % k]['content']
121 for k in ('Data', 'Detail')
123 for v in app_state.values():
124 content = try_get(v, getters, dict)
125 if content and content.get('contentId') == video_id:
129 title = video_data['title']
131 if video_data.get('drmProtected'):
132 raise ExtractorError('This video is DRM protected.', expected=True)
134 headers = {'Referer': url}
136 geo_restricted = False
138 if not self._USER_TOKEN:
139 self._DEVICE_ID = compat_str(uuid.uuid4())
140 self._USER_TOKEN = self._call_api_v2('um/v3/users', video_id, {
141 'X-HS-Platform': 'PCTV',
142 'Content-Type': 'application/json',
145 'id': self._DEVICE_ID,
148 }).encode())['user_identity']
150 playback_sets = self._call_api_v2(
151 'play/v2/playback/content/' + video_id, video_id, {
152 'X-HS-Platform': 'web',
153 'X-HS-AppVersion': '6.99.1',
154 'X-HS-UserToken': self._USER_TOKEN,
156 'device-id': self._DEVICE_ID,
157 'desired-config': 'encryption:plain',
158 'os-name': 'Windows',
160 })['data']['playBackSets']
161 for playback_set in playback_sets:
162 if not isinstance(playback_set, dict):
164 format_url = url_or_none(playback_set.get('playbackUrl'))
168 r'(?<=//staragvod)(\d)', r'web\1', format_url)
169 tags = str_or_none(playback_set.get('tagsCombination')) or ''
170 if tags and 'encryption:plain' not in tags:
172 ext = determine_ext(format_url)
174 if 'package:hls' in tags or ext == 'm3u8':
175 formats.extend(self._extract_m3u8_formats(
176 format_url, video_id, 'mp4',
177 entry_protocol='m3u8_native',
178 m3u8_id='hls', headers=headers))
179 elif 'package:dash' in tags or ext == 'mpd':
180 formats.extend(self._extract_mpd_formats(
181 format_url, video_id, mpd_id='dash', headers=headers))
183 # produce broken files
188 'width': int_or_none(playback_set.get('width')),
189 'height': int_or_none(playback_set.get('height')),
191 except ExtractorError as e:
192 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
193 geo_restricted = True
195 if not formats and geo_restricted:
196 self.raise_geo_restricted(countries=['IN'])
197 self._sort_formats(formats)
200 f.setdefault('http_headers', {}).update(headers)
202 image = try_get(video_data, lambda x: x['image']['h'], compat_str)
207 'thumbnail': 'https://img1.hotstarext.com/image/upload/' + image if image else None,
208 'description': video_data.get('description'),
209 'duration': int_or_none(video_data.get('duration')),
210 'timestamp': int_or_none(video_data.get('broadcastDate') or video_data.get('startDate')),
212 'channel': video_data.get('channelName'),
213 'channel_id': str_or_none(video_data.get('channelId')),
214 'series': video_data.get('showName'),
215 'season': video_data.get('seasonName'),
216 'season_number': int_or_none(video_data.get('seasonNo')),
217 'season_id': str_or_none(video_data.get('seasonId')),
219 'episode_number': int_or_none(video_data.get('episodeNo')),
223 class HotStarPlaylistIE(HotStarBaseIE):
224 IE_NAME = 'hotstar:playlist'
225 _VALID_URL = r'https?://(?:www\.)?hotstar\.com/(?:[a-z]{2}/)?tv/[^/]+/s-\w+/list/[^/]+/t-(?P<id>\w+)'
227 'url': 'https://www.hotstar.com/tv/savdhaan-india/s-26/list/popular-clips/t-3_2_26',
231 'playlist_mincount': 20,
233 'url': 'https://www.hotstar.com/tv/savdhaan-india/s-26/list/extras/t-2480',
234 'only_matching': True,
236 'url': 'https://www.hotstar.com/us/tv/masterchef-india/s-830/list/episodes/t-1_2_830',
237 'only_matching': True,
240 def _real_extract(self, url):
241 playlist_id = self._match_id(url)
243 collection = self._call_api('o/v1/tray/find', playlist_id, 'uqId')
247 'https://www.hotstar.com/%s' % video['contentId'],
248 ie=HotStarIE.ie_key(), video_id=video['contentId'])
249 for video in collection['assets']['items']
250 if video.get('contentId')]
252 return self.playlist_result(entries, playlist_id)