]>
Commit | Line | Data |
---|---|---|
d183af3c | 1 | import base64 |
d4f14a72 | 2 | import json |
3 | import re | |
d183af3c | 4 | import time |
ed711897 | 5 | import urllib.parse |
d4f14a72 | 6 | import xml.etree.ElementTree |
8b85ac3f | 7 | |
8 | from .common import InfoExtractor | |
64f03e5b S |
9 | from ..compat import ( |
10 | compat_str, | |
64f03e5b | 11 | ) |
52f7c75c | 12 | from ..utils import ( |
7a7b1376 | 13 | ExtractorError, |
54c2521c DS |
14 | int_or_none, |
15 | join_nonempty, | |
52f7c75c | 16 | js_to_json, |
f20f6365 | 17 | orderedSet, |
7a7b1376 | 18 | parse_iso8601, |
54c2521c | 19 | smuggle_url, |
986c0b02 | 20 | strip_or_none, |
7a7b1376 | 21 | traverse_obj, |
54c2521c | 22 | try_get, |
52f7c75c | 23 | ) |
8b85ac3f | 24 | |
25 | ||
26 | class CBCIE(InfoExtractor): | |
30afe4ae | 27 | IE_NAME = 'cbc.ca' |
043dc9d3 | 28 | _VALID_URL = r'https?://(?:www\.)?cbc\.ca/(?!player/)(?:[^/]+/)+(?P<id>[^/?#]+)' |
8b85ac3f | 29 | _TESTS = [{ |
30 | # with mediaId | |
31 | 'url': 'http://www.cbc.ca/22minutes/videos/clips-season-23/don-cherry-play-offs', | |
52f7c75c | 32 | 'md5': '97e24d09672fc4cf56256d6faa6c25bc', |
8b85ac3f | 33 | 'info_dict': { |
34 | 'id': '2682904050', | |
52f7c75c | 35 | 'ext': 'mp4', |
8b85ac3f | 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.', | |
52f7c75c | 38 | 'timestamp': 1454463000, |
8b85ac3f | 39 | 'upload_date': '20160203', |
52f7c75c | 40 | 'uploader': 'CBCC-NEW', |
8b85ac3f | 41 | }, |
21ba7d09 | 42 | 'skip': 'Geo-restricted to Canada', |
8b85ac3f | 43 | }, { |
88bd486b S |
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 | }, | |
0d32e124 | 56 | 'skip': 'No longer available', |
88bd486b S |
57 | }, { |
58 | # with clipId, feed only available via tpfeed.cbc.ca | |
8b85ac3f | 59 | 'url': 'http://www.cbc.ca/archives/entry/1978-robin-williams-freestyles-on-90-minutes-live', |
64413f75 | 60 | 'md5': '0274a90b51a9b4971fe005c63f592f12', |
8b85ac3f | 61 | 'info_dict': { |
62 | 'id': '2487345465', | |
52f7c75c | 63 | 'ext': 'mp4', |
8b85ac3f | 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.', | |
52f7c75c | 66 | 'upload_date': '19780210', |
0738187f | 67 | 'uploader': 'CBCC-NEW', |
52f7c75c | 68 | 'timestamp': 255977160, |
8b85ac3f | 69 | }, |
19c90e40 | 70 | 'skip': '404 Not Found', |
8b85ac3f | 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': [{ | |
52f7c75c | 75 | 'md5': '377572d0b49c4ce0c9ad77470e0b96b4', |
8b85ac3f | 76 | 'info_dict': { |
77 | 'id': '2680832926', | |
52f7c75c | 78 | 'ext': 'mp4', |
8b85ac3f | 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.', | |
52f7c75c | 81 | 'upload_date': '20160201', |
82 | 'timestamp': 1454342820, | |
83 | 'uploader': 'CBCC-NEW', | |
8b85ac3f | 84 | }, |
85 | }, { | |
52f7c75c | 86 | 'md5': '415a0e3f586113894174dfb31aa5bb1a', |
8b85ac3f | 87 | 'info_dict': { |
88 | 'id': '2658915080', | |
52f7c75c | 89 | 'ext': 'mp4', |
8b85ac3f | 90 | 'title': 'Fly like an eagle!', |
91 | 'description': 'Eagle equipped with a mini camera flies from the world\'s tallest tower', | |
52f7c75c | 92 | 'upload_date': '20150315', |
93 | 'timestamp': 1426443984, | |
94 | 'uploader': 'CBCC-NEW', | |
8b85ac3f | 95 | }, |
96 | }], | |
21ba7d09 | 97 | 'skip': 'Geo-restricted to Canada', |
abe8cb76 S |
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': { | |
19c90e40 | 102 | 'title': 'Keep Rover active during the deep freeze with doggie pushups and other fun indoor tasks', # FIXME |
abe8cb76 | 103 | 'id': 'dog-indoor-exercise-winter-1.3928238', |
c95e2b59 | 104 | 'description': 'md5:c18552e41726ee95bd75210d1ca9194c', |
abe8cb76 S |
105 | }, |
106 | 'playlist_mincount': 6, | |
8b85ac3f | 107 | }] |
108 | ||
109 | @classmethod | |
110 | def suitable(cls, url): | |
111 | return False if CBCPlayerIE.suitable(url) else super(CBCIE, cls).suitable(url) | |
112 | ||
abe8cb76 S |
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 | ||
8b85ac3f | 129 | def _real_extract(self, url): |
130 | display_id = self._match_id(url) | |
131 | webpage = self._download_webpage(url, display_id) | |
04f3fd2c | 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)) | |
abe8cb76 S |
135 | entries = [ |
136 | self._extract_player_init(player_init, display_id) | |
137 | for player_init in re.findall(r'CBC\.APP\.Caffeine\.initInstance\(({.+?})\);', webpage)] | |
f20f6365 S |
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)) | |
abe8cb76 S |
144 | entries.extend([ |
145 | self.url_result('cbcplayer:%s' % media_id, 'CBCPlayer', media_id) | |
f20f6365 | 146 | for media_id in orderedSet(media_ids)]) |
abe8cb76 | 147 | return self.playlist_result( |
986c0b02 | 148 | entries, display_id, strip_or_none(title), |
abe8cb76 | 149 | self._og_search_description(webpage)) |
8b85ac3f | 150 | |
151 | ||
152 | class CBCPlayerIE(InfoExtractor): | |
30afe4ae | 153 | IE_NAME = 'cbc.ca:player' |
8b85ac3f | 154 | _VALID_URL = r'(?:cbcplayer:|https?://(?:www\.)?cbc\.ca/(?:player/play/|i/caffeine/syndicate/\?mediaId=))(?P<id>\d+)' |
31a70191 | 155 | _TESTS = [{ |
8b85ac3f | 156 | 'url': 'http://www.cbc.ca/player/play/2683190193', |
64413f75 | 157 | 'md5': '64d25f841ddf4ddb28a235338af32e2c', |
8b85ac3f | 158 | 'info_dict': { |
159 | 'id': '2683190193', | |
52f7c75c | 160 | 'ext': 'mp4', |
8b85ac3f | 161 | 'title': 'Gerry Runs a Sweat Shop', |
162 | 'description': 'md5:b457e1c01e8ff408d9d801c1c2cd29b0', | |
52f7c75c | 163 | 'timestamp': 1455071400, |
8b85ac3f | 164 | 'upload_date': '20160210', |
52f7c75c | 165 | 'uploader': 'CBCC-NEW', |
8b85ac3f | 166 | }, |
339c339f | 167 | 'skip': 'Geo-restricted to Canada and no longer available', |
31a70191 YCH |
168 | }, { |
169 | # Redirected from http://www.cbc.ca/player/AudioMobile/All%20in%20a%20Weekend%20Montreal/ID/2657632011/ | |
170 | 'url': 'http://www.cbc.ca/player/play/2657631896', | |
171 | 'md5': 'e5e708c34ae6fca156aafe17c43e8b75', | |
172 | 'info_dict': { | |
173 | 'id': '2657631896', | |
174 | 'ext': 'mp3', | |
175 | 'title': 'CBC Montreal is organizing its first ever community hackathon!', | |
176 | '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.', | |
177 | 'timestamp': 1425704400, | |
178 | 'upload_date': '20150307', | |
179 | 'uploader': 'CBCC-NEW', | |
339c339f | 180 | 'thumbnail': 'http://thumbnails.cbc.ca/maven_legacy/thumbnails/sonali-karnick-220.jpg', |
181 | 'chapters': [], | |
182 | 'duration': 494.811, | |
7e09c147 | 183 | 'categories': ['AudioMobile/All in a Weekend Montreal'], |
184 | 'tags': 'count:8', | |
185 | 'location': 'Quebec', | |
186 | 'series': 'All in a Weekend Montreal', | |
187 | 'season': 'Season 2015', | |
188 | 'season_number': 2015, | |
189 | 'media_type': 'Excerpt', | |
31a70191 | 190 | }, |
64413f75 | 191 | }, { |
64413f75 | 192 | 'url': 'http://www.cbc.ca/player/play/2164402062', |
c95e2b59 | 193 | 'md5': '33fcd8f6719b9dd60a5e73adcb83b9f6', |
64413f75 | 194 | 'info_dict': { |
195 | 'id': '2164402062', | |
c95e2b59 | 196 | 'ext': 'mp4', |
64413f75 | 197 | 'title': 'Cancer survivor four times over', |
198 | 'description': 'Tim Mayer has beaten three different forms of cancer four times in five years.', | |
199 | 'timestamp': 1320410746, | |
200 | 'upload_date': '20111104', | |
201 | 'uploader': 'CBCC-NEW', | |
339c339f | 202 | 'thumbnail': 'https://thumbnails.cbc.ca/maven_legacy/thumbnails/277/67/cancer_852x480_2164412612.jpg', |
203 | 'chapters': [], | |
204 | 'duration': 186.867, | |
7e09c147 | 205 | 'series': 'CBC News: Windsor at 6:00', |
206 | 'categories': ['News/Canada/Windsor'], | |
207 | 'location': 'Windsor', | |
208 | 'tags': ['cancer'], | |
209 | 'creator': 'Allison Johnson', | |
210 | 'media_type': 'Excerpt', | |
339c339f | 211 | }, |
212 | }, { | |
213 | # Has subtitles | |
214 | # These broadcasts expire after ~1 month, can find new test URL here: | |
215 | # https://www.cbc.ca/player/news/TV%20Shows/The%20National/Latest%20Broadcast | |
7e09c147 | 216 | 'url': 'http://www.cbc.ca/player/play/2284799043667', |
217 | 'md5': '9b49f0839e88b6ec0b01d840cf3d42b5', | |
339c339f | 218 | 'info_dict': { |
7e09c147 | 219 | 'id': '2284799043667', |
339c339f | 220 | 'ext': 'mp4', |
7e09c147 | 221 | 'title': 'The National | Hockey coach charged, Green grants, Safer drugs', |
222 | 'description': 'md5:84ef46321c94bcf7d0159bb565d26bfa', | |
223 | 'timestamp': 1700272800, | |
224 | 'duration': 2718.833, | |
339c339f | 225 | 'subtitles': {'eng': [{'ext': 'vtt', 'protocol': 'm3u8_native'}]}, |
7e09c147 | 226 | 'thumbnail': 'https://thumbnails.cbc.ca/maven_legacy/thumbnails/907/171/thumbnail.jpeg', |
339c339f | 227 | 'uploader': 'CBCC-NEW', |
228 | 'chapters': 'count:5', | |
7e09c147 | 229 | 'upload_date': '20231118', |
230 | 'categories': 'count:4', | |
231 | 'series': 'The National - Full Show', | |
232 | 'tags': 'count:1', | |
233 | 'creator': 'News', | |
234 | 'location': 'Canada', | |
235 | 'media_type': 'Full Program', | |
64413f75 | 236 | }, |
31a70191 | 237 | }] |
8b85ac3f | 238 | |
239 | def _real_extract(self, url): | |
240 | video_id = self._match_id(url) | |
52f7c75c | 241 | return { |
242 | '_type': 'url_transparent', | |
243 | 'ie_key': 'ThePlatform', | |
244 | 'url': smuggle_url( | |
64413f75 | 245 | 'http://link.theplatform.com/s/ExhSPC/media/guid/2655402169/%s?mbr=true&formats=MPEG4,FLV,MP3' % video_id, { |
52f7c75c | 246 | 'force_smil_url': True |
247 | }), | |
248 | 'id': video_id, | |
339c339f | 249 | '_format_sort_fields': ('res', 'proto') # Prioritize direct http formats over HLS |
52f7c75c | 250 | } |
30afe4ae RA |
251 | |
252 | ||
ed711897 | 253 | class CBCPlayerPlaylistIE(InfoExtractor): |
254 | IE_NAME = 'cbc.ca:player:playlist' | |
255 | _VALID_URL = r'https?://(?:www\.)?cbc\.ca/(?:player/)(?!play/)(?P<id>[^?#]+)' | |
256 | _TESTS = [{ | |
257 | 'url': 'https://www.cbc.ca/player/news/TV%20Shows/The%20National/Latest%20Broadcast', | |
258 | 'playlist_mincount': 25, | |
259 | 'info_dict': { | |
260 | 'id': 'news/tv shows/the national/latest broadcast', | |
261 | } | |
262 | }, { | |
263 | 'url': 'https://www.cbc.ca/player/news/Canada/North', | |
264 | 'playlist_mincount': 25, | |
265 | 'info_dict': { | |
266 | 'id': 'news/canada/north', | |
267 | } | |
268 | }] | |
269 | ||
270 | def _real_extract(self, url): | |
271 | playlist_id = urllib.parse.unquote(self._match_id(url)).lower() | |
272 | webpage = self._download_webpage(url, playlist_id) | |
273 | json_content = self._search_json( | |
274 | r'window\.__INITIAL_STATE__\s*=', webpage, 'initial state', playlist_id) | |
275 | ||
276 | def entries(): | |
277 | for video_id in traverse_obj(json_content, ( | |
278 | 'video', 'clipsByCategory', lambda k, _: k.lower() == playlist_id, 'items', ..., 'id' | |
279 | )): | |
280 | yield self.url_result(f'https://www.cbc.ca/player/play/{video_id}', CBCPlayerIE) | |
281 | ||
282 | return self.playlist_result(entries(), playlist_id) | |
283 | ||
284 | ||
0d32e124 | 285 | class CBCGemIE(InfoExtractor): |
286 | IE_NAME = 'gem.cbc.ca' | |
871c9074 | 287 | _VALID_URL = r'https?://gem\.cbc\.ca/(?:media/)?(?P<id>[0-9a-z-]+/s[0-9]+[a-z][0-9]+)' |
0d32e124 | 288 | _TESTS = [{ |
0d32e124 | 289 | # This is a normal, public, TV show video |
290 | 'url': 'https://gem.cbc.ca/media/schitts-creek/s06e01', | |
291 | 'md5': '93dbb31c74a8e45b378cf13bd3f6f11e', | |
292 | 'info_dict': { | |
293 | 'id': 'schitts-creek/s06e01', | |
294 | 'ext': 'mp4', | |
295 | 'title': 'Smoke Signals', | |
296 | 'description': 'md5:929868d20021c924020641769eb3e7f1', | |
297 | 'thumbnail': 'https://images.radio-canada.ca/v1/synps-cbc/episode/perso/cbc_schitts_creek_season_06e01_thumbnail_v01.jpg?im=Resize=(Size)', | |
298 | 'duration': 1314, | |
299 | 'categories': ['comedy'], | |
300 | 'series': 'Schitt\'s Creek', | |
301 | 'season': 'Season 6', | |
302 | 'season_number': 6, | |
303 | 'episode': 'Smoke Signals', | |
304 | 'episode_number': 1, | |
305 | 'episode_id': 'schitts-creek/s06e01', | |
306 | }, | |
307 | 'params': {'format': 'bv'}, | |
308 | 'skip': 'Geo-restricted to Canada', | |
309 | }, { | |
0d32e124 | 310 | # This video requires an account in the browser, but works fine in yt-dlp |
311 | 'url': 'https://gem.cbc.ca/media/schitts-creek/s01e01', | |
312 | 'md5': '297a9600f554f2258aed01514226a697', | |
313 | 'info_dict': { | |
314 | 'id': 'schitts-creek/s01e01', | |
315 | 'ext': 'mp4', | |
316 | 'title': 'The Cup Runneth Over', | |
317 | 'description': 'md5:9bca14ea49ab808097530eb05a29e797', | |
318 | 'thumbnail': 'https://images.radio-canada.ca/v1/synps-cbc/episode/perso/cbc_schitts_creek_season_01e01_thumbnail_v01.jpg?im=Resize=(Size)', | |
319 | 'series': 'Schitt\'s Creek', | |
320 | 'season_number': 1, | |
321 | 'season': 'Season 1', | |
322 | 'episode_number': 1, | |
323 | 'episode': 'The Cup Runneth Over', | |
324 | 'episode_id': 'schitts-creek/s01e01', | |
325 | 'duration': 1309, | |
326 | 'categories': ['comedy'], | |
327 | }, | |
328 | 'params': {'format': 'bv'}, | |
329 | 'skip': 'Geo-restricted to Canada', | |
871c9074 | 330 | }, { |
331 | 'url': 'https://gem.cbc.ca/nadiyas-family-favourites/s01e01', | |
332 | 'only_matching': True, | |
0d32e124 | 333 | }] |
d183af3c | 334 | |
335 | _GEO_COUNTRIES = ['CA'] | |
336 | _TOKEN_API_KEY = '3f4beddd-2061-49b0-ae80-6f1f2ed65b37' | |
337 | _NETRC_MACHINE = 'cbcgem' | |
338 | _claims_token = None | |
339 | ||
340 | def _new_claims_token(self, email, password): | |
341 | data = json.dumps({ | |
342 | 'email': email, | |
343 | 'password': password, | |
344 | }).encode() | |
345 | headers = {'content-type': 'application/json'} | |
346 | query = {'apikey': self._TOKEN_API_KEY} | |
347 | resp = self._download_json('https://api.loginradius.com/identity/v2/auth/login', | |
348 | None, data=data, headers=headers, query=query) | |
349 | access_token = resp['access_token'] | |
350 | ||
351 | query = { | |
352 | 'access_token': access_token, | |
353 | 'apikey': self._TOKEN_API_KEY, | |
354 | 'jwtapp': 'jwt', | |
355 | } | |
356 | resp = self._download_json('https://cloud-api.loginradius.com/sso/jwt/api/token', | |
357 | None, headers=headers, query=query) | |
358 | sig = resp['signature'] | |
359 | ||
360 | data = json.dumps({'jwt': sig}).encode() | |
361 | headers = {'content-type': 'application/json', 'ott-device-type': 'web'} | |
362 | resp = self._download_json('https://services.radio-canada.ca/ott/cbc-api/v2/token', | |
9bf14be7 | 363 | None, data=data, headers=headers, expected_status=426) |
d183af3c | 364 | cbc_access_token = resp['accessToken'] |
365 | ||
366 | headers = {'content-type': 'application/json', 'ott-device-type': 'web', 'ott-access-token': cbc_access_token} | |
367 | resp = self._download_json('https://services.radio-canada.ca/ott/cbc-api/v2/profile', | |
9bf14be7 | 368 | None, headers=headers, expected_status=426) |
d183af3c | 369 | return resp['claimsToken'] |
370 | ||
371 | def _get_claims_token_expiry(self): | |
372 | # Token is a JWT | |
373 | # JWT is decoded here and 'exp' field is extracted | |
374 | # It is a Unix timestamp for when the token expires | |
375 | b64_data = self._claims_token.split('.')[1] | |
376 | data = base64.urlsafe_b64decode(b64_data + "==") | |
377 | return json.loads(data)['exp'] | |
378 | ||
379 | def claims_token_expired(self): | |
380 | exp = self._get_claims_token_expiry() | |
381 | if exp - time.time() < 10: | |
382 | # It will expire in less than 10 seconds, or has already expired | |
383 | return True | |
384 | return False | |
385 | ||
386 | def claims_token_valid(self): | |
387 | return self._claims_token is not None and not self.claims_token_expired() | |
388 | ||
389 | def _get_claims_token(self, email, password): | |
390 | if not self.claims_token_valid(): | |
391 | self._claims_token = self._new_claims_token(email, password) | |
9809740b | 392 | self.cache.store(self._NETRC_MACHINE, 'claims_token', self._claims_token) |
d183af3c | 393 | return self._claims_token |
394 | ||
395 | def _real_initialize(self): | |
396 | if self.claims_token_valid(): | |
397 | return | |
9809740b | 398 | self._claims_token = self.cache.load(self._NETRC_MACHINE, 'claims_token') |
30afe4ae | 399 | |
54c2521c DS |
400 | def _find_secret_formats(self, formats, video_id): |
401 | """ Find a valid video url and convert it to the secret variant """ | |
402 | base_format = next((f for f in formats if f.get('vcodec') != 'none'), None) | |
403 | if not base_format: | |
404 | return | |
405 | ||
406 | base_url = re.sub(r'(Manifest\(.*?),filter=[\w-]+(.*?\))', r'\1\2', base_format['url']) | |
407 | url = re.sub(r'(Manifest\(.*?),format=[\w-]+(.*?\))', r'\1\2', base_url) | |
408 | ||
409 | secret_xml = self._download_xml(url, video_id, note='Downloading secret XML', fatal=False) | |
d4f14a72 | 410 | if not isinstance(secret_xml, xml.etree.ElementTree.Element): |
54c2521c DS |
411 | return |
412 | ||
413 | for child in secret_xml: | |
414 | if child.attrib.get('Type') != 'video': | |
415 | continue | |
416 | for video_quality in child: | |
417 | bitrate = int_or_none(video_quality.attrib.get('Bitrate')) | |
418 | if not bitrate or 'Index' not in video_quality.attrib: | |
419 | continue | |
420 | height = int_or_none(video_quality.attrib.get('MaxHeight')) | |
421 | ||
422 | yield { | |
423 | **base_format, | |
424 | 'format_id': join_nonempty('sec', height), | |
332da56f | 425 | # Note: \g<1> is necessary instead of \1 since bitrate is a number |
426 | 'url': re.sub(r'(QualityLevels\()\d+(\))', fr'\g<1>{bitrate}\2', base_url), | |
54c2521c DS |
427 | 'width': int_or_none(video_quality.attrib.get('MaxWidth')), |
428 | 'tbr': bitrate / 1000.0, | |
429 | 'height': height, | |
430 | } | |
431 | ||
30afe4ae RA |
432 | def _real_extract(self, url): |
433 | video_id = self._match_id(url) | |
4afb208c | 434 | video_info = self._download_json( |
435 | f'https://services.radio-canada.ca/ott/cbc-api/v2/assets/{video_id}', | |
436 | video_id, expected_status=426) | |
d183af3c | 437 | |
438 | email, password = self._get_login_info() | |
439 | if email and password: | |
440 | claims_token = self._get_claims_token(email, password) | |
441 | headers = {'x-claims-token': claims_token} | |
442 | else: | |
443 | headers = {} | |
444 | m3u8_info = self._download_json(video_info['playSession']['url'], video_id, headers=headers) | |
445 | m3u8_url = m3u8_info.get('url') | |
0d32e124 | 446 | |
d183af3c | 447 | if m3u8_info.get('errorCode') == 1: |
448 | self.raise_geo_restricted(countries=['CA']) | |
449 | elif m3u8_info.get('errorCode') == 35: | |
450 | self.raise_login_required(method='password') | |
451 | elif m3u8_info.get('errorCode') != 0: | |
452 | raise ExtractorError(f'{self.IE_NAME} said: {m3u8_info.get("errorCode")} - {m3u8_info.get("message")}') | |
0d32e124 | 453 | |
454 | formats = self._extract_m3u8_formats(m3u8_url, video_id, m3u8_id='hls') | |
455 | self._remove_duplicate_formats(formats) | |
54c2521c | 456 | formats.extend(self._find_secret_formats(formats, video_id)) |
0d32e124 | 457 | |
d183af3c | 458 | for format in formats: |
0d32e124 | 459 | if format.get('vcodec') == 'none': |
460 | if format.get('ext') is None: | |
461 | format['ext'] = 'm4a' | |
462 | if format.get('acodec') is None: | |
463 | format['acodec'] = 'mp4a.40.2' | |
464 | ||
465 | # Put described audio at the beginning of the list, so that it | |
466 | # isn't chosen by default, as most people won't want it. | |
467 | if 'descriptive' in format['format_id'].lower(): | |
468 | format['preference'] = -2 | |
469 | ||
0d32e124 | 470 | return { |
30afe4ae | 471 | 'id': video_id, |
0d32e124 | 472 | 'title': video_info['title'], |
473 | 'description': video_info.get('description'), | |
474 | 'thumbnail': video_info.get('image'), | |
475 | 'series': video_info.get('series'), | |
476 | 'season_number': video_info.get('season'), | |
477 | 'season': f'Season {video_info.get("season")}', | |
478 | 'episode_number': video_info.get('episode'), | |
479 | 'episode': video_info.get('title'), | |
480 | 'episode_id': video_id, | |
481 | 'duration': video_info.get('duration'), | |
482 | 'categories': [video_info.get('category')], | |
30afe4ae | 483 | 'formats': formats, |
0d32e124 | 484 | 'release_timestamp': video_info.get('airDate'), |
485 | 'timestamp': video_info.get('availableDate'), | |
30afe4ae RA |
486 | } |
487 | ||
30afe4ae | 488 | |
0d32e124 | 489 | class CBCGemPlaylistIE(InfoExtractor): |
490 | IE_NAME = 'gem.cbc.ca:playlist' | |
7a7b1376 | 491 | _VALID_URL = r'https?://gem\.cbc\.ca/(?:media/)?(?P<id>(?P<show>[0-9a-z-]+)/s(?P<season>[0-9]+))/?(?:[?#]|$)' |
30afe4ae | 492 | _TESTS = [{ |
0d32e124 | 493 | # TV show playlist, all public videos |
494 | 'url': 'https://gem.cbc.ca/media/schitts-creek/s06', | |
495 | 'playlist_count': 16, | |
30afe4ae | 496 | 'info_dict': { |
0d32e124 | 497 | 'id': 'schitts-creek/s06', |
498 | 'title': 'Season 6', | |
499 | 'description': 'md5:6a92104a56cbeb5818cc47884d4326a2', | |
19c90e40 | 500 | 'series': 'Schitt\'s Creek', |
501 | 'season_number': 6, | |
502 | 'season': 'Season 6', | |
503 | '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', | |
30afe4ae | 504 | }, |
7a7b1376 | 505 | }, { |
506 | 'url': 'https://gem.cbc.ca/schitts-creek/s06', | |
507 | 'only_matching': True, | |
30afe4ae | 508 | }] |
0d32e124 | 509 | _API_BASE = 'https://services.radio-canada.ca/ott/cbc-api/v2/shows/' |
30afe4ae RA |
510 | |
511 | def _real_extract(self, url): | |
0d32e124 | 512 | match = self._match_valid_url(url) |
513 | season_id = match.group('id') | |
514 | show = match.group('show') | |
4afb208c | 515 | show_info = self._download_json(self._API_BASE + show, season_id, expected_status=426) |
0d32e124 | 516 | season = int(match.group('season')) |
013ae2e5 | 517 | |
518 | season_info = next((s for s in show_info['seasons'] if s.get('season') == season), None) | |
0d32e124 | 519 | |
520 | if season_info is None: | |
521 | raise ExtractorError(f'Couldn\'t find season {season} of {show}') | |
522 | ||
523 | episodes = [] | |
524 | for episode in season_info['assets']: | |
525 | episodes.append({ | |
526 | '_type': 'url_transparent', | |
527 | 'ie_key': 'CBCGem', | |
528 | 'url': 'https://gem.cbc.ca/media/' + episode['id'], | |
529 | 'id': episode['id'], | |
530 | 'title': episode.get('title'), | |
531 | 'description': episode.get('description'), | |
532 | 'thumbnail': episode.get('image'), | |
533 | 'series': episode.get('series'), | |
534 | 'season_number': episode.get('season'), | |
535 | 'season': season_info['title'], | |
536 | 'season_id': season_info.get('id'), | |
537 | 'episode_number': episode.get('episode'), | |
538 | 'episode': episode.get('title'), | |
539 | 'episode_id': episode['id'], | |
540 | 'duration': episode.get('duration'), | |
541 | 'categories': [episode.get('category')], | |
542 | }) | |
b12cf31b | 543 | |
0d32e124 | 544 | thumbnail = None |
545 | tn_uri = season_info.get('image') | |
546 | # the-national was observed to use a "data:image/png;base64" | |
547 | # URI for their 'image' value. The image was 1x1, and is | |
548 | # probably just a placeholder, so it is ignored. | |
549 | if tn_uri is not None and not tn_uri.startswith('data:'): | |
550 | thumbnail = tn_uri | |
b12cf31b | 551 | |
0d32e124 | 552 | return { |
553 | '_type': 'playlist', | |
554 | 'entries': episodes, | |
555 | 'id': season_id, | |
556 | 'title': season_info['title'], | |
557 | 'description': season_info.get('description'), | |
558 | 'thumbnail': thumbnail, | |
559 | 'series': show_info.get('title'), | |
560 | 'season_number': season_info.get('season'), | |
561 | 'season': season_info['title'], | |
562 | } | |
563 | ||
564 | ||
565 | class CBCGemLiveIE(InfoExtractor): | |
566 | IE_NAME = 'gem.cbc.ca:live' | |
7a7b1376 | 567 | _VALID_URL = r'https?://gem\.cbc\.ca/live(?:-event)?/(?P<id>\d+)' |
568 | _TESTS = [ | |
569 | { | |
570 | 'url': 'https://gem.cbc.ca/live/920604739687', | |
571 | 'info_dict': { | |
572 | 'title': 'Ottawa', | |
573 | 'description': 'The live TV channel and local programming from Ottawa', | |
574 | 'thumbnail': 'https://thumbnails.cbc.ca/maven_legacy/thumbnails/CBC_OTT_VMS/Live_Channel_Static_Images/Ottawa_2880x1620.jpg', | |
575 | 'is_live': True, | |
576 | 'id': 'AyqZwxRqh8EH', | |
577 | 'ext': 'mp4', | |
578 | 'timestamp': 1492106160, | |
579 | 'upload_date': '20170413', | |
580 | 'uploader': 'CBCC-NEW', | |
581 | }, | |
582 | 'skip': 'Live might have ended', | |
0d32e124 | 583 | }, |
7a7b1376 | 584 | { |
585 | 'url': 'https://gem.cbc.ca/live/44', | |
586 | 'info_dict': { | |
587 | 'id': '44', | |
588 | 'ext': 'mp4', | |
589 | 'is_live': True, | |
590 | 'title': r're:^Ottawa [0-9\-: ]+', | |
591 | 'description': 'The live TV channel and local programming from Ottawa', | |
592 | 'live_status': 'is_live', | |
593 | 'thumbnail': r're:https://images.gem.cbc.ca/v1/cbc-gem/live/.*' | |
594 | }, | |
595 | 'params': {'skip_download': True}, | |
596 | 'skip': 'Live might have ended', | |
597 | }, | |
598 | { | |
599 | 'url': 'https://gem.cbc.ca/live-event/10835', | |
600 | 'info_dict': { | |
601 | 'id': '10835', | |
602 | 'ext': 'mp4', | |
603 | 'is_live': True, | |
604 | 'title': r're:^The National \| Biden’s trip wraps up, Paltrow testifies, Bird flu [0-9\-: ]+', | |
605 | '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.', | |
606 | 'live_status': 'is_live', | |
607 | 'thumbnail': r're:https://images.gem.cbc.ca/v1/cbc-gem/live/.*', | |
608 | 'timestamp': 1679706000, | |
609 | 'upload_date': '20230325', | |
610 | }, | |
611 | 'params': {'skip_download': True}, | |
612 | 'skip': 'Live might have ended', | |
613 | } | |
614 | ] | |
b12cf31b RA |
615 | |
616 | def _real_extract(self, url): | |
0d32e124 | 617 | video_id = self._match_id(url) |
7a7b1376 | 618 | webpage = self._download_webpage(url, video_id) |
619 | video_info = self._search_nextjs_data(webpage, video_id)['props']['pageProps']['data'] | |
0d32e124 | 620 | |
7a7b1376 | 621 | # Two types of metadata JSON |
622 | if not video_info.get('formattedIdMedia'): | |
623 | video_info = traverse_obj( | |
624 | video_info, (('freeTv', ('streams', ...)), 'items', lambda _, v: v['key'] == video_id, {dict}), | |
625 | get_all=False, default={}) | |
626 | ||
627 | video_stream_id = video_info.get('formattedIdMedia') | |
628 | if not video_stream_id: | |
3c239332 | 629 | raise ExtractorError('Couldn\'t find video metadata, maybe this livestream is now offline', expected=True) |
b12cf31b | 630 | |
7a7b1376 | 631 | stream_data = self._download_json( |
632 | 'https://services.radio-canada.ca/media/validation/v2/', video_id, query={ | |
633 | 'appCode': 'mpx', | |
634 | 'connectionType': 'hd', | |
635 | 'deviceType': 'ipad', | |
636 | 'idMedia': video_stream_id, | |
637 | 'multibitrate': 'true', | |
638 | 'output': 'json', | |
639 | 'tech': 'hls', | |
640 | 'manifestType': 'desktop', | |
641 | }) | |
642 | ||
b12cf31b RA |
643 | return { |
644 | 'id': video_id, | |
7a7b1376 | 645 | 'formats': self._extract_m3u8_formats(stream_data['url'], video_id, 'mp4', live=True), |
0d32e124 | 646 | 'is_live': True, |
7a7b1376 | 647 | **traverse_obj(video_info, { |
648 | 'title': 'title', | |
649 | 'description': 'description', | |
650 | 'thumbnail': ('images', 'card', 'url'), | |
651 | 'timestamp': ('airDate', {parse_iso8601}), | |
652 | }) | |
b12cf31b | 653 | } |