]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/radiocanada.py
New upstream version 2017.02.24.1
[youtubedl] / youtube_dl / extractor / radiocanada.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 xpath_text,
9 find_xpath_attr,
10 determine_ext,
11 int_or_none,
12 unified_strdate,
13 xpath_element,
14 ExtractorError,
15 determine_protocol,
16 unsmuggle_url,
17 )
18
19
20 class RadioCanadaIE(InfoExtractor):
21 IE_NAME = 'radiocanada'
22 _VALID_URL = r'(?:radiocanada:|https?://ici\.radio-canada\.ca/widgets/mediaconsole/)(?P<app_code>[^:/]+)[:/](?P<id>[0-9]+)'
23 _TEST = {
24 'url': 'http://ici.radio-canada.ca/widgets/mediaconsole/medianet/7184272',
25 'info_dict': {
26 'id': '7184272',
27 'ext': 'mp4',
28 'title': 'Le parcours du tireur capté sur vidéo',
29 'description': 'Images des caméras de surveillance fournies par la GRC montrant le parcours du tireur d\'Ottawa',
30 'upload_date': '20141023',
31 },
32 'params': {
33 # m3u8 download
34 'skip_download': True,
35 },
36 }
37
38 def _real_extract(self, url):
39 url, smuggled_data = unsmuggle_url(url, {})
40 app_code, video_id = re.match(self._VALID_URL, url).groups()
41
42 metadata = self._download_xml(
43 'http://api.radio-canada.ca/metaMedia/v1/index.ashx',
44 video_id, note='Downloading metadata XML', query={
45 'appCode': app_code,
46 'idMedia': video_id,
47 })
48
49 def get_meta(name):
50 el = find_xpath_attr(metadata, './/Meta', 'name', name)
51 return el.text if el is not None else None
52
53 if get_meta('protectionType'):
54 raise ExtractorError('This video is DRM protected.', expected=True)
55
56 device_types = ['ipad']
57 if not smuggled_data:
58 device_types.append('flash')
59 device_types.append('android')
60
61 formats = []
62 # TODO: extract f4m formats
63 # f4m formats can be extracted using flashhd device_type but they produce unplayable file
64 for device_type in device_types:
65 validation_url = 'http://api.radio-canada.ca/validationMedia/v1/Validation.ashx'
66 query = {
67 'appCode': app_code,
68 'idMedia': video_id,
69 'connectionType': 'broadband',
70 'multibitrate': 'true',
71 'deviceType': device_type,
72 }
73 if smuggled_data:
74 validation_url = 'https://services.radio-canada.ca/media/validation/v2/'
75 query.update(smuggled_data)
76 else:
77 query.update({
78 # paysJ391wsHjbOJwvCs26toz and bypasslock are used to bypass geo-restriction
79 'paysJ391wsHjbOJwvCs26toz': 'CA',
80 'bypasslock': 'NZt5K62gRqfc',
81 })
82 v_data = self._download_xml(validation_url, video_id, note='Downloading %s XML' % device_type, query=query, fatal=False)
83 v_url = xpath_text(v_data, 'url')
84 if not v_url:
85 continue
86 if v_url == 'null':
87 raise ExtractorError('%s said: %s' % (
88 self.IE_NAME, xpath_text(v_data, 'message')), expected=True)
89 ext = determine_ext(v_url)
90 if ext == 'm3u8':
91 formats.extend(self._extract_m3u8_formats(
92 v_url, video_id, 'mp4', m3u8_id='hls', fatal=False))
93 elif ext == 'f4m':
94 formats.extend(self._extract_f4m_formats(
95 v_url, video_id, f4m_id='hds', fatal=False))
96 else:
97 ext = determine_ext(v_url)
98 bitrates = xpath_element(v_data, 'bitrates')
99 for url_e in bitrates.findall('url'):
100 tbr = int_or_none(url_e.get('bitrate'))
101 if not tbr:
102 continue
103 f_url = re.sub(r'\d+\.%s' % ext, '%d.%s' % (tbr, ext), v_url)
104 protocol = determine_protocol({'url': f_url})
105 f = {
106 'format_id': '%s-%d' % (protocol, tbr),
107 'url': f_url,
108 'ext': 'flv' if protocol == 'rtmp' else ext,
109 'protocol': protocol,
110 'width': int_or_none(url_e.get('width')),
111 'height': int_or_none(url_e.get('height')),
112 'tbr': tbr,
113 }
114 mobj = re.match(r'(?P<url>rtmp://[^/]+/[^/]+)/(?P<playpath>[^?]+)(?P<auth>\?.+)', f_url)
115 if mobj:
116 f.update({
117 'url': mobj.group('url') + mobj.group('auth'),
118 'play_path': mobj.group('playpath'),
119 })
120 formats.append(f)
121 if protocol == 'rtsp':
122 base_url = self._search_regex(
123 r'rtsp://([^?]+)', f_url, 'base url', default=None)
124 if base_url:
125 base_url = 'http://' + base_url
126 formats.extend(self._extract_m3u8_formats(
127 base_url + '/playlist.m3u8', video_id, 'mp4',
128 'm3u8_native', m3u8_id='hls', fatal=False))
129 formats.extend(self._extract_f4m_formats(
130 base_url + '/manifest.f4m', video_id,
131 f4m_id='hds', fatal=False))
132 self._sort_formats(formats)
133
134 subtitles = {}
135 closed_caption_url = get_meta('closedCaption') or get_meta('closedCaptionHTML5')
136 if closed_caption_url:
137 subtitles['fr'] = [{
138 'url': closed_caption_url,
139 'ext': determine_ext(closed_caption_url, 'vtt'),
140 }]
141
142 return {
143 'id': video_id,
144 'title': get_meta('Title'),
145 'description': get_meta('Description') or get_meta('ShortDescription'),
146 'thumbnail': get_meta('imageHR') or get_meta('imageMR') or get_meta('imageBR'),
147 'duration': int_or_none(get_meta('length')),
148 'series': get_meta('Emission'),
149 'season_number': int_or_none('SrcSaison'),
150 'episode_number': int_or_none('SrcEpisode'),
151 'upload_date': unified_strdate(get_meta('Date')),
152 'subtitles': subtitles,
153 'formats': formats,
154 }
155
156
157 class RadioCanadaAudioVideoIE(InfoExtractor):
158 'radiocanada:audiovideo'
159 _VALID_URL = r'https?://ici\.radio-canada\.ca/audio-video/media-(?P<id>[0-9]+)'
160 _TEST = {
161 'url': 'http://ici.radio-canada.ca/audio-video/media-7527184/barack-obama-au-vietnam',
162 'info_dict': {
163 'id': '7527184',
164 'ext': 'mp4',
165 'title': 'Barack Obama au Vietnam',
166 'description': 'Les États-Unis lèvent l\'embargo sur la vente d\'armes qui datait de la guerre du Vietnam',
167 'upload_date': '20160523',
168 },
169 'params': {
170 # m3u8 download
171 'skip_download': True,
172 },
173 }
174
175 def _real_extract(self, url):
176 return self.url_result('radiocanada:medianet:%s' % self._match_id(url))