]>
Commit | Line | Data |
---|---|---|
fb8e402a | 1 | # coding: utf-8 |
2 | from __future__ import unicode_literals | |
3 | ||
85cd69ad RA |
4 | import hashlib |
5 | import hmac | |
6 | import time | |
909191de | 7 | |
fb8e402a | 8 | from .common import InfoExtractor |
85cd69ad | 9 | from ..compat import compat_HTTPError |
fb8e402a | 10 | from ..utils import ( |
fb8e402a | 11 | determine_ext, |
909191de | 12 | ExtractorError, |
fb8e402a | 13 | int_or_none, |
05e7c184 | 14 | try_get, |
fb8e402a | 15 | ) |
16 | ||
17 | ||
909191de | 18 | class HotStarBaseIE(InfoExtractor): |
85cd69ad RA |
19 | _AKAMAI_ENCRYPTION_KEY = b'\x05\xfc\x1a\x01\xca\xc9\x4b\xc4\x12\xfc\x53\x12\x07\x75\xf9\xee' |
20 | ||
21 | def _call_api(self, path, video_id, query_name='contentId'): | |
22 | st = int(time.time()) | |
23 | exp = st + 6000 | |
24 | auth = 'st=%d~exp=%d~acl=/*' % (st, exp) | |
25 | auth += '~hmac=' + hmac.new(self._AKAMAI_ENCRYPTION_KEY, auth.encode(), hashlib.sha256).hexdigest() | |
26 | response = self._download_json( | |
27 | 'https://api.hotstar.com/' + path, | |
28 | video_id, headers={ | |
29 | 'hotstarauth': auth, | |
30 | 'x-country-code': 'IN', | |
31 | 'x-platform-code': 'JIO', | |
32 | }, query={ | |
33 | query_name: video_id, | |
34 | 'tas': 10000, | |
35 | }) | |
36 | if response['statusCode'] != 'OK': | |
37 | raise ExtractorError( | |
38 | response['body']['message'], expected=True) | |
39 | return response['body']['results'] | |
909191de S |
40 | |
41 | ||
42 | class HotStarIE(HotStarBaseIE): | |
85cd69ad | 43 | IE_NAME = 'hotstar' |
909191de | 44 | _VALID_URL = r'https?://(?:www\.)?hotstar\.com/(?:.+?[/-])?(?P<id>\d{10})' |
89d23f37 | 45 | _TESTS = [{ |
85cd69ad | 46 | 'url': 'https://www.hotstar.com/can-you-not-spread-rumours/1000076273', |
fb8e402a | 47 | 'info_dict': { |
48 | 'id': '1000076273', | |
49 | 'ext': 'mp4', | |
85cd69ad | 50 | 'title': 'Can You Not Spread Rumours?', |
fb8e402a | 51 | 'description': 'md5:c957d8868e9bc793ccb813691cc4c434', |
85cd69ad | 52 | 'timestamp': 1447248600, |
fb8e402a | 53 | 'upload_date': '20151111', |
54 | 'duration': 381, | |
55 | }, | |
56 | 'params': { | |
57 | # m3u8 download | |
58 | 'skip_download': True, | |
59 | } | |
89d23f37 S |
60 | }, { |
61 | 'url': 'http://www.hotstar.com/sports/cricket/rajitha-sizzles-on-debut-with-329/2001477583', | |
62 | 'only_matching': True, | |
63 | }, { | |
64 | 'url': 'http://www.hotstar.com/1000000515', | |
65 | 'only_matching': True, | |
66 | }] | |
85cd69ad | 67 | _GEO_BYPASS = False |
fb8e402a | 68 | |
fb8e402a | 69 | def _real_extract(self, url): |
70 | video_id = self._match_id(url) | |
909191de | 71 | |
85cd69ad RA |
72 | webpage = self._download_webpage(url, video_id) |
73 | app_state = self._parse_json(self._search_regex( | |
74 | r'<script>window\.APP_STATE\s*=\s*({.+?})</script>', | |
75 | webpage, 'app state'), video_id) | |
05e7c184 RA |
76 | video_data = {} |
77 | for v in app_state.values(): | |
78 | content = try_get(v, lambda x: x['initialState']['contentData']['content'], dict) | |
79 | if content and content.get('contentId') == video_id: | |
80 | video_data = content | |
909191de | 81 | |
85cd69ad | 82 | title = video_data['title'] |
0dac7cbb | 83 | |
85cd69ad | 84 | if video_data.get('drmProtected'): |
0dac7cbb | 85 | raise ExtractorError('This video is DRM protected.', expected=True) |
fb8e402a | 86 | |
87 | formats = [] | |
85cd69ad RA |
88 | format_data = self._call_api('h/v1/play', video_id)['item'] |
89 | format_url = format_data['playbackUrl'] | |
90 | ext = determine_ext(format_url) | |
91 | if ext == 'm3u8': | |
92 | try: | |
93 | formats.extend(self._extract_m3u8_formats( | |
94 | format_url, video_id, 'mp4', m3u8_id='hls')) | |
95 | except ExtractorError as e: | |
96 | if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403: | |
97 | self.raise_geo_restricted(countries=['IN']) | |
98 | raise | |
99 | elif ext == 'f4m': | |
100 | # produce broken files | |
101 | pass | |
102 | else: | |
103 | formats.append({ | |
104 | 'url': format_url, | |
105 | 'width': int_or_none(format_data.get('width')), | |
106 | 'height': int_or_none(format_data.get('height')), | |
107 | }) | |
fb8e402a | 108 | self._sort_formats(formats) |
109 | ||
110 | return { | |
111 | 'id': video_id, | |
0dac7cbb | 112 | 'title': title, |
fb8e402a | 113 | 'description': video_data.get('description'), |
114 | 'duration': int_or_none(video_data.get('duration')), | |
85cd69ad | 115 | 'timestamp': int_or_none(video_data.get('broadcastDate') or video_data.get('startDate')), |
fb8e402a | 116 | 'formats': formats, |
85cd69ad RA |
117 | 'channel': video_data.get('channelName'), |
118 | 'channel_id': video_data.get('channelId'), | |
119 | 'series': video_data.get('showName'), | |
120 | 'season': video_data.get('seasonName'), | |
121 | 'season_number': int_or_none(video_data.get('seasonNo')), | |
122 | 'season_id': video_data.get('seasonId'), | |
0dac7cbb | 123 | 'episode': title, |
85cd69ad | 124 | 'episode_number': int_or_none(video_data.get('episodeNo')), |
fb8e402a | 125 | } |
477c97f8 AV |
126 | |
127 | ||
909191de | 128 | class HotStarPlaylistIE(HotStarBaseIE): |
477c97f8 | 129 | IE_NAME = 'hotstar:playlist' |
85cd69ad | 130 | _VALID_URL = r'https?://(?:www\.)?hotstar\.com/tv/[^/]+/s-\w+/list/[^/]+/t-(?P<id>\w+)' |
477c97f8 | 131 | _TESTS = [{ |
85cd69ad | 132 | 'url': 'https://www.hotstar.com/tv/savdhaan-india/s-26/list/popular-clips/t-3_2_26', |
477c97f8 | 133 | 'info_dict': { |
85cd69ad | 134 | 'id': '3_2_26', |
477c97f8 | 135 | }, |
85cd69ad | 136 | 'playlist_mincount': 20, |
477c97f8 | 137 | }, { |
85cd69ad | 138 | 'url': 'https://www.hotstar.com/tv/savdhaan-india/s-26/list/extras/t-2480', |
477c97f8 AV |
139 | 'only_matching': True, |
140 | }] | |
477c97f8 AV |
141 | |
142 | def _real_extract(self, url): | |
85cd69ad RA |
143 | playlist_id = self._match_id(url) |
144 | ||
145 | collection = self._call_api('o/v1/tray/find', playlist_id, 'uqId') | |
477c97f8 | 146 | |
477c97f8 | 147 | entries = [ |
909191de | 148 | self.url_result( |
85cd69ad | 149 | 'https://www.hotstar.com/%s' % video['contentId'], |
909191de | 150 | ie=HotStarIE.ie_key(), video_id=video['contentId']) |
85cd69ad | 151 | for video in collection['assets']['items'] |
909191de S |
152 | if video.get('contentId')] |
153 | ||
154 | return self.playlist_result(entries, playlist_id) |