From de796eaae99b9c7fe9836f83a686dd479112e678 Mon Sep 17 00:00:00 2001 From: Dev Bali Date: Tue, 22 Jun 2021 18:41:47 -0700 Subject: [PATCH] Added Cookie Functionality to Hotstar --- youtube_dl/extractor/hotstar.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/youtube_dl/extractor/hotstar.py b/youtube_dl/extractor/hotstar.py index 1620822b6..c45de6428 100644 --- a/youtube_dl/extractor/hotstar.py +++ b/youtube_dl/extractor/hotstar.py @@ -20,22 +20,27 @@ from ..utils import ( str_or_none, try_get, url_or_none, + urlencode_postdata, + sanitized_Request ) class HotStarBaseIE(InfoExtractor): _AKAMAI_ENCRYPTION_KEY = b'\x05\xfc\x1a\x01\xca\xc9\x4b\xc4\x12\xfc\x53\x12\x07\x75\xf9\xee' - def _call_api_impl(self, path, video_id, headers, query, data=None): + def _call_api_impl(self, path, video_id, headers, query, data=None,method=None): st = int(time.time()) exp = st + 6000 auth = 'st=%d~exp=%d~acl=/*' % (st, exp) auth += '~hmac=' + hmac.new(self._AKAMAI_ENCRYPTION_KEY, auth.encode(), hashlib.sha256).hexdigest() h = {'hotstarauth': auth} h.update(headers) + + req = sanitized_Request('https://api.hotstar.com/' + path, data=data) + if method is not None: req.get_method = lambda: method + return self._download_json( - 'https://api.hotstar.com/' + path, - video_id, headers=h, query=query, data=data) + req,video_id, headers=h, query=query, data=data) def _call_api(self, path, video_id, query_name='contentId'): response = self._call_api_impl(path, video_id, { @@ -53,13 +58,15 @@ class HotStarBaseIE(InfoExtractor): def _call_api_v2(self, path, video_id, headers, query=None, data=None): h = {'X-Request-Id': compat_str(uuid.uuid4())} h.update(headers) + try: return self._call_api_impl( path, video_id, h, query, data) except ExtractorError as e: if isinstance(e.cause, compat_HTTPError): if e.cause.code == 402: - self.raise_login_required() + raise ExtractorError('This video is only available for registered users. You may want to use --cookies.', expected=True) + message = self._parse_json(e.cause.read().decode(), video_id)['message'] if message in ('Content not available in region', 'Country is not supported'): raise self.raise_geo_restricted(message) @@ -135,6 +142,13 @@ class HotStarIE(HotStarBaseIE): formats = [] geo_restricted = False + for cookie in self._downloader.cookiejar: + if "hotstar" in cookie.domain: + if cookie.name == "userUP": + self._USER_TOKEN = cookie.value + elif cookie.name == "device-id": + self._DEVICE_ID = cookie.value + if not self._USER_TOKEN: self._DEVICE_ID = compat_str(uuid.uuid4()) self._USER_TOKEN = self._call_api_v2('um/v3/users', video_id, {