[polskieradio] Add support for downloading whole programmes.
[youtube-dl] / youtube_dl / extractor / canvas.py
1 from __future__ import unicode_literals
2
3 from .common import InfoExtractor
4 from ..utils import float_or_none
5
6
7 class CanvasIE(InfoExtractor):
8     _VALID_URL = r'https?://(?:www\.)?canvas\.be/video/(?:[^/]+/)*(?P<id>[^/?#&]+)'
9     _TESTS = [{
10         'url': 'http://www.canvas.be/video/de-afspraak/najaar-2015/de-afspraak-veilt-voor-de-warmste-week',
11         'md5': 'ea838375a547ac787d4064d8c7860a6c',
12         'info_dict': {
13             'id': 'mz-ast-5e5f90b6-2d72-4c40-82c2-e134f884e93e',
14             'display_id': 'de-afspraak-veilt-voor-de-warmste-week',
15             'ext': 'mp4',
16             'title': 'De afspraak veilt voor de Warmste Week',
17             'description': 'md5:24cb860c320dc2be7358e0e5aa317ba6',
18             'thumbnail': 're:^https?://.*\.jpg$',
19             'duration': 49.02,
20         }
21     }, {
22         # with subtitles
23         'url': 'http://www.canvas.be/video/panorama/2016/pieter-0167',
24         'info_dict': {
25             'id': 'mz-ast-5240ff21-2d30-4101-bba6-92b5ec67c625',
26             'display_id': 'pieter-0167',
27             'ext': 'mp4',
28             'title': 'Pieter 0167',
29             'description': 'md5:943cd30f48a5d29ba02c3a104dc4ec4e',
30             'thumbnail': 're:^https?://.*\.jpg$',
31             'duration': 2553.08,
32             'subtitles': {
33                 'nl': [{
34                     'ext': 'vtt',
35                 }],
36             },
37         },
38         'params': {
39             'skip_download': True,
40         }
41     }]
42
43     def _real_extract(self, url):
44         display_id = self._match_id(url)
45
46         webpage = self._download_webpage(url, display_id)
47
48         title = self._search_regex(
49             r'<h1[^>]+class="video__body__header__title"[^>]*>(.+?)</h1>',
50             webpage, 'title', default=None) or self._og_search_title(webpage)
51
52         video_id = self._html_search_regex(
53             r'data-video=(["\'])(?P<id>.+?)\1', webpage, 'video id', group='id')
54
55         data = self._download_json(
56             'https://mediazone.vrt.be/api/v1/canvas/assets/%s' % video_id, display_id)
57
58         formats = []
59         for target in data['targetUrls']:
60             format_url, format_type = target.get('url'), target.get('type')
61             if not format_url or not format_type:
62                 continue
63             if format_type == 'HLS':
64                 formats.extend(self._extract_m3u8_formats(
65                     format_url, display_id, entry_protocol='m3u8_native',
66                     ext='mp4', preference=0, fatal=False, m3u8_id=format_type))
67             elif format_type == 'HDS':
68                 formats.extend(self._extract_f4m_formats(
69                     format_url, display_id, f4m_id=format_type, fatal=False))
70             else:
71                 formats.append({
72                     'format_id': format_type,
73                     'url': format_url,
74                 })
75         self._sort_formats(formats)
76
77         subtitles = {}
78         subtitle_urls = data.get('subtitleUrls')
79         if isinstance(subtitle_urls, list):
80             for subtitle in subtitle_urls:
81                 subtitle_url = subtitle.get('url')
82                 if subtitle_url and subtitle.get('type') == 'CLOSED':
83                     subtitles.setdefault('nl', []).append({'url': subtitle_url})
84
85         return {
86             'id': video_id,
87             'display_id': display_id,
88             'title': title,
89             'description': self._og_search_description(webpage),
90             'formats': formats,
91             'duration': float_or_none(data.get('duration'), 1000),
92             'thumbnail': data.get('posterImageUrl'),
93             'subtitles': subtitles,
94         }