]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/radiocanada.py
[tumblr] Fix 403 errors and handle vimeo embeds (#2542)
[yt-dlp.git] / yt_dlp / extractor / radiocanada.py
CommitLineData
444417ed 1# coding: utf-8
2from __future__ import unicode_literals
3
444417ed 4
5from .common import InfoExtractor
8fecc735 6from ..compat import compat_HTTPError
444417ed 7from ..utils import (
444417ed 8 determine_ext,
07fbfef1 9 ExtractorError,
444417ed 10 int_or_none,
11 unified_strdate,
444417ed 12)
13
14
15class 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]+)'
931edb2a
OB
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 }
444417ed 32 },
931edb2a
OB
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 },
29cfcb43
AH
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,
931edb2a
OB
57 }
58 ]
07fbfef1 59 _GEO_COUNTRIES = ['CA']
8fecc735
RA
60 _access_token = None
61 _claims = None
07fbfef1 62
8fecc735
RA
63 def _call_api(self, path, video_id=None, app_code=None, query=None):
64 if not query:
65 query = {}
07fbfef1 66 query.update({
8fecc735 67 'client_key': '773aea60-0e80-41bb-9c7f-e6d7c3ad17fb',
07fbfef1
RA
68 'output': 'json',
69 })
8fecc735
RA
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
444417ed 86
8fecc735
RA
87 def _extract_info(self, app_code, video_id):
88 metas = self._call_api('meta/v1/index.ashx', video_id, app_code)['Metas']
98b7506e
RA
89
90 def get_meta(name):
07fbfef1
RA
91 for meta in metas:
92 if meta.get('name') == name:
93 text = meta.get('text')
94 if text:
95 return text
98b7506e 96
29cfcb43 97 # protectionType does not necessarily mean the video is DRM protected (see
067aa17e 98 # https://github.com/ytdl-org/youtube-dl/pull/18609).
98b7506e 99 if get_meta('protectionType'):
29cfcb43 100 self.report_warning('This video is probably DRM protected.')
98b7506e 101
07fbfef1
RA
102 query = {
103 'connectionType': 'hd',
104 'deviceType': 'ipad',
105 'multibitrate': 'true',
106 }
8fecc735
RA
107 if self._claims:
108 query['claims'] = self._claims
07fbfef1
RA
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)
8fecc735
RA
115 if error == 'Le contenu sélectionné est disponible seulement en premium':
116 self.raise_login_required(error)
a3431e12
S
117 raise ExtractorError(
118 '%s said: %s' % (self.IE_NAME, error), expected=True)
07fbfef1 119 formats = self._extract_m3u8_formats(v_url, video_id, 'mp4')
444417ed 120 self._sort_formats(formats)
121
4f9cd4d3
RA
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
444417ed 130 return {
131 'id': video_id,
931edb2a 132 'title': get_meta('Title') or get_meta('AV-nomEmission'),
444417ed 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')),
4f9cd4d3 140 'subtitles': subtitles,
444417ed 141 'formats': formats,
142 }
143
8fecc735 144 def _real_extract(self, url):
5ad28e7f 145 return self._extract_info(*self._match_valid_url(url).groups())
8fecc735 146
444417ed 147
148class RadioCanadaAudioVideoIE(InfoExtractor):
034f5fb5 149 IE_NAME = 'radiocanada:audiovideo'
07fbfef1
RA
150 _VALID_URL = r'https?://ici\.radio-canada\.ca/([^/]+/)*media-(?P<id>[0-9]+)'
151 _TESTS = [{
444417ed 152 'url': 'http://ici.radio-canada.ca/audio-video/media-7527184/barack-obama-au-vietnam',
153 'info_dict': {
154 'id': '7527184',
882af14d 155 'ext': 'mp4',
444417ed 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': {
882af14d 161 # m3u8 download
444417ed 162 'skip_download': True,
163 },
07fbfef1
RA
164 }, {
165 'url': 'https://ici.radio-canada.ca/info/videos/media-7527184/barack-obama-au-vietnam',
166 'only_matching': True,
167 }]
444417ed 168
169 def _real_extract(self, url):
170 return self.url_result('radiocanada:medianet:%s' % self._match_id(url))