]> jfr.im git - yt-dlp.git/blobdiff - yt_dlp/extractor/viu.py
[extractor] Deprecate `_sort_formats`
[yt-dlp.git] / yt_dlp / extractor / viu.py
index 3bd37525b6ffcc53d8cb35bed9ab8974847893cc..19d48234e444a1df4ac8e207ddf8f5664ecaf18e 100644 (file)
@@ -1,49 +1,29 @@
-# coding: utf-8
-from __future__ import unicode_literals
-
 import re
+import json
+import uuid
+import random
+import urllib.parse
 
 from .common import InfoExtractor
-from ..compat import (
-    compat_kwargs,
-    compat_str,
-)
+from ..compat import compat_str
 from ..utils import (
     ExtractorError,
     int_or_none,
+    strip_or_none,
+    try_get,
+    smuggle_url,
+    unsmuggle_url,
+    url_or_none,
 )
 
 
 class ViuBaseIE(InfoExtractor):
-    def _real_initialize(self):
-        viu_auth_res = self._request_webpage(
-            'https://www.viu.com/api/apps/v2/authenticate', None,
-            'Requesting Viu auth', query={
-                'acct': 'test',
-                'appid': 'viu_desktop',
-                'fmt': 'json',
-                'iid': 'guest',
-                'languageid': 'default',
-                'platform': 'desktop',
-                'userid': 'guest',
-                'useridtype': 'guest',
-                'ver': '1.0'
-            }, headers=self.geo_verification_headers())
-        self._auth_token = viu_auth_res.info()['X-VIU-AUTH']
-
-    def _call_api(self, path, *args, **kwargs):
-        headers = self.geo_verification_headers()
-        headers.update({
-            'X-VIU-AUTH': self._auth_token
-        })
-        headers.update(kwargs.get('headers', {}))
-        kwargs['headers'] = headers
+    def _call_api(self, path, *args, headers={}, **kwargs):
         response = self._download_json(
-            'https://www.viu.com/api/' + path, *args,
-            **compat_kwargs(kwargs))['response']
+            f'https://www.viu.com/api/{path}', *args, **kwargs,
+            headers={**self.geo_verification_headers(), **headers})['response']
         if response.get('status') != 'success':
-            raise ExtractorError('%s said: %s' % (
-                self.IE_NAME, response['message']), expected=True)
+            raise ExtractorError(f'{self.IE_NAME} said: {response["message"]}', expected=True)
         return response
 
 
@@ -95,6 +75,7 @@ def _real_extract(self, url):
         tdirforwhole = video_data.get('tdirforwhole')
         # #EXT-X-BYTERANGE is not supported by native hls downloader
         # and ffmpeg (#10955)
+        # FIXME: It is supported in yt-dlp
         # hls_file = video_data.get('hlsfile')
         hls_file = video_data.get('jwhlsfile')
         if url_path and tdirforwhole and hls_file:
@@ -104,10 +85,8 @@ def _real_extract(self, url):
             #     r'(/hlsc_)[a-z]+(\d+\.m3u8)',
             #     r'\1whe\2', video_data['href'])
             m3u8_url = video_data['href']
-        formats = self._extract_m3u8_formats(m3u8_url, video_id, 'mp4')
-        self._sort_formats(formats)
+        formats, subtitles = self._extract_m3u8_formats_and_subtitles(m3u8_url, video_id, 'mp4')
 
-        subtitles = {}
         for key, value in video_data.items():
             mobj = re.match(r'^subtitle_(?P<lang>[^_]+)_(?P<ext>(vtt|srt))', key)
             if not mobj:
@@ -168,7 +147,8 @@ def _real_extract(self, url):
 
 class ViuOTTIE(InfoExtractor):
     IE_NAME = 'viu:ott'
-    _VALID_URL = r'https?://(?:www\.)?viu\.com/ott/(?P<country_code>[a-z]{2})/[a-z]{2}-[a-z]{2}/vod/(?P<id>\d+)'
+    _NETRC_MACHINE = 'viu'
+    _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+)'
     _TESTS = [{
         'url': 'http://www.viu.com/ott/sg/en-us/vod/3421/The%20Prime%20Minister%20and%20I',
         'info_dict': {
@@ -179,18 +159,38 @@ class ViuOTTIE(InfoExtractor):
         },
         'params': {
             'skip_download': 'm3u8 download',
+            'noplaylist': True,
         },
         'skip': 'Geo-restricted to Singapore',
     }, {
-        'url': 'http://www.viu.com/ott/hk/zh-hk/vod/7123/%E5%A4%A7%E4%BA%BA%E5%A5%B3%E5%AD%90',
+        'url': 'https://www.viu.com/ott/hk/zh-hk/vod/430078/%E7%AC%AC%E5%85%AD%E6%84%9F-3',
         'info_dict': {
-            'id': '7123',
+            'id': '430078',
             'ext': 'mp4',
-            'title': '這就是我的生活之道',
-            'description': 'md5:4eb0d8b08cf04fcdc6bbbeb16043434f',
+            'title': '大韓民國的1%',
+            'description': 'md5:74d6db47ddd9ddb9c89a05739103ccdb',
+            'episode_number': 1,
+            'duration': 6614,
+            'episode': '大韓民國的1%',
+            'series': '第六感 3',
+            'thumbnail': 'https://d2anahhhmp1ffz.cloudfront.net/1313295781/d2b14f48d008ef2f3a9200c98d8e9b63967b9cc2',
         },
         'params': {
             'skip_download': 'm3u8 download',
+            'noplaylist': True,
+        },
+        'skip': 'Geo-restricted to Hong Kong',
+    }, {
+        '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',
+        'playlist_count': 16,
+        'info_dict': {
+            'id': '23807',
+            'title': '我的室友是九尾狐',
+            'description': 'md5:b42c95f2b4a316cdd6ae14ca695f33b9',
+        },
+        'params': {
+            'skip_download': 'm3u8 download',
+            'noplaylist': False,
         },
         'skip': 'Geo-restricted to Hong Kong',
     }]
@@ -201,9 +201,72 @@ class ViuOTTIE(InfoExtractor):
         'TH': 4,
         'PH': 5,
     }
+    _LANGUAGE_FLAG = {
+        'zh-hk': 1,
+        'zh-cn': 2,
+        'en-us': 3,
+    }
+
+    _user_token = None
+    _auth_codes = {}
+
+    def _detect_error(self, response):
+        code = try_get(response, lambda x: x['status']['code'])
+        if code and code > 0:
+            message = try_get(response, lambda x: x['status']['message'])
+            raise ExtractorError(f'{self.IE_NAME} said: {message} ({code})', expected=True)
+        return response.get('data') or {}
+
+    def _login(self, country_code, video_id):
+        if self._user_token is None:
+            username, password = self._get_login_info()
+            if username is None:
+                return
+            headers = {
+                'Authorization': f'Bearer {self._auth_codes[country_code]}',
+                'Content-Type': 'application/json'
+            }
+            data = self._download_json(
+                'https://api-gateway-global.viu.com/api/account/validate',
+                video_id, 'Validating email address', headers=headers,
+                data=json.dumps({
+                    'principal': username,
+                    'provider': 'email'
+                }).encode())
+            if not data.get('exists'):
+                raise ExtractorError('Invalid email address')
+
+            data = self._download_json(
+                'https://api-gateway-global.viu.com/api/auth/login',
+                video_id, 'Logging in', headers=headers,
+                data=json.dumps({
+                    'email': username,
+                    'password': password,
+                    'provider': 'email',
+                }).encode())
+            self._detect_error(data)
+            self._user_token = data.get('identity')
+            # need to update with valid user's token else will throw an error again
+            self._auth_codes[country_code] = data.get('token')
+        return self._user_token
+
+    def _get_token(self, country_code, video_id):
+        rand = ''.join(random.choice('0123456789') for _ in range(10))
+        return self._download_json(
+            f'https://api-gateway-global.viu.com/api/auth/token?v={rand}000', video_id,
+            headers={'Content-Type': 'application/json'}, note='Getting bearer token',
+            data=json.dumps({
+                'countryCode': country_code.upper(),
+                'platform': 'browser',
+                'platformFlagLabel': 'web',
+                'language': 'en',
+                'uuid': str(uuid.uuid4()),
+                'carrierId': '0'
+            }).encode('utf-8'))['token']
 
     def _real_extract(self, url):
-        country_code, video_id = re.match(self._VALID_URL, url).groups()
+        url, idata = unsmuggle_url(url, {})
+        country_code, lang_code, video_id = self._match_valid_url(url).groups()
 
         query = {
             'r': 'vod/ajax-detail',
@@ -216,57 +279,118 @@ def _real_extract(self, url):
             query['area_id'] = area_id
 
         product_data = self._download_json(
-            'http://www.viu.com/ott/%s/index.php' % country_code, video_id,
+            f'http://www.viu.com/ott/{country_code}/index.php', video_id,
             'Downloading video info', query=query)['data']
 
         video_data = product_data.get('current_product')
         if not video_data:
-            raise ExtractorError('This video is not available in your region.', expected=True)
-
-        stream_data = self._download_json(
-            'https://d1k2us671qcoau.cloudfront.net/distribute_web_%s.php' % country_code,
-            video_id, 'Downloading stream info', query={
-                'ccs_product_id': video_data['ccs_product_id'],
-            }, headers={
-                'Referer': url,
-                'Origin': re.search(r'https?://[^/]+', url).group(0),
-            })['data']['stream']
-
-        stream_sizes = stream_data.get('size', {})
+            self.raise_geo_restricted()
+
+        series_id = video_data.get('series_id')
+        if self._yes_playlist(series_id, video_id, idata):
+            series = product_data.get('series') or {}
+            product = series.get('product')
+            if product:
+                entries = []
+                for entry in sorted(product, key=lambda x: int_or_none(x.get('number', 0))):
+                    item_id = entry.get('product_id')
+                    if not item_id:
+                        continue
+                    entries.append(self.url_result(
+                        smuggle_url(f'http://www.viu.com/ott/{country_code}/{lang_code}/vod/{item_id}/',
+                                    {'force_noplaylist': True}),
+                        ViuOTTIE, str(item_id), entry.get('synopsis', '').strip()))
+
+                return self.playlist_result(entries, series_id, series.get('name'), series.get('description'))
+
+        duration_limit = False
+        query = {
+            'ccs_product_id': video_data['ccs_product_id'],
+            'language_flag_id': self._LANGUAGE_FLAG.get(lang_code.lower()) or '3',
+        }
+
+        def download_playback():
+            stream_data = self._download_json(
+                'https://api-gateway-global.viu.com/api/playback/distribute',
+                video_id=video_id, query=query, fatal=False, note='Downloading stream info',
+                headers={
+                    'Authorization': f'Bearer {self._auth_codes[country_code]}',
+                    'Referer': url,
+                    'Origin': url
+                })
+            return self._detect_error(stream_data).get('stream')
+
+        if not self._auth_codes.get(country_code):
+            self._auth_codes[country_code] = self._get_token(country_code, video_id)
+
+        stream_data = None
+        try:
+            stream_data = download_playback()
+        except (ExtractorError, KeyError):
+            token = self._login(country_code, video_id)
+            if token is not None:
+                query['identity'] = token
+            else:
+                # The content is Preview or for VIP only.
+                # We can try to bypass the duration which is limited to 3mins only
+                duration_limit, query['duration'] = True, '180'
+            try:
+                stream_data = download_playback()
+            except (ExtractorError, KeyError):
+                if token is not None:
+                    raise
+                self.raise_login_required(method='password')
+        if not stream_data:
+            raise ExtractorError('Cannot get stream info', expected=True)
+
         formats = []
-        for vid_format, stream_url in stream_data.get('url', {}).items():
-            height = int_or_none(self._search_regex(
-                r's(\d+)p', vid_format, 'height', default=None))
+        for vid_format, stream_url in (stream_data.get('url') or {}).items():
+            height = int(self._search_regex(r's(\d+)p', vid_format, 'height', default=None))
+
+            # bypass preview duration limit
+            if duration_limit:
+                old_stream_url = urllib.parse.urlparse(stream_url)
+                query = dict(urllib.parse.parse_qsl(old_stream_url.query, keep_blank_values=True))
+                query.update({
+                    'duration': video_data.get('time_duration') or '9999999',
+                    'duration_start': '0',
+                })
+                stream_url = old_stream_url._replace(query=urllib.parse.urlencode(query)).geturl()
+
             formats.append({
                 'format_id': vid_format,
                 'url': stream_url,
                 'height': height,
                 'ext': 'mp4',
-                'filesize': int_or_none(stream_sizes.get(vid_format))
+                'filesize': try_get(stream_data, lambda x: x['size'][vid_format], int)
             })
-        self._sort_formats(formats)
 
         subtitles = {}
-        for sub in video_data.get('subtitle', []):
-            sub_url = sub.get('url')
-            if not sub_url:
-                continue
-            subtitles.setdefault(sub.get('name'), []).append({
-                'url': sub_url,
-                'ext': 'srt',
-            })
-
-        title = video_data['synopsis'].strip()
+        for sub in video_data.get('subtitle') or []:
+            lang = sub.get('name') or 'und'
+            if sub.get('url'):
+                subtitles.setdefault(lang, []).append({
+                    'url': sub['url'],
+                    'ext': 'srt',
+                    'name': f'Spoken text for {lang}',
+                })
+            if sub.get('second_subtitle_url'):
+                subtitles.setdefault(f'{lang}_ost', []).append({
+                    'url': sub['second_subtitle_url'],
+                    'ext': 'srt',
+                    'name': f'On-screen text for {lang}',
+                })
 
+        title = strip_or_none(video_data.get('synopsis'))
         return {
             'id': video_id,
             'title': title,
             'description': video_data.get('description'),
-            'series': product_data.get('series', {}).get('name'),
+            'series': try_get(product_data, lambda x: x['series']['name']),
             'episode': title,
             'episode_number': int_or_none(video_data.get('number')),
             'duration': int_or_none(stream_data.get('duration')),
-            'thumbnail': video_data.get('cover_image_url'),
+            'thumbnail': url_or_none(video_data.get('cover_image_url')),
             'formats': formats,
             'subtitles': subtitles,
         }