]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/cbs.py
[extractor] Deprecate `_sort_formats`
[yt-dlp.git] / yt_dlp / extractor / cbs.py
CommitLineData
43518503 1from .theplatform import ThePlatformFeedIE
5c2266df 2from ..utils import (
21dedcb5 3 ExtractorError,
63c55e9f 4 int_or_none,
63c55e9f 5 find_xpath_attr,
45cae3b0
RA
6 xpath_element,
7 xpath_text,
8 update_url_query,
6e6e0d95 9 url_or_none,
5c2266df 10)
fa3ae234
PH
11
12
6368e2e6 13class CBSBaseIE(ThePlatformFeedIE): # XXX: Do not subclass from concrete IE
3e0c3d14 14 def _parse_smil_subtitles(self, smil, namespace=None, subtitles_lang='en'):
180a9dff
RA
15 subtitles = {}
16 for k, ext in [('sMPTE-TTCCURL', 'tt'), ('ClosedCaptionURL', 'ttml'), ('webVTTCaptionURL', 'vtt')]:
17 cc_e = find_xpath_attr(smil, self._xpath_ns('.//param', namespace), 'name', k)
18 if cc_e is not None:
19 cc_url = cc_e.get('value')
20 if cc_url:
21 subtitles.setdefault(subtitles_lang, []).append({
22 'ext': ext,
23 'url': cc_url,
24 })
25 return subtitles
3e0c3d14 26
6e6e0d95 27 def _extract_common_video_info(self, content_id, asset_types, mpx_acc, extra_info):
28 tp_path = 'dJ5BDC/media/guid/%d/%s' % (mpx_acc, content_id)
29 tp_release_url = f'https://link.theplatform.com/s/{tp_path}'
30 info = self._extract_theplatform_metadata(tp_path, content_id)
31
32 formats, subtitles = [], {}
33 last_e = None
34 for asset_type, query in asset_types.items():
35 try:
36 tp_formats, tp_subtitles = self._extract_theplatform_smil(
37 update_url_query(tp_release_url, query), content_id,
38 'Downloading %s SMIL data' % asset_type)
39 except ExtractorError as e:
40 last_e = e
41 if asset_type != 'fallback':
42 continue
43 query['formats'] = '' # blank query to check if expired
44 try:
45 tp_formats, tp_subtitles = self._extract_theplatform_smil(
46 update_url_query(tp_release_url, query), content_id,
47 'Downloading %s SMIL data, trying again with another format' % asset_type)
48 except ExtractorError as e:
49 last_e = e
50 continue
51 formats.extend(tp_formats)
52 subtitles = self._merge_subtitles(subtitles, tp_subtitles)
53 if last_e and not formats:
54 self.raise_no_formats(last_e, True, content_id)
6e6e0d95 55
56 extra_info.update({
57 'id': content_id,
58 'formats': formats,
59 'subtitles': subtitles,
60 })
61 info.update({k: v for k, v in extra_info.items() if v is not None})
62 return info
63
64 def _extract_video_info(self, *args, **kwargs):
65 # Extract assets + metadata and call _extract_common_video_info
66 raise NotImplementedError('This method must be implemented by subclasses')
67
68 def _real_extract(self, url):
69 return self._extract_video_info(self._match_id(url))
70
3e0c3d14 71
72class CBSIE(CBSBaseIE):
c755f190 73 _VALID_URL = r'''(?x)
74 (?:
75 cbs:|
76 https?://(?:www\.)?(?:
43c38abd 77 cbs\.com/(?:shows|movies)/(?:video|[^/]+/video|[^/]+)/|
c755f190 78 colbertlateshow\.com/(?:video|podcasts)/)
79 )(?P<id>[\w-]+)'''
fa3ae234 80
6e6e0d95 81 # All tests are blocked outside US
2871d489 82 _TESTS = [{
43c38abd 83 'url': 'https://www.cbs.com/shows/video/xrUyNLtl9wd8D_RWWAg9NU2F_V6QpB3R/',
e42a692f 84 'info_dict': {
43c38abd 85 'id': 'xrUyNLtl9wd8D_RWWAg9NU2F_V6QpB3R',
63c55e9f 86 'ext': 'mp4',
43c38abd
S
87 'title': 'Tough As Nails - Dreams Never Die',
88 'description': 'md5:a3535a62531cdd52b0364248a2c1ae33',
89 'duration': 2588,
90 'timestamp': 1639015200,
91 'upload_date': '20211209',
79ba9140 92 'uploader': 'CBSI-NEW',
fa3ae234 93 },
dabe1570
RA
94 'params': {
95 # m3u8 download
96 'skip_download': True,
97 },
68f5867c 98 }, {
43c38abd 99 'url': 'https://www.cbs.com/shows/video/sZH1MGgomIosZgxGJ1l263MFq16oMtW1/',
68f5867c 100 'info_dict': {
43c38abd
S
101 'id': 'sZH1MGgomIosZgxGJ1l263MFq16oMtW1',
102 'title': 'The Late Show - 3/16/22 (Michael Buble, Rose Matafeo)',
103 'timestamp': 1647488100,
104 'description': 'md5:d0e6ec23c544b7fa8e39a8e6844d2439',
68f5867c 105 'uploader': 'CBSI-NEW',
43c38abd 106 'upload_date': '20220317',
68f5867c
L
107 },
108 'params': {
109 'ignore_no_formats_error': True,
110 'skip_download': True,
111 },
112 'expected_warnings': [
113 'This content expired on', 'No video formats found', 'Requested format is not available'],
9bf99891
S
114 }, {
115 'url': 'http://colbertlateshow.com/video/8GmB0oY0McANFvp2aEffk9jZZZ2YyXxy/the-colbeard/',
116 'only_matching': True,
117 }, {
9d581f3d 118 'url': 'http://www.colbertlateshow.com/podcasts/dYSwjqPs_X1tvbV_P2FcPWRa_qT6akTC/in-the-bad-room-with-stephen/',
9bf99891 119 'only_matching': True,
2871d489 120 }]
dabe1570 121
96820c1c 122 def _extract_video_info(self, content_id, site='cbs', mpx_acc=2198311517):
45cae3b0 123 items_data = self._download_xml(
2c736b4f 124 'https://can.cbs.com/thunder/player/videoPlayerService.php',
96820c1c 125 content_id, query={'partner': site, 'contentId': content_id})
45cae3b0 126 video_data = xpath_element(items_data, './/item')
430c2757 127 title = xpath_text(video_data, 'videoTitle', 'title') or xpath_text(video_data, 'videotitle', 'title')
45cae3b0 128
6e6e0d95 129 asset_types = {}
8f70b0b8 130 has_drm = False
45cae3b0
RA
131 for item in items_data.findall('.//item'):
132 asset_type = xpath_text(item, 'assetType')
45cae3b0
RA
133 query = {
134 'mbr': 'true',
135 'assetTypes': asset_type,
136 }
68f5867c
L
137 if not asset_type:
138 # fallback for content_ids that videoPlayerService doesn't return anything for
68f5867c
L
139 asset_type = 'fallback'
140 query['formats'] = 'M3U+none,MPEG4,M3U+appleHlsEncryption,MP3'
141 del query['assetTypes']
6e6e0d95 142 if asset_type in asset_types:
68f5867c
L
143 continue
144 elif any(excluded in asset_type for excluded in ('HLS_FPS', 'DASH_CENC', 'OnceURL')):
8f70b0b8 145 if 'DASH_CENC' in asset_type:
146 has_drm = True
68f5867c 147 continue
68f5867c 148 if asset_type.startswith('HLS') or 'StreamPack' in asset_type:
45cae3b0
RA
149 query['formats'] = 'MPEG4,M3U'
150 elif asset_type in ('RTMP', 'WIFI', '3G'):
151 query['formats'] = 'MPEG4,FLV'
6e6e0d95 152 asset_types[asset_type] = query
45cae3b0 153
8f70b0b8 154 if not asset_types and has_drm:
155 self.report_drm(content_id)
156
6e6e0d95 157 return self._extract_common_video_info(content_id, asset_types, mpx_acc, extra_info={
158 'title': title,
159 'series': xpath_text(video_data, 'seriesTitle'),
160 'season_number': int_or_none(xpath_text(video_data, 'seasonNumber')),
161 'episode_number': int_or_none(xpath_text(video_data, 'episodeNumber')),
162 'duration': int_or_none(xpath_text(video_data, 'videoLength'), 1000),
163 'thumbnail': url_or_none(xpath_text(video_data, 'previewImageURL')),
dabe1570 164 })