[Hotstar] Support cookies (#584)

Closes #583 
Authored by: Ashish0804
This commit is contained in:
Ashish 2021-07-29 10:06:38 +05:30 committed by GitHub
parent 89ce723edd
commit fe07e2c69f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,7 +7,6 @@ import re
import time import time
import uuid import uuid
import json import json
import random
from .common import InfoExtractor from .common import InfoExtractor
from ..compat import ( from ..compat import (
@ -27,34 +26,24 @@ from ..utils import (
class HotStarBaseIE(InfoExtractor): class HotStarBaseIE(InfoExtractor):
_AKAMAI_ENCRYPTION_KEY = b'\x05\xfc\x1a\x01\xca\xc9\x4b\xc4\x12\xfc\x53\x12\x07\x75\xf9\xee' _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, query, st=None): def _call_api_impl(self, path, video_id, query, st=None, cookies=None):
st = int_or_none(st) or int(time.time()) st = int_or_none(st) or int(time.time())
exp = st + 6000 exp = st + 6000
auth = 'st=%d~exp=%d~acl=/*' % (st, exp) auth = 'st=%d~exp=%d~acl=/*' % (st, exp)
auth += '~hmac=' + hmac.new(self._AKAMAI_ENCRYPTION_KEY, auth.encode(), hashlib.sha256).hexdigest() auth += '~hmac=' + hmac.new(self._AKAMAI_ENCRYPTION_KEY, auth.encode(), hashlib.sha256).hexdigest()
def _generate_device_id(): if cookies.get('userUP'):
""" token = cookies.get('userUP').value
Reversed from javascript library. else:
JS function is generateUUID token = self._download_json(
""" 'https://api.hotstar.com/um/v3/users',
t = int(round(time.time() * 1000)) video_id, note='Downloading token',
e = "xxxxxxxx-xxxx-4xxx-xxxx-xxxxxxxxxxxx" # 4 seems to be interchangeable data=json.dumps({"device_ids": [{"id": compat_str(uuid.uuid4()), "type": "device_id"}]}).encode('utf-8'),
headers={
def _replacer(): 'hotstarauth': auth,
n = int((t + 16 * random.random())) % 16 | 0 'x-hs-platform': 'PCTV', # or 'web'
return hex(n if "x" == e else 3 & n | 8)[2:] 'Content-Type': 'application/json',
return "".join([_.replace('x', _replacer()) for _ in e]) })['user_identity']
token = self._download_json(
'https://api.hotstar.com/um/v3/users',
video_id, note='Downloading token',
data=json.dumps({"device_ids": [{"id": compat_str(uuid.uuid4()), "type": "device_id"}]}).encode('utf-8'),
headers={
'hotstarauth': auth,
'x-hs-platform': 'PCTV', # or 'web'
'Content-Type': 'application/json',
})['user_identity']
response = self._download_json( response = self._download_json(
'https://api.hotstar.com/' + path, video_id, headers={ 'https://api.hotstar.com/' + path, video_id, headers={
@ -75,11 +64,11 @@ class HotStarBaseIE(InfoExtractor):
'tas': 10000, 'tas': 10000,
}) })
def _call_api_v2(self, path, video_id, st=None): def _call_api_v2(self, path, video_id, st=None, cookies=None):
return self._call_api_impl( return self._call_api_impl(
'%s/content/%s' % (path, video_id), video_id, st=st, query={ '%s/content/%s' % (path, video_id), video_id, st=st, cookies=cookies, query={
'desired-config': 'audio_channel:stereo|dynamic_range:sdr|encryption:plain|ladder:tv|package:dash|resolution:hd|subs-tag:HotstarVIP|video_codec:vp9', 'desired-config': 'audio_channel:stereo|dynamic_range:sdr|encryption:plain|ladder:tv|package:dash|resolution:hd|subs-tag:HotstarVIP|video_codec:vp9',
'device-id': compat_str(uuid.uuid4()), 'device-id': cookies.get('device_id').value if cookies.get('device_id') else compat_str(uuid.uuid4()),
'os-name': 'Windows', 'os-name': 'Windows',
'os-version': '10', 'os-version': '10',
}) })
@ -130,7 +119,7 @@ class HotStarIE(HotStarBaseIE):
def _real_extract(self, url): def _real_extract(self, url):
video_id = self._match_id(url) video_id = self._match_id(url)
cookies = self._get_cookies(url)
webpage, urlh = self._download_webpage_handle(url, video_id) webpage, urlh = self._download_webpage_handle(url, video_id)
st = urlh.headers.get('x-origin-date') st = urlh.headers.get('x-origin-date')
app_state = self._parse_json(self._search_regex( app_state = self._parse_json(self._search_regex(
@ -156,7 +145,7 @@ class HotStarIE(HotStarBaseIE):
formats = [] formats = []
geo_restricted = False geo_restricted = False
# change to v2 in the future # change to v2 in the future
playback_sets = self._call_api_v2('play/v1/playback', video_id, st=st)['playBackSets'] playback_sets = self._call_api_v2('play/v1/playback', video_id, st=st, cookies=cookies)['playBackSets']
for playback_set in playback_sets: for playback_set in playback_sets:
if not isinstance(playback_set, dict): if not isinstance(playback_set, dict):
continue continue