[toutv] fix info extraction(closes #1792)(closes #2082)
[youtube-dl] / 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': 'mp4',
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             # m3u8 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         device_types = ['ipad']
40         if app_code != 'toutv':
41             device_types.append('flash')
42
43         formats = []
44         # TODO: extract f4m formats
45         # f4m formats can be extracted using flashhd device_type but they produce unplayable file
46         for device_type in device_types:
47             v_data = self._download_xml(
48                 'http://api.radio-canada.ca/validationMedia/v1/Validation.ashx',
49                 video_id, note='Downloading %s XML' % device_type, query={
50                     'appCode': app_code,
51                     'idMedia': video_id,
52                     'connectionType': 'broadband',
53                     'multibitrate': 'true',
54                     'deviceType': device_type,
55                     # paysJ391wsHjbOJwvCs26toz and bypasslock are used to bypass geo-restriction
56                     'paysJ391wsHjbOJwvCs26toz': 'CA',
57                     'bypasslock': 'NZt5K62gRqfc',
58                 })
59             v_url = xpath_text(v_data, 'url')
60             if not v_url:
61                 continue
62             if v_url == 'null':
63                 raise ExtractorError('%s said: %s' % (
64                     self.IE_NAME, xpath_text(v_data, 'message')), expected=True)
65             ext = determine_ext(v_url)
66             if ext == 'm3u8':
67                 formats.extend(self._extract_m3u8_formats(
68                     v_url, video_id, 'mp4', m3u8_id='hls', fatal=False))
69             elif ext == 'f4m':
70                 formats.extend(self._extract_f4m_formats(v_url, video_id, f4m_id='hds', fatal=False))
71             else:
72                 ext = determine_ext(v_url)
73                 bitrates = xpath_element(v_data, 'bitrates')
74                 for url_e in bitrates.findall('url'):
75                     tbr = int_or_none(url_e.get('bitrate'))
76                     if not tbr:
77                         continue
78                     formats.append({
79                         'format_id': 'rtmp-%d' % tbr,
80                         'url': re.sub(r'\d+\.%s' % ext, '%d.%s' % (tbr, ext), v_url),
81                         'ext': 'flv',
82                         'protocol': 'rtmp',
83                         'width': int_or_none(url_e.get('width')),
84                         'height': int_or_none(url_e.get('height')),
85                         'tbr': tbr,
86                     })
87         self._sort_formats(formats)
88
89         metadata = self._download_xml(
90             'http://api.radio-canada.ca/metaMedia/v1/index.ashx',
91             video_id, note='Downloading metadata XML', query={
92                 'appCode': app_code,
93                 'idMedia': video_id,
94             })
95
96         def get_meta(name):
97             el = find_xpath_attr(metadata, './/Meta', 'name', name)
98             return el.text if el is not None else None
99
100         return {
101             'id': video_id,
102             'title': get_meta('Title'),
103             'description': get_meta('Description') or get_meta('ShortDescription'),
104             'thumbnail': get_meta('imageHR') or get_meta('imageMR') or get_meta('imageBR'),
105             'duration': int_or_none(get_meta('length')),
106             'series': get_meta('Emission'),
107             'season_number': int_or_none('SrcSaison'),
108             'episode_number': int_or_none('SrcEpisode'),
109             'upload_date': unified_strdate(get_meta('Date')),
110             'formats': formats,
111         }
112
113
114 class RadioCanadaAudioVideoIE(InfoExtractor):
115     'radiocanada:audiovideo'
116     _VALID_URL = r'https?://ici\.radio-canada\.ca/audio-video/media-(?P<id>[0-9]+)'
117     _TEST = {
118         'url': 'http://ici.radio-canada.ca/audio-video/media-7527184/barack-obama-au-vietnam',
119         'info_dict': {
120             'id': '7527184',
121             'ext': 'mp4',
122             'title': 'Barack Obama au Vietnam',
123             'description': 'Les États-Unis lèvent l\'embargo sur la vente d\'armes qui datait de la guerre du Vietnam',
124             'upload_date': '20160523',
125         },
126         'params': {
127             # m3u8 download
128             'skip_download': True,
129         },
130     }
131
132     def _real_extract(self, url):
133         return self.url_result('radiocanada:medianet:%s' % self._match_id(url))