]>
Commit | Line | Data |
---|---|---|
e7b6caef | 1 | import re |
1fb707ba | 2 | import json |
3 | import uuid | |
4 | import random | |
5 | import urllib.parse | |
e7b6caef | 6 | |
7 | from .common import InfoExtractor | |
1fb707ba | 8 | from ..compat import compat_str |
e7b6caef | 9 | from ..utils import ( |
10 | ExtractorError, | |
11 | int_or_none, | |
1fb707ba | 12 | strip_or_none, |
a7191c6f | 13 | try_get, |
baa58739 | 14 | smuggle_url, |
15 | unsmuggle_url, | |
1fb707ba | 16 | url_or_none, |
e7b6caef | 17 | ) |
18 | ||
19 | ||
20 | class ViuBaseIE(InfoExtractor): | |
1fb707ba | 21 | def _call_api(self, path, *args, headers={}, **kwargs): |
72310315 | 22 | response = self._download_json( |
1fb707ba | 23 | f'https://www.viu.com/api/{path}', *args, **kwargs, |
24 | headers={**self.geo_verification_headers(), **headers})['response'] | |
72310315 | 25 | if response.get('status') != 'success': |
1fb707ba | 26 | raise ExtractorError(f'{self.IE_NAME} said: {response["message"]}', expected=True) |
72310315 | 27 | return response |
e7b6caef | 28 | |
29 | ||
30 | class ViuIE(ViuBaseIE): | |
c183e14f | 31 | _VALID_URL = r'(?:viu:|https?://[^/]+\.viu\.com/[a-z]{2}/media/)(?P<id>\d+)' |
e7b6caef | 32 | _TESTS = [{ |
e7b6caef | 33 | 'url': 'https://www.viu.com/en/media/1116705532?containerId=playlist-22168059', |
34 | 'info_dict': { | |
35 | 'id': '1116705532', | |
36 | 'ext': 'mp4', | |
72310315 | 37 | 'title': 'Citizen Khan - Ep 1', |
e7b6caef | 38 | 'description': 'md5:d7ea1604f49e5ba79c212c551ce2110e', |
39 | }, | |
40 | 'params': { | |
41 | 'skip_download': 'm3u8 download', | |
42 | }, | |
43 | 'skip': 'Geo-restricted to India', | |
44 | }, { | |
45 | 'url': 'https://www.viu.com/en/media/1130599965', | |
46 | 'info_dict': { | |
47 | 'id': '1130599965', | |
48 | 'ext': 'mp4', | |
49 | 'title': 'Jealousy Incarnate - Episode 1', | |
50 | 'description': 'md5:d3d82375cab969415d2720b6894361e9', | |
51 | }, | |
52 | 'params': { | |
53 | 'skip_download': 'm3u8 download', | |
54 | }, | |
55 | 'skip': 'Geo-restricted to Indonesia', | |
c183e14f S |
56 | }, { |
57 | 'url': 'https://india.viu.com/en/media/1126286865', | |
58 | 'only_matching': True, | |
e7b6caef | 59 | }] |
60 | ||
61 | def _real_extract(self, url): | |
62 | video_id = self._match_id(url) | |
63 | ||
72310315 RA |
64 | video_data = self._call_api( |
65 | 'clip/load', video_id, 'Downloading video data', query={ | |
66 | 'appid': 'viu_desktop', | |
67 | 'fmt': 'json', | |
68 | 'id': video_id | |
69 | })['item'][0] | |
70 | ||
71 | title = video_data['title'] | |
72 | ||
73 | m3u8_url = None | |
74 | url_path = video_data.get('urlpathd') or video_data.get('urlpath') | |
75 | tdirforwhole = video_data.get('tdirforwhole') | |
ed7b333f RA |
76 | # #EXT-X-BYTERANGE is not supported by native hls downloader |
77 | # and ffmpeg (#10955) | |
1fb707ba | 78 | # FIXME: It is supported in yt-dlp |
ed7b333f RA |
79 | # hls_file = video_data.get('hlsfile') |
80 | hls_file = video_data.get('jwhlsfile') | |
72310315 RA |
81 | if url_path and tdirforwhole and hls_file: |
82 | m3u8_url = '%s/%s/%s' % (url_path, tdirforwhole, hls_file) | |
83 | else: | |
ed7b333f RA |
84 | # m3u8_url = re.sub( |
85 | # r'(/hlsc_)[a-z]+(\d+\.m3u8)', | |
86 | # r'\1whe\2', video_data['href']) | |
87 | m3u8_url = video_data['href'] | |
47b8bf20 | 88 | formats, subtitles = self._extract_m3u8_formats_and_subtitles(m3u8_url, video_id, 'mp4') |
e7b6caef | 89 | self._sort_formats(formats) |
90 | ||
72310315 RA |
91 | for key, value in video_data.items(): |
92 | mobj = re.match(r'^subtitle_(?P<lang>[^_]+)_(?P<ext>(vtt|srt))', key) | |
93 | if not mobj: | |
94 | continue | |
95 | subtitles.setdefault(mobj.group('lang'), []).append({ | |
96 | 'url': value, | |
97 | 'ext': mobj.group('ext') | |
98 | }) | |
e7b6caef | 99 | |
100 | return { | |
101 | 'id': video_id, | |
102 | 'title': title, | |
72310315 RA |
103 | 'description': video_data.get('description'), |
104 | 'series': video_data.get('moviealbumshowname'), | |
105 | 'episode': title, | |
106 | 'episode_number': int_or_none(video_data.get('episodeno')), | |
107 | 'duration': int_or_none(video_data.get('duration')), | |
e7b6caef | 108 | 'formats': formats, |
109 | 'subtitles': subtitles, | |
110 | } | |
111 | ||
112 | ||
113 | class ViuPlaylistIE(ViuBaseIE): | |
114 | IE_NAME = 'viu:playlist' | |
72310315 | 115 | _VALID_URL = r'https?://www\.viu\.com/[^/]+/listing/playlist-(?P<id>\d+)' |
e7b6caef | 116 | _TEST = { |
117 | 'url': 'https://www.viu.com/en/listing/playlist-22461380', | |
118 | 'info_dict': { | |
72310315 | 119 | 'id': '22461380', |
e7b6caef | 120 | 'title': 'The Good Wife', |
121 | }, | |
122 | 'playlist_count': 16, | |
123 | 'skip': 'Geo-restricted to Indonesia', | |
124 | } | |
125 | ||
126 | def _real_extract(self, url): | |
127 | playlist_id = self._match_id(url) | |
72310315 RA |
128 | playlist_data = self._call_api( |
129 | 'container/load', playlist_id, | |
130 | 'Downloading playlist info', query={ | |
131 | 'appid': 'viu_desktop', | |
132 | 'fmt': 'json', | |
133 | 'id': 'playlist-' + playlist_id | |
134 | })['container'] | |
135 | ||
136 | entries = [] | |
137 | for item in playlist_data.get('item', []): | |
138 | item_id = item.get('id') | |
139 | if not item_id: | |
140 | continue | |
141 | item_id = compat_str(item_id) | |
142 | entries.append(self.url_result( | |
143 | 'viu:' + item_id, 'Viu', item_id)) | |
144 | ||
145 | return self.playlist_result( | |
146 | entries, playlist_id, playlist_data.get('title')) | |
147 | ||
148 | ||
149 | class ViuOTTIE(InfoExtractor): | |
150 | IE_NAME = 'viu:ott' | |
baa58739 | 151 | _NETRC_MACHINE = 'viu' |
152 | _VALID_URL = r'https?://(?:www\.)?viu\.com/ott/(?P<country_code>[a-z]{2})/(?P<lang_code>[a-z]{2}-[a-z]{2})/vod/(?P<id>\d+)' | |
72310315 RA |
153 | _TESTS = [{ |
154 | 'url': 'http://www.viu.com/ott/sg/en-us/vod/3421/The%20Prime%20Minister%20and%20I', | |
155 | 'info_dict': { | |
156 | 'id': '3421', | |
157 | 'ext': 'mp4', | |
158 | 'title': 'A New Beginning', | |
159 | 'description': 'md5:1e7486a619b6399b25ba6a41c0fe5b2c', | |
160 | }, | |
161 | 'params': { | |
162 | 'skip_download': 'm3u8 download', | |
baa58739 | 163 | 'noplaylist': True, |
72310315 RA |
164 | }, |
165 | 'skip': 'Geo-restricted to Singapore', | |
166 | }, { | |
167 | 'url': 'http://www.viu.com/ott/hk/zh-hk/vod/7123/%E5%A4%A7%E4%BA%BA%E5%A5%B3%E5%AD%90', | |
168 | 'info_dict': { | |
169 | 'id': '7123', | |
170 | 'ext': 'mp4', | |
171 | 'title': '這就是我的生活之道', | |
172 | 'description': 'md5:4eb0d8b08cf04fcdc6bbbeb16043434f', | |
173 | }, | |
174 | 'params': { | |
175 | 'skip_download': 'm3u8 download', | |
baa58739 | 176 | 'noplaylist': True, |
177 | }, | |
178 | 'skip': 'Geo-restricted to Hong Kong', | |
179 | }, { | |
180 | 'url': 'https://www.viu.com/ott/hk/zh-hk/vod/68776/%E6%99%82%E5%B0%9A%E5%AA%BD%E5%92%AA', | |
181 | 'playlist_count': 12, | |
182 | 'info_dict': { | |
183 | 'id': '3916', | |
184 | 'title': '時尚媽咪', | |
185 | }, | |
186 | 'params': { | |
187 | 'skip_download': 'm3u8 download', | |
188 | 'noplaylist': False, | |
72310315 RA |
189 | }, |
190 | 'skip': 'Geo-restricted to Hong Kong', | |
191 | }] | |
192 | ||
ecb6b6ae S |
193 | _AREA_ID = { |
194 | 'HK': 1, | |
195 | 'SG': 2, | |
196 | 'TH': 4, | |
197 | 'PH': 5, | |
198 | } | |
baa58739 | 199 | _LANGUAGE_FLAG = { |
200 | 'zh-hk': 1, | |
201 | 'zh-cn': 2, | |
202 | 'en-us': 3, | |
203 | } | |
1fb707ba | 204 | |
205 | _user_token = None | |
206 | _auth_codes = {} | |
baa58739 | 207 | |
208 | def _detect_error(self, response): | |
1fb707ba | 209 | code = try_get(response, lambda x: x['status']['code']) |
210 | if code and code > 0: | |
baa58739 | 211 | message = try_get(response, lambda x: x['status']['message']) |
1fb707ba | 212 | raise ExtractorError(f'{self.IE_NAME} said: {message} ({code})', expected=True) |
213 | return response.get('data') or {} | |
baa58739 | 214 | |
215 | def _login(self, country_code, video_id): | |
1fb707ba | 216 | if self._user_token is None: |
baa58739 | 217 | username, password = self._get_login_info() |
1fb707ba | 218 | if username is None: |
baa58739 | 219 | return |
1fb707ba | 220 | headers = { |
221 | 'Authorization': f'Bearer {self._auth_codes[country_code]}', | |
222 | 'Content-Type': 'application/json' | |
223 | } | |
224 | data = self._download_json( | |
225 | 'https://api-gateway-global.viu.com/api/account/validate', | |
226 | video_id, 'Validating email address', headers=headers, | |
227 | data=json.dumps({ | |
228 | 'principal': username, | |
229 | 'provider': 'email' | |
230 | }).encode()) | |
231 | if not data.get('exists'): | |
232 | raise ExtractorError('Invalid email address') | |
baa58739 | 233 | |
234 | data = self._download_json( | |
1fb707ba | 235 | 'https://api-gateway-global.viu.com/api/auth/login', |
236 | video_id, 'Logging in', headers=headers, | |
baa58739 | 237 | data=json.dumps({ |
1fb707ba | 238 | 'email': username, |
baa58739 | 239 | 'password': password, |
1fb707ba | 240 | 'provider': 'email', |
baa58739 | 241 | }).encode()) |
1fb707ba | 242 | self._detect_error(data) |
243 | self._user_token = data.get('identity') | |
244 | # need to update with valid user's token else will throw an error again | |
245 | self._auth_codes[country_code] = data.get('token') | |
246 | return self._user_token | |
247 | ||
248 | def _get_token(self, country_code, video_id): | |
249 | rand = ''.join(random.choice('0123456789') for _ in range(10)) | |
250 | return self._download_json( | |
251 | f'https://api-gateway-global.viu.com/api/auth/token?v={rand}000', video_id, | |
252 | headers={'Content-Type': 'application/json'}, note='Getting bearer token', | |
253 | data=json.dumps({ | |
254 | 'countryCode': country_code.upper(), | |
255 | 'platform': 'browser', | |
256 | 'platformFlagLabel': 'web', | |
257 | 'language': 'en', | |
258 | 'uuid': str(uuid.uuid4()), | |
259 | 'carrierId': '0' | |
260 | }).encode('utf-8'))['token'] | |
ecb6b6ae | 261 | |
72310315 | 262 | def _real_extract(self, url): |
baa58739 | 263 | url, idata = unsmuggle_url(url, {}) |
5ad28e7f | 264 | country_code, lang_code, video_id = self._match_valid_url(url).groups() |
72310315 | 265 | |
ecb6b6ae S |
266 | query = { |
267 | 'r': 'vod/ajax-detail', | |
268 | 'platform_flag_label': 'web', | |
269 | 'product_id': video_id, | |
270 | } | |
271 | ||
272 | area_id = self._AREA_ID.get(country_code.upper()) | |
273 | if area_id: | |
274 | query['area_id'] = area_id | |
275 | ||
72310315 | 276 | product_data = self._download_json( |
1fb707ba | 277 | f'http://www.viu.com/ott/{country_code}/index.php', video_id, |
ecb6b6ae | 278 | 'Downloading video info', query=query)['data'] |
72310315 RA |
279 | |
280 | video_data = product_data.get('current_product') | |
281 | if not video_data: | |
1fb707ba | 282 | self.raise_geo_restricted() |
72310315 | 283 | |
baa58739 | 284 | series_id = video_data.get('series_id') |
f40ee5e9 | 285 | if self._yes_playlist(series_id, video_id, idata): |
1fb707ba | 286 | series = product_data.get('series') or {} |
baa58739 | 287 | product = series.get('product') |
288 | if product: | |
289 | entries = [] | |
290 | for entry in sorted(product, key=lambda x: int_or_none(x.get('number', 0))): | |
291 | item_id = entry.get('product_id') | |
292 | if not item_id: | |
293 | continue | |
baa58739 | 294 | entries.append(self.url_result( |
1fb707ba | 295 | smuggle_url(f'http://www.viu.com/ott/{country_code}/{lang_code}/vod/{item_id}/', |
296 | {'force_noplaylist': True}), | |
297 | ViuOTTIE, str(item_id), entry.get('synopsis', '').strip())) | |
baa58739 | 298 | |
299 | return self.playlist_result(entries, series_id, series.get('name'), series.get('description')) | |
300 | ||
baa58739 | 301 | duration_limit = False |
302 | query = { | |
303 | 'ccs_product_id': video_data['ccs_product_id'], | |
304 | 'language_flag_id': self._LANGUAGE_FLAG.get(lang_code.lower()) or '3', | |
305 | } | |
1fb707ba | 306 | |
307 | def download_playback(): | |
baa58739 | 308 | stream_data = self._download_json( |
1fb707ba | 309 | 'https://api-gateway-global.viu.com/api/playback/distribute', |
310 | video_id=video_id, query=query, fatal=False, note='Downloading stream info', | |
311 | headers={ | |
312 | 'Authorization': f'Bearer {self._auth_codes[country_code]}', | |
313 | 'Referer': url, | |
314 | 'Origin': url | |
315 | }) | |
316 | return self._detect_error(stream_data).get('stream') | |
317 | ||
318 | if not self._auth_codes.get(country_code): | |
319 | self._auth_codes[country_code] = self._get_token(country_code, video_id) | |
baa58739 | 320 | |
1fb707ba | 321 | stream_data = None |
322 | try: | |
323 | stream_data = download_playback() | |
324 | except (ExtractorError, KeyError): | |
325 | token = self._login(country_code, video_id) | |
326 | if token is not None: | |
327 | query['identity'] = token | |
328 | else: | |
c418e6b5 | 329 | # The content is Preview or for VIP only. |
330 | # We can try to bypass the duration which is limited to 3mins only | |
1fb707ba | 331 | duration_limit, query['duration'] = True, '180' |
332 | try: | |
333 | stream_data = download_playback() | |
334 | except (ExtractorError, KeyError): | |
335 | if token is not None: | |
336 | raise | |
337 | self.raise_login_required(method='password') | |
baa58739 | 338 | if not stream_data: |
339 | raise ExtractorError('Cannot get stream info', expected=True) | |
72310315 | 340 | |
72310315 | 341 | formats = [] |
1fb707ba | 342 | for vid_format, stream_url in (stream_data.get('url') or {}).items(): |
343 | height = int(self._search_regex(r's(\d+)p', vid_format, 'height', default=None)) | |
baa58739 | 344 | |
345 | # bypass preview duration limit | |
346 | if duration_limit: | |
c418e6b5 | 347 | old_stream_url = urllib.parse.urlparse(stream_url) |
348 | query = dict(urllib.parse.parse_qsl(old_stream_url.query, keep_blank_values=True)) | |
baa58739 | 349 | query.update({ |
1fb707ba | 350 | 'duration': video_data.get('time_duration') or '9999999', |
baa58739 | 351 | 'duration_start': '0', |
352 | }) | |
c418e6b5 | 353 | stream_url = old_stream_url._replace(query=urllib.parse.urlencode(query)).geturl() |
baa58739 | 354 | |
72310315 RA |
355 | formats.append({ |
356 | 'format_id': vid_format, | |
357 | 'url': stream_url, | |
358 | 'height': height, | |
359 | 'ext': 'mp4', | |
1fb707ba | 360 | 'filesize': try_get(stream_data, lambda x: x['size'][vid_format], int) |
72310315 RA |
361 | }) |
362 | self._sort_formats(formats) | |
363 | ||
364 | subtitles = {} | |
1fb707ba | 365 | for sub in video_data.get('subtitle') or []: |
72310315 RA |
366 | sub_url = sub.get('url') |
367 | if not sub_url: | |
368 | continue | |
369 | subtitles.setdefault(sub.get('name'), []).append({ | |
370 | 'url': sub_url, | |
371 | 'ext': 'srt', | |
372 | }) | |
373 | ||
1fb707ba | 374 | title = strip_or_none(video_data.get('synopsis')) |
72310315 RA |
375 | return { |
376 | 'id': video_id, | |
377 | 'title': title, | |
378 | 'description': video_data.get('description'), | |
1fb707ba | 379 | 'series': try_get(product_data, lambda x: x['series']['name']), |
72310315 RA |
380 | 'episode': title, |
381 | 'episode_number': int_or_none(video_data.get('number')), | |
382 | 'duration': int_or_none(stream_data.get('duration')), | |
1fb707ba | 383 | 'thumbnail': url_or_none(video_data.get('cover_image_url')), |
72310315 RA |
384 | 'formats': formats, |
385 | 'subtitles': subtitles, | |
386 | } |