[common] add helper method for Wowza Streaming Engine format extraction
[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     float_or_none,
9 )
10
11
12 class VRTIE(InfoExtractor):
13     _VALID_URL = r'https?://(?:deredactie|sporza|cobra(?:\.canvas)?)\.be/cm/(?:[^/]+/)+(?P<id>[^/]+)/*'
14     _TESTS = [
15         # deredactie.be
16         {
17             'url': 'http://deredactie.be/cm/vrtnieuws/videozone/programmas/journaal/EP_141025_JOL',
18             'md5': '4cebde1eb60a53782d4f3992cbd46ec8',
19             'info_dict': {
20                 'id': '2129880',
21                 'ext': 'flv',
22                 'title': 'Het journaal L - 25/10/14',
23                 'description': None,
24                 'timestamp': 1414271750.949,
25                 'upload_date': '20141025',
26                 'duration': 929,
27             },
28             'skip': 'HTTP Error 404: Not Found',
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             'skip': 'HTTP Error 404: Not Found',
44         },
45         # cobra.be
46         {
47             'url': 'http://cobra.be/cm/cobra/videozone/rubriek/film-videozone/141022-mv-ellis-cafecorsari',
48             'md5': '78a2b060a5083c4f055449a72477409d',
49             'info_dict': {
50                 'id': '2126050',
51                 'ext': 'flv',
52                 'title': 'Bret Easton Ellis in Café Corsari',
53                 'description': 'md5:f699986e823f32fd6036c1855a724ee9',
54                 'timestamp': 1413967500.494,
55                 'upload_date': '20141022',
56                 'duration': 661,
57             },
58             'skip': 'HTTP Error 404: Not Found',
59         },
60         {
61             # YouTube video
62             'url': 'http://deredactie.be/cm/vrtnieuws/videozone/nieuws/cultuurenmedia/1.2622957',
63             'md5': 'b8b93da1df1cea6c8556255a796b7d61',
64             'info_dict': {
65                 'id': 'Wji-BZ0oCwg',
66                 'ext': 'mp4',
67                 'title': 'ROGUE ONE: A STAR WARS STORY Official Teaser Trailer',
68                 'description': 'md5:8e468944dce15567a786a67f74262583',
69                 'uploader': 'Star Wars',
70                 'uploader_id': 'starwars',
71                 'upload_date': '20160407',
72             },
73             'add_ie': ['Youtube'],
74         },
75         {
76             'url': 'http://cobra.canvas.be/cm/cobra/videozone/rubriek/film-videozone/1.2377055',
77             'info_dict': {
78                 'id': '2377055',
79                 'ext': 'mp4',
80                 'title': 'Cafe Derby',
81                 'description': 'Lenny Van Wesemael debuteert met de langspeelfilm Café Derby. Een waar gebeurd maar ook verzonnen verhaal.',
82                 'upload_date': '20150626',
83                 'timestamp': 1435305240.769,
84             },
85             'params': {
86                 # m3u8 download
87                 'skip_download': True,
88             }
89         }
90     ]
91
92     def _real_extract(self, url):
93         video_id = self._match_id(url)
94
95         webpage = self._download_webpage(url, video_id)
96
97         video_id = self._search_regex(
98             r'data-video-id="([^"]+)_[^"]+"', webpage, 'video id', fatal=False)
99
100         src = self._search_regex(
101             r'data-video-src="([^"]+)"', webpage, 'video src', default=None)
102
103         video_type = self._search_regex(
104             r'data-video-type="([^"]+)"', webpage, 'video type', default=None)
105
106         if video_type == 'YouTubeVideo':
107             return self.url_result(src, 'Youtube')
108
109         formats = []
110
111         mobj = re.search(
112             r'data-video-iphone-server="(?P<server>[^"]+)"\s+data-video-iphone-path="(?P<path>[^"]+)"',
113             webpage)
114         if mobj:
115             formats.extend(self._extract_m3u8_formats(
116                 '%s/%s' % (mobj.group('server'), mobj.group('path')),
117                 video_id, 'mp4', m3u8_id='hls', fatal=False))
118
119         if src:
120             formats = self._extract_wowza_formats(src, video_id)
121             if 'data-video-geoblocking="true"' not in webpage:
122                 for f in formats:
123                     if f['url'].startswith('rtsp://'):
124                         http_format = f.copy()
125                         http_format.update({
126                             'url': f['url'].replace('rtsp://', 'http://').replace('vod.', 'download.').replace('/_definst_/', '/').replace('mp4:', ''),
127                             'format_id': f['format_id'].replace('rtsp', 'http'),
128                             'protocol': 'http',
129                         })
130                         formats.append(http_format)
131
132         if not formats and 'data-video-geoblocking="true"' in webpage:
133             self.raise_geo_restricted('This video is only available in Belgium')
134
135         self._sort_formats(formats)
136
137         title = self._og_search_title(webpage)
138         description = self._og_search_description(webpage, default=None)
139         thumbnail = self._og_search_thumbnail(webpage)
140         timestamp = float_or_none(self._search_regex(
141             r'data-video-sitestat-pubdate="(\d+)"', webpage, 'timestamp', fatal=False), 1000)
142         duration = float_or_none(self._search_regex(
143             r'data-video-duration="(\d+)"', webpage, 'duration', fatal=False), 1000)
144
145         return {
146             'id': video_id,
147             'title': title,
148             'description': description,
149             'thumbnail': thumbnail,
150             'timestamp': timestamp,
151             'duration': duration,
152             'formats': formats,
153         }