8 from .common
import InfoExtractor
24 class HotStarBaseIE(InfoExtractor
):
25 _BASE_URL
= 'https://www.hotstar.com'
26 _API_URL
= 'https://api.hotstar.com'
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
, query
, st
=None, cookies
=None):
30 st
= int_or_none(st
) or int(time
.time())
32 auth
= 'st=%d~exp=%d~acl=/*' % (st
, exp
)
33 auth
+= '~hmac=' + hmac
.new(self
._AKAMAI
_ENCRYPTION
_KEY
, auth
.encode(), hashlib
.sha256
).hexdigest()
35 if cookies
and cookies
.get('userUP'):
36 token
= cookies
.get('userUP').value
38 token
= self
._download
_json
(
39 f
'{self._API_URL}/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'),
44 'x-hs-platform': 'PCTV', # or 'web'
45 'Content-Type': 'application/json',
48 response
= self
._download
_json
(
49 f
'{self._API_URL}/{path}', video_id
, query
=query
,
52 'x-hs-appversion': '6.72.2',
53 'x-hs-platform': 'web',
54 'x-hs-usertoken': token
,
57 if response
['message'] != "Playback URL's fetched successfully":
59 response
['message'], expected
=True)
60 return response
['data']
62 def _call_api(self
, path
, video_id
, query_name
='contentId'):
63 return self
._download
_json
(
64 f
'{self._API_URL}/{path}', video_id
=video_id
,
69 'x-country-code': 'IN',
70 'x-platform-code': 'PCTV',
73 def _call_api_v2(self
, path
, video_id
, st
=None, cookies
=None):
74 return self
._call
_api
_impl
(
75 f
'{path}/content/{video_id}', video_id
, st
=st
, cookies
=cookies
, query
={
76 'desired-config': 'audio_channel:stereo|container:fmp4|dynamic_range:hdr|encryption:plain|ladder:tv|package:dash|resolution:fhd|subs-tag:HotstarVIP|video_codec:h265',
77 'device-id': cookies
.get('device_id').value
if cookies
.get('device_id') else compat_str(uuid
.uuid4()),
83 class HotStarIE(HotStarBaseIE
):
86 https?://(?:www\.)?hotstar\.com(?:/in)?/(?!in/)
88 (?P<type>movies|sports|episode|(?P<tv>tv))/
89 (?(tv)(?:[^/?#]+/){2}|[^?#]*)
96 'url': 'https://www.hotstar.com/can-you-not-spread-rumours/1000076273',
100 'title': 'Can You Not Spread Rumours?',
101 'description': 'md5:c957d8868e9bc793ccb813691cc4c434',
102 'timestamp': 1447248600,
103 'upload_date': '20151111',
105 'episode': 'Can You Not Spread Rumours?',
108 'url': 'https://www.hotstar.com/tv/ek-bhram-sarvagun-sampanna/s-2116/janhvi-targets-suman/1000234847',
112 'title': 'Janhvi Targets Suman',
113 'description': 'md5:78a85509348910bd1ca31be898c5796b',
114 'timestamp': 1556670600,
115 'upload_date': '20190501',
117 'channel': 'StarPlus',
119 'series': 'Ek Bhram - Sarvagun Sampanna',
120 'season': 'Chapter 1',
123 'episode': 'Janhvi Targets Suman',
127 'url': 'https://www.hotstar.com/movies/radha-gopalam/1000057157',
128 'only_matching': True,
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,
133 'url': 'https://www.hotstar.com/in/sports/football/most-costly-pl-transfers-ft-grealish/1260065956',
134 'only_matching': True,
141 'episode': 'episode',
148 'vcodec': 'video_codec',
149 'dr': 'dynamic_range',
153 def _video_url(cls
, video_id
, video_type
=None, *, slug
='ignore_me', root
=None):
154 assert None in (video_type
, root
)
156 root
= join_nonempty(cls
._BASE
_URL
, video_type
, delim
='/')
157 return f
'{root}/{slug}/{video_id}'
159 def _real_extract(self
, url
):
160 video_id
, video_type
= self
._match
_valid
_url
(url
).group('id', 'type')
161 video_type
= self
._TYPE
.get(video_type
, video_type
)
162 cookies
= self
._get
_cookies
(url
) # Cookies before any request
164 video_data
= self
._call
_api
(f
'o/v1/{video_type}/detail', video_id
)['body']['results']['item']
165 if not self
.get_param('allow_unplayable_formats') and video_data
.get('drmProtected'):
166 self
.report_drm(video_id
)
168 # See https://github.com/yt-dlp/yt-dlp/issues/396
169 st
= self
._download
_webpage
_handle
(f
'{self._BASE_URL}/in', video_id
)[1].headers
.get('x-origin-date')
171 geo_restricted
= False
172 formats
, subs
= [], {}
173 headers
= {'Referer': f'{self._BASE_URL}
/in'}
175 # change to v2 in the future
176 playback_sets = self._call_api_v2('play
/v1
/playback
', video_id, st=st, cookies=cookies)['playBackSets
']
177 for playback_set in playback_sets:
178 if not isinstance(playback_set, dict):
180 tags = str_or_none(playback_set.get('tagsCombination
')) or ''
181 if any(f'{prefix}
:{ignore}
' in tags
182 for key, prefix in self._IGNORE_MAP.items()
183 for ignore in self._configuration_arg(key)):
186 format_url = url_or_none(playback_set.get('playbackUrl
'))
189 format_url = re.sub(r'(?
<=//staragvod
)(\d
)', r'web\
1', format_url)
190 dr = re.search(r'dynamic_range
:(?P
<dr
>[a
-z
]+)', playback_set.get('tagsCombination
')).group('dr
')
191 ext = determine_ext(format_url)
193 current_formats, current_subs = [], {}
195 if 'package
:hls
' in tags or ext == 'm3u8
':
196 current_formats, current_subs = self._extract_m3u8_formats_and_subtitles(
197 format_url, video_id, 'mp4
',
198 entry_protocol='m3u8_native
',
199 m3u8_id=f'{dr}
-hls
', headers=headers)
200 elif 'package
:dash
' in tags or ext == 'mpd
':
201 current_formats, current_subs = self._extract_mpd_formats_and_subtitles(
202 format_url, video_id, mpd_id=f'{dr}
-dash
', headers=headers)
204 pass # XXX: produce broken files
208 'width
': int_or_none(playback_set.get('width
')),
209 'height
': int_or_none(playback_set.get('height
')),
211 except ExtractorError as e:
212 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
213 geo_restricted = True
216 if tags and 'encryption
:plain
' not in tags:
217 for f in current_formats:
219 if tags and 'language
' in tags:
220 lang = re.search(r'language
:(?P
<lang
>[a
-z
]+)', tags).group('lang
')
221 for f in current_formats:
222 if not f.get('langauge
'):
225 formats.extend(current_formats)
226 subs = self._merge_subtitles(subs, current_subs)
228 if not formats and geo_restricted:
229 self.raise_geo_restricted(countries=['IN
'], metadata_available=True)
230 self._sort_formats(formats)
232 f.setdefault('http_headers
', {}).update(headers)
236 'title
': video_data.get('title
'),
237 'description
': video_data.get('description
'),
238 'duration
': int_or_none(video_data.get('duration
')),
239 'timestamp
': int_or_none(video_data.get('broadcastDate
') or video_data.get('startDate
')),
242 'channel
': video_data.get('channelName
'),
243 'channel_id
': video_data.get('channelId
'),
244 'series
': video_data.get('showName
'),
245 'season
': video_data.get('seasonName
'),
246 'season_number
': int_or_none(video_data.get('seasonNo
')),
247 'season_id
': video_data.get('seasonId
'),
248 'episode
': video_data.get('title
'),
249 'episode_number
': int_or_none(video_data.get('episodeNo
')),
253 class HotStarPrefixIE(InfoExtractor):
254 """ The "hotstar:" prefix is no longer in use, but this is kept for backward compatibility """
256 _VALID_URL = r'hotstar
:(?
:(?P
<type>\w
+):)?
(?P
<id>\d
+)$
'
258 'url
': 'hotstar
:1000076273',
259 'only_matching
': True,
261 'url
': 'hotstar
:movies
:1000057157',
265 'title
': 'Radha Gopalam
',
266 'description
': 'md5
:be3bc342cc120bbc95b3b0960e2b0d22
',
267 'timestamp
': 1140805800,
268 'upload_date
': '20060224',
270 'episode
': 'Radha Gopalam
',
273 'url
': 'hotstar
:episode
:1000234847',
274 'only_matching
': True,
277 'url
': 'hotstar
:sports
:1260065956',
278 'only_matching
': True,
281 'url
': 'hotstar
:sports
:1260066104',
282 'only_matching
': True,
285 def _real_extract(self, url):
286 video_id, video_type = self._match_valid_url(url).group('id', 'type')
287 return self.url_result(HotStarIE._video_url(video_id, video_type), HotStarIE, video_id)
290 class HotStarPlaylistIE(HotStarBaseIE):
291 IE_NAME = 'hotstar
:playlist
'
292 _VALID_URL = r'https?
://(?
:www\
.)?hotstar\
.com
/tv
/[^
/]+/s
-\w
+/list/[^
/]+/t
-(?P
<id>\w
+)'
294 'url
': 'https
://www
.hotstar
.com
/tv
/savdhaan
-india
/s
-26/list/popular
-clips
/t
-3_2_26',
298 'playlist_mincount
': 20,
300 'url
': 'https
://www
.hotstar
.com
/tv
/savdhaan
-india
/s
-26/list/extras
/t
-2480',
301 'only_matching
': True,
304 def _real_extract(self, url):
305 playlist_id = self._match_id(url)
307 collection = self._call_api('o
/v1
/tray
/find
', playlist_id, 'uqId
')['body
']['results
']
309 self.url_result(HotStarIE._video_url(video['contentId
']), HotStarIE, video['contentId
'])
310 for video in collection['assets
']['items
'] if video.get('contentId
')]
312 return self.playlist_result(entries, playlist_id)
315 class HotStarSeriesIE(HotStarBaseIE):
316 IE_NAME = 'hotstar
:series
'
317 _VALID_URL = r'(?P
<url
>https?
://(?
:www\
.)?hotstar\
.com(?
:/in)?
/tv
/[^
/]+/(?P
<id>\d
+))'
319 'url
': 'https
://www
.hotstar
.com
/in/tv
/radhakrishn
/1260000646',
323 'playlist_mincount
': 690,
325 'url
': 'https
://www
.hotstar
.com
/tv
/dancee
-/1260050431',
329 'playlist_mincount
': 43,
331 'url
': 'https
://www
.hotstar
.com
/in/tv
/mahabharat
/435/',
335 'playlist_mincount
': 269,
338 def _real_extract(self, url):
339 url, series_id = self._match_valid_url(url).groups()
341 'x
-country
-code
': 'IN
',
342 'x
-platform
-code
': 'PCTV
',
344 detail_json = self._download_json(
345 f'{self._API_URL}
/o
/v1
/show
/detail?contentId
={series_id}
', series_id, headers=headers)
346 id = try_get(detail_json, lambda x: x['body
']['results
']['item
']['id'], int)
347 item_json = self._download_json(
348 f'{self._API_URL}
/o
/v1
/tray
/g
/1/items?etid
=0&tao
=0&tas
=10000&eid
={id}
', series_id, headers=headers)
350 return self.playlist_result([
351 self.url_result(HotStarIE._video_url(video['contentId
'], root=url), HotStarIE, video['contentId
'])
352 for video in item_json['body
']['results
']['items
'] if video.get('contentId
')