[radiocanada] switch to the new media requests(closes #19115)
[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     determine_ext,
9     ExtractorError,
10     int_or_none,
11     unified_strdate,
12     unsmuggle_url,
13 )
14
15
16 class RadioCanadaIE(InfoExtractor):
17     IE_NAME = 'radiocanada'
18     _VALID_URL = r'(?:radiocanada:|https?://ici\.radio-canada\.ca/widgets/mediaconsole/)(?P<app_code>[^:/]+)[:/](?P<id>[0-9]+)'
19     _TESTS = [
20         {
21             'url': 'http://ici.radio-canada.ca/widgets/mediaconsole/medianet/7184272',
22             'info_dict': {
23                 'id': '7184272',
24                 'ext': 'mp4',
25                 'title': 'Le parcours du tireur capté sur vidéo',
26                 'description': 'Images des caméras de surveillance fournies par la GRC montrant le parcours du tireur d\'Ottawa',
27                 'upload_date': '20141023',
28             },
29             'params': {
30                 # m3u8 download
31                 'skip_download': True,
32             }
33         },
34         {
35             # empty Title
36             'url': 'http://ici.radio-canada.ca/widgets/mediaconsole/medianet/7754998/',
37             'info_dict': {
38                 'id': '7754998',
39                 'ext': 'mp4',
40                 'title': 'letelejournal22h',
41                 'description': 'INTEGRALE WEB 22H-TJ',
42                 'upload_date': '20170720',
43             },
44             'params': {
45                 # m3u8 download
46                 'skip_download': True,
47             },
48         },
49         {
50             # with protectionType but not actually DRM protected
51             'url': 'radiocanada:toutv:140872',
52             'info_dict': {
53                 'id': '140872',
54                 'title': 'Épisode 1',
55                 'series': 'District 31',
56             },
57             'only_matching': True,
58         }
59     ]
60     _GEO_COUNTRIES = ['CA']
61
62     def _call_api(self, path, video_id, app_code, query):
63         query.update({
64             'appCode': app_code,
65             'idMedia': video_id,
66             'output': 'json',
67         })
68         return self._download_json(
69             'https://services.radio-canada.ca/media/' + path, video_id, headers={
70                 'Authorization': 'Client-Key 773aea60-0e80-41bb-9c7f-e6d7c3ad17fb'
71             }, query=query)
72
73     def _real_extract(self, url):
74         url, smuggled_data = unsmuggle_url(url, {})
75         app_code, video_id = re.match(self._VALID_URL, url).groups()
76
77         metas = self._call_api('meta/v1/index.ashx', video_id, app_code, {})['Metas']
78
79         def get_meta(name):
80             for meta in metas:
81                 if meta.get('name') == name:
82                     text = meta.get('text')
83                     if text:
84                         return text
85
86         # protectionType does not necessarily mean the video is DRM protected (see
87         # https://github.com/rg3/youtube-dl/pull/18609).
88         if get_meta('protectionType'):
89             self.report_warning('This video is probably DRM protected.')
90
91         query = {
92             'connectionType': 'hd',
93             'deviceType': 'ipad',
94             'multibitrate': 'true',
95         }
96         if smuggled_data:
97             query.update(smuggled_data)
98         v_data = self._call_api('validation/v2/', video_id, app_code, query)
99         v_url = v_data.get('url')
100         if not v_url:
101             error = v_data['message']
102             if error == "Le contenu sélectionné n'est pas disponible dans votre pays":
103                 raise self.raise_geo_restricted(error, self._GEO_COUNTRIES)
104             raise ExtractorError(
105                 '%s said: %s' % (self.IE_NAME, error), expected=True)
106         formats = self._extract_m3u8_formats(v_url, video_id, 'mp4')
107         self._sort_formats(formats)
108
109         subtitles = {}
110         closed_caption_url = get_meta('closedCaption') or get_meta('closedCaptionHTML5')
111         if closed_caption_url:
112             subtitles['fr'] = [{
113                 'url': closed_caption_url,
114                 'ext': determine_ext(closed_caption_url, 'vtt'),
115             }]
116
117         return {
118             'id': video_id,
119             'title': get_meta('Title') or get_meta('AV-nomEmission'),
120             'description': get_meta('Description') or get_meta('ShortDescription'),
121             'thumbnail': get_meta('imageHR') or get_meta('imageMR') or get_meta('imageBR'),
122             'duration': int_or_none(get_meta('length')),
123             'series': get_meta('Emission'),
124             'season_number': int_or_none('SrcSaison'),
125             'episode_number': int_or_none('SrcEpisode'),
126             'upload_date': unified_strdate(get_meta('Date')),
127             'subtitles': subtitles,
128             'formats': formats,
129         }
130
131
132 class RadioCanadaAudioVideoIE(InfoExtractor):
133     'radiocanada:audiovideo'
134     _VALID_URL = r'https?://ici\.radio-canada\.ca/([^/]+/)*media-(?P<id>[0-9]+)'
135     _TESTS = [{
136         'url': 'http://ici.radio-canada.ca/audio-video/media-7527184/barack-obama-au-vietnam',
137         'info_dict': {
138             'id': '7527184',
139             'ext': 'mp4',
140             'title': 'Barack Obama au Vietnam',
141             'description': 'Les États-Unis lèvent l\'embargo sur la vente d\'armes qui datait de la guerre du Vietnam',
142             'upload_date': '20160523',
143         },
144         'params': {
145             # m3u8 download
146             'skip_download': True,
147         },
148     }, {
149         'url': 'https://ici.radio-canada.ca/info/videos/media-7527184/barack-obama-au-vietnam',
150         'only_matching': True,
151     }]
152
153     def _real_extract(self, url):
154         return self.url_result('radiocanada:medianet:%s' % self._match_id(url))