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