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