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