[youtube] Fix extraction.
[youtube-dl] / youtube_dl / extractor / dctp.py
index 5382255f0007243c177b83b7cbc9e5db30f6ad43..e700f8d86531415da0f1db0f2ccdaef6ea10ac53 100644 (file)
-# encoding: utf-8
+# coding: utf-8
 from __future__ import unicode_literals
 
 from .common import InfoExtractor
+from ..compat import compat_str
+from ..utils import (
+    float_or_none,
+    int_or_none,
+    unified_timestamp,
+    url_or_none,
+)
+
 
 class DctpTvIE(InfoExtractor):
-    _VALID_URL = r'^http://www.dctp.tv/(#/)?filme/(?P<id>.+?)/$'
+    _VALID_URL = r'https?://(?:www\.)?dctp\.tv/(?:#/)?filme/(?P<id>[^/?#&]+)'
+    _TESTS = [{
+        # 4x3
+        'url': 'http://www.dctp.tv/filme/videoinstallation-fuer-eine-kaufhausfassade/',
+        'md5': '3ffbd1556c3fe210724d7088fad723e3',
+        'info_dict': {
+            'id': '95eaa4f33dad413aa17b4ee613cccc6c',
+            'display_id': 'videoinstallation-fuer-eine-kaufhausfassade',
+            'ext': 'm4v',
+            'title': 'Videoinstallation für eine Kaufhausfassade',
+            'description': 'Kurzfilm',
+            'thumbnail': r're:^https?://.*\.jpg$',
+            'duration': 71.24,
+            'timestamp': 1302172322,
+            'upload_date': '20110407',
+        },
+    }, {
+        # 16x9
+        'url': 'http://www.dctp.tv/filme/sind-youtuber-die-besseren-lehrer/',
+        'only_matching': True,
+    }]
+
+    _BASE_URL = 'http://dctp-ivms2-restapi.s3.amazonaws.com'
 
     def _real_extract(self, url):
-        video_id = self._match_id(url)
-        base_url = 'http://dctp-ivms2-restapi.s3.amazonaws.com/'
-        version_json = self._download_json(base_url + 'version.json', video_id)
-        version = version_json['version_name']
-        info_json = self._download_json(
-            '{}{}/restapi/slugs/{}.json'.format(base_url, version, video_id), video_id)
-        object_id = info_json['object_id']
-        meta_json = self._download_json(
-            '{}{}/restapi/media/{}.json'.format(base_url, version, object_id), video_id)
-        uuid = meta_json['uuid']
-        title = meta_json['title']
-        wide = meta_json['is_wide']
-        if wide:
-            ratio = '16x9'
-        else:
-            ratio = '4x3'
-        play_path = 'mp4:{}_dctp_0500_{}.m4v'.format(uuid, ratio)
-
-        servers_json = self._download_json('http://www.dctp.tv/streaming_servers/', video_id)
-        url = servers_json[0]['endpoint']
+        display_id = self._match_id(url)
+
+        version = self._download_json(
+            '%s/version.json' % self._BASE_URL, display_id,
+            'Downloading version JSON')
+
+        restapi_base = '%s/%s/restapi' % (
+            self._BASE_URL, version['version_name'])
+
+        info = self._download_json(
+            '%s/slugs/%s.json' % (restapi_base, display_id), display_id,
+            'Downloading video info JSON')
+
+        media = self._download_json(
+            '%s/media/%s.json' % (restapi_base, compat_str(info['object_id'])),
+            display_id, 'Downloading media JSON')
+
+        uuid = media['uuid']
+        title = media['title']
+        is_wide = media.get('is_wide')
+        formats = []
+
+        def add_formats(suffix):
+            templ = 'https://%%s/%s_dctp_%s.m4v' % (uuid, suffix)
+            formats.extend([{
+                'format_id': 'hls-' + suffix,
+                'url': templ % 'cdn-segments.dctp.tv' + '/playlist.m3u8',
+                'protocol': 'm3u8_native',
+            }, {
+                'format_id': 's3-' + suffix,
+                'url': templ % 'completed-media.s3.amazonaws.com',
+            }, {
+                'format_id': 'http-' + suffix,
+                'url': templ % 'cdn-media.dctp.tv',
+            }])
+
+        add_formats('0500_' + ('16x9' if is_wide else '4x3'))
+        if is_wide:
+            add_formats('720p')
+
+        thumbnails = []
+        images = media.get('images')
+        if isinstance(images, list):
+            for image in images:
+                if not isinstance(image, dict):
+                    continue
+                image_url = url_or_none(image.get('url'))
+                if not image_url:
+                    continue
+                thumbnails.append({
+                    'url': image_url,
+                    'width': int_or_none(image.get('width')),
+                    'height': int_or_none(image.get('height')),
+                })
 
         return {
-            'id': video_id,
+            'id': uuid,
+            'display_id': display_id,
             'title': title,
-            'format': 'rtmp',
-            'url': url,
-            'play_path': play_path,
-            'real_time': True,
-            'ext': 'flv'
+            'alt_title': media.get('subtitle'),
+            'description': media.get('description') or media.get('teaser'),
+            'timestamp': unified_timestamp(media.get('created')),
+            'duration': float_or_none(media.get('duration_in_ms'), scale=1000),
+            'thumbnails': thumbnails,
+            'formats': formats,
         }
-
-