]> jfr.im git - yt-dlp.git/blob - youtube_dlc/extractor/hotstar.py
Fix `--windows-filenames` removing `/` from UNIX paths
[yt-dlp.git] / youtube_dlc / extractor / hotstar.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import hashlib
5 import hmac
6 import re
7 import time
8 import uuid
9 import json
10 import random
11
12 from .common import InfoExtractor
13 from ..compat import (
14 compat_HTTPError,
15 compat_str
16 )
17 from ..utils import (
18 determine_ext,
19 ExtractorError,
20 int_or_none,
21 str_or_none,
22 try_get,
23 url_or_none,
24 )
25
26
27 class HotStarBaseIE(InfoExtractor):
28 _AKAMAI_ENCRYPTION_KEY = b'\x05\xfc\x1a\x01\xca\xc9\x4b\xc4\x12\xfc\x53\x12\x07\x75\xf9\xee'
29
30 def _call_api_impl(self, path, video_id, query):
31 st = int(time.time())
32 exp = st + 6000
33 auth = 'st=%d~exp=%d~acl=/*' % (st, exp)
34 auth += '~hmac=' + hmac.new(self._AKAMAI_ENCRYPTION_KEY, auth.encode(), hashlib.sha256).hexdigest()
35
36 def _generate_device_id():
37 """
38 Reversed from javascript library.
39 JS function is generateUUID
40 """
41 t = int(round(time.time() * 1000))
42 e = "xxxxxxxx-xxxx-4xxx-xxxx-xxxxxxxxxxxx" # 4 seems to be interchangeable
43
44 def _replacer():
45 n = int((t + 16 * random.random())) % 16 | 0
46 return hex(n if "x" == e else 3 & n | 8)[2:]
47 return "".join([_.replace('x', _replacer()) for _ in e])
48
49 token = self._download_json(
50 'https://api.hotstar.com/um/v3/users',
51 video_id, note='Downloading token',
52 data=json.dumps({"device_ids": [{"id": compat_str(uuid.uuid4()), "type": "device_id"}]}).encode('utf-8'),
53 headers={
54 'hotstarauth': auth,
55 'x-hs-platform': 'PCTV', # or 'web'
56 'Content-Type': 'application/json',
57 })['user_identity']
58
59 response = self._download_json(
60 'https://api.hotstar.com/' + path, video_id, headers={
61 'hotstarauth': auth,
62 'x-hs-appversion': '6.72.2',
63 'x-hs-platform': 'web',
64 'x-hs-usertoken': token,
65 }, query=query)
66
67 if response['message'] != "Playback URL's fetched successfully":
68 raise ExtractorError(
69 response['message'], expected=True)
70 return response['data']
71
72 def _call_api(self, path, video_id, query_name='contentId'):
73 return self._call_api_impl(path, video_id, {
74 query_name: video_id,
75 'tas': 10000,
76 })
77
78 def _call_api_v2(self, path, video_id):
79 return self._call_api_impl(
80 '%s/content/%s' % (path, video_id), video_id, {
81 'desired-config': 'audio_channel:stereo|dynamic_range:sdr|encryption:plain|ladder:tv|package:dash|resolution:hd|subs-tag:HotstarVIP|video_codec:vp9',
82 'device-id': compat_str(uuid.uuid4()),
83 'os-name': 'Windows',
84 'os-version': '10',
85 })
86
87
88 class HotStarIE(HotStarBaseIE):
89 IE_NAME = 'hotstar'
90 _VALID_URL = r'https?://(?:www\.)?hotstar\.com/.*(?P<id>\d{10})'
91 _TESTS = [{
92 # contentData
93 'url': 'https://www.hotstar.com/can-you-not-spread-rumours/1000076273',
94 'info_dict': {
95 'id': '1000076273',
96 'ext': 'mp4',
97 'title': 'Can You Not Spread Rumours?',
98 'description': 'md5:c957d8868e9bc793ccb813691cc4c434',
99 'timestamp': 1447248600,
100 'upload_date': '20151111',
101 'duration': 381,
102 },
103 'params': {
104 # m3u8 download
105 'skip_download': True,
106 }
107 }, {
108 # contentDetail
109 'url': 'https://www.hotstar.com/movies/radha-gopalam/1000057157',
110 'only_matching': True,
111 }, {
112 'url': 'http://www.hotstar.com/sports/cricket/rajitha-sizzles-on-debut-with-329/2001477583',
113 'only_matching': True,
114 }, {
115 'url': 'http://www.hotstar.com/1000000515',
116 'only_matching': True,
117 }, {
118 # only available via api v2
119 'url': 'https://www.hotstar.com/tv/ek-bhram-sarvagun-sampanna/s-2116/janhvi-targets-suman/1000234847',
120 'only_matching': True,
121 }]
122 _GEO_BYPASS = False
123
124 def _real_extract(self, url):
125 video_id = self._match_id(url)
126
127 webpage = self._download_webpage(url, video_id)
128 app_state = self._parse_json(self._search_regex(
129 r'<script>window\.APP_STATE\s*=\s*({.+?})</script>',
130 webpage, 'app state'), video_id)
131 video_data = {}
132 getters = list(
133 lambda x, k=k: x['initialState']['content%s' % k]['content']
134 for k in ('Data', 'Detail')
135 )
136 for v in app_state.values():
137 content = try_get(v, getters, dict)
138 if content and content.get('contentId') == video_id:
139 video_data = content
140 break
141
142 title = video_data['title']
143
144 if not self._downloader.params.get('allow_unplayable_formats') and video_data.get('drmProtected'):
145 raise ExtractorError('This video is DRM protected.', expected=True)
146
147 headers = {'Referer': url}
148 formats = []
149 geo_restricted = False
150 # change to v2 in the future
151 playback_sets = self._call_api_v2('play/v1/playback', video_id)['playBackSets']
152 for playback_set in playback_sets:
153 if not isinstance(playback_set, dict):
154 continue
155 format_url = url_or_none(playback_set.get('playbackUrl'))
156 if not format_url:
157 continue
158 format_url = re.sub(
159 r'(?<=//staragvod)(\d)', r'web\1', format_url)
160 tags = str_or_none(playback_set.get('tagsCombination')) or ''
161 if tags and 'encryption:plain' not in tags:
162 continue
163 ext = determine_ext(format_url)
164 try:
165 if 'package:hls' in tags or ext == 'm3u8':
166 formats.extend(self._extract_m3u8_formats(
167 format_url, video_id, 'mp4',
168 entry_protocol='m3u8_native',
169 m3u8_id='hls', headers=headers))
170 elif 'package:dash' in tags or ext == 'mpd':
171 formats.extend(self._extract_mpd_formats(
172 format_url, video_id, mpd_id='dash', headers=headers))
173 elif ext == 'f4m':
174 # produce broken files
175 pass
176 else:
177 formats.append({
178 'url': format_url,
179 'width': int_or_none(playback_set.get('width')),
180 'height': int_or_none(playback_set.get('height')),
181 })
182 except ExtractorError as e:
183 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
184 geo_restricted = True
185 continue
186 if not formats and geo_restricted:
187 self.raise_geo_restricted(countries=['IN'])
188 self._sort_formats(formats)
189
190 for f in formats:
191 f.setdefault('http_headers', {}).update(headers)
192
193 return {
194 'id': video_id,
195 'title': title,
196 'description': video_data.get('description'),
197 'duration': int_or_none(video_data.get('duration')),
198 'timestamp': int_or_none(video_data.get('broadcastDate') or video_data.get('startDate')),
199 'formats': formats,
200 'channel': video_data.get('channelName'),
201 'channel_id': video_data.get('channelId'),
202 'series': video_data.get('showName'),
203 'season': video_data.get('seasonName'),
204 'season_number': int_or_none(video_data.get('seasonNo')),
205 'season_id': video_data.get('seasonId'),
206 'episode': title,
207 'episode_number': int_or_none(video_data.get('episodeNo')),
208 }
209
210
211 class HotStarPlaylistIE(HotStarBaseIE):
212 IE_NAME = 'hotstar:playlist'
213 _VALID_URL = r'https?://(?:www\.)?hotstar\.com/tv/[^/]+/s-\w+/list/[^/]+/t-(?P<id>\w+)'
214 _TESTS = [{
215 'url': 'https://www.hotstar.com/tv/savdhaan-india/s-26/list/popular-clips/t-3_2_26',
216 'info_dict': {
217 'id': '3_2_26',
218 },
219 'playlist_mincount': 20,
220 }, {
221 'url': 'https://www.hotstar.com/tv/savdhaan-india/s-26/list/extras/t-2480',
222 'only_matching': True,
223 }]
224
225 def _real_extract(self, url):
226 playlist_id = self._match_id(url)
227
228 collection = self._call_api('o/v1/tray/find', playlist_id, 'uqId')
229
230 entries = [
231 self.url_result(
232 'https://www.hotstar.com/%s' % video['contentId'],
233 ie=HotStarIE.ie_key(), video_id=video['contentId'])
234 for video in collection['assets']['items']
235 if video.get('contentId')]
236
237 return self.playlist_result(entries, playlist_id)