7 from .common
import InfoExtractor
8 from ..compat
import compat_str
23 class ViuBaseIE(InfoExtractor
):
24 def _call_api(self
, path
, *args
, headers
={}, **kwargs
):
25 response
= self
._download
_json
(
26 f
'https://www.viu.com/api/{path}', *args
, **kwargs
,
27 headers
={**self.geo_verification_headers(), **headers}
)['response']
28 if response
.get('status') != 'success':
29 raise ExtractorError(f
'{self.IE_NAME} said: {response["message"]}', expected
=True)
33 class ViuIE(ViuBaseIE
):
34 _VALID_URL
= r
'(?:viu:|https?://[^/]+\.viu\.com/[a-z]{2}/media/)(?P<id>\d+)'
36 'url': 'https://www.viu.com/en/media/1116705532?containerId=playlist-22168059',
40 'title': 'Citizen Khan - Ep 1',
41 'description': 'md5:d7ea1604f49e5ba79c212c551ce2110e',
44 'skip_download': 'm3u8 download',
46 'skip': 'Geo-restricted to India',
48 'url': 'https://www.viu.com/en/media/1130599965',
52 'title': 'Jealousy Incarnate - Episode 1',
53 'description': 'md5:d3d82375cab969415d2720b6894361e9',
56 'skip_download': 'm3u8 download',
58 'skip': 'Geo-restricted to Indonesia',
60 'url': 'https://india.viu.com/en/media/1126286865',
61 'only_matching': True,
64 def _real_extract(self
, url
):
65 video_id
= self
._match
_id
(url
)
67 video_data
= self
._call
_api
(
68 'clip/load', video_id
, 'Downloading video data', query
={
69 'appid': 'viu_desktop',
74 title
= video_data
['title']
77 url_path
= video_data
.get('urlpathd') or video_data
.get('urlpath')
78 tdirforwhole
= video_data
.get('tdirforwhole')
79 # #EXT-X-BYTERANGE is not supported by native hls downloader
81 # FIXME: It is supported in yt-dlp
82 # hls_file = video_data.get('hlsfile')
83 hls_file
= video_data
.get('jwhlsfile')
84 if url_path
and tdirforwhole
and hls_file
:
85 m3u8_url
= '%s/%s/%s' % (url_path
, tdirforwhole
, hls_file
)
88 # r'(/hlsc_)[a-z]+(\d+\.m3u8)',
89 # r'\1whe\2', video_data['href'])
90 m3u8_url
= video_data
['href']
91 formats
, subtitles
= self
._extract
_m
3u8_formats
_and
_subtitles
(m3u8_url
, video_id
, 'mp4')
93 for key
, value
in video_data
.items():
94 mobj
= re
.match(r
'^subtitle_(?P<lang>[^_]+)_(?P<ext>(vtt|srt))', key
)
97 subtitles
.setdefault(mobj
.group('lang'), []).append({
99 'ext': mobj
.group('ext')
105 'description': video_data
.get('description'),
106 'series': video_data
.get('moviealbumshowname'),
108 'episode_number': int_or_none(video_data
.get('episodeno')),
109 'duration': int_or_none(video_data
.get('duration')),
111 'subtitles': subtitles
,
115 class ViuPlaylistIE(ViuBaseIE
):
116 IE_NAME
= 'viu:playlist'
117 _VALID_URL
= r
'https?://www\.viu\.com/[^/]+/listing/playlist-(?P<id>\d+)'
119 'url': 'https://www.viu.com/en/listing/playlist-22461380',
122 'title': 'The Good Wife',
124 'playlist_count': 16,
125 'skip': 'Geo-restricted to Indonesia',
128 def _real_extract(self
, url
):
129 playlist_id
= self
._match
_id
(url
)
130 playlist_data
= self
._call
_api
(
131 'container/load', playlist_id
,
132 'Downloading playlist info', query
={
133 'appid': 'viu_desktop',
135 'id': 'playlist-' + playlist_id
139 for item
in playlist_data
.get('item', []):
140 item_id
= item
.get('id')
143 item_id
= compat_str(item_id
)
144 entries
.append(self
.url_result(
145 'viu:' + item_id
, 'Viu', item_id
))
147 return self
.playlist_result(
148 entries
, playlist_id
, playlist_data
.get('title'))
151 class ViuOTTIE(InfoExtractor
):
153 _NETRC_MACHINE
= 'viu'
154 _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+)'
156 'url': 'http://www.viu.com/ott/sg/en-us/vod/3421/The%20Prime%20Minister%20and%20I',
160 'title': 'A New Beginning',
161 'description': 'md5:1e7486a619b6399b25ba6a41c0fe5b2c',
164 'skip_download': 'm3u8 download',
167 'skip': 'Geo-restricted to Singapore',
169 'url': 'https://www.viu.com/ott/hk/zh-hk/vod/430078/%E7%AC%AC%E5%85%AD%E6%84%9F-3',
174 'description': 'md5:74d6db47ddd9ddb9c89a05739103ccdb',
177 'episode': '大韓民國的1%',
179 'thumbnail': 'https://d2anahhhmp1ffz.cloudfront.net/1313295781/d2b14f48d008ef2f3a9200c98d8e9b63967b9cc2',
182 'skip_download': 'm3u8 download',
185 'skip': 'Geo-restricted to Hong Kong',
187 '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',
188 'playlist_count': 16,
192 'description': 'md5:b42c95f2b4a316cdd6ae14ca695f33b9',
195 'skip_download': 'm3u8 download',
198 'skip': 'Geo-restricted to Hong Kong',
216 def _detect_error(self
, response
):
217 code
= try_get(response
, lambda x
: x
['status']['code'])
218 if code
and code
> 0:
219 message
= try_get(response
, lambda x
: x
['status']['message'])
220 raise ExtractorError(f
'{self.IE_NAME} said: {message} ({code})', expected
=True)
221 return response
.get('data') or {}
223 def _login(self
, country_code
, video_id
):
224 if self
._user
_token
is None:
225 username
, password
= self
._get
_login
_info
()
229 'Authorization': f
'Bearer {self._auth_codes[country_code]}',
230 'Content-Type': 'application/json'
232 data
= self
._download
_json
(
233 'https://api-gateway-global.viu.com/api/account/validate',
234 video_id
, 'Validating email address', headers
=headers
,
236 'principal': username
,
239 if not data
.get('exists'):
240 raise ExtractorError('Invalid email address')
242 data
= self
._download
_json
(
243 'https://api-gateway-global.viu.com/api/auth/login',
244 video_id
, 'Logging in', headers
=headers
,
247 'password': password
,
250 self
._detect
_error
(data
)
251 self
._user
_token
= data
.get('identity')
252 # need to update with valid user's token else will throw an error again
253 self
._auth
_codes
[country_code
] = data
.get('token')
254 return self
._user
_token
256 def _get_token(self
, country_code
, video_id
):
257 rand
= ''.join(random
.choices('0123456789', k
=10))
258 return self
._download
_json
(
259 f
'https://api-gateway-global.viu.com/api/auth/token?v={rand}000', video_id
,
260 headers
={'Content-Type': 'application/json'}
, note
='Getting bearer token',
262 'countryCode': country_code
.upper(),
263 'platform': 'browser',
264 'platformFlagLabel': 'web',
266 'uuid': str(uuid
.uuid4()),
268 }).encode('utf-8'))['token']
270 def _real_extract(self
, url
):
271 url
, idata
= unsmuggle_url(url
, {})
272 country_code
, lang_code
, video_id
= self
._match
_valid
_url
(url
).groups()
275 'r': 'vod/ajax-detail',
276 'platform_flag_label': 'web',
277 'product_id': video_id
,
280 area_id
= self
._AREA
_ID
.get(country_code
.upper())
282 query
['area_id'] = area_id
284 product_data
= self
._download
_json
(
285 f
'http://www.viu.com/ott/{country_code}/index.php', video_id
,
286 'Downloading video info', query
=query
)['data']
288 video_data
= product_data
.get('current_product')
290 self
.raise_geo_restricted()
292 series_id
= video_data
.get('series_id')
293 if self
._yes
_playlist
(series_id
, video_id
, idata
):
294 series
= product_data
.get('series') or {}
295 product
= series
.get('product')
298 for entry
in sorted(product
, key
=lambda x
: int_or_none(x
.get('number', 0))):
299 item_id
= entry
.get('product_id')
302 entries
.append(self
.url_result(
303 smuggle_url(f
'http://www.viu.com/ott/{country_code}/{lang_code}/vod/{item_id}/',
304 {'force_noplaylist': True}
),
305 ViuOTTIE
, str(item_id
), entry
.get('synopsis', '').strip()))
307 return self
.playlist_result(entries
, series_id
, series
.get('name'), series
.get('description'))
309 duration_limit
= False
311 'ccs_product_id': video_data
['ccs_product_id'],
312 'language_flag_id': self
._LANGUAGE
_FLAG
.get(lang_code
.lower()) or '3',
315 def download_playback():
316 stream_data
= self
._download
_json
(
317 'https://api-gateway-global.viu.com/api/playback/distribute',
318 video_id
=video_id
, query
=query
, fatal
=False, note
='Downloading stream info',
320 'Authorization': f
'Bearer {self._auth_codes[country_code]}',
324 return self
._detect
_error
(stream_data
).get('stream')
326 if not self
._auth
_codes
.get(country_code
):
327 self
._auth
_codes
[country_code
] = self
._get
_token
(country_code
, video_id
)
331 stream_data
= download_playback()
332 except (ExtractorError
, KeyError):
333 token
= self
._login
(country_code
, video_id
)
334 if token
is not None:
335 query
['identity'] = token
337 # The content is Preview or for VIP only.
338 # We can try to bypass the duration which is limited to 3mins only
339 duration_limit
, query
['duration'] = True, '180'
341 stream_data
= download_playback()
342 except (ExtractorError
, KeyError):
343 if token
is not None:
345 self
.raise_login_required(method
='password')
347 raise ExtractorError('Cannot get stream info', expected
=True)
350 for vid_format
, stream_url
in (stream_data
.get('url') or {}).items():
351 height
= int(self
._search
_regex
(r
's(\d+)p', vid_format
, 'height', default
=None))
353 # bypass preview duration limit
355 old_stream_url
= urllib
.parse
.urlparse(stream_url
)
356 query
= dict(urllib
.parse
.parse_qsl(old_stream_url
.query
, keep_blank_values
=True))
358 'duration': video_data
.get('time_duration') or '9999999',
359 'duration_start': '0',
361 stream_url
= old_stream_url
._replace
(query
=urllib
.parse
.urlencode(query
)).geturl()
364 'format_id': vid_format
,
368 'filesize': try_get(stream_data
, lambda x
: x
['size'][vid_format
], int)
372 for sub
in video_data
.get('subtitle') or []:
373 lang
= sub
.get('name') or 'und'
375 subtitles
.setdefault(lang
, []).append({
378 'name': f
'Spoken text for {lang}',
380 if sub
.get('second_subtitle_url'):
381 subtitles
.setdefault(f
'{lang}_ost', []).append({
382 'url': sub
['second_subtitle_url'],
384 'name': f
'On-screen text for {lang}',
387 title
= strip_or_none(video_data
.get('synopsis'))
391 'description': video_data
.get('description'),
392 'series': try_get(product_data
, lambda x
: x
['series']['name']),
394 'episode_number': int_or_none(video_data
.get('number')),
395 'duration': int_or_none(stream_data
.get('duration')),
396 'thumbnail': url_or_none(video_data
.get('cover_image_url')),
398 'subtitles': subtitles
,
402 class ViuOTTIndonesiaBaseIE(InfoExtractor
):
408 'appid': 'viu_desktop',
409 'platform': 'desktop',
412 _DEVICE_ID
= str(uuid
.uuid4())
413 _SESSION_ID
= str(uuid
.uuid4())
417 'x-session-id': _SESSION_ID
,
418 'x-client': 'browser'
421 _AGE_RATINGS_MAPPER
= {
426 def _real_initialize(self
):
427 ViuOTTIndonesiaBaseIE
._TOKEN
= self
._download
_json
(
428 'https://um.viuapi.io/user/identity', None,
429 headers
={'Content-type': 'application/json', **self._HEADERS}
,
430 query
={**self._BASE_QUERY, 'iid': self._DEVICE_ID}
,
431 data
=json
.dumps({'deviceId': self._DEVICE_ID}
).encode(),
432 note
='Downloading token information')['token']
435 class ViuOTTIndonesiaIE(ViuOTTIndonesiaBaseIE
):
436 _VALID_URL
= r
'https?://www\.viu\.com/ott/\w+/\w+/all/video-[\w-]+-(?P<id>\d+)'
438 'url': 'https://www.viu.com/ott/id/id/all/video-japanese-drama-tv_shows-detective_conan_episode_793-1165863142?containerId=playlist-26271226',
442 'episode_number': 793,
443 'episode': 'Episode 793',
444 'title': 'Detective Conan - Episode 793',
446 'description': 'md5:b79d55345bc1e0217ece22616267c9a5',
447 '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',
448 'upload_date': '20210101',
449 'timestamp': 1609459200,
452 'url': 'https://www.viu.com/ott/id/id/all/video-korean-reality-tv_shows-entertainment_weekly_episode_1622-1118617054',
456 'episode_number': 1622,
457 'episode': 'Episode 1622',
458 'description': 'md5:6d68ca450004020113e9bf27ad99f0f8',
459 'title': 'Entertainment Weekly - Episode 1622',
461 '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',
462 'timestamp': 1420070400,
463 'upload_date': '20150101',
464 'cast': ['Shin Hyun-joon', 'Lee Da-Hee']
468 'url': 'https://www.viu.com/ott/id/id/all/video-japanese-trailer-tv_shows-trailer_jujutsu_kaisen_ver_01-1166044219?containerId=playlist-26273140',
472 'upload_date': '20200101',
473 'timestamp': 1577836800,
474 'title': 'Trailer \'Jujutsu Kaisen\' Ver.01',
476 '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',
477 'description': 'Trailer \'Jujutsu Kaisen\' Ver.01',
478 'cast': ['Junya Enoki', ' Yûichi Nakamura', ' Yuma Uchida', 'Asami Seto'],
482 # json ld metadata type equal to Movie instead of TVEpisodes
483 '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',
487 'timestamp': 1577836800,
488 'upload_date': '20200101',
489 'title': 'Demon Slayer - Kimetsu no Yaiba - The Movie: Mugen Train',
492 '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',
493 'description': 'md5:1ce9c35a3aeab384085533f746c87469',
498 def _real_extract(self
, url
):
499 display_id
= self
._match
_id
(url
)
500 webpage
= self
._download
_webpage
(url
, display_id
)
502 video_data
= self
._download
_json
(
503 f
'https://um.viuapi.io/drm/v1/content/{display_id}', display_id
, data
=b
'',
504 headers
={'Authorization': ViuOTTIndonesiaBaseIE._TOKEN, **self._HEADERS, 'ccode': 'ID'}
)
505 formats
, subtitles
= self
._extract
_m
3u8_formats
_and
_subtitles
(video_data
['playUrl'], display_id
)
507 initial_state
= self
._search
_json
(
508 r
'window\.__INITIAL_STATE__\s*=', webpage
, 'initial state',
509 display_id
)['content']['clipDetails']
510 for key
, url
in initial_state
.items():
511 lang
, ext
= self
._search
_regex
(
512 r
'^subtitle_(?P<lang>[\w-]+)_(?P<ext>\w+)$', key
, 'subtitle metadata',
513 default
=(None, None), group
=('lang', 'ext'))
515 subtitles
.setdefault(lang
, []).append({
521 subtitles
[lang
].append({
523 'url': f
'{remove_end(initial_state[key], "vtt")}srt',
526 episode
= traverse_obj(list(filter(
527 lambda x
: x
.get('@type') in ('TVEpisode', 'Movie'), self
._yield
_json
_ld
(webpage
, display_id
))), 0) or {}
530 'title': (traverse_obj(initial_state
, 'title', 'display_title')
531 or episode
.get('name')),
532 'description': initial_state
.get('description') or episode
.get('description'),
533 'duration': initial_state
.get('duration'),
534 'thumbnail': traverse_obj(episode
, ('image', 'url')),
535 'timestamp': unified_timestamp(episode
.get('dateCreated')),
537 'subtitles': subtitles
,
538 'episode_number': (traverse_obj(initial_state
, 'episode_no', 'episodeno', expected_type
=int_or_none
)
539 or int_or_none(episode
.get('episodeNumber'))),
540 'cast': traverse_obj(episode
, ('actor', ..., 'name'), default
=None),
541 'age_limit': self
._AGE
_RATINGS
_MAPPER
.get(initial_state
.get('internal_age_rating'))