]> git.bitcoin.ninja Git - youtube-dl/commitdiff
[viu] improve extraction(closes #10607)(closes #11329)
authorRemita Amine <remitamine@gmail.com>
Sun, 18 Dec 2016 16:15:53 +0000 (17:15 +0100)
committerRemita Amine <remitamine@gmail.com>
Sun, 18 Dec 2016 16:20:53 +0000 (17:20 +0100)
youtube_dl/extractor/extractors.py
youtube_dl/extractor/viu.py

index cae429f67c29bfeafdcfdcb84481fb4b0cb35d52..d75ea0c92967eb6aaff77303e8488c9e3267f280 100644 (file)
@@ -1108,6 +1108,7 @@ from .viki import (
 from .viu import (
     ViuIE,
     ViuPlaylistIE,
+    ViuOTTIE,
 )
 from .vk import (
     VKIE,
index 2cab3495fbfb1f8abfec1616153a0603e4ead298..1a81b484541a00648cec80b7106886c20b615dd1 100644 (file)
@@ -4,60 +4,53 @@ from __future__ import unicode_literals
 import re
 
 from .common import InfoExtractor
+from ..compat import compat_str
 from ..utils import (
     ExtractorError,
     int_or_none,
-    clean_html,
 )
 
 
 class ViuBaseIE(InfoExtractor):
-
-    def _get_viu_auth(self, video_id):
+    def _real_initialize(self):
         viu_auth_res = self._request_webpage(
-            'https://www.viu.com/api/apps/v2/authenticate', video_id,
-            note='Requesting Viu auth',
-            query={
-                'acct': 'test', 'appid': 'viu_desktop', 'fmt': 'json',
-                'iid': 'guest', 'languageid': 'default', 'platform': 'desktop',
-                'userid': 'guest', 'useridtype': 'guest', 'ver': '1.0'
+            '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'
             })
-        return viu_auth_res.info().get('X-VIU-AUTH')
+        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
+        response = self._download_json(
+            'https://www.viu.com/api/' + path, *args, **kwargs)['response']
+        if response.get('status') != 'success':
+            raise ExtractorError('%s said: %s' % (
+                self.IE_NAME, response['message']), expected=True)
+        return response
 
 
 class ViuIE(ViuBaseIE):
-    IE_NAME = 'viu:show'
-    _VALID_URL = r'https?://www\.viu\.com/.+/(?:vod|media)/(?P<id>[0-9]+)'
+    _VALID_URL = r'(?:viu:|https?://www\.viu\.com/[a-z]{2}/media/)(?P<id>\d+)'
     _TESTS = [{
-        'url': 'http://www.viu.com/ott/sg/en-us/vod/3421/The%20Prime%20Minister%20and%20I',
-        'info_dict': {
-            'id': '3421',
-            'ext': 'mp4',
-            'title': 'The Prime Minister and I - Episode 17',
-            'description': 'md5:1e7486a619b6399b25ba6a41c0fe5b2c',
-        },
-        'params': {
-            'skip_download': 'm3u8 download',
-        },
-        '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',
-        'info_dict': {
-            'id': '7123',
-            'ext': 'mp4',
-            'title': '大人女子 - Episode 10',
-            'description': 'md5:4eb0d8b08cf04fcdc6bbbeb16043434f',
-        },
-        'params': {
-            'skip_download': 'm3u8 download',
-        },
-        'skip': 'Geo-restricted to Hong Kong',
-    }, {
         'url': 'https://www.viu.com/en/media/1116705532?containerId=playlist-22168059',
         'info_dict': {
             'id': '1116705532',
             'ext': 'mp4',
-            'title': 'Citizen Khan - Episode 1',
+            'title': 'Citizen Khan - Ep 1',
             'description': 'md5:d7ea1604f49e5ba79c212c551ce2110e',
         },
         'params': {
@@ -81,142 +74,46 @@ class ViuIE(ViuBaseIE):
     def _real_extract(self, url):
         video_id = self._match_id(url)
 
-        webpage = self._download_webpage(
-            url, video_id, note='Downloading video page')
-
-        mobj = re.search(
-            r'<div class=["\']error-title[^<>]+?>(?P<err>.+?)</div>', webpage, flags=re.DOTALL)
-
-        if mobj:
-            raise ExtractorError(clean_html(mobj.group('err')), expected=True)
-
-        config_js_url = self._search_regex(
-            r'src=(["\'])(?P<api_url>.+?/js/config\.js)(?:\?.+?)?\1', webpage, 'config_js',
-            group='api_url', default=None)
-
-        if not config_js_url:
-            # content is from ID, IN, MY
-            video_info = self._download_json(
-                'https://www.viu.com/api/clip/load', video_id,
-                headers={'X-VIU-AUTH': self._get_viu_auth(video_id)},
-                query={'appid': 'viu_desktop', 'fmt': 'json', 'id': video_id},
-                note='Downloading video info').get('response', {}).get('item', [{}])[0]
-
-            formats = self._extract_m3u8_formats(
-                video_info['href'], video_id, 'mp4',
-                m3u8_id='hls', fatal=False)
-            self._sort_formats(formats)
-
-            subtitles = {}
-            for key, value in list(video_info.items()):
-                mobj = re.match(r'^subtitle_(?P<lang>[^_]+?)_(?P<ext>(vtt|srt))', key)
-                if not mobj:
-                    continue
-                if not subtitles.get(mobj.group('lang')):
-                    subtitles[mobj.group('lang')] = []
-                subtitles[mobj.group('lang')].append(
-                    {'url': value, 'ext': mobj.group('ext')})
-
-            title = '%s - Episode %s' % (video_info['moviealbumshowname'],
-                                         video_info.get('episodeno'))
-            description = video_info.get('description')
-            duration = int_or_none(video_info.get('duration'))
-            series = video_info.get('moviealbumshowname')
-            episode_title = video_info.get('title')
-            episode_num = int_or_none(video_info.get('episodeno'))
-
-            return {
-                'id': video_id,
-                'title': title,
-                'description': description,
-                'series': series,
-                'episode': episode_title,
-                'episode_number': episode_num,
-                'duration': duration,
-                'formats': formats,
-                'subtitles': subtitles,
-            }
-
-        # content from HK, SG
-        config_js = self._download_webpage(
-            'http://www.viu.com' + config_js_url, video_id, note='Downloading config js')
-        
-        # try to strip away commented code which contains test urls
-        config_js = re.sub(r'^//.*?$', '', config_js, flags=re.MULTILINE)
-        config_js = re.sub(r'/\*.*?\*/', '', config_js, flags=re.DOTALL)
-        
-        # Slightly different api_url between HK and SG config.js
-        # http://www.viu.com/ott/hk/v1/js/config.js =>  '//www.viu.com/ott/hk/index.php?r='
-        # http://www.viu.com/ott/sg/v1/js/config.js => 'http://www.viu.com/ott/sg/index.php?r='
-        api_url = self._proto_relative_url(
-            self._search_regex(
-                r'var\s+api_url\s*=\s*(["\'])(?P<api_url>(?:https?:)?//.+?\?r=)\1',
-                config_js, 'api_url', group='api_url'), scheme='http:')
-
-        stream_info_url = self._proto_relative_url(
-            self._search_regex(
-                r'var\s+video_url\s*=\s*(["\'])(?P<video_url>(?:https?:)?//.+?\?ccs_product_id=)\1',
-                config_js, 'video_url', group='video_url'), scheme='http:')
-
-        if url.startswith('https://'):
-            api_url = re.sub('^http://', 'https://', api_url)
-
-        video_info = self._download_json(
-            api_url + 'vod/ajax-detail&platform_flag_label=web&product_id=' + video_id,
-            video_id, note='Downloading video info').get('data', {})
-
-        ccs_product_id = video_info.get('current_product', {}).get('ccs_product_id')
-
-        if not ccs_product_id:
-            raise ExtractorError('This video is not available in your region.', expected=True)
-
-        stream_info = self._download_json(
-            stream_info_url + ccs_product_id, video_id,
-            note='Downloading stream info').get('data', {}).get('stream', {})
-
-        formats = []
-        for vid_format, stream_url in stream_info.get('url', {}).items():
-            br = int_or_none(self._search_regex(
-                r's(?P<br>[0-9]+)p', vid_format, 'bitrate', group='br'))
-            formats.append({
-                'format_id': vid_format,
-                'url': stream_url,
-                'vbr': br,
-                'ext': 'mp4',
-                'filesize': stream_info.get('size', {}).get(vid_format)
-            })
+        video_data = self._call_api(
+            'clip/load', video_id, 'Downloading video data', query={
+                'appid': 'viu_desktop',
+                'fmt': 'json',
+                'id': video_id
+            })['item'][0]
+
+        title = video_data['title']
+
+        m3u8_url = None
+        url_path = video_data.get('urlpathd') or video_data.get('urlpath')
+        tdirforwhole = video_data.get('tdirforwhole')
+        hls_file = video_data.get('hlsfile')
+        if url_path and tdirforwhole and hls_file:
+            m3u8_url = '%s/%s/%s' % (url_path, tdirforwhole, hls_file)
+        else:
+            m3u8_url = re.sub(
+                r'(/hlsc_)[a-z]+(\d+\.m3u8)',
+                r'\1whe\2', video_data['href'])
+        formats = self._extract_m3u8_formats(m3u8_url, video_id, 'mp4')
         self._sort_formats(formats)
 
         subtitles = {}
-        if video_info.get('current_product', {}).get('subtitle', []):
-            for sub in video_info.get('current_product', {}).get('subtitle', []):
-                subtitles[sub.get('name')] = [{
-                    'url': sub.get('url'),
-                    'ext': 'srt',
-                }]
-
-        episode_info = next(
-            p for p in video_info.get('series', {}).get('product', [])
-            if p.get('product_id') == video_id)
-
-        title = '%s - Episode %s' % (video_info.get('series', {}).get('name'),
-                                     episode_info.get('number'))
-        description = episode_info.get('description')
-        thumbnail = episode_info.get('cover_image_url')
-        duration = int_or_none(stream_info.get('duration'))
-        series = video_info.get('series', {}).get('name')
-        episode_title = episode_info.get('synopsis')
-        episode_num = int_or_none(episode_info.get('number'))
+        for key, value in video_data.items():
+            mobj = re.match(r'^subtitle_(?P<lang>[^_]+)_(?P<ext>(vtt|srt))', key)
+            if not mobj:
+                continue
+            subtitles.setdefault(mobj.group('lang'), []).append({
+                'url': value,
+                'ext': mobj.group('ext')
+            })
 
         return {
             'id': video_id,
             'title': title,
-            'description': description,
-            'series': series,
-            'episode': episode_title,
-            'episode_number': episode_num,
-            'duration': duration,
-            'thumbnail': thumbnail,
+            'description': video_data.get('description'),
+            'series': video_data.get('moviealbumshowname'),
+            'episode': title,
+            'episode_number': int_or_none(video_data.get('episodeno')),
+            'duration': int_or_none(video_data.get('duration')),
             'formats': formats,
             'subtitles': subtitles,
         }
@@ -224,11 +121,11 @@ class ViuIE(ViuBaseIE):
 
 class ViuPlaylistIE(ViuBaseIE):
     IE_NAME = 'viu:playlist'
-    _VALID_URL = r'https?://www\.viu\.com/.+/listing/(?P<id>playlist\-[0-9]+)'
+    _VALID_URL = r'https?://www\.viu\.com/[^/]+/listing/playlist-(?P<id>\d+)'
     _TEST = {
         'url': 'https://www.viu.com/en/listing/playlist-22461380',
         'info_dict': {
-            'id': 'playlist-22461380',
+            'id': '22461380',
             'title': 'The Good Wife',
         },
         'playlist_count': 16,
@@ -237,17 +134,112 @@ class ViuPlaylistIE(ViuBaseIE):
 
     def _real_extract(self, url):
         playlist_id = self._match_id(url)
-        playlist_info = self._download_json(
-            'https://www.viu.com/api/container/load', playlist_id,
-            headers={'X-VIU-AUTH': self._get_viu_auth(playlist_id)},
-            query={'appid': 'viu_desktop', 'fmt': 'json', 'id': playlist_id},
-            note='Downloading playlist info').get('response', {}).get('container')
-
-        name = playlist_info['title']
-        entries = [
-            self.url_result(
-                'https://www.viu.com/en/media/%s' % item['id'],
-                'Viu', item['id'])
-            for item in playlist_info['item'] if item['id']]
-
-        return self.playlist_result(entries, playlist_id, name)
+        playlist_data = self._call_api(
+            'container/load', playlist_id,
+            'Downloading playlist info', query={
+                'appid': 'viu_desktop',
+                'fmt': 'json',
+                'id': 'playlist-' + playlist_id
+            })['container']
+
+        entries = []
+        for item in playlist_data.get('item', []):
+            item_id = item.get('id')
+            if not item_id:
+                continue
+            item_id = compat_str(item_id)
+            entries.append(self.url_result(
+                'viu:' + item_id, 'Viu', item_id))
+
+        return self.playlist_result(
+            entries, playlist_id, playlist_data.get('title'))
+
+
+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+)'
+    _TESTS = [{
+        'url': 'http://www.viu.com/ott/sg/en-us/vod/3421/The%20Prime%20Minister%20and%20I',
+        'info_dict': {
+            'id': '3421',
+            'ext': 'mp4',
+            'title': 'A New Beginning',
+            'description': 'md5:1e7486a619b6399b25ba6a41c0fe5b2c',
+        },
+        'params': {
+            'skip_download': 'm3u8 download',
+        },
+        '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',
+        'info_dict': {
+            'id': '7123',
+            'ext': 'mp4',
+            'title': '這就是我的生活之道',
+            'description': 'md5:4eb0d8b08cf04fcdc6bbbeb16043434f',
+        },
+        'params': {
+            'skip_download': 'm3u8 download',
+        },
+        'skip': 'Geo-restricted to Hong Kong',
+    }]
+
+    def _real_extract(self, url):
+        country_code, video_id = re.match(self._VALID_URL, url).groups()
+
+        product_data = self._download_json(
+            'http://www.viu.com/ott/%s/index.php' % country_code, video_id,
+            'Downloading video info', query={
+                'r': 'vod/ajax-detail',
+                'platform_flag_label': 'web',
+                'product_id': video_id,
+            })['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'],
+            })['data']['stream']
+
+        stream_sizes = stream_data.get('size', {})
+        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))
+            formats.append({
+                'format_id': vid_format,
+                'url': stream_url,
+                'height': height,
+                'ext': 'mp4',
+                'filesize': int_or_none(stream_sizes.get(vid_format))
+            })
+        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()
+
+        return {
+            'id': video_id,
+            'title': title,
+            'description': video_data.get('description'),
+            'series': product_data.get('series', {}).get('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'),
+            'formats': formats,
+            'subtitles': subtitles,
+        }