2 from __future__ import unicode_literals
10 from .common import InfoExtractor
11 from ..compat import (
25 class HotStarBaseIE(InfoExtractor):
26 _AKAMAI_ENCRYPTION_KEY = b'\x05\xfc\x1a\x01\xca\xc9\x4b\xc4\x12\xfc\x53\x12\x07\x75\xf9\xee'
28 def _call_api_impl(self, path, video_id, query):
31 auth = 'st=%d~exp=%d~acl=/*' % (st, exp)
32 auth += '~hmac=' + hmac.new(self._AKAMAI_ENCRYPTION_KEY, auth.encode(), hashlib.sha256).hexdigest()
33 response = self._download_json(
34 'https://api.hotstar.com/' + path, video_id, headers={
36 'x-country-code': 'IN',
37 'x-platform-code': 'JIO',
39 if response['statusCode'] != 'OK':
41 response['body']['message'], expected=True)
42 return response['body']['results']
44 def _call_api(self, path, video_id, query_name='contentId'):
45 return self._call_api_impl(path, video_id, {
50 def _call_api_v2(self, path, video_id):
51 return self._call_api_impl(
52 '%s/in/contents/%s' % (path, video_id), video_id, {
53 'desiredConfig': 'encryption:plain;ladder:phone,tv;package:hls,dash',
55 'clientVersion': '6.18.0',
56 'deviceId': compat_str(uuid.uuid4()),
62 class HotStarIE(HotStarBaseIE):
64 _VALID_URL = r'https?://(?:www\.)?hotstar\.com/(?:.+?[/-])?(?P<id>\d{10})'
67 'url': 'https://www.hotstar.com/can-you-not-spread-rumours/1000076273',
71 'title': 'Can You Not Spread Rumours?',
72 'description': 'md5:c957d8868e9bc793ccb813691cc4c434',
73 'timestamp': 1447248600,
74 'upload_date': '20151111',
79 'skip_download': True,
83 'url': 'https://www.hotstar.com/movies/radha-gopalam/1000057157',
84 'only_matching': True,
86 'url': 'http://www.hotstar.com/sports/cricket/rajitha-sizzles-on-debut-with-329/2001477583',
87 'only_matching': True,
89 'url': 'http://www.hotstar.com/1000000515',
90 'only_matching': True,
92 # only available via api v2
93 'url': 'https://www.hotstar.com/tv/ek-bhram-sarvagun-sampanna/s-2116/janhvi-targets-suman/1000234847',
94 'only_matching': True,
98 def _real_extract(self, url):
99 video_id = self._match_id(url)
101 webpage = self._download_webpage(url, video_id)
102 app_state = self._parse_json(self._search_regex(
103 r'<script>window\.APP_STATE\s*=\s*({.+?})</script>',
104 webpage, 'app state'), video_id)
107 lambda x, k=k: x['initialState']['content%s' % k]['content']
108 for k in ('Data', 'Detail')
110 for v in app_state.values():
111 content = try_get(v, getters, dict)
112 if content and content.get('contentId') == video_id:
116 title = video_data['title']
118 if video_data.get('drmProtected'):
119 raise ExtractorError('This video is DRM protected.', expected=True)
121 headers = {'Referer': url}
123 geo_restricted = False
124 playback_sets = self._call_api_v2('h/v2/play', video_id)['playBackSets']
125 for playback_set in playback_sets:
126 if not isinstance(playback_set, dict):
128 format_url = url_or_none(playback_set.get('playbackUrl'))
132 r'(?<=//staragvod)(\d)', r'web\1', format_url)
133 tags = str_or_none(playback_set.get('tagsCombination')) or ''
134 if tags and 'encryption:plain' not in tags:
136 ext = determine_ext(format_url)
138 if 'package:hls' in tags or ext == 'm3u8':
139 formats.extend(self._extract_m3u8_formats(
140 format_url, video_id, 'mp4',
141 entry_protocol='m3u8_native',
142 m3u8_id='hls', headers=headers))
143 elif 'package:dash' in tags or ext == 'mpd':
144 formats.extend(self._extract_mpd_formats(
145 format_url, video_id, mpd_id='dash', headers=headers))
147 # produce broken files
152 'width': int_or_none(playback_set.get('width')),
153 'height': int_or_none(playback_set.get('height')),
155 except ExtractorError as e:
156 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
157 geo_restricted = True
159 if not formats and geo_restricted:
160 self.raise_geo_restricted(countries=['IN'])
161 self._sort_formats(formats)
164 f.setdefault('http_headers', {}).update(headers)
169 'description': video_data.get('description'),
170 'duration': int_or_none(video_data.get('duration')),
171 'timestamp': int_or_none(video_data.get('broadcastDate') or video_data.get('startDate')),
173 'channel': video_data.get('channelName'),
174 'channel_id': video_data.get('channelId'),
175 'series': video_data.get('showName'),
176 'season': video_data.get('seasonName'),
177 'season_number': int_or_none(video_data.get('seasonNo')),
178 'season_id': video_data.get('seasonId'),
180 'episode_number': int_or_none(video_data.get('episodeNo')),
184 class HotStarPlaylistIE(HotStarBaseIE):
185 IE_NAME = 'hotstar:playlist'
186 _VALID_URL = r'https?://(?:www\.)?hotstar\.com/tv/[^/]+/s-\w+/list/[^/]+/t-(?P<id>\w+)'
188 'url': 'https://www.hotstar.com/tv/savdhaan-india/s-26/list/popular-clips/t-3_2_26',
192 'playlist_mincount': 20,
194 'url': 'https://www.hotstar.com/tv/savdhaan-india/s-26/list/extras/t-2480',
195 'only_matching': True,
198 def _real_extract(self, url):
199 playlist_id = self._match_id(url)
201 collection = self._call_api('o/v1/tray/find', playlist_id, 'uqId')
205 'https://www.hotstar.com/%s' % video['contentId'],
206 ie=HotStarIE.ie_key(), video_id=video['contentId'])
207 for video in collection['assets']['items']
208 if video.get('contentId')]
210 return self.playlist_result(entries, playlist_id)