]> jfr.im git - yt-dlp.git/blame - youtube_dlc/extractor/hotstar.py
[skip travis] renaming
[yt-dlp.git] / youtube_dlc / extractor / hotstar.py
CommitLineData
fb8e402a 1# coding: utf-8
2from __future__ import unicode_literals
3
85cd69ad
RA
4import hashlib
5import hmac
1cb812d3 6import re
85cd69ad 7import time
2533f5b6 8import uuid
909191de 9
fb8e402a 10from .common import InfoExtractor
2533f5b6
S
11from ..compat import (
12 compat_HTTPError,
13 compat_str,
14)
fb8e402a 15from ..utils import (
fb8e402a 16 determine_ext,
909191de 17 ExtractorError,
fb8e402a 18 int_or_none,
2533f5b6 19 str_or_none,
05e7c184 20 try_get,
2533f5b6 21 url_or_none,
fb8e402a 22)
23
24
909191de 25class HotStarBaseIE(InfoExtractor):
85cd69ad
RA
26 _AKAMAI_ENCRYPTION_KEY = b'\x05\xfc\x1a\x01\xca\xc9\x4b\xc4\x12\xfc\x53\x12\x07\x75\xf9\xee'
27
2533f5b6 28 def _call_api_impl(self, path, video_id, query):
85cd69ad
RA
29 st = int(time.time())
30 exp = st + 6000
31 auth = 'st=%d~exp=%d~acl=/*' % (st, exp)
32 auth += '~hmac=' + hmac.new(self._AKAMAI_ENCRYPTION_KEY, auth.encode(), hashlib.sha256).hexdigest()
33 response = self._download_json(
2533f5b6 34 'https://api.hotstar.com/' + path, video_id, headers={
85cd69ad
RA
35 'hotstarauth': auth,
36 'x-country-code': 'IN',
37 'x-platform-code': 'JIO',
2533f5b6 38 }, query=query)
85cd69ad
RA
39 if response['statusCode'] != 'OK':
40 raise ExtractorError(
41 response['body']['message'], expected=True)
42 return response['body']['results']
909191de 43
2533f5b6
S
44 def _call_api(self, path, video_id, query_name='contentId'):
45 return self._call_api_impl(path, video_id, {
46 query_name: video_id,
47 'tas': 10000,
48 })
49
50 def _call_api_v2(self, path, video_id):
51 return self._call_api_impl(
52 '%s/in/contents/%s' % (path, video_id), video_id, {
53 'desiredConfig': 'encryption:plain;ladder:phone,tv;package:hls,dash',
54 'client': 'mweb',
55 'clientVersion': '6.18.0',
56 'deviceId': compat_str(uuid.uuid4()),
57 'osName': 'Windows',
58 'osVersion': '10',
59 })
60
909191de
S
61
62class HotStarIE(HotStarBaseIE):
85cd69ad 63 IE_NAME = 'hotstar'
909191de 64 _VALID_URL = r'https?://(?:www\.)?hotstar\.com/(?:.+?[/-])?(?P<id>\d{10})'
89d23f37 65 _TESTS = [{
adbbdefc 66 # contentData
85cd69ad 67 'url': 'https://www.hotstar.com/can-you-not-spread-rumours/1000076273',
fb8e402a 68 'info_dict': {
69 'id': '1000076273',
70 'ext': 'mp4',
85cd69ad 71 'title': 'Can You Not Spread Rumours?',
fb8e402a 72 'description': 'md5:c957d8868e9bc793ccb813691cc4c434',
85cd69ad 73 'timestamp': 1447248600,
fb8e402a 74 'upload_date': '20151111',
75 'duration': 381,
76 },
77 'params': {
78 # m3u8 download
79 'skip_download': True,
80 }
adbbdefc
S
81 }, {
82 # contentDetail
83 'url': 'https://www.hotstar.com/movies/radha-gopalam/1000057157',
84 'only_matching': True,
89d23f37
S
85 }, {
86 'url': 'http://www.hotstar.com/sports/cricket/rajitha-sizzles-on-debut-with-329/2001477583',
87 'only_matching': True,
88 }, {
89 'url': 'http://www.hotstar.com/1000000515',
90 'only_matching': True,
2533f5b6
S
91 }, {
92 # only available via api v2
93 'url': 'https://www.hotstar.com/tv/ek-bhram-sarvagun-sampanna/s-2116/janhvi-targets-suman/1000234847',
94 'only_matching': True,
89d23f37 95 }]
85cd69ad 96 _GEO_BYPASS = False
fb8e402a 97
fb8e402a 98 def _real_extract(self, url):
99 video_id = self._match_id(url)
909191de 100
85cd69ad
RA
101 webpage = self._download_webpage(url, video_id)
102 app_state = self._parse_json(self._search_regex(
103 r'<script>window\.APP_STATE\s*=\s*({.+?})</script>',
104 webpage, 'app state'), video_id)
05e7c184 105 video_data = {}
c3c098dc 106 getters = list(
adbbdefc
S
107 lambda x, k=k: x['initialState']['content%s' % k]['content']
108 for k in ('Data', 'Detail')
109 )
05e7c184 110 for v in app_state.values():
adbbdefc 111 content = try_get(v, getters, dict)
05e7c184
RA
112 if content and content.get('contentId') == video_id:
113 video_data = content
c3c098dc 114 break
909191de 115
85cd69ad 116 title = video_data['title']
0dac7cbb 117
85cd69ad 118 if video_data.get('drmProtected'):
0dac7cbb 119 raise ExtractorError('This video is DRM protected.', expected=True)
fb8e402a 120
d7def23d 121 headers = {'Referer': url}
fb8e402a 122 formats = []
2533f5b6
S
123 geo_restricted = False
124 playback_sets = self._call_api_v2('h/v2/play', video_id)['playBackSets']
125 for playback_set in playback_sets:
126 if not isinstance(playback_set, dict):
127 continue
128 format_url = url_or_none(playback_set.get('playbackUrl'))
129 if not format_url:
130 continue
1cb812d3
S
131 format_url = re.sub(
132 r'(?<=//staragvod)(\d)', r'web\1', format_url)
2533f5b6
S
133 tags = str_or_none(playback_set.get('tagsCombination')) or ''
134 if tags and 'encryption:plain' not in tags:
135 continue
136 ext = determine_ext(format_url)
85cd69ad 137 try:
2533f5b6
S
138 if 'package:hls' in tags or ext == 'm3u8':
139 formats.extend(self._extract_m3u8_formats(
d9d30986 140 format_url, video_id, 'mp4',
d7def23d
RA
141 entry_protocol='m3u8_native',
142 m3u8_id='hls', headers=headers))
2533f5b6
S
143 elif 'package:dash' in tags or ext == 'mpd':
144 formats.extend(self._extract_mpd_formats(
d7def23d 145 format_url, video_id, mpd_id='dash', headers=headers))
2533f5b6
S
146 elif ext == 'f4m':
147 # produce broken files
148 pass
149 else:
150 formats.append({
151 'url': format_url,
152 'width': int_or_none(playback_set.get('width')),
153 'height': int_or_none(playback_set.get('height')),
154 })
85cd69ad
RA
155 except ExtractorError as e:
156 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
2533f5b6
S
157 geo_restricted = True
158 continue
159 if not formats and geo_restricted:
160 self.raise_geo_restricted(countries=['IN'])
fb8e402a 161 self._sort_formats(formats)
162
d7def23d
RA
163 for f in formats:
164 f.setdefault('http_headers', {}).update(headers)
165
fb8e402a 166 return {
167 'id': video_id,
0dac7cbb 168 'title': title,
fb8e402a 169 'description': video_data.get('description'),
170 'duration': int_or_none(video_data.get('duration')),
85cd69ad 171 'timestamp': int_or_none(video_data.get('broadcastDate') or video_data.get('startDate')),
fb8e402a 172 'formats': formats,
85cd69ad
RA
173 'channel': video_data.get('channelName'),
174 'channel_id': video_data.get('channelId'),
175 'series': video_data.get('showName'),
176 'season': video_data.get('seasonName'),
177 'season_number': int_or_none(video_data.get('seasonNo')),
178 'season_id': video_data.get('seasonId'),
0dac7cbb 179 'episode': title,
85cd69ad 180 'episode_number': int_or_none(video_data.get('episodeNo')),
fb8e402a 181 }
477c97f8
AV
182
183
909191de 184class HotStarPlaylistIE(HotStarBaseIE):
477c97f8 185 IE_NAME = 'hotstar:playlist'
85cd69ad 186 _VALID_URL = r'https?://(?:www\.)?hotstar\.com/tv/[^/]+/s-\w+/list/[^/]+/t-(?P<id>\w+)'
477c97f8 187 _TESTS = [{
85cd69ad 188 'url': 'https://www.hotstar.com/tv/savdhaan-india/s-26/list/popular-clips/t-3_2_26',
477c97f8 189 'info_dict': {
85cd69ad 190 'id': '3_2_26',
477c97f8 191 },
85cd69ad 192 'playlist_mincount': 20,
477c97f8 193 }, {
85cd69ad 194 'url': 'https://www.hotstar.com/tv/savdhaan-india/s-26/list/extras/t-2480',
477c97f8
AV
195 'only_matching': True,
196 }]
477c97f8
AV
197
198 def _real_extract(self, url):
85cd69ad
RA
199 playlist_id = self._match_id(url)
200
201 collection = self._call_api('o/v1/tray/find', playlist_id, 'uqId')
477c97f8 202
477c97f8 203 entries = [
909191de 204 self.url_result(
85cd69ad 205 'https://www.hotstar.com/%s' % video['contentId'],
909191de 206 ie=HotStarIE.ie_key(), video_id=video['contentId'])
85cd69ad 207 for video in collection['assets']['items']
909191de
S
208 if video.get('contentId')]
209
210 return self.playlist_result(entries, playlist_id)