]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/cbc.py
[ie/orf:on] Improve extraction (#9677)
[yt-dlp.git] / yt_dlp / extractor / cbc.py
1 import base64
2 import json
3 import re
4 import time
5 import urllib.parse
6 import xml.etree.ElementTree
7
8 from .common import InfoExtractor
9 from ..compat import (
10 compat_str,
11 )
12 from ..utils import (
13 ExtractorError,
14 int_or_none,
15 join_nonempty,
16 js_to_json,
17 orderedSet,
18 parse_iso8601,
19 smuggle_url,
20 strip_or_none,
21 traverse_obj,
22 try_get,
23 )
24
25
26 class CBCIE(InfoExtractor):
27 IE_NAME = 'cbc.ca'
28 _VALID_URL = r'https?://(?:www\.)?cbc\.ca/(?!player/)(?:[^/]+/)+(?P<id>[^/?#]+)'
29 _TESTS = [{
30 # with mediaId
31 'url': 'http://www.cbc.ca/22minutes/videos/clips-season-23/don-cherry-play-offs',
32 'md5': '97e24d09672fc4cf56256d6faa6c25bc',
33 'info_dict': {
34 'id': '2682904050',
35 'ext': 'mp4',
36 'title': 'Don Cherry – All-Stars',
37 'description': 'Don Cherry has a bee in his bonnet about AHL player John Scott because that guy’s got heart.',
38 'timestamp': 1454463000,
39 'upload_date': '20160203',
40 'uploader': 'CBCC-NEW',
41 },
42 'skip': 'Geo-restricted to Canada',
43 }, {
44 # with clipId, feed available via tpfeed.cbc.ca and feed.theplatform.com
45 'url': 'http://www.cbc.ca/22minutes/videos/22-minutes-update/22-minutes-update-episode-4',
46 'md5': '162adfa070274b144f4fdc3c3b8207db',
47 'info_dict': {
48 'id': '2414435309',
49 'ext': 'mp4',
50 'title': '22 Minutes Update: What Not To Wear Quebec',
51 'description': "This week's latest Canadian top political story is What Not To Wear Quebec.",
52 'upload_date': '20131025',
53 'uploader': 'CBCC-NEW',
54 'timestamp': 1382717907,
55 },
56 'skip': 'No longer available',
57 }, {
58 # with clipId, feed only available via tpfeed.cbc.ca
59 'url': 'http://www.cbc.ca/archives/entry/1978-robin-williams-freestyles-on-90-minutes-live',
60 'md5': '0274a90b51a9b4971fe005c63f592f12',
61 'info_dict': {
62 'id': '2487345465',
63 'ext': 'mp4',
64 'title': 'Robin Williams freestyles on 90 Minutes Live',
65 'description': 'Wacky American comedian Robin Williams shows off his infamous "freestyle" comedic talents while being interviewed on CBC\'s 90 Minutes Live.',
66 'upload_date': '19780210',
67 'uploader': 'CBCC-NEW',
68 'timestamp': 255977160,
69 },
70 'skip': '404 Not Found',
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', # FIXME
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 title = (self._og_search_title(webpage, default=None)
133 or self._html_search_meta('twitter:title', webpage, 'title', default=None)
134 or self._html_extract_title(webpage))
135 entries = [
136 self._extract_player_init(player_init, display_id)
137 for player_init in re.findall(r'CBC\.APP\.Caffeine\.initInstance\(({.+?})\);', webpage)]
138 media_ids = []
139 for media_id_re in (
140 r'<iframe[^>]+src="[^"]+?mediaId=(\d+)"',
141 r'<div[^>]+\bid=["\']player-(\d+)',
142 r'guid["\']\s*:\s*["\'](\d+)'):
143 media_ids.extend(re.findall(media_id_re, webpage))
144 entries.extend([
145 self.url_result('cbcplayer:%s' % media_id, 'CBCPlayer', media_id)
146 for media_id in orderedSet(media_ids)])
147 return self.playlist_result(
148 entries, display_id, strip_or_none(title),
149 self._og_search_description(webpage))
150
151
152 class CBCPlayerIE(InfoExtractor):
153 IE_NAME = 'cbc.ca:player'
154 _VALID_URL = r'(?:cbcplayer:|https?://(?:www\.)?cbc\.ca/(?:player/play/(?:video/)?|i/caffeine/syndicate/\?mediaId=))(?P<id>(?:\d\.)?\d+)'
155 _TESTS = [{
156 'url': 'http://www.cbc.ca/player/play/2683190193',
157 'md5': '64d25f841ddf4ddb28a235338af32e2c',
158 'info_dict': {
159 'id': '2683190193',
160 'ext': 'mp4',
161 'title': 'Gerry Runs a Sweat Shop',
162 'description': 'md5:b457e1c01e8ff408d9d801c1c2cd29b0',
163 'timestamp': 1455071400,
164 'upload_date': '20160210',
165 'uploader': 'CBCC-NEW',
166 },
167 'skip': 'Geo-restricted to Canada and no longer available',
168 }, {
169 'url': 'http://www.cbc.ca/i/caffeine/syndicate/?mediaId=2657631896',
170 'md5': 'e5e708c34ae6fca156aafe17c43e8b75',
171 'info_dict': {
172 'id': '2657631896',
173 'ext': 'mp3',
174 'title': 'CBC Montreal is organizing its first ever community hackathon!',
175 'description': 'md5:dd3b692f0a139b0369943150bd1c46a9',
176 'timestamp': 1425704400,
177 'upload_date': '20150307',
178 'uploader': 'CBCC-NEW',
179 'thumbnail': 'http://thumbnails.cbc.ca/maven_legacy/thumbnails/sonali-karnick-220.jpg',
180 'chapters': [],
181 'duration': 494.811,
182 'categories': ['AudioMobile/All in a Weekend Montreal'],
183 'tags': 'count:8',
184 'location': 'Quebec',
185 'series': 'All in a Weekend Montreal',
186 'season': 'Season 2015',
187 'season_number': 2015,
188 'media_type': 'Excerpt',
189 },
190 }, {
191 'url': 'http://www.cbc.ca/i/caffeine/syndicate/?mediaId=2164402062',
192 'md5': '33fcd8f6719b9dd60a5e73adcb83b9f6',
193 'info_dict': {
194 'id': '2164402062',
195 'ext': 'mp4',
196 'title': 'Cancer survivor four times over',
197 'description': 'Tim Mayer has beaten three different forms of cancer four times in five years.',
198 'timestamp': 1320410746,
199 'upload_date': '20111104',
200 'uploader': 'CBCC-NEW',
201 'thumbnail': 'https://thumbnails.cbc.ca/maven_legacy/thumbnails/277/67/cancer_852x480_2164412612.jpg',
202 'chapters': [],
203 'duration': 186.867,
204 'series': 'CBC News: Windsor at 6:00',
205 'categories': ['News/Canada/Windsor'],
206 'location': 'Windsor',
207 'tags': ['cancer'],
208 'creators': ['Allison Johnson'],
209 'media_type': 'Excerpt',
210 },
211 }, {
212 # Redirected from http://www.cbc.ca/player/AudioMobile/All%20in%20a%20Weekend%20Montreal/ID/2657632011/
213 'url': 'https://www.cbc.ca/player/play/1.2985700',
214 'md5': 'e5e708c34ae6fca156aafe17c43e8b75',
215 'info_dict': {
216 'id': '2657631896',
217 'ext': 'mp3',
218 'title': 'CBC Montreal is organizing its first ever community hackathon!',
219 '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.',
220 'timestamp': 1425704400,
221 'upload_date': '20150307',
222 'uploader': 'CBCC-NEW',
223 'thumbnail': 'http://thumbnails.cbc.ca/maven_legacy/thumbnails/sonali-karnick-220.jpg',
224 'chapters': [],
225 'duration': 494.811,
226 'categories': ['AudioMobile/All in a Weekend Montreal'],
227 'tags': 'count:8',
228 'location': 'Quebec',
229 'series': 'All in a Weekend Montreal',
230 'season': 'Season 2015',
231 'season_number': 2015,
232 'media_type': 'Excerpt',
233 },
234 }, {
235 'url': 'https://www.cbc.ca/player/play/1.1711287',
236 'md5': '33fcd8f6719b9dd60a5e73adcb83b9f6',
237 'info_dict': {
238 'id': '2164402062',
239 'ext': 'mp4',
240 'title': 'Cancer survivor four times over',
241 'description': 'Tim Mayer has beaten three different forms of cancer four times in five years.',
242 'timestamp': 1320410746,
243 'upload_date': '20111104',
244 'uploader': 'CBCC-NEW',
245 'thumbnail': 'https://thumbnails.cbc.ca/maven_legacy/thumbnails/277/67/cancer_852x480_2164412612.jpg',
246 'chapters': [],
247 'duration': 186.867,
248 'series': 'CBC News: Windsor at 6:00',
249 'categories': ['News/Canada/Windsor'],
250 'location': 'Windsor',
251 'tags': ['cancer'],
252 'creators': ['Allison Johnson'],
253 'media_type': 'Excerpt',
254 },
255 }, {
256 # Has subtitles
257 # These broadcasts expire after ~1 month, can find new test URL here:
258 # https://www.cbc.ca/player/news/TV%20Shows/The%20National/Latest%20Broadcast
259 'url': 'https://www.cbc.ca/player/play/1.7159484',
260 'md5': '6ed6cd0fc2ef568d2297ba68a763d455',
261 'info_dict': {
262 'id': '2324213316001',
263 'ext': 'mp4',
264 'title': 'The National | School boards sue social media giants',
265 'description': 'md5:4b4db69322fa32186c3ce426da07402c',
266 'timestamp': 1711681200,
267 'duration': 2743.400,
268 'subtitles': {'eng': [{'ext': 'vtt', 'protocol': 'm3u8_native'}]},
269 'thumbnail': 'https://thumbnails.cbc.ca/maven_legacy/thumbnails/607/559/thumbnail.jpeg',
270 'uploader': 'CBCC-NEW',
271 'chapters': 'count:5',
272 'upload_date': '20240329',
273 'categories': 'count:4',
274 'series': 'The National - Full Show',
275 'tags': 'count:1',
276 'creators': ['News'],
277 'location': 'Canada',
278 'media_type': 'Full Program',
279 },
280 }, {
281 'url': 'https://www.cbc.ca/player/play/video/1.7194274',
282 'md5': '188b96cf6bdcb2540e178a6caa957128',
283 'info_dict': {
284 'id': '2334524995812',
285 'ext': 'mp4',
286 'title': '#TheMoment a rare white spirit moose was spotted in Alberta',
287 'description': 'md5:18ae269a2d0265c5b0bbe4b2e1ac61a3',
288 'timestamp': 1714788791,
289 'duration': 77.678,
290 'subtitles': {'eng': [{'ext': 'vtt', 'protocol': 'm3u8_native'}]},
291 'thumbnail': 'https://thumbnails.cbc.ca/maven_legacy/thumbnails/201/543/THE_MOMENT.jpg',
292 'uploader': 'CBCC-NEW',
293 'chapters': 'count:0',
294 'upload_date': '20240504',
295 'categories': 'count:3',
296 'series': 'The National',
297 'tags': 'count:15',
298 'creators': ['encoder'],
299 'location': 'Canada',
300 'media_type': 'Excerpt',
301 },
302 }, {
303 'url': 'cbcplayer:1.7159484',
304 'only_matching': True,
305 }, {
306 'url': 'cbcplayer:2164402062',
307 'only_matching': True,
308 }, {
309 'url': 'http://www.cbc.ca/player/play/2657631896',
310 'only_matching': True,
311 }]
312
313 def _real_extract(self, url):
314 video_id = self._match_id(url)
315 if '.' in video_id:
316 webpage = self._download_webpage(f'https://www.cbc.ca/player/play/{video_id}', video_id)
317 video_id = self._search_json(
318 r'window\.__INITIAL_STATE__\s*=', webpage,
319 'initial state', video_id)['video']['currentClip']['mediaId']
320
321 return {
322 '_type': 'url_transparent',
323 'ie_key': 'ThePlatform',
324 'url': smuggle_url(
325 'http://link.theplatform.com/s/ExhSPC/media/guid/2655402169/%s?mbr=true&formats=MPEG4,FLV,MP3' % video_id, {
326 'force_smil_url': True
327 }),
328 'id': video_id,
329 '_format_sort_fields': ('res', 'proto') # Prioritize direct http formats over HLS
330 }
331
332
333 class CBCPlayerPlaylistIE(InfoExtractor):
334 IE_NAME = 'cbc.ca:player:playlist'
335 _VALID_URL = r'https?://(?:www\.)?cbc\.ca/(?:player/)(?!play/)(?P<id>[^?#]+)'
336 _TESTS = [{
337 'url': 'https://www.cbc.ca/player/news/TV%20Shows/The%20National/Latest%20Broadcast',
338 'playlist_mincount': 25,
339 'info_dict': {
340 'id': 'news/tv shows/the national/latest broadcast',
341 }
342 }, {
343 'url': 'https://www.cbc.ca/player/news/Canada/North',
344 'playlist_mincount': 25,
345 'info_dict': {
346 'id': 'news/canada/north',
347 }
348 }]
349
350 def _real_extract(self, url):
351 playlist_id = urllib.parse.unquote(self._match_id(url)).lower()
352 webpage = self._download_webpage(url, playlist_id)
353 json_content = self._search_json(
354 r'window\.__INITIAL_STATE__\s*=', webpage, 'initial state', playlist_id)
355
356 def entries():
357 for video_id in traverse_obj(json_content, (
358 'video', 'clipsByCategory', lambda k, _: k.lower() == playlist_id, 'items', ..., 'id'
359 )):
360 yield self.url_result(f'https://www.cbc.ca/player/play/{video_id}', CBCPlayerIE)
361
362 return self.playlist_result(entries(), playlist_id)
363
364
365 class CBCGemIE(InfoExtractor):
366 IE_NAME = 'gem.cbc.ca'
367 _VALID_URL = r'https?://gem\.cbc\.ca/(?:media/)?(?P<id>[0-9a-z-]+/s[0-9]+[a-z][0-9]+)'
368 _TESTS = [{
369 # This is a normal, public, TV show video
370 'url': 'https://gem.cbc.ca/media/schitts-creek/s06e01',
371 'md5': '93dbb31c74a8e45b378cf13bd3f6f11e',
372 'info_dict': {
373 'id': 'schitts-creek/s06e01',
374 'ext': 'mp4',
375 'title': 'Smoke Signals',
376 'description': 'md5:929868d20021c924020641769eb3e7f1',
377 'thumbnail': 'https://images.radio-canada.ca/v1/synps-cbc/episode/perso/cbc_schitts_creek_season_06e01_thumbnail_v01.jpg?im=Resize=(Size)',
378 'duration': 1314,
379 'categories': ['comedy'],
380 'series': 'Schitt\'s Creek',
381 'season': 'Season 6',
382 'season_number': 6,
383 'episode': 'Smoke Signals',
384 'episode_number': 1,
385 'episode_id': 'schitts-creek/s06e01',
386 },
387 'params': {'format': 'bv'},
388 'skip': 'Geo-restricted to Canada',
389 }, {
390 # This video requires an account in the browser, but works fine in yt-dlp
391 'url': 'https://gem.cbc.ca/media/schitts-creek/s01e01',
392 'md5': '297a9600f554f2258aed01514226a697',
393 'info_dict': {
394 'id': 'schitts-creek/s01e01',
395 'ext': 'mp4',
396 'title': 'The Cup Runneth Over',
397 'description': 'md5:9bca14ea49ab808097530eb05a29e797',
398 'thumbnail': 'https://images.radio-canada.ca/v1/synps-cbc/episode/perso/cbc_schitts_creek_season_01e01_thumbnail_v01.jpg?im=Resize=(Size)',
399 'series': 'Schitt\'s Creek',
400 'season_number': 1,
401 'season': 'Season 1',
402 'episode_number': 1,
403 'episode': 'The Cup Runneth Over',
404 'episode_id': 'schitts-creek/s01e01',
405 'duration': 1309,
406 'categories': ['comedy'],
407 },
408 'params': {'format': 'bv'},
409 'skip': 'Geo-restricted to Canada',
410 }, {
411 'url': 'https://gem.cbc.ca/nadiyas-family-favourites/s01e01',
412 'only_matching': True,
413 }]
414
415 _GEO_COUNTRIES = ['CA']
416 _TOKEN_API_KEY = '3f4beddd-2061-49b0-ae80-6f1f2ed65b37'
417 _NETRC_MACHINE = 'cbcgem'
418 _claims_token = None
419
420 def _new_claims_token(self, email, password):
421 data = json.dumps({
422 'email': email,
423 'password': password,
424 }).encode()
425 headers = {'content-type': 'application/json'}
426 query = {'apikey': self._TOKEN_API_KEY}
427 resp = self._download_json('https://api.loginradius.com/identity/v2/auth/login',
428 None, data=data, headers=headers, query=query)
429 access_token = resp['access_token']
430
431 query = {
432 'access_token': access_token,
433 'apikey': self._TOKEN_API_KEY,
434 'jwtapp': 'jwt',
435 }
436 resp = self._download_json('https://cloud-api.loginradius.com/sso/jwt/api/token',
437 None, headers=headers, query=query)
438 sig = resp['signature']
439
440 data = json.dumps({'jwt': sig}).encode()
441 headers = {'content-type': 'application/json', 'ott-device-type': 'web'}
442 resp = self._download_json('https://services.radio-canada.ca/ott/cbc-api/v2/token',
443 None, data=data, headers=headers, expected_status=426)
444 cbc_access_token = resp['accessToken']
445
446 headers = {'content-type': 'application/json', 'ott-device-type': 'web', 'ott-access-token': cbc_access_token}
447 resp = self._download_json('https://services.radio-canada.ca/ott/cbc-api/v2/profile',
448 None, headers=headers, expected_status=426)
449 return resp['claimsToken']
450
451 def _get_claims_token_expiry(self):
452 # Token is a JWT
453 # JWT is decoded here and 'exp' field is extracted
454 # It is a Unix timestamp for when the token expires
455 b64_data = self._claims_token.split('.')[1]
456 data = base64.urlsafe_b64decode(b64_data + "==")
457 return json.loads(data)['exp']
458
459 def claims_token_expired(self):
460 exp = self._get_claims_token_expiry()
461 if exp - time.time() < 10:
462 # It will expire in less than 10 seconds, or has already expired
463 return True
464 return False
465
466 def claims_token_valid(self):
467 return self._claims_token is not None and not self.claims_token_expired()
468
469 def _get_claims_token(self, email, password):
470 if not self.claims_token_valid():
471 self._claims_token = self._new_claims_token(email, password)
472 self.cache.store(self._NETRC_MACHINE, 'claims_token', self._claims_token)
473 return self._claims_token
474
475 def _real_initialize(self):
476 if self.claims_token_valid():
477 return
478 self._claims_token = self.cache.load(self._NETRC_MACHINE, 'claims_token')
479
480 def _find_secret_formats(self, formats, video_id):
481 """ Find a valid video url and convert it to the secret variant """
482 base_format = next((f for f in formats if f.get('vcodec') != 'none'), None)
483 if not base_format:
484 return
485
486 base_url = re.sub(r'(Manifest\(.*?),filter=[\w-]+(.*?\))', r'\1\2', base_format['url'])
487 url = re.sub(r'(Manifest\(.*?),format=[\w-]+(.*?\))', r'\1\2', base_url)
488
489 secret_xml = self._download_xml(url, video_id, note='Downloading secret XML', fatal=False)
490 if not isinstance(secret_xml, xml.etree.ElementTree.Element):
491 return
492
493 for child in secret_xml:
494 if child.attrib.get('Type') != 'video':
495 continue
496 for video_quality in child:
497 bitrate = int_or_none(video_quality.attrib.get('Bitrate'))
498 if not bitrate or 'Index' not in video_quality.attrib:
499 continue
500 height = int_or_none(video_quality.attrib.get('MaxHeight'))
501
502 yield {
503 **base_format,
504 'format_id': join_nonempty('sec', height),
505 # Note: \g<1> is necessary instead of \1 since bitrate is a number
506 'url': re.sub(r'(QualityLevels\()\d+(\))', fr'\g<1>{bitrate}\2', base_url),
507 'width': int_or_none(video_quality.attrib.get('MaxWidth')),
508 'tbr': bitrate / 1000.0,
509 'height': height,
510 }
511
512 def _real_extract(self, url):
513 video_id = self._match_id(url)
514 video_info = self._download_json(
515 f'https://services.radio-canada.ca/ott/cbc-api/v2/assets/{video_id}',
516 video_id, expected_status=426)
517
518 email, password = self._get_login_info()
519 if email and password:
520 claims_token = self._get_claims_token(email, password)
521 headers = {'x-claims-token': claims_token}
522 else:
523 headers = {}
524 m3u8_info = self._download_json(video_info['playSession']['url'], video_id, headers=headers)
525 m3u8_url = m3u8_info.get('url')
526
527 if m3u8_info.get('errorCode') == 1:
528 self.raise_geo_restricted(countries=['CA'])
529 elif m3u8_info.get('errorCode') == 35:
530 self.raise_login_required(method='password')
531 elif m3u8_info.get('errorCode') != 0:
532 raise ExtractorError(f'{self.IE_NAME} said: {m3u8_info.get("errorCode")} - {m3u8_info.get("message")}')
533
534 formats = self._extract_m3u8_formats(m3u8_url, video_id, m3u8_id='hls')
535 self._remove_duplicate_formats(formats)
536 formats.extend(self._find_secret_formats(formats, video_id))
537
538 for format in formats:
539 if format.get('vcodec') == 'none':
540 if format.get('ext') is None:
541 format['ext'] = 'm4a'
542 if format.get('acodec') is None:
543 format['acodec'] = 'mp4a.40.2'
544
545 # Put described audio at the beginning of the list, so that it
546 # isn't chosen by default, as most people won't want it.
547 if 'descriptive' in format['format_id'].lower():
548 format['preference'] = -2
549
550 return {
551 'id': video_id,
552 'title': video_info['title'],
553 'description': video_info.get('description'),
554 'thumbnail': video_info.get('image'),
555 'series': video_info.get('series'),
556 'season_number': video_info.get('season'),
557 'season': f'Season {video_info.get("season")}',
558 'episode_number': video_info.get('episode'),
559 'episode': video_info.get('title'),
560 'episode_id': video_id,
561 'duration': video_info.get('duration'),
562 'categories': [video_info.get('category')],
563 'formats': formats,
564 'release_timestamp': video_info.get('airDate'),
565 'timestamp': video_info.get('availableDate'),
566 }
567
568
569 class CBCGemPlaylistIE(InfoExtractor):
570 IE_NAME = 'gem.cbc.ca:playlist'
571 _VALID_URL = r'https?://gem\.cbc\.ca/(?:media/)?(?P<id>(?P<show>[0-9a-z-]+)/s(?P<season>[0-9]+))/?(?:[?#]|$)'
572 _TESTS = [{
573 # TV show playlist, all public videos
574 'url': 'https://gem.cbc.ca/media/schitts-creek/s06',
575 'playlist_count': 16,
576 'info_dict': {
577 'id': 'schitts-creek/s06',
578 'title': 'Season 6',
579 'description': 'md5:6a92104a56cbeb5818cc47884d4326a2',
580 'series': 'Schitt\'s Creek',
581 'season_number': 6,
582 'season': 'Season 6',
583 'thumbnail': 'https://images.radio-canada.ca/v1/synps-cbc/season/perso/cbc_schitts_creek_season_06_carousel_v03.jpg?impolicy=ott&im=Resize=(_Size_)&quality=75',
584 },
585 }, {
586 'url': 'https://gem.cbc.ca/schitts-creek/s06',
587 'only_matching': True,
588 }]
589 _API_BASE = 'https://services.radio-canada.ca/ott/cbc-api/v2/shows/'
590
591 def _real_extract(self, url):
592 match = self._match_valid_url(url)
593 season_id = match.group('id')
594 show = match.group('show')
595 show_info = self._download_json(self._API_BASE + show, season_id, expected_status=426)
596 season = int(match.group('season'))
597
598 season_info = next((s for s in show_info['seasons'] if s.get('season') == season), None)
599
600 if season_info is None:
601 raise ExtractorError(f'Couldn\'t find season {season} of {show}')
602
603 episodes = []
604 for episode in season_info['assets']:
605 episodes.append({
606 '_type': 'url_transparent',
607 'ie_key': 'CBCGem',
608 'url': 'https://gem.cbc.ca/media/' + episode['id'],
609 'id': episode['id'],
610 'title': episode.get('title'),
611 'description': episode.get('description'),
612 'thumbnail': episode.get('image'),
613 'series': episode.get('series'),
614 'season_number': episode.get('season'),
615 'season': season_info['title'],
616 'season_id': season_info.get('id'),
617 'episode_number': episode.get('episode'),
618 'episode': episode.get('title'),
619 'episode_id': episode['id'],
620 'duration': episode.get('duration'),
621 'categories': [episode.get('category')],
622 })
623
624 thumbnail = None
625 tn_uri = season_info.get('image')
626 # the-national was observed to use a "data:image/png;base64"
627 # URI for their 'image' value. The image was 1x1, and is
628 # probably just a placeholder, so it is ignored.
629 if tn_uri is not None and not tn_uri.startswith('data:'):
630 thumbnail = tn_uri
631
632 return {
633 '_type': 'playlist',
634 'entries': episodes,
635 'id': season_id,
636 'title': season_info['title'],
637 'description': season_info.get('description'),
638 'thumbnail': thumbnail,
639 'series': show_info.get('title'),
640 'season_number': season_info.get('season'),
641 'season': season_info['title'],
642 }
643
644
645 class CBCGemLiveIE(InfoExtractor):
646 IE_NAME = 'gem.cbc.ca:live'
647 _VALID_URL = r'https?://gem\.cbc\.ca/live(?:-event)?/(?P<id>\d+)'
648 _TESTS = [
649 {
650 'url': 'https://gem.cbc.ca/live/920604739687',
651 'info_dict': {
652 'title': 'Ottawa',
653 'description': 'The live TV channel and local programming from Ottawa',
654 'thumbnail': 'https://thumbnails.cbc.ca/maven_legacy/thumbnails/CBC_OTT_VMS/Live_Channel_Static_Images/Ottawa_2880x1620.jpg',
655 'is_live': True,
656 'id': 'AyqZwxRqh8EH',
657 'ext': 'mp4',
658 'timestamp': 1492106160,
659 'upload_date': '20170413',
660 'uploader': 'CBCC-NEW',
661 },
662 'skip': 'Live might have ended',
663 },
664 {
665 'url': 'https://gem.cbc.ca/live/44',
666 'info_dict': {
667 'id': '44',
668 'ext': 'mp4',
669 'is_live': True,
670 'title': r're:^Ottawa [0-9\-: ]+',
671 'description': 'The live TV channel and local programming from Ottawa',
672 'live_status': 'is_live',
673 'thumbnail': r're:https://images.gem.cbc.ca/v1/cbc-gem/live/.*'
674 },
675 'params': {'skip_download': True},
676 'skip': 'Live might have ended',
677 },
678 {
679 'url': 'https://gem.cbc.ca/live-event/10835',
680 'info_dict': {
681 'id': '10835',
682 'ext': 'mp4',
683 'is_live': True,
684 'title': r're:^The National \| Biden’s trip wraps up, Paltrow testifies, Bird flu [0-9\-: ]+',
685 'description': 'March 24, 2023 | President Biden’s Ottawa visit ends with big pledges from both countries. Plus, Gwyneth Paltrow testifies in her ski collision trial.',
686 'live_status': 'is_live',
687 'thumbnail': r're:https://images.gem.cbc.ca/v1/cbc-gem/live/.*',
688 'timestamp': 1679706000,
689 'upload_date': '20230325',
690 },
691 'params': {'skip_download': True},
692 'skip': 'Live might have ended',
693 }
694 ]
695
696 def _real_extract(self, url):
697 video_id = self._match_id(url)
698 webpage = self._download_webpage(url, video_id)
699 video_info = self._search_nextjs_data(webpage, video_id)['props']['pageProps']['data']
700
701 # Two types of metadata JSON
702 if not video_info.get('formattedIdMedia'):
703 video_info = traverse_obj(
704 video_info, (('freeTv', ('streams', ...)), 'items', lambda _, v: v['key'] == video_id, {dict}),
705 get_all=False, default={})
706
707 video_stream_id = video_info.get('formattedIdMedia')
708 if not video_stream_id:
709 raise ExtractorError('Couldn\'t find video metadata, maybe this livestream is now offline', expected=True)
710
711 stream_data = self._download_json(
712 'https://services.radio-canada.ca/media/validation/v2/', video_id, query={
713 'appCode': 'mpx',
714 'connectionType': 'hd',
715 'deviceType': 'ipad',
716 'idMedia': video_stream_id,
717 'multibitrate': 'true',
718 'output': 'json',
719 'tech': 'hls',
720 'manifestType': 'desktop',
721 })
722
723 return {
724 'id': video_id,
725 'formats': self._extract_m3u8_formats(stream_data['url'], video_id, 'mp4', live=True),
726 'is_live': True,
727 **traverse_obj(video_info, {
728 'title': 'title',
729 'description': 'description',
730 'thumbnail': ('images', 'card', 'url'),
731 'timestamp': ('airDate', {parse_iso8601}),
732 })
733 }