]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/cbc.py
New upstream version 2018.03.14
[youtubedl] / youtube_dl / extractor / cbc.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import json
5 import re
6
7 from .common import InfoExtractor
8 from ..compat import compat_str
9 from ..utils import (
10 js_to_json,
11 smuggle_url,
12 try_get,
13 xpath_text,
14 xpath_element,
15 xpath_with_ns,
16 find_xpath_attr,
17 parse_duration,
18 parse_iso8601,
19 parse_age_limit,
20 int_or_none,
21 ExtractorError,
22 )
23
24
25 class CBCIE(InfoExtractor):
26 IE_NAME = 'cbc.ca'
27 _VALID_URL = r'https?://(?:www\.)?cbc\.ca/(?!player/)(?:[^/]+/)+(?P<id>[^/?#]+)'
28 _TESTS = [{
29 # with mediaId
30 'url': 'http://www.cbc.ca/22minutes/videos/clips-season-23/don-cherry-play-offs',
31 'md5': '97e24d09672fc4cf56256d6faa6c25bc',
32 'info_dict': {
33 'id': '2682904050',
34 'ext': 'mp4',
35 'title': 'Don Cherry – All-Stars',
36 'description': 'Don Cherry has a bee in his bonnet about AHL player John Scott because that guy’s got heart.',
37 'timestamp': 1454463000,
38 'upload_date': '20160203',
39 'uploader': 'CBCC-NEW',
40 },
41 'skip': 'Geo-restricted to Canada',
42 }, {
43 # with clipId, feed available via tpfeed.cbc.ca and feed.theplatform.com
44 'url': 'http://www.cbc.ca/22minutes/videos/22-minutes-update/22-minutes-update-episode-4',
45 'md5': '162adfa070274b144f4fdc3c3b8207db',
46 'info_dict': {
47 'id': '2414435309',
48 'ext': 'mp4',
49 'title': '22 Minutes Update: What Not To Wear Quebec',
50 'description': "This week's latest Canadian top political story is What Not To Wear Quebec.",
51 'upload_date': '20131025',
52 'uploader': 'CBCC-NEW',
53 'timestamp': 1382717907,
54 },
55 }, {
56 # with clipId, feed only available via tpfeed.cbc.ca
57 'url': 'http://www.cbc.ca/archives/entry/1978-robin-williams-freestyles-on-90-minutes-live',
58 'md5': '0274a90b51a9b4971fe005c63f592f12',
59 'info_dict': {
60 'id': '2487345465',
61 'ext': 'mp4',
62 'title': 'Robin Williams freestyles on 90 Minutes Live',
63 'description': 'Wacky American comedian Robin Williams shows off his infamous "freestyle" comedic talents while being interviewed on CBC\'s 90 Minutes Live.',
64 'upload_date': '19780210',
65 'uploader': 'CBCC-NEW',
66 'timestamp': 255977160,
67 },
68 }, {
69 # multiple iframes
70 'url': 'http://www.cbc.ca/natureofthings/blog/birds-eye-view-from-vancouvers-burrard-street-bridge-how-we-got-the-shot',
71 'playlist': [{
72 'md5': '377572d0b49c4ce0c9ad77470e0b96b4',
73 'info_dict': {
74 'id': '2680832926',
75 'ext': 'mp4',
76 'title': 'An Eagle\'s-Eye View Off Burrard Bridge',
77 'description': 'Hercules the eagle flies from Vancouver\'s Burrard Bridge down to a nearby park with a mini-camera strapped to his back.',
78 'upload_date': '20160201',
79 'timestamp': 1454342820,
80 'uploader': 'CBCC-NEW',
81 },
82 }, {
83 'md5': '415a0e3f586113894174dfb31aa5bb1a',
84 'info_dict': {
85 'id': '2658915080',
86 'ext': 'mp4',
87 'title': 'Fly like an eagle!',
88 'description': 'Eagle equipped with a mini camera flies from the world\'s tallest tower',
89 'upload_date': '20150315',
90 'timestamp': 1426443984,
91 'uploader': 'CBCC-NEW',
92 },
93 }],
94 'skip': 'Geo-restricted to Canada',
95 }, {
96 # multiple CBC.APP.Caffeine.initInstance(...)
97 'url': 'http://www.cbc.ca/news/canada/calgary/dog-indoor-exercise-winter-1.3928238',
98 'info_dict': {
99 'title': 'Keep Rover active during the deep freeze with doggie pushups and other fun indoor tasks',
100 'id': 'dog-indoor-exercise-winter-1.3928238',
101 'description': 'md5:c18552e41726ee95bd75210d1ca9194c',
102 },
103 'playlist_mincount': 6,
104 }]
105
106 @classmethod
107 def suitable(cls, url):
108 return False if CBCPlayerIE.suitable(url) else super(CBCIE, cls).suitable(url)
109
110 def _extract_player_init(self, player_init, display_id):
111 player_info = self._parse_json(player_init, display_id, js_to_json)
112 media_id = player_info.get('mediaId')
113 if not media_id:
114 clip_id = player_info['clipId']
115 feed = self._download_json(
116 'http://tpfeed.cbc.ca/f/ExhSPC/vms_5akSXx4Ng_Zn?byCustomValue={:mpsReleases}{%s}' % clip_id,
117 clip_id, fatal=False)
118 if feed:
119 media_id = try_get(feed, lambda x: x['entries'][0]['guid'], compat_str)
120 if not media_id:
121 media_id = self._download_json(
122 'http://feed.theplatform.com/f/h9dtGB/punlNGjMlc1F?fields=id&byContent=byReleases%3DbyId%253D' + clip_id,
123 clip_id)['entries'][0]['id'].split('/')[-1]
124 return self.url_result('cbcplayer:%s' % media_id, 'CBCPlayer', media_id)
125
126 def _real_extract(self, url):
127 display_id = self._match_id(url)
128 webpage = self._download_webpage(url, display_id)
129 entries = [
130 self._extract_player_init(player_init, display_id)
131 for player_init in re.findall(r'CBC\.APP\.Caffeine\.initInstance\(({.+?})\);', webpage)]
132 entries.extend([
133 self.url_result('cbcplayer:%s' % media_id, 'CBCPlayer', media_id)
134 for media_id in re.findall(r'<iframe[^>]+src="[^"]+?mediaId=(\d+)"', webpage)])
135 return self.playlist_result(
136 entries, display_id,
137 self._og_search_title(webpage, fatal=False),
138 self._og_search_description(webpage))
139
140
141 class CBCPlayerIE(InfoExtractor):
142 IE_NAME = 'cbc.ca:player'
143 _VALID_URL = r'(?:cbcplayer:|https?://(?:www\.)?cbc\.ca/(?:player/play/|i/caffeine/syndicate/\?mediaId=))(?P<id>\d+)'
144 _TESTS = [{
145 'url': 'http://www.cbc.ca/player/play/2683190193',
146 'md5': '64d25f841ddf4ddb28a235338af32e2c',
147 'info_dict': {
148 'id': '2683190193',
149 'ext': 'mp4',
150 'title': 'Gerry Runs a Sweat Shop',
151 'description': 'md5:b457e1c01e8ff408d9d801c1c2cd29b0',
152 'timestamp': 1455071400,
153 'upload_date': '20160210',
154 'uploader': 'CBCC-NEW',
155 },
156 'skip': 'Geo-restricted to Canada',
157 }, {
158 # Redirected from http://www.cbc.ca/player/AudioMobile/All%20in%20a%20Weekend%20Montreal/ID/2657632011/
159 'url': 'http://www.cbc.ca/player/play/2657631896',
160 'md5': 'e5e708c34ae6fca156aafe17c43e8b75',
161 'info_dict': {
162 'id': '2657631896',
163 'ext': 'mp3',
164 'title': 'CBC Montreal is organizing its first ever community hackathon!',
165 'description': 'The modern technology we tend to depend on so heavily, is never without it\'s share of hiccups and headaches. Next weekend - CBC Montreal will be getting members of the public for its first Hackathon.',
166 'timestamp': 1425704400,
167 'upload_date': '20150307',
168 'uploader': 'CBCC-NEW',
169 },
170 }, {
171 'url': 'http://www.cbc.ca/player/play/2164402062',
172 'md5': '33fcd8f6719b9dd60a5e73adcb83b9f6',
173 'info_dict': {
174 'id': '2164402062',
175 'ext': 'mp4',
176 'title': 'Cancer survivor four times over',
177 'description': 'Tim Mayer has beaten three different forms of cancer four times in five years.',
178 'timestamp': 1320410746,
179 'upload_date': '20111104',
180 'uploader': 'CBCC-NEW',
181 },
182 }]
183
184 def _real_extract(self, url):
185 video_id = self._match_id(url)
186 return {
187 '_type': 'url_transparent',
188 'ie_key': 'ThePlatform',
189 'url': smuggle_url(
190 'http://link.theplatform.com/s/ExhSPC/media/guid/2655402169/%s?mbr=true&formats=MPEG4,FLV,MP3' % video_id, {
191 'force_smil_url': True
192 }),
193 'id': video_id,
194 }
195
196
197 class CBCWatchBaseIE(InfoExtractor):
198 _device_id = None
199 _device_token = None
200 _API_BASE_URL = 'https://api-cbc.cloud.clearleap.com/cloffice/client/'
201 _NS_MAP = {
202 'media': 'http://search.yahoo.com/mrss/',
203 'clearleap': 'http://www.clearleap.com/namespace/clearleap/1.0/',
204 }
205 _GEO_COUNTRIES = ['CA']
206
207 def _call_api(self, path, video_id):
208 url = path if path.startswith('http') else self._API_BASE_URL + path
209 result = self._download_xml(url, video_id, headers={
210 'X-Clearleap-DeviceId': self._device_id,
211 'X-Clearleap-DeviceToken': self._device_token,
212 })
213 error_message = xpath_text(result, 'userMessage') or xpath_text(result, 'systemMessage')
214 if error_message:
215 raise ExtractorError('%s said: %s' % (self.IE_NAME, error_message))
216 return result
217
218 def _real_initialize(self):
219 if not self._device_id or not self._device_token:
220 device = self._downloader.cache.load('cbcwatch', 'device') or {}
221 self._device_id, self._device_token = device.get('id'), device.get('token')
222 if not self._device_id or not self._device_token:
223 result = self._download_xml(
224 self._API_BASE_URL + 'device/register',
225 None, data=b'<device><type>web</type></device>')
226 self._device_id = xpath_text(result, 'deviceId', fatal=True)
227 self._device_token = xpath_text(result, 'deviceToken', fatal=True)
228 self._downloader.cache.store(
229 'cbcwatch', 'device', {
230 'id': self._device_id,
231 'token': self._device_token,
232 })
233
234 def _parse_rss_feed(self, rss):
235 channel = xpath_element(rss, 'channel', fatal=True)
236
237 def _add_ns(path):
238 return xpath_with_ns(path, self._NS_MAP)
239
240 entries = []
241 for item in channel.findall('item'):
242 guid = xpath_text(item, 'guid', fatal=True)
243 title = xpath_text(item, 'title', fatal=True)
244
245 media_group = xpath_element(item, _add_ns('media:group'), fatal=True)
246 content = xpath_element(media_group, _add_ns('media:content'), fatal=True)
247 content_url = content.attrib['url']
248
249 thumbnails = []
250 for thumbnail in media_group.findall(_add_ns('media:thumbnail')):
251 thumbnail_url = thumbnail.get('url')
252 if not thumbnail_url:
253 continue
254 thumbnails.append({
255 'id': thumbnail.get('profile'),
256 'url': thumbnail_url,
257 'width': int_or_none(thumbnail.get('width')),
258 'height': int_or_none(thumbnail.get('height')),
259 })
260
261 timestamp = None
262 release_date = find_xpath_attr(
263 item, _add_ns('media:credit'), 'role', 'releaseDate')
264 if release_date is not None:
265 timestamp = parse_iso8601(release_date.text)
266
267 entries.append({
268 '_type': 'url_transparent',
269 'url': content_url,
270 'id': guid,
271 'title': title,
272 'description': xpath_text(item, 'description'),
273 'timestamp': timestamp,
274 'duration': int_or_none(content.get('duration')),
275 'age_limit': parse_age_limit(xpath_text(item, _add_ns('media:rating'))),
276 'episode': xpath_text(item, _add_ns('clearleap:episode')),
277 'episode_number': int_or_none(xpath_text(item, _add_ns('clearleap:episodeInSeason'))),
278 'series': xpath_text(item, _add_ns('clearleap:series')),
279 'season_number': int_or_none(xpath_text(item, _add_ns('clearleap:season'))),
280 'thumbnails': thumbnails,
281 'ie_key': 'CBCWatchVideo',
282 })
283
284 return self.playlist_result(
285 entries, xpath_text(channel, 'guid'),
286 xpath_text(channel, 'title'),
287 xpath_text(channel, 'description'))
288
289
290 class CBCWatchVideoIE(CBCWatchBaseIE):
291 IE_NAME = 'cbc.ca:watch:video'
292 _VALID_URL = r'https?://api-cbc\.cloud\.clearleap\.com/cloffice/client/web/play/?\?.*?\bcontentId=(?P<id>[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12})'
293 _TEST = {
294 # geo-restricted to Canada, bypassable
295 'url': 'https://api-cbc.cloud.clearleap.com/cloffice/client/web/play/?contentId=3c84472a-1eea-4dee-9267-2655d5055dcf&categoryId=ebc258f5-ee40-4cca-b66b-ba6bd55b7235',
296 'only_matching': True,
297 }
298
299 def _real_extract(self, url):
300 video_id = self._match_id(url)
301 result = self._call_api(url, video_id)
302
303 m3u8_url = xpath_text(result, 'url', fatal=True)
304 formats = self._extract_m3u8_formats(re.sub(r'/([^/]+)/[^/?]+\.m3u8', r'/\1/\1.m3u8', m3u8_url), video_id, 'mp4', fatal=False)
305 if len(formats) < 2:
306 formats = self._extract_m3u8_formats(m3u8_url, video_id, 'mp4')
307 for f in formats:
308 format_id = f.get('format_id')
309 if format_id.startswith('AAC'):
310 f['acodec'] = 'aac'
311 elif format_id.startswith('AC3'):
312 f['acodec'] = 'ac-3'
313 self._sort_formats(formats)
314
315 info = {
316 'id': video_id,
317 'title': video_id,
318 'formats': formats,
319 }
320
321 rss = xpath_element(result, 'rss')
322 if rss:
323 info.update(self._parse_rss_feed(rss)['entries'][0])
324 del info['url']
325 del info['_type']
326 del info['ie_key']
327 return info
328
329
330 class CBCWatchIE(CBCWatchBaseIE):
331 IE_NAME = 'cbc.ca:watch'
332 _VALID_URL = r'https?://watch\.cbc\.ca/(?:[^/]+/)+(?P<id>[0-9a-f-]+)'
333 _TESTS = [{
334 # geo-restricted to Canada, bypassable
335 'url': 'http://watch.cbc.ca/doc-zone/season-6/customer-disservice/38e815a-009e3ab12e4',
336 'info_dict': {
337 'id': '9673749a-5e77-484c-8b62-a1092a6b5168',
338 'ext': 'mp4',
339 'title': 'Customer (Dis)Service',
340 'description': 'md5:8bdd6913a0fe03d4b2a17ebe169c7c87',
341 'upload_date': '20160219',
342 'timestamp': 1455840000,
343 },
344 'params': {
345 # m3u8 download
346 'skip_download': True,
347 'format': 'bestvideo',
348 },
349 }, {
350 # geo-restricted to Canada, bypassable
351 'url': 'http://watch.cbc.ca/arthur/all/1ed4b385-cd84-49cf-95f0-80f004680057',
352 'info_dict': {
353 'id': '1ed4b385-cd84-49cf-95f0-80f004680057',
354 'title': 'Arthur',
355 'description': 'Arthur, the sweetest 8-year-old aardvark, and his pals solve all kinds of problems with humour, kindness and teamwork.',
356 },
357 'playlist_mincount': 30,
358 }]
359
360 def _real_extract(self, url):
361 video_id = self._match_id(url)
362 rss = self._call_api('web/browse/' + video_id, video_id)
363 return self._parse_rss_feed(rss)
364
365
366 class CBCOlympicsIE(InfoExtractor):
367 IE_NAME = 'cbc.ca:olympics'
368 _VALID_URL = r'https?://olympics\.cbc\.ca/video/[^/]+/(?P<id>[^/?#]+)'
369 _TESTS = [{
370 'url': 'https://olympics.cbc.ca/video/whats-on-tv/olympic-morning-featuring-the-opening-ceremony/',
371 'only_matching': True,
372 }]
373
374 def _real_extract(self, url):
375 display_id = self._match_id(url)
376 webpage = self._download_webpage(url, display_id)
377 video_id = self._hidden_inputs(webpage)['videoId']
378 video_doc = self._download_xml(
379 'https://olympics.cbc.ca/videodata/%s.xml' % video_id, video_id)
380 title = xpath_text(video_doc, 'title', fatal=True)
381 is_live = xpath_text(video_doc, 'kind') == 'Live'
382 if is_live:
383 title = self._live_title(title)
384
385 formats = []
386 for video_source in video_doc.findall('videoSources/videoSource'):
387 uri = xpath_text(video_source, 'uri')
388 if not uri:
389 continue
390 tokenize = self._download_json(
391 'https://olympics.cbc.ca/api/api-akamai/tokenize',
392 video_id, data=json.dumps({
393 'VideoSource': uri,
394 }).encode(), headers={
395 'Content-Type': 'application/json',
396 'Referer': url,
397 # d3.VideoPlayer._init in https://olympics.cbc.ca/components/script/base.js
398 'Cookie': '_dvp=TK:C0ObxjerU', # AKAMAI CDN cookie
399 }, fatal=False)
400 if not tokenize:
401 continue
402 content_url = tokenize['ContentUrl']
403 video_source_format = video_source.get('format')
404 if video_source_format == 'IIS':
405 formats.extend(self._extract_ism_formats(
406 content_url, video_id, ism_id=video_source_format, fatal=False))
407 else:
408 formats.extend(self._extract_m3u8_formats(
409 content_url, video_id, 'mp4',
410 'm3u8' if is_live else 'm3u8_native',
411 m3u8_id=video_source_format, fatal=False))
412 self._sort_formats(formats)
413
414 return {
415 'id': video_id,
416 'display_id': display_id,
417 'title': title,
418 'description': xpath_text(video_doc, 'description'),
419 'thumbnail': xpath_text(video_doc, 'thumbnailUrl'),
420 'duration': parse_duration(xpath_text(video_doc, 'duration')),
421 'formats': formats,
422 'is_live': is_live,
423 }