]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/hotstar.py
[bitchute] Fix test (#758)
[yt-dlp.git] / yt_dlp / extractor / hotstar.py
CommitLineData
fb8e402a 1# coding: utf-8
2from __future__ import unicode_literals
3
85cd69ad
RA
4import hashlib
5import hmac
1cb812d3 6import re
85cd69ad 7import time
2533f5b6 8import uuid
7078ec64 9import json
909191de 10
fb8e402a 11from .common import InfoExtractor
2533f5b6
S
12from ..compat import (
13 compat_HTTPError,
6923b538 14 compat_str
2533f5b6 15)
fb8e402a 16from ..utils import (
fb8e402a 17 determine_ext,
909191de 18 ExtractorError,
fb8e402a 19 int_or_none,
2533f5b6 20 str_or_none,
05e7c184 21 try_get,
2533f5b6 22 url_or_none,
fb8e402a 23)
24
25
909191de 26class HotStarBaseIE(InfoExtractor):
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(
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']
6923b538 47
85cd69ad 48 response = self._download_json(
2533f5b6 49 'https://api.hotstar.com/' + path, video_id, headers={
85cd69ad 50 'hotstarauth': auth,
7078ec64
N
51 'x-hs-appversion': '6.72.2',
52 'x-hs-platform': 'web',
53 'x-hs-usertoken': token,
2533f5b6 54 }, query=query)
6923b538 55
7078ec64 56 if response['message'] != "Playback URL's fetched successfully":
85cd69ad 57 raise ExtractorError(
7078ec64
N
58 response['message'], expected=True)
59 return response['data']
909191de 60
2533f5b6 61 def _call_api(self, path, video_id, query_name='contentId'):
b6a35ad8 62 return self._download_json('https://api.hotstar.com/' + path, video_id=video_id, query={
2533f5b6
S
63 query_name: video_id,
64 'tas': 10000,
b6a35ad8
A
65 }, headers={
66 'x-country-code': 'IN',
67 'x-platform-code': 'PCTV',
2533f5b6
S
68 })
69
fe07e2c6 70 def _call_api_v2(self, path, video_id, st=None, cookies=None):
2533f5b6 71 return self._call_api_impl(
fe07e2c6 72 '%s/content/%s' % (path, video_id), video_id, st=st, cookies=cookies, query={
6923b538 73 'desired-config': 'audio_channel:stereo|dynamic_range:sdr|encryption:plain|ladder:tv|package:dash|resolution:hd|subs-tag:HotstarVIP|video_codec:vp9',
fe07e2c6 74 'device-id': cookies.get('device_id').value if cookies.get('device_id') else compat_str(uuid.uuid4()),
7078ec64
N
75 'os-name': 'Windows',
76 'os-version': '10',
2533f5b6
S
77 })
78
909191de
S
79
80class HotStarIE(HotStarBaseIE):
85cd69ad 81 IE_NAME = 'hotstar'
6e639032 82 _VALID_URL = r'''(?x)
b6a35ad8
A
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})
6e639032 100 '''
89d23f37 101 _TESTS = [{
85cd69ad 102 'url': 'https://www.hotstar.com/can-you-not-spread-rumours/1000076273',
fb8e402a 103 'info_dict': {
104 'id': '1000076273',
105 'ext': 'mp4',
85cd69ad 106 'title': 'Can You Not Spread Rumours?',
fb8e402a 107 'description': 'md5:c957d8868e9bc793ccb813691cc4c434',
85cd69ad 108 'timestamp': 1447248600,
fb8e402a 109 'upload_date': '20151111',
110 'duration': 381,
111 },
adbbdefc 112 }, {
b6a35ad8
A
113 'url': 'hotstar:1000076273',
114 'only_matching': True,
115 }, {
adbbdefc 116 'url': 'https://www.hotstar.com/movies/radha-gopalam/1000057157',
b6a35ad8
A
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',
adbbdefc 128 'only_matching': True,
89d23f37 129 }, {
b6a35ad8 130 'url': 'https://www.hotstar.com/in/sports/cricket/follow-the-blues-2021/recap-eng-fight-back-on-day-2/1260066104',
89d23f37
S
131 'only_matching': True,
132 }, {
b6a35ad8
A
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',
89d23f37 142 'only_matching': True,
2533f5b6 143 }, {
2533f5b6 144 'url': 'https://www.hotstar.com/tv/ek-bhram-sarvagun-sampanna/s-2116/janhvi-targets-suman/1000234847',
b6a35ad8
A
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',
2533f5b6 164 'only_matching': True,
89d23f37 165 }]
85cd69ad 166 _GEO_BYPASS = False
b6a35ad8
A
167 _TYPE = {
168 'movies': 'movie',
169 'sports': 'match',
170 'episode': 'episode',
171 'tv': 'episode',
172 None: 'content',
173 }
fb8e402a 174
fb8e402a 175 def _real_extract(self, url):
5ad28e7f 176 mobj = self._match_valid_url(url)
b6a35ad8
A
177 video_id = mobj.group('id')
178 video_type = mobj.group('type')
fe07e2c6 179 cookies = self._get_cookies(url)
b6a35ad8
A
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']
85cd69ad 182 title = video_data['title']
0dac7cbb 183
a06916d9 184 if not self.get_param('allow_unplayable_formats') and video_data.get('drmProtected'):
0dac7cbb 185 raise ExtractorError('This video is DRM protected.', expected=True)
b6a35ad8 186 headers = {'Referer': 'https://www.hotstar.com/in'}
fb8e402a 187 formats = []
b6a35ad8 188 subs = {}
2533f5b6 189 geo_restricted = False
b6a35ad8
A
190 _, urlh = self._download_webpage_handle('https://www.hotstar.com/in', video_id)
191 # Required to fix https://github.com/yt-dlp/yt-dlp/issues/396
192 st = urlh.headers.get('x-origin-date')
6923b538 193 # change to v2 in the future
fe07e2c6 194 playback_sets = self._call_api_v2('play/v1/playback', video_id, st=st, cookies=cookies)['playBackSets']
2533f5b6
S
195 for playback_set in playback_sets:
196 if not isinstance(playback_set, dict):
197 continue
198 format_url = url_or_none(playback_set.get('playbackUrl'))
199 if not format_url:
200 continue
1cb812d3
S
201 format_url = re.sub(
202 r'(?<=//staragvod)(\d)', r'web\1', format_url)
2533f5b6
S
203 tags = str_or_none(playback_set.get('tagsCombination')) or ''
204 if tags and 'encryption:plain' not in tags:
205 continue
206 ext = determine_ext(format_url)
85cd69ad 207 try:
2533f5b6 208 if 'package:hls' in tags or ext == 'm3u8':
b6a35ad8 209 hls_formats, hls_subs = self._extract_m3u8_formats_and_subtitles(
d9d30986 210 format_url, video_id, 'mp4',
d7def23d 211 entry_protocol='m3u8_native',
b6a35ad8
A
212 m3u8_id='hls', headers=headers)
213 formats.extend(hls_formats)
214 subs = self._merge_subtitles(subs, hls_subs)
2533f5b6 215 elif 'package:dash' in tags or ext == 'mpd':
b6a35ad8
A
216 dash_formats, dash_subs = self._extract_mpd_formats_and_subtitles(
217 format_url, video_id, mpd_id='dash', headers=headers)
218 formats.extend(dash_formats)
219 subs = self._merge_subtitles(subs, dash_subs)
2533f5b6
S
220 elif ext == 'f4m':
221 # produce broken files
222 pass
223 else:
224 formats.append({
225 'url': format_url,
226 'width': int_or_none(playback_set.get('width')),
227 'height': int_or_none(playback_set.get('height')),
228 })
85cd69ad
RA
229 except ExtractorError as e:
230 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
2533f5b6
S
231 geo_restricted = True
232 continue
233 if not formats and geo_restricted:
b7da73eb 234 self.raise_geo_restricted(countries=['IN'], metadata_available=True)
fb8e402a 235 self._sort_formats(formats)
236
d7def23d
RA
237 for f in formats:
238 f.setdefault('http_headers', {}).update(headers)
239
fb8e402a 240 return {
241 'id': video_id,
0dac7cbb 242 'title': title,
fb8e402a 243 'description': video_data.get('description'),
244 'duration': int_or_none(video_data.get('duration')),
85cd69ad 245 'timestamp': int_or_none(video_data.get('broadcastDate') or video_data.get('startDate')),
fb8e402a 246 'formats': formats,
b6a35ad8 247 'subtitles': subs,
85cd69ad
RA
248 'channel': video_data.get('channelName'),
249 'channel_id': video_data.get('channelId'),
250 'series': video_data.get('showName'),
251 'season': video_data.get('seasonName'),
252 'season_number': int_or_none(video_data.get('seasonNo')),
253 'season_id': video_data.get('seasonId'),
0dac7cbb 254 'episode': title,
85cd69ad 255 'episode_number': int_or_none(video_data.get('episodeNo')),
fb8e402a 256 }
477c97f8
AV
257
258
909191de 259class HotStarPlaylistIE(HotStarBaseIE):
477c97f8 260 IE_NAME = 'hotstar:playlist'
85cd69ad 261 _VALID_URL = r'https?://(?:www\.)?hotstar\.com/tv/[^/]+/s-\w+/list/[^/]+/t-(?P<id>\w+)'
477c97f8 262 _TESTS = [{
85cd69ad 263 'url': 'https://www.hotstar.com/tv/savdhaan-india/s-26/list/popular-clips/t-3_2_26',
477c97f8 264 'info_dict': {
85cd69ad 265 'id': '3_2_26',
477c97f8 266 },
85cd69ad 267 'playlist_mincount': 20,
477c97f8 268 }, {
85cd69ad 269 'url': 'https://www.hotstar.com/tv/savdhaan-india/s-26/list/extras/t-2480',
477c97f8
AV
270 'only_matching': True,
271 }]
477c97f8
AV
272
273 def _real_extract(self, url):
85cd69ad
RA
274 playlist_id = self._match_id(url)
275
b6a35ad8 276 collection = self._call_api('o/v1/tray/find', playlist_id, 'uqId')['body']['results']
477c97f8 277 entries = [
909191de 278 self.url_result(
85cd69ad 279 'https://www.hotstar.com/%s' % video['contentId'],
909191de 280 ie=HotStarIE.ie_key(), video_id=video['contentId'])
85cd69ad 281 for video in collection['assets']['items']
909191de
S
282 if video.get('contentId')]
283
284 return self.playlist_result(entries, playlist_id)
6e639032
A
285
286
287class HotStarSeriesIE(HotStarBaseIE):
288 IE_NAME = 'hotstar:series'
8242bf22 289 _VALID_URL = r'(?:https?://)(?:www\.)?hotstar\.com(?:/in)?/tv/[^/]+/(?P<id>\d+)'
6e639032
A
290 _TESTS = [{
291 'url': 'https://www.hotstar.com/in/tv/radhakrishn/1260000646',
292 'info_dict': {
293 'id': '1260000646',
294 },
295 'playlist_mincount': 690,
296 }, {
297 'url': 'https://www.hotstar.com/tv/dancee-/1260050431',
298 'info_dict': {
299 'id': '1260050431',
300 },
301 'playlist_mincount': 43,
8242bf22
A
302 }, {
303 'url': 'https://www.hotstar.com/in/tv/mahabharat/435/',
304 'info_dict': {
305 'id': '435',
306 },
307 'playlist_mincount': 269,
6e639032
A
308 }]
309
310 def _real_extract(self, url):
311 series_id = self._match_id(url)
312 headers = {
313 'x-country-code': 'IN',
314 'x-platform-code': 'PCTV',
315 }
316 detail_json = self._download_json('https://api.hotstar.com/o/v1/show/detail?contentId=' + series_id,
317 video_id=series_id, headers=headers)
318 id = compat_str(try_get(detail_json, lambda x: x['body']['results']['item']['id'], int))
319 item_json = self._download_json('https://api.hotstar.com/o/v1/tray/g/1/items?etid=0&tao=0&tas=10000&eid=' + id,
320 video_id=series_id, headers=headers)
321 entries = [
322 self.url_result(
b6a35ad8 323 'hotstar:episode:%d' % video['contentId'],
6e639032
A
324 ie=HotStarIE.ie_key(), video_id=video['contentId'])
325 for video in item_json['body']['results']['items']
326 if video.get('contentId')]
327
328 return self.playlist_result(entries, series_id)