7 from .common
import InfoExtractor
8 from ..compat
import compat_str
20 class ViuBaseIE(InfoExtractor
):
21 def _call_api(self
, path
, *args
, headers
={}, **kwargs
):
22 response
= self
._download
_json
(
23 f
'https://www.viu.com/api/{path}', *args
, **kwargs
,
24 headers
={**self.geo_verification_headers(), **headers}
)['response']
25 if response
.get('status') != 'success':
26 raise ExtractorError(f
'{self.IE_NAME} said: {response["message"]}', expected
=True)
30 class ViuIE(ViuBaseIE
):
31 _VALID_URL
= r
'(?:viu:|https?://[^/]+\.viu\.com/[a-z]{2}/media/)(?P<id>\d+)'
33 'url': 'https://www.viu.com/en/media/1116705532?containerId=playlist-22168059',
37 'title': 'Citizen Khan - Ep 1',
38 'description': 'md5:d7ea1604f49e5ba79c212c551ce2110e',
41 'skip_download': 'm3u8 download',
43 'skip': 'Geo-restricted to India',
45 'url': 'https://www.viu.com/en/media/1130599965',
49 'title': 'Jealousy Incarnate - Episode 1',
50 'description': 'md5:d3d82375cab969415d2720b6894361e9',
53 'skip_download': 'm3u8 download',
55 'skip': 'Geo-restricted to Indonesia',
57 'url': 'https://india.viu.com/en/media/1126286865',
58 'only_matching': True,
61 def _real_extract(self
, url
):
62 video_id
= self
._match
_id
(url
)
64 video_data
= self
._call
_api
(
65 'clip/load', video_id
, 'Downloading video data', query
={
66 'appid': 'viu_desktop',
71 title
= video_data
['title']
74 url_path
= video_data
.get('urlpathd') or video_data
.get('urlpath')
75 tdirforwhole
= video_data
.get('tdirforwhole')
76 # #EXT-X-BYTERANGE is not supported by native hls downloader
78 # FIXME: It is supported in yt-dlp
79 # hls_file = video_data.get('hlsfile')
80 hls_file
= video_data
.get('jwhlsfile')
81 if url_path
and tdirforwhole
and hls_file
:
82 m3u8_url
= '%s/%s/%s' % (url_path
, tdirforwhole
, hls_file
)
85 # r'(/hlsc_)[a-z]+(\d+\.m3u8)',
86 # r'\1whe\2', video_data['href'])
87 m3u8_url
= video_data
['href']
88 formats
, subtitles
= self
._extract
_m
3u8_formats
_and
_subtitles
(m3u8_url
, video_id
, 'mp4')
90 for key
, value
in video_data
.items():
91 mobj
= re
.match(r
'^subtitle_(?P<lang>[^_]+)_(?P<ext>(vtt|srt))', key
)
94 subtitles
.setdefault(mobj
.group('lang'), []).append({
96 'ext': mobj
.group('ext')
102 'description': video_data
.get('description'),
103 'series': video_data
.get('moviealbumshowname'),
105 'episode_number': int_or_none(video_data
.get('episodeno')),
106 'duration': int_or_none(video_data
.get('duration')),
108 'subtitles': subtitles
,
112 class ViuPlaylistIE(ViuBaseIE
):
113 IE_NAME
= 'viu:playlist'
114 _VALID_URL
= r
'https?://www\.viu\.com/[^/]+/listing/playlist-(?P<id>\d+)'
116 'url': 'https://www.viu.com/en/listing/playlist-22461380',
119 'title': 'The Good Wife',
121 'playlist_count': 16,
122 'skip': 'Geo-restricted to Indonesia',
125 def _real_extract(self
, url
):
126 playlist_id
= self
._match
_id
(url
)
127 playlist_data
= self
._call
_api
(
128 'container/load', playlist_id
,
129 'Downloading playlist info', query
={
130 'appid': 'viu_desktop',
132 'id': 'playlist-' + playlist_id
136 for item
in playlist_data
.get('item', []):
137 item_id
= item
.get('id')
140 item_id
= compat_str(item_id
)
141 entries
.append(self
.url_result(
142 'viu:' + item_id
, 'Viu', item_id
))
144 return self
.playlist_result(
145 entries
, playlist_id
, playlist_data
.get('title'))
148 class ViuOTTIE(InfoExtractor
):
150 _NETRC_MACHINE
= 'viu'
151 _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+)'
153 'url': 'http://www.viu.com/ott/sg/en-us/vod/3421/The%20Prime%20Minister%20and%20I',
157 'title': 'A New Beginning',
158 'description': 'md5:1e7486a619b6399b25ba6a41c0fe5b2c',
161 'skip_download': 'm3u8 download',
164 'skip': 'Geo-restricted to Singapore',
166 'url': 'https://www.viu.com/ott/hk/zh-hk/vod/430078/%E7%AC%AC%E5%85%AD%E6%84%9F-3',
171 'description': 'md5:74d6db47ddd9ddb9c89a05739103ccdb',
174 'episode': '大韓民國的1%',
176 'thumbnail': 'https://d2anahhhmp1ffz.cloudfront.net/1313295781/d2b14f48d008ef2f3a9200c98d8e9b63967b9cc2',
179 'skip_download': 'm3u8 download',
182 'skip': 'Geo-restricted to Hong Kong',
184 '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',
185 'playlist_count': 16,
189 'description': 'md5:b42c95f2b4a316cdd6ae14ca695f33b9',
192 'skip_download': 'm3u8 download',
195 'skip': 'Geo-restricted to Hong Kong',
213 def _detect_error(self
, response
):
214 code
= try_get(response
, lambda x
: x
['status']['code'])
215 if code
and code
> 0:
216 message
= try_get(response
, lambda x
: x
['status']['message'])
217 raise ExtractorError(f
'{self.IE_NAME} said: {message} ({code})', expected
=True)
218 return response
.get('data') or {}
220 def _login(self
, country_code
, video_id
):
221 if self
._user
_token
is None:
222 username
, password
= self
._get
_login
_info
()
226 'Authorization': f
'Bearer {self._auth_codes[country_code]}',
227 'Content-Type': 'application/json'
229 data
= self
._download
_json
(
230 'https://api-gateway-global.viu.com/api/account/validate',
231 video_id
, 'Validating email address', headers
=headers
,
233 'principal': username
,
236 if not data
.get('exists'):
237 raise ExtractorError('Invalid email address')
239 data
= self
._download
_json
(
240 'https://api-gateway-global.viu.com/api/auth/login',
241 video_id
, 'Logging in', headers
=headers
,
244 'password': password
,
247 self
._detect
_error
(data
)
248 self
._user
_token
= data
.get('identity')
249 # need to update with valid user's token else will throw an error again
250 self
._auth
_codes
[country_code
] = data
.get('token')
251 return self
._user
_token
253 def _get_token(self
, country_code
, video_id
):
254 rand
= ''.join(random
.choices('0123456789', k
=10))
255 return self
._download
_json
(
256 f
'https://api-gateway-global.viu.com/api/auth/token?v={rand}000', video_id
,
257 headers
={'Content-Type': 'application/json'}
, note
='Getting bearer token',
259 'countryCode': country_code
.upper(),
260 'platform': 'browser',
261 'platformFlagLabel': 'web',
263 'uuid': str(uuid
.uuid4()),
265 }).encode('utf-8'))['token']
267 def _real_extract(self
, url
):
268 url
, idata
= unsmuggle_url(url
, {})
269 country_code
, lang_code
, video_id
= self
._match
_valid
_url
(url
).groups()
272 'r': 'vod/ajax-detail',
273 'platform_flag_label': 'web',
274 'product_id': video_id
,
277 area_id
= self
._AREA
_ID
.get(country_code
.upper())
279 query
['area_id'] = area_id
281 product_data
= self
._download
_json
(
282 f
'http://www.viu.com/ott/{country_code}/index.php', video_id
,
283 'Downloading video info', query
=query
)['data']
285 video_data
= product_data
.get('current_product')
287 self
.raise_geo_restricted()
289 series_id
= video_data
.get('series_id')
290 if self
._yes
_playlist
(series_id
, video_id
, idata
):
291 series
= product_data
.get('series') or {}
292 product
= series
.get('product')
295 for entry
in sorted(product
, key
=lambda x
: int_or_none(x
.get('number', 0))):
296 item_id
= entry
.get('product_id')
299 entries
.append(self
.url_result(
300 smuggle_url(f
'http://www.viu.com/ott/{country_code}/{lang_code}/vod/{item_id}/',
301 {'force_noplaylist': True}
),
302 ViuOTTIE
, str(item_id
), entry
.get('synopsis', '').strip()))
304 return self
.playlist_result(entries
, series_id
, series
.get('name'), series
.get('description'))
306 duration_limit
= False
308 'ccs_product_id': video_data
['ccs_product_id'],
309 'language_flag_id': self
._LANGUAGE
_FLAG
.get(lang_code
.lower()) or '3',
312 def download_playback():
313 stream_data
= self
._download
_json
(
314 'https://api-gateway-global.viu.com/api/playback/distribute',
315 video_id
=video_id
, query
=query
, fatal
=False, note
='Downloading stream info',
317 'Authorization': f
'Bearer {self._auth_codes[country_code]}',
321 return self
._detect
_error
(stream_data
).get('stream')
323 if not self
._auth
_codes
.get(country_code
):
324 self
._auth
_codes
[country_code
] = self
._get
_token
(country_code
, video_id
)
328 stream_data
= download_playback()
329 except (ExtractorError
, KeyError):
330 token
= self
._login
(country_code
, video_id
)
331 if token
is not None:
332 query
['identity'] = token
334 # The content is Preview or for VIP only.
335 # We can try to bypass the duration which is limited to 3mins only
336 duration_limit
, query
['duration'] = True, '180'
338 stream_data
= download_playback()
339 except (ExtractorError
, KeyError):
340 if token
is not None:
342 self
.raise_login_required(method
='password')
344 raise ExtractorError('Cannot get stream info', expected
=True)
347 for vid_format
, stream_url
in (stream_data
.get('url') or {}).items():
348 height
= int(self
._search
_regex
(r
's(\d+)p', vid_format
, 'height', default
=None))
350 # bypass preview duration limit
352 old_stream_url
= urllib
.parse
.urlparse(stream_url
)
353 query
= dict(urllib
.parse
.parse_qsl(old_stream_url
.query
, keep_blank_values
=True))
355 'duration': video_data
.get('time_duration') or '9999999',
356 'duration_start': '0',
358 stream_url
= old_stream_url
._replace
(query
=urllib
.parse
.urlencode(query
)).geturl()
361 'format_id': vid_format
,
365 'filesize': try_get(stream_data
, lambda x
: x
['size'][vid_format
], int)
369 for sub
in video_data
.get('subtitle') or []:
370 lang
= sub
.get('name') or 'und'
372 subtitles
.setdefault(lang
, []).append({
375 'name': f
'Spoken text for {lang}',
377 if sub
.get('second_subtitle_url'):
378 subtitles
.setdefault(f
'{lang}_ost', []).append({
379 'url': sub
['second_subtitle_url'],
381 'name': f
'On-screen text for {lang}',
384 title
= strip_or_none(video_data
.get('synopsis'))
388 'description': video_data
.get('description'),
389 'series': try_get(product_data
, lambda x
: x
['series']['name']),
391 'episode_number': int_or_none(video_data
.get('number')),
392 'duration': int_or_none(stream_data
.get('duration')),
393 'thumbnail': url_or_none(video_data
.get('cover_image_url')),
395 'subtitles': subtitles
,