[HotStar] Use API for metadata and extract subtitles (#640)

The API is not rate-limited unlike the webpage

Authored by: Ashish0804
This commit is contained in:
Ashish 2021-08-08 09:45:06 +05:30 committed by GitHub
parent bfd56b74b9
commit b6a35ad83b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -32,7 +32,7 @@ def _call_api_impl(self, path, video_id, query, st=None, cookies=None):
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()
if cookies.get('userUP'): if cookies and cookies.get('userUP'):
token = cookies.get('userUP').value token = cookies.get('userUP').value
else: else:
token = self._download_json( token = self._download_json(
@ -59,9 +59,12 @@ def _call_api_impl(self, path, video_id, query, st=None, cookies=None):
return response['data'] return response['data']
def _call_api(self, path, video_id, query_name='contentId'): def _call_api(self, path, video_id, query_name='contentId'):
return self._call_api_impl(path, video_id, { return self._download_json('https://api.hotstar.com/' + path, video_id=video_id, query={
query_name: video_id, query_name: video_id,
'tas': 10000, '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):
@ -77,15 +80,25 @@ def _call_api_v2(self, path, video_id, st=None, cookies=None):
class HotStarIE(HotStarBaseIE): class HotStarIE(HotStarBaseIE):
IE_NAME = 'hotstar' IE_NAME = 'hotstar'
_VALID_URL = r'''(?x) _VALID_URL = r'''(?x)
https?://(?:www\.)?hotstar\.com(?:/in)?/(?!in/) (?:
(?: hotstar\:|
tv/(?:[^/?#]+/){3}| https?://(?:www\.)?hotstar\.com(?:/in)?/(?!in/)
(?!tv/)[^?#]+/ )
)? (?:
(?P<id>\d{10}) (?P<type>movies|sports|episode|(?P<tv>tv))
(?:
\:|
/[^/?#]+/
(?(tv)
(?:[^/?#]+/){2}|
(?:[^/?#]+/)*
)
)|
[^/?#]+/
)?
(?P<id>\d{10})
''' '''
_TESTS = [{ _TESTS = [{
# contentData
'url': 'https://www.hotstar.com/can-you-not-spread-rumours/1000076273', 'url': 'https://www.hotstar.com/can-you-not-spread-rumours/1000076273',
'info_dict': { 'info_dict': {
'id': '1000076273', 'id': '1000076273',
@ -96,54 +109,87 @@ class HotStarIE(HotStarBaseIE):
'upload_date': '20151111', 'upload_date': '20151111',
'duration': 381, 'duration': 381,
}, },
'params': {
# m3u8 download
'skip_download': True,
}
}, { }, {
# contentDetail 'url': 'hotstar:1000076273',
'only_matching': True,
}, {
'url': 'https://www.hotstar.com/movies/radha-gopalam/1000057157', 'url': 'https://www.hotstar.com/movies/radha-gopalam/1000057157',
'info_dict': {
'id': '1000057157',
'ext': 'mp4',
'title': 'Radha Gopalam',
'description': 'md5:be3bc342cc120bbc95b3b0960e2b0d22',
'timestamp': 1140805800,
'upload_date': '20060224',
'duration': 9182,
},
}, {
'url': 'hotstar:movies:1000057157',
'only_matching': True, 'only_matching': True,
}, { }, {
'url': 'http://www.hotstar.com/sports/cricket/rajitha-sizzles-on-debut-with-329/2001477583', 'url': 'https://www.hotstar.com/in/sports/cricket/follow-the-blues-2021/recap-eng-fight-back-on-day-2/1260066104',
'only_matching': True, 'only_matching': True,
}, { }, {
'url': 'http://www.hotstar.com/1000000515', 'url': 'https://www.hotstar.com/in/sports/football/most-costly-pl-transfers-ft-grealish/1260065956',
'only_matching': True,
}, {
# contentData
'url': 'hotstar:sports:1260065956',
'only_matching': True,
}, {
# contentData
'url': 'hotstar:sports:1260066104',
'only_matching': True, 'only_matching': True,
}, { }, {
# only available via api v2
'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': {
'id': '1000234847',
'ext': 'mp4',
'title': 'Janhvi Targets Suman',
'description': 'md5:78a85509348910bd1ca31be898c5796b',
'timestamp': 1556670600,
'upload_date': '20190501',
'duration': 1219,
'channel': 'StarPlus',
'channel_id': 3,
'series': 'Ek Bhram - Sarvagun Sampanna',
'season': 'Chapter 1',
'season_number': 1,
'season_id': 6771,
'episode': 'Janhvi Targets Suman',
'episode_number': 8,
},
}, {
'url': 'hotstar:episode:1000234847',
'only_matching': True, 'only_matching': True,
}] }]
_GEO_BYPASS = False _GEO_BYPASS = False
_TYPE = {
'movies': 'movie',
'sports': 'match',
'episode': 'episode',
'tv': 'episode',
None: 'content',
}
def _real_extract(self, url): def _real_extract(self, url):
video_id = self._match_id(url) mobj = re.match(self._VALID_URL, url)
video_id = mobj.group('id')
video_type = mobj.group('type')
cookies = self._get_cookies(url) cookies = self._get_cookies(url)
webpage, urlh = self._download_webpage_handle(url, video_id) video_type = self._TYPE.get(video_type, video_type)
st = urlh.headers.get('x-origin-date') video_data = self._call_api(f'o/v1/{video_type}/detail', video_id)['body']['results']['item']
app_state = self._parse_json(self._search_regex(
r'<script>window\.APP_STATE\s*=\s*({.+?})</script>',
webpage, 'app state'), video_id)
video_data = {}
getters = list(
lambda x, k=k: x['initialState']['content%s' % k]['content']
for k in ('Data', 'Detail')
)
for v in app_state.values():
content = try_get(v, getters, dict)
if content and content.get('contentId') == video_id:
video_data = content
break
title = video_data['title'] title = video_data['title']
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'):
raise ExtractorError('This video is DRM protected.', expected=True) raise ExtractorError('This video is DRM protected.', expected=True)
headers = {'Referer': 'https://www.hotstar.com/in'}
headers = {'Referer': url}
formats = [] formats = []
subs = {}
geo_restricted = False geo_restricted = False
_, urlh = self._download_webpage_handle('https://www.hotstar.com/in', video_id)
# Required to fix https://github.com/yt-dlp/yt-dlp/issues/396
st = urlh.headers.get('x-origin-date')
# 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, cookies=cookies)['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:
@ -160,13 +206,17 @@ def _real_extract(self, url):
ext = determine_ext(format_url) ext = determine_ext(format_url)
try: try:
if 'package:hls' in tags or ext == 'm3u8': if 'package:hls' in tags or ext == 'm3u8':
formats.extend(self._extract_m3u8_formats( hls_formats, hls_subs = self._extract_m3u8_formats_and_subtitles(
format_url, video_id, 'mp4', format_url, video_id, 'mp4',
entry_protocol='m3u8_native', entry_protocol='m3u8_native',
m3u8_id='hls', headers=headers)) m3u8_id='hls', headers=headers)
formats.extend(hls_formats)
subs = self._merge_subtitles(subs, hls_subs)
elif 'package:dash' in tags or ext == 'mpd': elif 'package:dash' in tags or ext == 'mpd':
formats.extend(self._extract_mpd_formats( dash_formats, dash_subs = self._extract_mpd_formats_and_subtitles(
format_url, video_id, mpd_id='dash', headers=headers)) format_url, video_id, mpd_id='dash', headers=headers)
formats.extend(dash_formats)
subs = self._merge_subtitles(subs, dash_subs)
elif ext == 'f4m': elif ext == 'f4m':
# produce broken files # produce broken files
pass pass
@ -194,6 +244,7 @@ def _real_extract(self, url):
'duration': int_or_none(video_data.get('duration')), 'duration': int_or_none(video_data.get('duration')),
'timestamp': int_or_none(video_data.get('broadcastDate') or video_data.get('startDate')), 'timestamp': int_or_none(video_data.get('broadcastDate') or video_data.get('startDate')),
'formats': formats, 'formats': formats,
'subtitles': subs,
'channel': video_data.get('channelName'), 'channel': video_data.get('channelName'),
'channel_id': video_data.get('channelId'), 'channel_id': video_data.get('channelId'),
'series': video_data.get('showName'), 'series': video_data.get('showName'),
@ -222,8 +273,7 @@ class HotStarPlaylistIE(HotStarBaseIE):
def _real_extract(self, url): def _real_extract(self, url):
playlist_id = self._match_id(url) playlist_id = self._match_id(url)
collection = self._call_api('o/v1/tray/find', playlist_id, 'uqId') collection = self._call_api('o/v1/tray/find', playlist_id, 'uqId')['body']['results']
entries = [ entries = [
self.url_result( self.url_result(
'https://www.hotstar.com/%s' % video['contentId'], 'https://www.hotstar.com/%s' % video['contentId'],
@ -270,7 +320,7 @@ def _real_extract(self, url):
video_id=series_id, headers=headers) video_id=series_id, headers=headers)
entries = [ entries = [
self.url_result( self.url_result(
'https://www.hotstar.com/%d' % video['contentId'], 'hotstar:episode:%d' % video['contentId'],
ie=HotStarIE.ie_key(), video_id=video['contentId']) ie=HotStarIE.ie_key(), video_id=video['contentId'])
for video in item_json['body']['results']['items'] for video in item_json['body']['results']['items']
if video.get('contentId')] if video.get('contentId')]