]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/cbs.py
[cbs] Report appropriate error for DRM
[yt-dlp.git] / yt_dlp / extractor / cbs.py
1 from __future__ import unicode_literals
2
3 from .theplatform import ThePlatformFeedIE
4 from ..utils import (
5 ExtractorError,
6 int_or_none,
7 find_xpath_attr,
8 xpath_element,
9 xpath_text,
10 update_url_query,
11 url_or_none,
12 )
13
14
15 class CBSBaseIE(ThePlatformFeedIE):
16 def _parse_smil_subtitles(self, smil, namespace=None, subtitles_lang='en'):
17 subtitles = {}
18 for k, ext in [('sMPTE-TTCCURL', 'tt'), ('ClosedCaptionURL', 'ttml'), ('webVTTCaptionURL', 'vtt')]:
19 cc_e = find_xpath_attr(smil, self._xpath_ns('.//param', namespace), 'name', k)
20 if cc_e is not None:
21 cc_url = cc_e.get('value')
22 if cc_url:
23 subtitles.setdefault(subtitles_lang, []).append({
24 'ext': ext,
25 'url': cc_url,
26 })
27 return subtitles
28
29 def _extract_common_video_info(self, content_id, asset_types, mpx_acc, extra_info):
30 tp_path = 'dJ5BDC/media/guid/%d/%s' % (mpx_acc, content_id)
31 tp_release_url = f'https://link.theplatform.com/s/{tp_path}'
32 info = self._extract_theplatform_metadata(tp_path, content_id)
33
34 formats, subtitles = [], {}
35 last_e = None
36 for asset_type, query in asset_types.items():
37 try:
38 tp_formats, tp_subtitles = self._extract_theplatform_smil(
39 update_url_query(tp_release_url, query), content_id,
40 'Downloading %s SMIL data' % asset_type)
41 except ExtractorError as e:
42 last_e = e
43 if asset_type != 'fallback':
44 continue
45 query['formats'] = '' # blank query to check if expired
46 try:
47 tp_formats, tp_subtitles = self._extract_theplatform_smil(
48 update_url_query(tp_release_url, query), content_id,
49 'Downloading %s SMIL data, trying again with another format' % asset_type)
50 except ExtractorError as e:
51 last_e = e
52 continue
53 formats.extend(tp_formats)
54 subtitles = self._merge_subtitles(subtitles, tp_subtitles)
55 if last_e and not formats:
56 self.raise_no_formats(last_e, True, content_id)
57 self._sort_formats(formats)
58
59 extra_info.update({
60 'id': content_id,
61 'formats': formats,
62 'subtitles': subtitles,
63 })
64 info.update({k: v for k, v in extra_info.items() if v is not None})
65 return info
66
67 def _extract_video_info(self, *args, **kwargs):
68 # Extract assets + metadata and call _extract_common_video_info
69 raise NotImplementedError('This method must be implemented by subclasses')
70
71 def _real_extract(self, url):
72 return self._extract_video_info(self._match_id(url))
73
74
75 class CBSIE(CBSBaseIE):
76 _VALID_URL = r'''(?x)
77 (?:
78 cbs:|
79 https?://(?:www\.)?(?:
80 cbs\.com/(?:shows/[^/]+/video|movies/[^/]+)/|
81 colbertlateshow\.com/(?:video|podcasts)/)
82 )(?P<id>[\w-]+)'''
83
84 # All tests are blocked outside US
85 _TESTS = [{
86 'url': 'https://www.cbs.com/shows/garth-brooks/video/_u7W953k6la293J7EPTd9oHkSPs6Xn6_/connect-chat-feat-garth-brooks/',
87 'info_dict': {
88 'id': '_u7W953k6la293J7EPTd9oHkSPs6Xn6_',
89 'ext': 'mp4',
90 'title': 'Connect Chat feat. Garth Brooks',
91 'description': 'Connect with country music singer Garth Brooks, as he chats with fans on Wednesday November 27, 2013. Be sure to tune in to Garth Brooks: Live from Las Vegas, Friday November 29, at 9/8c on CBS!',
92 'duration': 1495,
93 'timestamp': 1385585425,
94 'upload_date': '20131127',
95 'uploader': 'CBSI-NEW',
96 },
97 'params': {
98 # m3u8 download
99 'skip_download': True,
100 },
101 }, {
102 'url': 'https://www.cbs.com/shows/the-late-show-with-stephen-colbert/video/60icOhMb9NcjbcWnF_gub9XXHdeBcNk2/the-late-show-6-23-21-christine-baranski-joy-oladokun-',
103 'info_dict': {
104 'id': '60icOhMb9NcjbcWnF_gub9XXHdeBcNk2',
105 'title': 'The Late Show - 6/23/21 (Christine Baranski, Joy Oladokun)',
106 'timestamp': 1624507140,
107 'description': 'md5:e01af24e95c74d55e8775aef86117b95',
108 'uploader': 'CBSI-NEW',
109 'upload_date': '20210624',
110 },
111 'params': {
112 'ignore_no_formats_error': True,
113 'skip_download': True,
114 },
115 'expected_warnings': [
116 'This content expired on', 'No video formats found', 'Requested format is not available'],
117 }, {
118 'url': 'http://colbertlateshow.com/video/8GmB0oY0McANFvp2aEffk9jZZZ2YyXxy/the-colbeard/',
119 'only_matching': True,
120 }, {
121 'url': 'http://www.colbertlateshow.com/podcasts/dYSwjqPs_X1tvbV_P2FcPWRa_qT6akTC/in-the-bad-room-with-stephen/',
122 'only_matching': True,
123 }]
124
125 def _extract_video_info(self, content_id, site='cbs', mpx_acc=2198311517):
126 items_data = self._download_xml(
127 'https://can.cbs.com/thunder/player/videoPlayerService.php',
128 content_id, query={'partner': site, 'contentId': content_id})
129 video_data = xpath_element(items_data, './/item')
130 title = xpath_text(video_data, 'videoTitle', 'title') or xpath_text(video_data, 'videotitle', 'title')
131
132 asset_types = {}
133 has_drm = False
134 for item in items_data.findall('.//item'):
135 asset_type = xpath_text(item, 'assetType')
136 query = {
137 'mbr': 'true',
138 'assetTypes': asset_type,
139 }
140 if not asset_type:
141 # fallback for content_ids that videoPlayerService doesn't return anything for
142 asset_type = 'fallback'
143 query['formats'] = 'M3U+none,MPEG4,M3U+appleHlsEncryption,MP3'
144 del query['assetTypes']
145 if asset_type in asset_types:
146 continue
147 elif any(excluded in asset_type for excluded in ('HLS_FPS', 'DASH_CENC', 'OnceURL')):
148 if 'DASH_CENC' in asset_type:
149 has_drm = True
150 continue
151 if asset_type.startswith('HLS') or 'StreamPack' in asset_type:
152 query['formats'] = 'MPEG4,M3U'
153 elif asset_type in ('RTMP', 'WIFI', '3G'):
154 query['formats'] = 'MPEG4,FLV'
155 asset_types[asset_type] = query
156
157 if not asset_types and has_drm:
158 self.report_drm(content_id)
159
160 return self._extract_common_video_info(content_id, asset_types, mpx_acc, extra_info={
161 'title': title,
162 'series': xpath_text(video_data, 'seriesTitle'),
163 'season_number': int_or_none(xpath_text(video_data, 'seasonNumber')),
164 'episode_number': int_or_none(xpath_text(video_data, 'episodeNumber')),
165 'duration': int_or_none(xpath_text(video_data, 'videoLength'), 1000),
166 'thumbnail': url_or_none(xpath_text(video_data, 'previewImageURL')),
167 })