[dctptv] Restore extraction based on REST API (closes #16850)
[youtube-dl] / youtube_dl / extractor / dctp.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..compat import compat_str
6 from ..utils import (
7     float_or_none,
8     int_or_none,
9     unified_timestamp,
10 )
11
12
13 class DctpTvIE(InfoExtractor):
14     _VALID_URL = r'https?://(?:www\.)?dctp\.tv/(?:#/)?filme/(?P<id>[^/?#&]+)'
15     _TESTS = [{
16         # 4x3
17         'url': 'http://www.dctp.tv/filme/videoinstallation-fuer-eine-kaufhausfassade/',
18         'info_dict': {
19             'id': '95eaa4f33dad413aa17b4ee613cccc6c',
20             'display_id': 'videoinstallation-fuer-eine-kaufhausfassade',
21             'ext': 'flv',
22             'title': 'Videoinstallation für eine Kaufhausfassade',
23             'description': 'Kurzfilm',
24             'thumbnail': r're:^https?://.*\.jpg$',
25             'duration': 71.24,
26             'timestamp': 1302172322,
27             'upload_date': '20110407',
28         },
29         'params': {
30             # rtmp download
31             'skip_download': True,
32         },
33     }, {
34         # 16x9
35         'url': 'http://www.dctp.tv/filme/sind-youtuber-die-besseren-lehrer/',
36         'only_matching': True,
37     }]
38
39     _BASE_URL = 'http://dctp-ivms2-restapi.s3.amazonaws.com'
40
41     def _real_extract(self, url):
42         display_id = self._match_id(url)
43
44         version = self._download_json(
45             '%s/version.json' % self._BASE_URL, display_id,
46             'Downloading version JSON')
47
48         restapi_base = '%s/%s/restapi' % (
49             self._BASE_URL, version['version_name'])
50
51         info = self._download_json(
52             '%s/slugs/%s.json' % (restapi_base, display_id), display_id,
53             'Downloading video info JSON')
54
55         media = self._download_json(
56             '%s/media/%s.json' % (restapi_base, compat_str(info['object_id'])),
57             display_id, 'Downloading media JSON')
58
59         uuid = media['uuid']
60         title = media['title']
61         ratio = '16x9' if media.get('is_wide') else '4x3'
62         play_path = 'mp4:%s_dctp_0500_%s.m4v' % (uuid, ratio)
63
64         servers = self._download_json(
65             'http://www.dctp.tv/streaming_servers/', display_id,
66             note='Downloading server list JSON', fatal=False)
67
68         if servers:
69             endpoint = next(
70                 server['endpoint']
71                 for server in servers
72                 if isinstance(server.get('endpoint'), compat_str) and
73                 'cloudfront' in server['endpoint'])
74         else:
75             endpoint = 'rtmpe://s2pqqn4u96e4j8.cloudfront.net/cfx/st/'
76
77         app = self._search_regex(
78             r'^rtmpe?://[^/]+/(?P<app>.*)$', endpoint, 'app')
79
80         formats = [{
81             'url': endpoint,
82             'app': app,
83             'play_path': play_path,
84             'page_url': url,
85             'player_url': 'http://svm-prod-dctptv-static.s3.amazonaws.com/dctptv-relaunch2012-110.swf',
86             'ext': 'flv',
87         }]
88
89         thumbnails = []
90         images = media.get('images')
91         if isinstance(images, list):
92             for image in images:
93                 if not isinstance(image, dict):
94                     continue
95                 image_url = image.get('url')
96                 if not image_url or not isinstance(image_url, compat_str):
97                     continue
98                 thumbnails.append({
99                     'url': image_url,
100                     'width': int_or_none(image.get('width')),
101                     'height': int_or_none(image.get('height')),
102                 })
103
104         return {
105             'id': uuid,
106             'display_id': display_id,
107             'title': title,
108             'alt_title': media.get('subtitle'),
109             'description': media.get('description') or media.get('teaser'),
110             'timestamp': unified_timestamp(media.get('created')),
111             'duration': float_or_none(media.get('duration_in_ms'), scale=1000),
112             'thumbnails': thumbnails,
113             'formats': formats,
114         }