]>
Commit | Line | Data |
---|---|---|
fb8e402a | 1 | # coding: utf-8 |
2 | from __future__ import unicode_literals | |
3 | ||
85cd69ad RA |
4 | import hashlib |
5 | import hmac | |
1cb812d3 | 6 | import re |
85cd69ad | 7 | import time |
2533f5b6 | 8 | import uuid |
7078ec64 | 9 | import json |
6923b538 | 10 | import random |
909191de | 11 | |
fb8e402a | 12 | from .common import InfoExtractor |
2533f5b6 S |
13 | from ..compat import ( |
14 | compat_HTTPError, | |
6923b538 | 15 | compat_str |
2533f5b6 | 16 | ) |
fb8e402a | 17 | from ..utils import ( |
fb8e402a | 18 | determine_ext, |
909191de | 19 | ExtractorError, |
fb8e402a | 20 | int_or_none, |
2533f5b6 | 21 | str_or_none, |
05e7c184 | 22 | try_get, |
2533f5b6 | 23 | url_or_none, |
fb8e402a | 24 | ) |
25 | ||
26 | ||
909191de | 27 | class HotStarBaseIE(InfoExtractor): |
85cd69ad RA |
28 | _AKAMAI_ENCRYPTION_KEY = b'\x05\xfc\x1a\x01\xca\xc9\x4b\xc4\x12\xfc\x53\x12\x07\x75\xf9\xee' |
29 | ||
2533f5b6 | 30 | def _call_api_impl(self, path, video_id, query): |
85cd69ad RA |
31 | st = int(time.time()) |
32 | exp = st + 6000 | |
33 | auth = 'st=%d~exp=%d~acl=/*' % (st, exp) | |
34 | auth += '~hmac=' + hmac.new(self._AKAMAI_ENCRYPTION_KEY, auth.encode(), hashlib.sha256).hexdigest() | |
6923b538 U |
35 | |
36 | def _generate_device_id(): | |
37 | """ | |
38 | Reversed from javascript library. | |
39 | JS function is generateUUID | |
40 | """ | |
41 | t = int(round(time.time() * 1000)) | |
42 | e = "xxxxxxxx-xxxx-4xxx-xxxx-xxxxxxxxxxxx" # 4 seems to be interchangeable | |
43 | ||
44 | def _replacer(): | |
45 | n = int((t + 16 * random.random())) % 16 | 0 | |
46 | return hex(n if "x" == e else 3 & n | 8)[2:] | |
47 | return "".join([_.replace('x', _replacer()) for _ in e]) | |
48 | ||
7078ec64 | 49 | token = self._download_json( |
6923b538 | 50 | 'https://api.hotstar.com/um/v3/users', |
7078ec64 | 51 | video_id, note='Downloading token', |
6923b538 | 52 | data=json.dumps({"device_ids": [{"id": compat_str(uuid.uuid4()), "type": "device_id"}]}).encode('utf-8'), |
7078ec64 N |
53 | headers={ |
54 | 'hotstarauth': auth, | |
6923b538 | 55 | 'x-hs-platform': 'PCTV', # or 'web' |
7078ec64 | 56 | 'Content-Type': 'application/json', |
6923b538 U |
57 | })['user_identity'] |
58 | ||
85cd69ad | 59 | response = self._download_json( |
2533f5b6 | 60 | 'https://api.hotstar.com/' + path, video_id, headers={ |
85cd69ad | 61 | 'hotstarauth': auth, |
7078ec64 N |
62 | 'x-hs-appversion': '6.72.2', |
63 | 'x-hs-platform': 'web', | |
64 | 'x-hs-usertoken': token, | |
2533f5b6 | 65 | }, query=query) |
6923b538 | 66 | |
7078ec64 | 67 | if response['message'] != "Playback URL's fetched successfully": |
85cd69ad | 68 | raise ExtractorError( |
7078ec64 N |
69 | response['message'], expected=True) |
70 | return response['data'] | |
909191de | 71 | |
2533f5b6 S |
72 | def _call_api(self, path, video_id, query_name='contentId'): |
73 | return self._call_api_impl(path, video_id, { | |
74 | query_name: video_id, | |
75 | 'tas': 10000, | |
76 | }) | |
77 | ||
78 | def _call_api_v2(self, path, video_id): | |
79 | return self._call_api_impl( | |
7078ec64 | 80 | '%s/content/%s' % (path, video_id), video_id, { |
6923b538 | 81 | 'desired-config': 'audio_channel:stereo|dynamic_range:sdr|encryption:plain|ladder:tv|package:dash|resolution:hd|subs-tag:HotstarVIP|video_codec:vp9', |
7078ec64 N |
82 | 'device-id': compat_str(uuid.uuid4()), |
83 | 'os-name': 'Windows', | |
84 | 'os-version': '10', | |
2533f5b6 S |
85 | }) |
86 | ||
909191de S |
87 | |
88 | class HotStarIE(HotStarBaseIE): | |
85cd69ad | 89 | IE_NAME = 'hotstar' |
35d3b674 | 90 | _VALID_URL = r'https?://(?:www\.)?hotstar\.com/.*(?P<id>\d{10})' |
89d23f37 | 91 | _TESTS = [{ |
adbbdefc | 92 | # contentData |
85cd69ad | 93 | 'url': 'https://www.hotstar.com/can-you-not-spread-rumours/1000076273', |
fb8e402a | 94 | 'info_dict': { |
95 | 'id': '1000076273', | |
96 | 'ext': 'mp4', | |
85cd69ad | 97 | 'title': 'Can You Not Spread Rumours?', |
fb8e402a | 98 | 'description': 'md5:c957d8868e9bc793ccb813691cc4c434', |
85cd69ad | 99 | 'timestamp': 1447248600, |
fb8e402a | 100 | 'upload_date': '20151111', |
101 | 'duration': 381, | |
102 | }, | |
103 | 'params': { | |
104 | # m3u8 download | |
105 | 'skip_download': True, | |
106 | } | |
adbbdefc S |
107 | }, { |
108 | # contentDetail | |
109 | 'url': 'https://www.hotstar.com/movies/radha-gopalam/1000057157', | |
110 | 'only_matching': True, | |
89d23f37 S |
111 | }, { |
112 | 'url': 'http://www.hotstar.com/sports/cricket/rajitha-sizzles-on-debut-with-329/2001477583', | |
113 | 'only_matching': True, | |
114 | }, { | |
115 | 'url': 'http://www.hotstar.com/1000000515', | |
116 | 'only_matching': True, | |
2533f5b6 S |
117 | }, { |
118 | # only available via api v2 | |
119 | 'url': 'https://www.hotstar.com/tv/ek-bhram-sarvagun-sampanna/s-2116/janhvi-targets-suman/1000234847', | |
120 | 'only_matching': True, | |
89d23f37 | 121 | }] |
85cd69ad | 122 | _GEO_BYPASS = False |
fb8e402a | 123 | |
fb8e402a | 124 | def _real_extract(self, url): |
125 | video_id = self._match_id(url) | |
909191de | 126 | |
85cd69ad RA |
127 | webpage = self._download_webpage(url, video_id) |
128 | app_state = self._parse_json(self._search_regex( | |
129 | r'<script>window\.APP_STATE\s*=\s*({.+?})</script>', | |
130 | webpage, 'app state'), video_id) | |
05e7c184 | 131 | video_data = {} |
c3c098dc | 132 | getters = list( |
adbbdefc S |
133 | lambda x, k=k: x['initialState']['content%s' % k]['content'] |
134 | for k in ('Data', 'Detail') | |
135 | ) | |
05e7c184 | 136 | for v in app_state.values(): |
adbbdefc | 137 | content = try_get(v, getters, dict) |
05e7c184 RA |
138 | if content and content.get('contentId') == video_id: |
139 | video_data = content | |
c3c098dc | 140 | break |
909191de | 141 | |
85cd69ad | 142 | title = video_data['title'] |
0dac7cbb | 143 | |
a06916d9 | 144 | if not self.get_param('allow_unplayable_formats') and video_data.get('drmProtected'): |
0dac7cbb | 145 | raise ExtractorError('This video is DRM protected.', expected=True) |
fb8e402a | 146 | |
d7def23d | 147 | headers = {'Referer': url} |
fb8e402a | 148 | formats = [] |
2533f5b6 | 149 | geo_restricted = False |
6923b538 | 150 | # change to v2 in the future |
7078ec64 | 151 | playback_sets = self._call_api_v2('play/v1/playback', video_id)['playBackSets'] |
2533f5b6 S |
152 | for playback_set in playback_sets: |
153 | if not isinstance(playback_set, dict): | |
154 | continue | |
155 | format_url = url_or_none(playback_set.get('playbackUrl')) | |
156 | if not format_url: | |
157 | continue | |
1cb812d3 S |
158 | format_url = re.sub( |
159 | r'(?<=//staragvod)(\d)', r'web\1', format_url) | |
2533f5b6 S |
160 | tags = str_or_none(playback_set.get('tagsCombination')) or '' |
161 | if tags and 'encryption:plain' not in tags: | |
162 | continue | |
163 | ext = determine_ext(format_url) | |
85cd69ad | 164 | try: |
2533f5b6 S |
165 | if 'package:hls' in tags or ext == 'm3u8': |
166 | formats.extend(self._extract_m3u8_formats( | |
d9d30986 | 167 | format_url, video_id, 'mp4', |
d7def23d RA |
168 | entry_protocol='m3u8_native', |
169 | m3u8_id='hls', headers=headers)) | |
2533f5b6 S |
170 | elif 'package:dash' in tags or ext == 'mpd': |
171 | formats.extend(self._extract_mpd_formats( | |
d7def23d | 172 | format_url, video_id, mpd_id='dash', headers=headers)) |
2533f5b6 S |
173 | elif ext == 'f4m': |
174 | # produce broken files | |
175 | pass | |
176 | else: | |
177 | formats.append({ | |
178 | 'url': format_url, | |
179 | 'width': int_or_none(playback_set.get('width')), | |
180 | 'height': int_or_none(playback_set.get('height')), | |
181 | }) | |
85cd69ad RA |
182 | except ExtractorError as e: |
183 | if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403: | |
2533f5b6 S |
184 | geo_restricted = True |
185 | continue | |
186 | if not formats and geo_restricted: | |
b7da73eb | 187 | self.raise_geo_restricted(countries=['IN'], metadata_available=True) |
fb8e402a | 188 | self._sort_formats(formats) |
189 | ||
d7def23d RA |
190 | for f in formats: |
191 | f.setdefault('http_headers', {}).update(headers) | |
192 | ||
fb8e402a | 193 | return { |
194 | 'id': video_id, | |
0dac7cbb | 195 | 'title': title, |
fb8e402a | 196 | 'description': video_data.get('description'), |
197 | 'duration': int_or_none(video_data.get('duration')), | |
85cd69ad | 198 | 'timestamp': int_or_none(video_data.get('broadcastDate') or video_data.get('startDate')), |
fb8e402a | 199 | 'formats': formats, |
85cd69ad RA |
200 | 'channel': video_data.get('channelName'), |
201 | 'channel_id': video_data.get('channelId'), | |
202 | 'series': video_data.get('showName'), | |
203 | 'season': video_data.get('seasonName'), | |
204 | 'season_number': int_or_none(video_data.get('seasonNo')), | |
205 | 'season_id': video_data.get('seasonId'), | |
0dac7cbb | 206 | 'episode': title, |
85cd69ad | 207 | 'episode_number': int_or_none(video_data.get('episodeNo')), |
fb8e402a | 208 | } |
477c97f8 AV |
209 | |
210 | ||
909191de | 211 | class HotStarPlaylistIE(HotStarBaseIE): |
477c97f8 | 212 | IE_NAME = 'hotstar:playlist' |
85cd69ad | 213 | _VALID_URL = r'https?://(?:www\.)?hotstar\.com/tv/[^/]+/s-\w+/list/[^/]+/t-(?P<id>\w+)' |
477c97f8 | 214 | _TESTS = [{ |
85cd69ad | 215 | 'url': 'https://www.hotstar.com/tv/savdhaan-india/s-26/list/popular-clips/t-3_2_26', |
477c97f8 | 216 | 'info_dict': { |
85cd69ad | 217 | 'id': '3_2_26', |
477c97f8 | 218 | }, |
85cd69ad | 219 | 'playlist_mincount': 20, |
477c97f8 | 220 | }, { |
85cd69ad | 221 | 'url': 'https://www.hotstar.com/tv/savdhaan-india/s-26/list/extras/t-2480', |
477c97f8 AV |
222 | 'only_matching': True, |
223 | }] | |
477c97f8 AV |
224 | |
225 | def _real_extract(self, url): | |
85cd69ad RA |
226 | playlist_id = self._match_id(url) |
227 | ||
228 | collection = self._call_api('o/v1/tray/find', playlist_id, 'uqId') | |
477c97f8 | 229 | |
477c97f8 | 230 | entries = [ |
909191de | 231 | self.url_result( |
85cd69ad | 232 | 'https://www.hotstar.com/%s' % video['contentId'], |
909191de | 233 | ie=HotStarIE.ie_key(), video_id=video['contentId']) |
85cd69ad | 234 | for video in collection['assets']['items'] |
909191de S |
235 | if video.get('contentId')] |
236 | ||
237 | return self.playlist_result(entries, playlist_id) |