[extractor/hotstar] Refactor v1 API calls

This commit is contained in:
pukkandan 2022-11-09 08:35:34 +05:30
parent db6fa6960c
commit fad689c7b6
No known key found for this signature in database
GPG Key ID: 7EEE9E1E817D0A39

View File

@ -1,22 +1,19 @@
import hashlib import hashlib
import hmac import hmac
import json
import re import re
import time import time
import uuid import uuid
import json
from .common import InfoExtractor from .common import InfoExtractor
from ..compat import ( from ..compat import compat_HTTPError, compat_str
compat_HTTPError,
compat_str
)
from ..utils import ( from ..utils import (
determine_ext,
ExtractorError, ExtractorError,
determine_ext,
int_or_none, int_or_none,
join_nonempty, join_nonempty,
str_or_none, str_or_none,
try_get, traverse_obj,
url_or_none, url_or_none,
) )
@ -26,6 +23,11 @@ class HotStarBaseIE(InfoExtractor):
_API_URL = 'https://api.hotstar.com' _API_URL = 'https://api.hotstar.com'
_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_v1(self, path, *args, **kwargs):
return self._download_json(
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): 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
@ -59,17 +61,6 @@ def _call_api_impl(self, path, video_id, query, st=None, cookies=None):
response['message'], expected=True) response['message'], expected=True)
return response['data'] return response['data']
def _call_api(self, path, video_id, query_name='contentId'):
return self._download_json(
f'{self._API_URL}/{path}', video_id=video_id,
query={
query_name: video_id,
'tas': 10000,
}, headers={
'x-country-code': 'IN',
'x-platform-code': 'PCTV',
})
def _call_api_v2(self, path, video_id, st=None, cookies=None): def _call_api_v2(self, path, video_id, st=None, cookies=None):
return self._call_api_impl( return self._call_api_impl(
f'{path}/content/{video_id}', video_id, st=st, cookies=cookies, query={ f'{path}/content/{video_id}', video_id, st=st, cookies=cookies, query={
@ -79,6 +70,13 @@ def _call_api_v2(self, path, video_id, st=None, cookies=None):
'os-version': '10', 'os-version': '10',
}) })
def _playlist_entries(self, path, item_id, root=None, **kwargs):
results = self._call_api_v1(path, item_id, **kwargs)['body']['results']
for video in traverse_obj(results, (('assets', None), 'items', ...)):
if video.get('contentId'):
yield self.url_result(
HotStarIE._video_url(video['contentId'], root=root), HotStarIE, video['contentId'])
class HotStarIE(HotStarBaseIE): class HotStarIE(HotStarBaseIE):
IE_NAME = 'hotstar' IE_NAME = 'hotstar'
@ -104,6 +102,7 @@ class HotStarIE(HotStarBaseIE):
'duration': 381, 'duration': 381,
'episode': 'Can You Not Spread Rumours?', 'episode': 'Can You Not Spread Rumours?',
}, },
'params': {'skip_download': 'm3u8'},
}, { }, {
'url': 'https://www.hotstar.com/tv/ek-bhram-sarvagun-sampanna/s-2116/janhvi-targets-suman/1000234847', 'url': 'https://www.hotstar.com/tv/ek-bhram-sarvagun-sampanna/s-2116/janhvi-targets-suman/1000234847',
'info_dict': { 'info_dict': {
@ -161,7 +160,8 @@ def _real_extract(self, url):
video_type = self._TYPE.get(video_type, video_type) video_type = self._TYPE.get(video_type, video_type)
cookies = self._get_cookies(url) # Cookies before any request cookies = self._get_cookies(url) # Cookies before any request
video_data = self._call_api(f'o/v1/{video_type}/detail', video_id)['body']['results']['item'] video_data = self._call_api_v1(f'{video_type}/detail', video_id,
query={'tas': 10000, 'contentId': video_id})['body']['results']['item']
if not self.get_param('allow_unplayable_formats') and video_data.get('drmProtected'): if not self.get_param('allow_unplayable_formats') and video_data.get('drmProtected'):
self.report_drm(video_id) self.report_drm(video_id)
@ -305,14 +305,9 @@ class HotStarPlaylistIE(HotStarBaseIE):
}] }]
def _real_extract(self, url): def _real_extract(self, url):
playlist_id = self._match_id(url) id_ = self._match_id(url)
return self.playlist_result(
collection = self._call_api('o/v1/tray/find', playlist_id, 'uqId')['body']['results'] self._playlist_entries('tray/find', id_, query={'tas': 10000, 'uqId': id_}), id_)
entries = [
self.url_result(HotStarIE._video_url(video['contentId']), HotStarIE, video['contentId'])
for video in collection['assets']['items'] if video.get('contentId')]
return self.playlist_result(entries, playlist_id)
class HotStarSeasonIE(HotStarBaseIE): class HotStarSeasonIE(HotStarBaseIE):
@ -340,17 +335,8 @@ class HotStarSeasonIE(HotStarBaseIE):
def _real_extract(self, url): def _real_extract(self, url):
url, season_id = self._match_valid_url(url).groups() url, season_id = self._match_valid_url(url).groups()
headers = { return self.playlist_result(self._playlist_entries(
'x-country-code': 'IN', 'season/asset', season_id, url, query={'tao': 0, 'tas': 0, 'size': 10000, 'id': season_id}), season_id)
'x-platform-code': 'PCTV',
}
item_json = self._download_json(
f'{self._API_URL}/o/v1/season/asset?tao=0&tas=0&size=10000&id={season_id}', season_id, headers=headers)['body']['results']
entries = [
self.url_result(HotStarIE._video_url(video['contentId'], root=url), HotStarIE, video['contentId'])
for video in item_json['items'] if video.get('contentId')]
return self.playlist_result(entries, season_id)
class HotStarSeriesIE(HotStarBaseIE): class HotStarSeriesIE(HotStarBaseIE):
@ -378,17 +364,8 @@ class HotStarSeriesIE(HotStarBaseIE):
def _real_extract(self, url): def _real_extract(self, url):
url, series_id = self._match_valid_url(url).groups() url, series_id = self._match_valid_url(url).groups()
headers = { id_ = self._call_api_v1(
'x-country-code': 'IN', 'show/detail', series_id, query={'contentId': series_id})['body']['results']['item']['id']
'x-platform-code': 'PCTV',
}
detail_json = self._download_json(
f'{self._API_URL}/o/v1/show/detail?contentId={series_id}', series_id, headers=headers)
id = try_get(detail_json, lambda x: x['body']['results']['item']['id'], int)
item_json = self._download_json(
f'{self._API_URL}/o/v1/tray/g/1/items?etid=0&tao=0&tas=10000&eid={id}', series_id, headers=headers)
return self.playlist_result([ return self.playlist_result(self._playlist_entries(
self.url_result(HotStarIE._video_url(video['contentId'], root=url), HotStarIE, video['contentId']) 'tray/g/1/items', series_id, url, query={'tao': 0, 'tas': 10000, 'etid': 0, 'eid': id_}), series_id)
for video in item_json['body']['results']['items'] if video.get('contentId')
], series_id)