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