[vrt] Add support for direct hls playlists and YouTube (Closes #9108)
[youtube-dl] / youtube_dl / extractor / vrt.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8     determine_ext,
9     float_or_none,
10 )
11
12
13 class VRTIE(InfoExtractor):
14     _VALID_URL = r'https?://(?:deredactie|sporza|cobra(?:\.canvas)?)\.be/cm/(?:[^/]+/)+(?P<id>[^/]+)/*'
15     _TESTS = [
16         # deredactie.be
17         {
18             'url': 'http://deredactie.be/cm/vrtnieuws/videozone/programmas/journaal/EP_141025_JOL',
19             'md5': '4cebde1eb60a53782d4f3992cbd46ec8',
20             'info_dict': {
21                 'id': '2129880',
22                 'ext': 'flv',
23                 'title': 'Het journaal L - 25/10/14',
24                 'description': None,
25                 'timestamp': 1414271750.949,
26                 'upload_date': '20141025',
27                 'duration': 929,
28             }
29         },
30         # sporza.be
31         {
32             'url': 'http://sporza.be/cm/sporza/videozone/programmas/extratime/EP_141020_Extra_time',
33             'md5': '11f53088da9bf8e7cfc42456697953ff',
34             'info_dict': {
35                 'id': '2124639',
36                 'ext': 'flv',
37                 'title': 'Bekijk Extra Time van 20 oktober',
38                 'description': 'md5:83ac5415a4f1816c6a93f8138aef2426',
39                 'timestamp': 1413835980.560,
40                 'upload_date': '20141020',
41                 'duration': 3238,
42             }
43         },
44         # cobra.be
45         {
46             'url': 'http://cobra.be/cm/cobra/videozone/rubriek/film-videozone/141022-mv-ellis-cafecorsari',
47             'md5': '78a2b060a5083c4f055449a72477409d',
48             'info_dict': {
49                 'id': '2126050',
50                 'ext': 'flv',
51                 'title': 'Bret Easton Ellis in CafĂ© Corsari',
52                 'description': 'md5:f699986e823f32fd6036c1855a724ee9',
53                 'timestamp': 1413967500.494,
54                 'upload_date': '20141022',
55                 'duration': 661,
56             }
57         },
58         {
59             # YouTube video
60             'url': 'http://deredactie.be/cm/vrtnieuws/videozone/nieuws/cultuurenmedia/1.2622957',
61             'only_matching': True,
62         },
63         {
64             'url': 'http://cobra.canvas.be/cm/cobra/videozone/rubriek/film-videozone/1.2377055',
65             'only_matching': True,
66         }
67     ]
68
69     def _real_extract(self, url):
70         video_id = self._match_id(url)
71
72         webpage = self._download_webpage(url, video_id)
73
74         video_id = self._search_regex(
75             r'data-video-id="([^"]+)_[^"]+"', webpage, 'video id', fatal=False)
76
77         src = self._search_regex(
78             r'data-video-src="([^"]+)"', webpage, 'video src', default=None)
79
80         video_type = self._search_regex(
81             r'data-video-type="([^"]+)"', webpage, 'video type', default=None)
82
83         if video_type == 'YouTubeVideo':
84             return self.url_result(src, 'Youtube')
85
86         formats = []
87
88         mobj = re.search(
89             r'data-video-iphone-server="(?P<server>[^"]+)"\s+data-video-iphone-path="(?P<path>[^"]+)"',
90             webpage)
91         if mobj:
92             formats.extend(self._extract_m3u8_formats(
93                 '%s/%s' % (mobj.group('server'), mobj.group('path')),
94                 video_id, 'mp4', m3u8_id='hls', fatal=False))
95
96         if src:
97             if determine_ext(src) == 'm3u8':
98                 formats.extend(self._extract_m3u8_formats(
99                     src, video_id, 'mp4', entry_protocol='m3u8_native',
100                     m3u8_id='hls', fatal=False))
101             else:
102                 formats.extend(self._extract_f4m_formats(
103                     '%s/manifest.f4m' % src, video_id, f4m_id='hds', fatal=False))
104
105         if not formats and 'data-video-geoblocking="true"' in webpage:
106             self.raise_geo_restricted('This video is only available in Belgium')
107
108         self._sort_formats(formats)
109
110         title = self._og_search_title(webpage)
111         description = self._og_search_description(webpage, default=None)
112         thumbnail = self._og_search_thumbnail(webpage)
113         timestamp = float_or_none(self._search_regex(
114             r'data-video-sitestat-pubdate="(\d+)"', webpage, 'timestamp', fatal=False), 1000)
115         duration = float_or_none(self._search_regex(
116             r'data-video-duration="(\d+)"', webpage, 'duration', fatal=False), 1000)
117
118         return {
119             'id': video_id,
120             'title': title,
121             'description': description,
122             'thumbnail': thumbnail,
123             'timestamp': timestamp,
124             'duration': duration,
125             'formats': formats,
126         }