]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/hotstar.py
[extractor] Better error message for DRM (#729)
[yt-dlp.git] / yt_dlp / extractor / hotstar.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import hashlib
5 import hmac
6 import re
7 import time
8 import uuid
9 import json
10
11 from .common import InfoExtractor
12 from ..compat import (
13 compat_HTTPError,
14 compat_str
15 )
16 from ..utils import (
17 determine_ext,
18 ExtractorError,
19 int_or_none,
20 str_or_none,
21 try_get,
22 url_or_none,
23 )
24
25
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'
28
29 def _call_api_impl(self, path, video_id, query, st=None, cookies=None):
30 st = int_or_none(st) or int(time.time())
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()
34
35 if cookies and cookies.get('userUP'):
36 token = cookies.get('userUP').value
37 else:
38 token = self._download_json(
39 'https://api.hotstar.com/um/v3/users',
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']
47
48 response = self._download_json(
49 'https://api.hotstar.com/' + path, video_id, headers={
50 'hotstarauth': auth,
51 'x-hs-appversion': '6.72.2',
52 'x-hs-platform': 'web',
53 'x-hs-usertoken': token,
54 }, query=query)
55
56 if response['message'] != "Playback URL's fetched successfully":
57 raise ExtractorError(
58 response['message'], expected=True)
59 return response['data']
60
61 def _call_api(self, path, video_id, query_name='contentId'):
62 return self._download_json('https://api.hotstar.com/' + path, video_id=video_id, query={
63 query_name: video_id,
64 'tas': 10000,
65 }, headers={
66 'x-country-code': 'IN',
67 'x-platform-code': 'PCTV',
68 })
69
70 def _call_api_v2(self, path, video_id, st=None, cookies=None):
71 return self._call_api_impl(
72 '%s/content/%s' % (path, video_id), video_id, st=st, cookies=cookies, query={
73 'desired-config': 'audio_channel:stereo|dynamic_range:sdr|encryption:plain|ladder:tv|package:dash|resolution:hd|subs-tag:HotstarVIP|video_codec:vp9',
74 'device-id': cookies.get('device_id').value if cookies.get('device_id') else compat_str(uuid.uuid4()),
75 'os-name': 'Windows',
76 'os-version': '10',
77 })
78
79
80 class HotStarIE(HotStarBaseIE):
81 IE_NAME = 'hotstar'
82 _VALID_URL = r'''(?x)
83 (?:
84 hotstar\:|
85 https?://(?:www\.)?hotstar\.com(?:/in)?/(?!in/)
86 )
87 (?:
88 (?P<type>movies|sports|episode|(?P<tv>tv))
89 (?:
90 \:|
91 /[^/?#]+/
92 (?(tv)
93 (?:[^/?#]+/){2}|
94 (?:[^/?#]+/)*
95 )
96 )|
97 [^/?#]+/
98 )?
99 (?P<id>\d{10})
100 '''
101 _TESTS = [{
102 'url': 'https://www.hotstar.com/can-you-not-spread-rumours/1000076273',
103 'info_dict': {
104 'id': '1000076273',
105 'ext': 'mp4',
106 'title': 'Can You Not Spread Rumours?',
107 'description': 'md5:c957d8868e9bc793ccb813691cc4c434',
108 'timestamp': 1447248600,
109 'upload_date': '20151111',
110 'duration': 381,
111 },
112 }, {
113 'url': 'hotstar:1000076273',
114 'only_matching': True,
115 }, {
116 'url': 'https://www.hotstar.com/movies/radha-gopalam/1000057157',
117 'info_dict': {
118 'id': '1000057157',
119 'ext': 'mp4',
120 'title': 'Radha Gopalam',
121 'description': 'md5:be3bc342cc120bbc95b3b0960e2b0d22',
122 'timestamp': 1140805800,
123 'upload_date': '20060224',
124 'duration': 9182,
125 },
126 }, {
127 'url': 'hotstar:movies: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',
134 'only_matching': True,
135 }, {
136 # contentData
137 'url': 'hotstar:sports:1260065956',
138 'only_matching': True,
139 }, {
140 # contentData
141 'url': 'hotstar:sports:1260066104',
142 'only_matching': True,
143 }, {
144 'url': 'https://www.hotstar.com/tv/ek-bhram-sarvagun-sampanna/s-2116/janhvi-targets-suman/1000234847',
145 'info_dict': {
146 'id': '1000234847',
147 'ext': 'mp4',
148 'title': 'Janhvi Targets Suman',
149 'description': 'md5:78a85509348910bd1ca31be898c5796b',
150 'timestamp': 1556670600,
151 'upload_date': '20190501',
152 'duration': 1219,
153 'channel': 'StarPlus',
154 'channel_id': 3,
155 'series': 'Ek Bhram - Sarvagun Sampanna',
156 'season': 'Chapter 1',
157 'season_number': 1,
158 'season_id': 6771,
159 'episode': 'Janhvi Targets Suman',
160 'episode_number': 8,
161 },
162 }, {
163 'url': 'hotstar:episode:1000234847',
164 'only_matching': True,
165 }]
166 _GEO_BYPASS = False
167 _TYPE = {
168 'movies': 'movie',
169 'sports': 'match',
170 'episode': 'episode',
171 'tv': 'episode',
172 None: 'content',
173 }
174
175 def _real_extract(self, url):
176 mobj = self._match_valid_url(url)
177 video_id = mobj.group('id')
178 video_type = mobj.group('type')
179 cookies = self._get_cookies(url)
180 video_type = self._TYPE.get(video_type, video_type)
181 video_data = self._call_api(f'o/v1/{video_type}/detail', video_id)['body']['results']['item']
182 title = video_data['title']
183
184 if not self.get_param('allow_unplayable_formats') and video_data.get('drmProtected'):
185 self.report_drm(video_id)
186
187 headers = {'Referer': 'https://www.hotstar.com/in'}
188 formats = []
189 subs = {}
190 geo_restricted = False
191 _, urlh = self._download_webpage_handle('https://www.hotstar.com/in', video_id)
192 # Required to fix https://github.com/yt-dlp/yt-dlp/issues/396
193 st = urlh.headers.get('x-origin-date')
194 # change to v2 in the future
195 playback_sets = self._call_api_v2('play/v1/playback', video_id, st=st, cookies=cookies)['playBackSets']
196 for playback_set in playback_sets:
197 if not isinstance(playback_set, dict):
198 continue
199 format_url = url_or_none(playback_set.get('playbackUrl'))
200 if not format_url:
201 continue
202 format_url = re.sub(
203 r'(?<=//staragvod)(\d)', r'web\1', format_url)
204 tags = str_or_none(playback_set.get('tagsCombination')) or ''
205 if tags and 'encryption:plain' not in tags:
206 continue
207 ext = determine_ext(format_url)
208 try:
209 if 'package:hls' in tags or ext == 'm3u8':
210 hls_formats, hls_subs = self._extract_m3u8_formats_and_subtitles(
211 format_url, video_id, 'mp4',
212 entry_protocol='m3u8_native',
213 m3u8_id='hls', headers=headers)
214 formats.extend(hls_formats)
215 subs = self._merge_subtitles(subs, hls_subs)
216 elif 'package:dash' in tags or ext == 'mpd':
217 dash_formats, dash_subs = self._extract_mpd_formats_and_subtitles(
218 format_url, video_id, mpd_id='dash', headers=headers)
219 formats.extend(dash_formats)
220 subs = self._merge_subtitles(subs, dash_subs)
221 elif ext == 'f4m':
222 # produce broken files
223 pass
224 else:
225 formats.append({
226 'url': format_url,
227 'width': int_or_none(playback_set.get('width')),
228 'height': int_or_none(playback_set.get('height')),
229 })
230 except ExtractorError as e:
231 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
232 geo_restricted = True
233 continue
234 if not formats and geo_restricted:
235 self.raise_geo_restricted(countries=['IN'], metadata_available=True)
236 self._sort_formats(formats)
237
238 for f in formats:
239 f.setdefault('http_headers', {}).update(headers)
240
241 return {
242 'id': video_id,
243 'title': title,
244 'description': video_data.get('description'),
245 'duration': int_or_none(video_data.get('duration')),
246 'timestamp': int_or_none(video_data.get('broadcastDate') or video_data.get('startDate')),
247 'formats': formats,
248 'subtitles': subs,
249 'channel': video_data.get('channelName'),
250 'channel_id': video_data.get('channelId'),
251 'series': video_data.get('showName'),
252 'season': video_data.get('seasonName'),
253 'season_number': int_or_none(video_data.get('seasonNo')),
254 'season_id': video_data.get('seasonId'),
255 'episode': title,
256 'episode_number': int_or_none(video_data.get('episodeNo')),
257 }
258
259
260 class HotStarPlaylistIE(HotStarBaseIE):
261 IE_NAME = 'hotstar:playlist'
262 _VALID_URL = r'https?://(?:www\.)?hotstar\.com/tv/[^/]+/s-\w+/list/[^/]+/t-(?P<id>\w+)'
263 _TESTS = [{
264 'url': 'https://www.hotstar.com/tv/savdhaan-india/s-26/list/popular-clips/t-3_2_26',
265 'info_dict': {
266 'id': '3_2_26',
267 },
268 'playlist_mincount': 20,
269 }, {
270 'url': 'https://www.hotstar.com/tv/savdhaan-india/s-26/list/extras/t-2480',
271 'only_matching': True,
272 }]
273
274 def _real_extract(self, url):
275 playlist_id = self._match_id(url)
276
277 collection = self._call_api('o/v1/tray/find', playlist_id, 'uqId')['body']['results']
278 entries = [
279 self.url_result(
280 'https://www.hotstar.com/%s' % video['contentId'],
281 ie=HotStarIE.ie_key(), video_id=video['contentId'])
282 for video in collection['assets']['items']
283 if video.get('contentId')]
284
285 return self.playlist_result(entries, playlist_id)
286
287
288 class HotStarSeriesIE(HotStarBaseIE):
289 IE_NAME = 'hotstar:series'
290 _VALID_URL = r'(?:https?://)(?:www\.)?hotstar\.com(?:/in)?/tv/[^/]+/(?P<id>\d+)'
291 _TESTS = [{
292 'url': 'https://www.hotstar.com/in/tv/radhakrishn/1260000646',
293 'info_dict': {
294 'id': '1260000646',
295 },
296 'playlist_mincount': 690,
297 }, {
298 'url': 'https://www.hotstar.com/tv/dancee-/1260050431',
299 'info_dict': {
300 'id': '1260050431',
301 },
302 'playlist_mincount': 43,
303 }, {
304 'url': 'https://www.hotstar.com/in/tv/mahabharat/435/',
305 'info_dict': {
306 'id': '435',
307 },
308 'playlist_mincount': 269,
309 }]
310
311 def _real_extract(self, url):
312 series_id = self._match_id(url)
313 headers = {
314 'x-country-code': 'IN',
315 'x-platform-code': 'PCTV',
316 }
317 detail_json = self._download_json('https://api.hotstar.com/o/v1/show/detail?contentId=' + series_id,
318 video_id=series_id, headers=headers)
319 id = compat_str(try_get(detail_json, lambda x: x['body']['results']['item']['id'], int))
320 item_json = self._download_json('https://api.hotstar.com/o/v1/tray/g/1/items?etid=0&tao=0&tas=10000&eid=' + id,
321 video_id=series_id, headers=headers)
322 entries = [
323 self.url_result(
324 'hotstar:episode:%d' % video['contentId'],
325 ie=HotStarIE.ie_key(), video_id=video['contentId'])
326 for video in item_json['body']['results']['items']
327 if video.get('contentId')]
328
329 return self.playlist_result(entries, series_id)