]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/viu.py
[cleanup] Add more ruff rules (#10149)
[yt-dlp.git] / yt_dlp / extractor / viu.py
1 import json
2 import random
3 import re
4 import urllib.parse
5 import uuid
6
7 from .common import InfoExtractor
8 from ..utils import (
9 ExtractorError,
10 int_or_none,
11 remove_end,
12 smuggle_url,
13 strip_or_none,
14 traverse_obj,
15 try_get,
16 unified_timestamp,
17 unsmuggle_url,
18 url_or_none,
19 )
20
21
22 class ViuBaseIE(InfoExtractor):
23 def _call_api(self, path, *args, headers={}, **kwargs):
24 response = self._download_json(
25 f'https://www.viu.com/api/{path}', *args, **kwargs,
26 headers={**self.geo_verification_headers(), **headers})['response']
27 if response.get('status') != 'success':
28 raise ExtractorError(f'{self.IE_NAME} said: {response["message"]}', expected=True)
29 return response
30
31
32 class ViuIE(ViuBaseIE):
33 _VALID_URL = r'(?:viu:|https?://[^/]+\.viu\.com/[a-z]{2}/media/)(?P<id>\d+)'
34 _TESTS = [{
35 'url': 'https://www.viu.com/en/media/1116705532?containerId=playlist-22168059',
36 'info_dict': {
37 'id': '1116705532',
38 'ext': 'mp4',
39 'title': 'Citizen Khan - Ep 1',
40 'description': 'md5:d7ea1604f49e5ba79c212c551ce2110e',
41 },
42 'params': {
43 'skip_download': 'm3u8 download',
44 },
45 'skip': 'Geo-restricted to India',
46 }, {
47 'url': 'https://www.viu.com/en/media/1130599965',
48 'info_dict': {
49 'id': '1130599965',
50 'ext': 'mp4',
51 'title': 'Jealousy Incarnate - Episode 1',
52 'description': 'md5:d3d82375cab969415d2720b6894361e9',
53 },
54 'params': {
55 'skip_download': 'm3u8 download',
56 },
57 'skip': 'Geo-restricted to Indonesia',
58 }, {
59 'url': 'https://india.viu.com/en/media/1126286865',
60 'only_matching': True,
61 }]
62
63 def _real_extract(self, url):
64 video_id = self._match_id(url)
65
66 video_data = self._call_api(
67 'clip/load', video_id, 'Downloading video data', query={
68 'appid': 'viu_desktop',
69 'fmt': 'json',
70 'id': video_id,
71 })['item'][0]
72
73 title = video_data['title']
74
75 m3u8_url = None
76 url_path = video_data.get('urlpathd') or video_data.get('urlpath')
77 tdirforwhole = video_data.get('tdirforwhole')
78 # #EXT-X-BYTERANGE is not supported by native hls downloader
79 # and ffmpeg (#10955)
80 # FIXME: It is supported in yt-dlp
81 # hls_file = video_data.get('hlsfile')
82 hls_file = video_data.get('jwhlsfile')
83 if url_path and tdirforwhole and hls_file:
84 m3u8_url = f'{url_path}/{tdirforwhole}/{hls_file}'
85 else:
86 # m3u8_url = re.sub(
87 # r'(/hlsc_)[a-z]+(\d+\.m3u8)',
88 # r'\1whe\2', video_data['href'])
89 m3u8_url = video_data['href']
90 formats, subtitles = self._extract_m3u8_formats_and_subtitles(m3u8_url, video_id, 'mp4')
91
92 for key, value in video_data.items():
93 mobj = re.match(r'^subtitle_(?P<lang>[^_]+)_(?P<ext>(vtt|srt))', key)
94 if not mobj:
95 continue
96 subtitles.setdefault(mobj.group('lang'), []).append({
97 'url': value,
98 'ext': mobj.group('ext'),
99 })
100
101 return {
102 'id': video_id,
103 'title': title,
104 'description': video_data.get('description'),
105 'series': video_data.get('moviealbumshowname'),
106 'episode': title,
107 'episode_number': int_or_none(video_data.get('episodeno')),
108 'duration': int_or_none(video_data.get('duration')),
109 'formats': formats,
110 'subtitles': subtitles,
111 }
112
113
114 class ViuPlaylistIE(ViuBaseIE):
115 IE_NAME = 'viu:playlist'
116 _VALID_URL = r'https?://www\.viu\.com/[^/]+/listing/playlist-(?P<id>\d+)'
117 _TEST = {
118 'url': 'https://www.viu.com/en/listing/playlist-22461380',
119 'info_dict': {
120 'id': '22461380',
121 'title': 'The Good Wife',
122 },
123 'playlist_count': 16,
124 'skip': 'Geo-restricted to Indonesia',
125 }
126
127 def _real_extract(self, url):
128 playlist_id = self._match_id(url)
129 playlist_data = self._call_api(
130 'container/load', playlist_id,
131 'Downloading playlist info', query={
132 'appid': 'viu_desktop',
133 'fmt': 'json',
134 'id': 'playlist-' + playlist_id,
135 })['container']
136
137 entries = []
138 for item in playlist_data.get('item', []):
139 item_id = item.get('id')
140 if not item_id:
141 continue
142 item_id = str(item_id)
143 entries.append(self.url_result(
144 'viu:' + item_id, 'Viu', item_id))
145
146 return self.playlist_result(
147 entries, playlist_id, playlist_data.get('title'))
148
149
150 class ViuOTTIE(InfoExtractor):
151 IE_NAME = 'viu:ott'
152 _NETRC_MACHINE = 'viu'
153 _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+)'
154 _TESTS = [{
155 'url': 'http://www.viu.com/ott/sg/en-us/vod/3421/The%20Prime%20Minister%20and%20I',
156 'info_dict': {
157 'id': '3421',
158 'ext': 'mp4',
159 'title': 'A New Beginning',
160 'description': 'md5:1e7486a619b6399b25ba6a41c0fe5b2c',
161 },
162 'params': {
163 'skip_download': 'm3u8 download',
164 'noplaylist': True,
165 },
166 'skip': 'Geo-restricted to Singapore',
167 }, {
168 'url': 'https://www.viu.com/ott/hk/zh-hk/vod/430078/%E7%AC%AC%E5%85%AD%E6%84%9F-3',
169 'info_dict': {
170 'id': '430078',
171 'ext': 'mp4',
172 'title': '大韓民國的1%',
173 'description': 'md5:74d6db47ddd9ddb9c89a05739103ccdb',
174 'episode_number': 1,
175 'duration': 6614,
176 'episode': '大韓民國的1%',
177 'series': '第六感 3',
178 'thumbnail': 'https://d2anahhhmp1ffz.cloudfront.net/1313295781/d2b14f48d008ef2f3a9200c98d8e9b63967b9cc2',
179 },
180 'params': {
181 'skip_download': 'm3u8 download',
182 'noplaylist': True,
183 },
184 'skip': 'Geo-restricted to Hong Kong',
185 }, {
186 'url': 'https://www.viu.com/ott/hk/zh-hk/vod/444666/%E6%88%91%E7%9A%84%E5%AE%A4%E5%8F%8B%E6%98%AF%E4%B9%9D%E5%B0%BE%E7%8B%90',
187 'playlist_count': 16,
188 'info_dict': {
189 'id': '23807',
190 'title': '我的室友是九尾狐',
191 'description': 'md5:b42c95f2b4a316cdd6ae14ca695f33b9',
192 },
193 'params': {
194 'skip_download': 'm3u8 download',
195 'noplaylist': False,
196 },
197 'skip': 'Geo-restricted to Hong Kong',
198 }]
199
200 _AREA_ID = {
201 'HK': 1,
202 'SG': 2,
203 'TH': 4,
204 'PH': 5,
205 }
206 _LANGUAGE_FLAG = {
207 'zh-hk': 1,
208 'zh-cn': 2,
209 'en-us': 3,
210 }
211
212 _user_token = None
213 _auth_codes = {}
214
215 def _detect_error(self, response):
216 code = try_get(response, lambda x: x['status']['code'])
217 if code and code > 0:
218 message = try_get(response, lambda x: x['status']['message'])
219 raise ExtractorError(f'{self.IE_NAME} said: {message} ({code})', expected=True)
220 return response.get('data') or {}
221
222 def _login(self, country_code, video_id):
223 if self._user_token is None:
224 username, password = self._get_login_info()
225 if username is None:
226 return
227 headers = {
228 'Authorization': f'Bearer {self._auth_codes[country_code]}',
229 'Content-Type': 'application/json',
230 }
231 data = self._download_json(
232 'https://api-gateway-global.viu.com/api/account/validate',
233 video_id, 'Validating email address', headers=headers,
234 data=json.dumps({
235 'principal': username,
236 'provider': 'email',
237 }).encode())
238 if not data.get('exists'):
239 raise ExtractorError('Invalid email address')
240
241 data = self._download_json(
242 'https://api-gateway-global.viu.com/api/auth/login',
243 video_id, 'Logging in', headers=headers,
244 data=json.dumps({
245 'email': username,
246 'password': password,
247 'provider': 'email',
248 }).encode())
249 self._detect_error(data)
250 self._user_token = data.get('identity')
251 # need to update with valid user's token else will throw an error again
252 self._auth_codes[country_code] = data.get('token')
253 return self._user_token
254
255 def _get_token(self, country_code, video_id):
256 rand = ''.join(random.choices('0123456789', k=10))
257 return self._download_json(
258 f'https://api-gateway-global.viu.com/api/auth/token?v={rand}000', video_id,
259 headers={'Content-Type': 'application/json'}, note='Getting bearer token',
260 data=json.dumps({
261 'countryCode': country_code.upper(),
262 'platform': 'browser',
263 'platformFlagLabel': 'web',
264 'language': 'en',
265 'uuid': str(uuid.uuid4()),
266 'carrierId': '0',
267 }).encode())['token']
268
269 def _real_extract(self, url):
270 url, idata = unsmuggle_url(url, {})
271 country_code, lang_code, video_id = self._match_valid_url(url).groups()
272
273 query = {
274 'r': 'vod/ajax-detail',
275 'platform_flag_label': 'web',
276 'product_id': video_id,
277 }
278
279 area_id = self._AREA_ID.get(country_code.upper())
280 if area_id:
281 query['area_id'] = area_id
282
283 product_data = self._download_json(
284 f'http://www.viu.com/ott/{country_code}/index.php', video_id,
285 'Downloading video info', query=query)['data']
286
287 video_data = product_data.get('current_product')
288 if not video_data:
289 self.raise_geo_restricted()
290
291 series_id = video_data.get('series_id')
292 if self._yes_playlist(series_id, video_id, idata):
293 series = product_data.get('series') or {}
294 product = series.get('product')
295 if product:
296 entries = []
297 for entry in sorted(product, key=lambda x: int_or_none(x.get('number', 0))):
298 item_id = entry.get('product_id')
299 if not item_id:
300 continue
301 entries.append(self.url_result(
302 smuggle_url(f'http://www.viu.com/ott/{country_code}/{lang_code}/vod/{item_id}/',
303 {'force_noplaylist': True}),
304 ViuOTTIE, str(item_id), entry.get('synopsis', '').strip()))
305
306 return self.playlist_result(entries, series_id, series.get('name'), series.get('description'))
307
308 duration_limit = False
309 query = {
310 'ccs_product_id': video_data['ccs_product_id'],
311 'language_flag_id': self._LANGUAGE_FLAG.get(lang_code.lower()) or '3',
312 }
313
314 def download_playback():
315 stream_data = self._download_json(
316 'https://api-gateway-global.viu.com/api/playback/distribute',
317 video_id=video_id, query=query, fatal=False, note='Downloading stream info',
318 headers={
319 'Authorization': f'Bearer {self._auth_codes[country_code]}',
320 'Referer': url,
321 'Origin': url,
322 })
323 return self._detect_error(stream_data).get('stream')
324
325 if not self._auth_codes.get(country_code):
326 self._auth_codes[country_code] = self._get_token(country_code, video_id)
327
328 stream_data = None
329 try:
330 stream_data = download_playback()
331 except (ExtractorError, KeyError):
332 token = self._login(country_code, video_id)
333 if token is not None:
334 query['identity'] = token
335 else:
336 # The content is Preview or for VIP only.
337 # We can try to bypass the duration which is limited to 3mins only
338 duration_limit, query['duration'] = True, '180'
339 try:
340 stream_data = download_playback()
341 except (ExtractorError, KeyError):
342 if token is not None:
343 raise
344 self.raise_login_required(method='password')
345 if not stream_data:
346 raise ExtractorError('Cannot get stream info', expected=True)
347
348 formats = []
349 for vid_format, stream_url in (stream_data.get('url') or {}).items():
350 height = int(self._search_regex(r's(\d+)p', vid_format, 'height', default=None))
351
352 # bypass preview duration limit
353 if duration_limit:
354 old_stream_url = urllib.parse.urlparse(stream_url)
355 query = dict(urllib.parse.parse_qsl(old_stream_url.query, keep_blank_values=True))
356 query.update({
357 'duration': video_data.get('time_duration') or '9999999',
358 'duration_start': '0',
359 })
360 stream_url = old_stream_url._replace(query=urllib.parse.urlencode(query)).geturl()
361
362 formats.append({
363 'format_id': vid_format,
364 'url': stream_url,
365 'height': height,
366 'ext': 'mp4',
367 'filesize': try_get(stream_data, lambda x: x['size'][vid_format], int),
368 })
369
370 subtitles = {}
371 for sub in video_data.get('subtitle') or []:
372 lang = sub.get('name') or 'und'
373 if sub.get('url'):
374 subtitles.setdefault(lang, []).append({
375 'url': sub['url'],
376 'ext': 'srt',
377 'name': f'Spoken text for {lang}',
378 })
379 if sub.get('second_subtitle_url'):
380 subtitles.setdefault(f'{lang}_ost', []).append({
381 'url': sub['second_subtitle_url'],
382 'ext': 'srt',
383 'name': f'On-screen text for {lang}',
384 })
385
386 title = strip_or_none(video_data.get('synopsis'))
387 return {
388 'id': video_id,
389 'title': title,
390 'description': video_data.get('description'),
391 'series': try_get(product_data, lambda x: x['series']['name']),
392 'episode': title,
393 'episode_number': int_or_none(video_data.get('number')),
394 'duration': int_or_none(stream_data.get('duration')),
395 'thumbnail': url_or_none(video_data.get('cover_image_url')),
396 'formats': formats,
397 'subtitles': subtitles,
398 }
399
400
401 class ViuOTTIndonesiaBaseIE(InfoExtractor):
402 _BASE_QUERY = {
403 'ver': 1.0,
404 'fmt': 'json',
405 'aver': 5.0,
406 'appver': 2.0,
407 'appid': 'viu_desktop',
408 'platform': 'desktop',
409 }
410
411 _DEVICE_ID = str(uuid.uuid4())
412 _SESSION_ID = str(uuid.uuid4())
413 _TOKEN = None
414
415 _HEADERS = {
416 'x-session-id': _SESSION_ID,
417 'x-client': 'browser',
418 }
419
420 _AGE_RATINGS_MAPPER = {
421 'ADULTS': 18,
422 'teens': 13,
423 }
424
425 def _real_initialize(self):
426 ViuOTTIndonesiaBaseIE._TOKEN = self._download_json(
427 'https://um.viuapi.io/user/identity', None,
428 headers={'Content-type': 'application/json', **self._HEADERS},
429 query={**self._BASE_QUERY, 'iid': self._DEVICE_ID},
430 data=json.dumps({'deviceId': self._DEVICE_ID}).encode(),
431 note='Downloading token information')['token']
432
433
434 class ViuOTTIndonesiaIE(ViuOTTIndonesiaBaseIE):
435 _VALID_URL = r'https?://www\.viu\.com/ott/\w+/\w+/all/video-[\w-]+-(?P<id>\d+)'
436 _TESTS = [{
437 'url': 'https://www.viu.com/ott/id/id/all/video-japanese-drama-tv_shows-detective_conan_episode_793-1165863142?containerId=playlist-26271226',
438 'info_dict': {
439 'id': '1165863142',
440 'ext': 'mp4',
441 'episode_number': 793,
442 'episode': 'Episode 793',
443 'title': 'Detective Conan - Episode 793',
444 'duration': 1476,
445 'description': 'md5:b79d55345bc1e0217ece22616267c9a5',
446 'thumbnail': 'https://vuclipi-a.akamaihd.net/p/cloudinary/h_171,w_304,dpr_1.5,f_auto,c_thumb,q_auto:low/1165863189/d-1',
447 'upload_date': '20210101',
448 'timestamp': 1609459200,
449 },
450 }, {
451 'url': 'https://www.viu.com/ott/id/id/all/video-korean-reality-tv_shows-entertainment_weekly_episode_1622-1118617054',
452 'info_dict': {
453 'id': '1118617054',
454 'ext': 'mp4',
455 'episode_number': 1622,
456 'episode': 'Episode 1622',
457 'description': 'md5:6d68ca450004020113e9bf27ad99f0f8',
458 'title': 'Entertainment Weekly - Episode 1622',
459 'duration': 4729,
460 'thumbnail': 'https://vuclipi-a.akamaihd.net/p/cloudinary/h_171,w_304,dpr_1.5,f_auto,c_thumb,q_auto:low/1120187848/d-1',
461 'timestamp': 1420070400,
462 'upload_date': '20150101',
463 'cast': ['Shin Hyun-joon', 'Lee Da-Hee'],
464 },
465 }, {
466 # age-limit test
467 'url': 'https://www.viu.com/ott/id/id/all/video-japanese-trailer-tv_shows-trailer_jujutsu_kaisen_ver_01-1166044219?containerId=playlist-26273140',
468 'info_dict': {
469 'id': '1166044219',
470 'ext': 'mp4',
471 'upload_date': '20200101',
472 'timestamp': 1577836800,
473 'title': 'Trailer \'Jujutsu Kaisen\' Ver.01',
474 'duration': 92,
475 'thumbnail': 'https://vuclipi-a.akamaihd.net/p/cloudinary/h_171,w_304,dpr_1.5,f_auto,c_thumb,q_auto:low/1166044240/d-1',
476 'description': 'Trailer \'Jujutsu Kaisen\' Ver.01',
477 'cast': ['Junya Enoki', ' Yûichi Nakamura', ' Yuma Uchida', 'Asami Seto'],
478 'age_limit': 13,
479 },
480 }, {
481 # json ld metadata type equal to Movie instead of TVEpisodes
482 'url': 'https://www.viu.com/ott/id/id/all/video-japanese-animation-movies-demon_slayer_kimetsu_no_yaiba_the_movie_mugen_train-1165892707?containerId=1675060691786',
483 'info_dict': {
484 'id': '1165892707',
485 'ext': 'mp4',
486 'timestamp': 1577836800,
487 'upload_date': '20200101',
488 'title': 'Demon Slayer - Kimetsu no Yaiba - The Movie: Mugen Train',
489 'age_limit': 13,
490 'cast': 'count:9',
491 'thumbnail': 'https://vuclipi-a.akamaihd.net/p/cloudinary/h_171,w_304,dpr_1.5,f_auto,c_thumb,q_auto:low/1165895279/d-1',
492 'description': 'md5:1ce9c35a3aeab384085533f746c87469',
493 'duration': 7021,
494 },
495 }]
496
497 def _real_extract(self, url):
498 display_id = self._match_id(url)
499 webpage = self._download_webpage(url, display_id)
500
501 video_data = self._download_json(
502 f'https://um.viuapi.io/drm/v1/content/{display_id}', display_id, data=b'',
503 headers={'Authorization': ViuOTTIndonesiaBaseIE._TOKEN, **self._HEADERS, 'ccode': 'ID'})
504 formats, subtitles = self._extract_m3u8_formats_and_subtitles(video_data['playUrl'], display_id)
505
506 initial_state = self._search_json(
507 r'window\.__INITIAL_STATE__\s*=', webpage, 'initial state',
508 display_id)['content']['clipDetails']
509 for key, url in initial_state.items():
510 lang, ext = self._search_regex(
511 r'^subtitle_(?P<lang>[\w-]+)_(?P<ext>\w+)$', key, 'subtitle metadata',
512 default=(None, None), group=('lang', 'ext'))
513 if lang and ext:
514 subtitles.setdefault(lang, []).append({
515 'ext': ext,
516 'url': url,
517 })
518
519 if ext == 'vtt':
520 subtitles[lang].append({
521 'ext': 'srt',
522 'url': f'{remove_end(initial_state[key], "vtt")}srt',
523 })
524
525 episode = traverse_obj(list(filter(
526 lambda x: x.get('@type') in ('TVEpisode', 'Movie'), self._yield_json_ld(webpage, display_id))), 0) or {}
527 return {
528 'id': display_id,
529 'title': (traverse_obj(initial_state, 'title', 'display_title')
530 or episode.get('name')),
531 'description': initial_state.get('description') or episode.get('description'),
532 'duration': initial_state.get('duration'),
533 'thumbnail': traverse_obj(episode, ('image', 'url')),
534 'timestamp': unified_timestamp(episode.get('dateCreated')),
535 'formats': formats,
536 'subtitles': subtitles,
537 'episode_number': (traverse_obj(initial_state, 'episode_no', 'episodeno', expected_type=int_or_none)
538 or int_or_none(episode.get('episodeNumber'))),
539 'cast': traverse_obj(episode, ('actor', ..., 'name'), default=None),
540 'age_limit': self._AGE_RATINGS_MAPPER.get(initial_state.get('internal_age_rating')),
541 }