diff --git a/yt_dlp/extractor/hotstar.py b/yt_dlp/extractor/hotstar.py index 56a215e7a..34a30e637 100644 --- a/yt_dlp/extractor/hotstar.py +++ b/yt_dlp/extractor/hotstar.py @@ -14,6 +14,8 @@ determine_ext, int_or_none, join_nonempty, + jwt_decode_hs256, + parse_iso8601, str_or_none, url_or_none, ) @@ -21,6 +23,7 @@ class HotStarBaseIE(InfoExtractor): + _TOKEN_NAME = 'userUP' _BASE_URL = 'https://www.hotstar.com' _API_URL = 'https://api.hotstar.com' _API_URL_V2 = 'https://apix.hotstar.com/v2' @@ -31,9 +34,7 @@ def _call_api_v1(self, path, *args, **kwargs): f'{self._API_URL}/o/v1/{path}', *args, **kwargs, headers={'x-country-code': 'IN', 'x-platform-code': 'PCTV'}) - def _call_api_impl(self, path, video_id, query, st=None, cookies=None): - if not cookies or not cookies.get('userUP'): - self.raise_login_required() + def _call_api_impl(self, path, video_id, query, acc_static_values, cookies=None, st=None): st = int_or_none(st) or int(time.time()) exp = st + 6000 @@ -42,12 +43,12 @@ def _call_api_impl(self, path, video_id, query, st=None, cookies=None): response = self._download_json( f'{self._API_URL_V2}/{path}', video_id, query=query, headers={ - 'user-agent': 'Disney+;in.startv.hotstar.dplus.tv/23.08.14.4.2915 (Android/13)', + 'user-agent': traverse_obj(acc_static_values, ('HEADERS', 'user-agent', {str})), 'hotstarauth': auth, - 'x-hs-usertoken': cookies['userUP'].value, + 'x-hs-usertoken': traverse_obj(cookies, (self._TOKEN_NAME, 'value')), 'x-hs-device-id': traverse_obj(cookies, ('deviceId', 'value')) or str(uuid.uuid4()), - 'x-hs-client': 'platform:android;app_id:in.startv.hotstar.dplus.tv;app_version:23.08.14.4;os:Android;os_version:13;schema_version:0.0.970', - 'x-hs-platform': 'android', + 'x-hs-client': traverse_obj(acc_static_values, ('HEADERS', 'x-hs-client', {str})), + 'x-hs-platform': traverse_obj(acc_static_values, ('HEADERS', 'x-hs-platform', {str})), 'content-type': 'application/json', }) @@ -55,7 +56,7 @@ def _call_api_impl(self, path, video_id, query, st=None, cookies=None): raise ExtractorError('API call was unsuccessful') return response['success'] - def _call_api_v2(self, path, video_id, content_type, cookies=None, st=None): + def _call_api_v2(self, path, video_id, content_type, acc_static_values, cookies=None, st=None): return self._call_api_impl(f'{path}', video_id, query={ 'content_id': video_id, 'filters': f'content_type={content_type}', @@ -76,7 +77,7 @@ def _call_api_v2(self, path, video_id, content_type, cookies=None, st=None): 'widevine_security_level': ['SW_SECURE_DECODE', 'SW_SECURE_CRYPTO'], 'hdcp_version': ['HDCP_V2_2', 'HDCP_V2_1', 'HDCP_V2', 'HDCP_V1'], }, separators=(',', ':')), - }, st=st, cookies=cookies) + }, acc_static_values=acc_static_values, cookies=cookies, st=st) @staticmethod def _parse_metadata_v1(video_data): @@ -264,6 +265,21 @@ class HotStarIE(HotStarBaseIE): 'vcodec': 'video_codec', } + _FREE_ACC_STATIC_VALUES = { + 'HEADERS': { + 'user-agent': 'Hotstar;in.startv.hotstar/25.06.30.0.11580 (Android/12)', + 'x-hs-client': 'platform:android;app_id:in.startv.hotstar;app_version:25.06.30.0;os:Android;os_version:12;schema_version:0.0.1523', + 'x-hs-platform': 'android', + }, + } + _PREMIUM_ACC_STATIC_VALUES = { + 'HEADERS': { + 'user-agent': 'Disney+;in.startv.hotstar.dplus.tv/23.08.14.4.2915 (Android/13)', + 'x-hs-client': 'platform:androidtv;app_id:in.startv.hotstar.dplus.tv;app_version:23.08.14.4;os:Android;os_version:13;schema_version:0.0.970', + 'x-hs-platform': 'androidtv', + }, + } + @classmethod def _video_url(cls, video_id, video_type=None, *, slug='ignore_me', root=None): assert None in (video_type, root) @@ -271,10 +287,23 @@ def _video_url(cls, video_id, video_type=None, *, slug='ignore_me', root=None): root = join_nonempty(cls._BASE_URL, video_type, delim='/') return f'{root}/{slug}/{video_id}' + def _has_active_subscription(self, cookies): + expiry = traverse_obj(cookies, ( + self._TOKEN_NAME, 'value', {jwt_decode_hs256}, 'sub', {json.loads}, + 'subscriptions', 'in', ..., 'expiry', {parse_iso8601}, all, {max})) or 0 + return expiry > time.time() + def _real_extract(self, url): video_id, video_type = self._match_valid_url(url).group('id', 'type') video_type = self._TYPE[video_type] cookies = self._get_cookies(url) # Cookies before any request + if not cookies or not cookies.get(self._TOKEN_NAME): + self.raise_login_required() + + if self._has_active_subscription(cookies): + acc_static_values = self._PREMIUM_ACC_STATIC_VALUES + else: + acc_static_values = self._FREE_ACC_STATIC_VALUES video_data = traverse_obj( self._call_api_v1(f'{video_type}/detail', video_id, fatal=False, query={ @@ -293,7 +322,7 @@ def _real_extract(self, url): # See https://github.com/yt-dlp/yt-dlp/issues/396 st = self._request_webpage( f'{self._BASE_URL}/in', video_id, 'Fetching server time').get_header('x-origin-date') - watch = self._call_api_v2('pages/watch', video_id, content_type, cookies=cookies, st=st) + watch = self._call_api_v2('pages/watch', video_id, content_type, acc_static_values, cookies, st) player_config = traverse_obj(watch, ( 'page', 'spaces', 'player', 'widget_wrappers', lambda _, v: v['template'] == 'PlayerWidget', 'widget', 'data', 'player_config', {dict}, any, {require('player config')}))