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