]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/cspan.py
[ie/youtube] Suppress "Unavailable videos are hidden" warning (#10159)
[yt-dlp.git] / yt_dlp / extractor / cspan.py
CommitLineData
aa0c8739
JMF
1import re
2
3from .common import InfoExtractor
e897bd82
SS
4from .senategov import SenateISVPIE
5from .ustream import UstreamIE
edecb5f8 6from ..compat import compat_HTMLParseError
aa0c8739 7from ..utils import (
355c7ad3 8 ExtractorError,
e897bd82 9 determine_ext,
5996d21a 10 extract_attributes,
7e810109 11 find_xpath_attr,
30a074c2 12 get_element_by_attribute,
7e810109
RA
13 get_element_by_class,
14 int_or_none,
edecb5f8 15 join_nonempty,
29f7c58a 16 js_to_json,
17 merge_dicts,
30a074c2 18 parse_iso8601,
edecb5f8 19 parse_qs,
7e810109 20 smuggle_url,
30a074c2 21 str_to_int,
7e810109 22 unescapeHTML,
aa0c8739
JMF
23)
24
ca9e7922 25
aa0c8739 26class CSpanIE(InfoExtractor):
5886b38d 27 _VALID_URL = r'https?://(?:www\.)?c-span\.org/video/\?(?P<id>[0-9a-f]+)'
ca9e7922 28 IE_DESC = 'C-SPAN'
11a15be4 29 _TESTS = [{
009a3408 30 'url': 'http://www.c-span.org/video/?313572-1/HolderonV',
355c7ad3 31 'md5': '94b29a4f131ff03d23471dd6f60b6a1d',
ca9e7922 32 'info_dict': {
009a3408 33 'id': '315139',
ca9e7922 34 'title': 'Attorney General Eric Holder on Voting Rights Act Decision',
6f5ac90c 35 },
f3c21cb7 36 'playlist_mincount': 2,
11577ec0 37 'skip': 'Regularly fails on travis, for unknown reasons',
11a15be4
PH
38 }, {
39 'url': 'http://www.c-span.org/video/?c4486943/cspan-international-health-care-models',
f3c21cb7 40 # md5 is unstable
11a15be4 41 'info_dict': {
30787f72 42 'id': 'c4486943',
11a15be4 43 'ext': 'mp4',
30787f72 44 'title': 'CSPAN - International Health Care Models',
11a15be4 45 'description': 'md5:7a985a2d595dba00af3d9c9f0783c967',
add96eb9 46 },
22a6f150
PH
47 }, {
48 'url': 'http://www.c-span.org/video/?318608-1/gm-ignition-switch-recall',
49 'info_dict': {
50 'id': '342759',
51 'title': 'General Motors Ignition Switch Recall',
52 },
f3c21cb7 53 'playlist_mincount': 6,
2fe1b5bd
YCH
54 }, {
55 # Video from senate.gov
56 'url': 'http://www.c-span.org/video/?104517-1/immigration-reforms-needed-protect-skilled-american-workers',
2fe1b5bd
YCH
57 'info_dict': {
58 'id': 'judiciary031715',
7b0d333a 59 'ext': 'mp4',
2fe1b5bd 60 'title': 'Immigration Reforms Needed to Protect Skilled American Workers',
7b0d333a
NP
61 },
62 'params': {
63 'skip_download': True, # m3u8 downloads
add96eb9 64 },
4447fb23
YCH
65 }, {
66 # Ustream embedded video
67 'url': 'https://www.c-span.org/video/?114917-1/armed-services',
68 'info_dict': {
69 'id': '58428542',
70 'ext': 'flv',
71 'title': 'USHR07 Armed Services Committee',
72 'description': 'hsas00-2118-20150204-1000et-07\n\n\nUSHR07 Armed Services Committee',
73 'timestamp': 1423060374,
74 'upload_date': '20150204',
75 'uploader': 'HouseCommittee',
76 'uploader_id': '12987475',
77 },
7e810109
RA
78 }, {
79 # Audio Only
80 'url': 'https://www.c-span.org/video/?437336-1/judiciary-antitrust-competition-policy-consumer-rights',
81 'only_matching': True,
11a15be4 82 }]
5996d21a 83 BRIGHTCOVE_URL_TEMPLATE = 'http://players.brightcove.net/%s/%s_%s/index.html?videoId=%s'
aa0c8739
JMF
84
85 def _real_extract(self, url):
30787f72 86 video_id = self._match_id(url)
04e24906 87 video_type = None
30787f72 88 webpage = self._download_webpage(url, video_id)
4447fb23
YCH
89
90 ustream_url = UstreamIE._extract_url(webpage)
91 if ustream_url:
92 return self.url_result(ustream_url, UstreamIE.ie_key())
93
5996d21a
RA
94 if '&vod' not in url:
95 bc = self._search_regex(
96 r"(<[^>]+id='brightcove-player-embed'[^>]+>)",
97 webpage, 'brightcove embed', default=None)
98 if bc:
99 bc_attr = extract_attributes(bc)
100 bc_url = self.BRIGHTCOVE_URL_TEMPLATE % (
101 bc_attr.get('data-bcaccountid', '3162030207001'),
102 bc_attr.get('data-noprebcplayerid', 'SyGGpuJy3g'),
103 bc_attr.get('data-newbcplayerid', 'default'),
104 bc_attr['data-bcid'])
105 return self.url_result(smuggle_url(bc_url, {'source_url': url}))
106
29f7c58a 107 def add_referer(formats):
108 for f in formats:
109 f.setdefault('http_headers', {})['Referer'] = url
110
111 # As of 01.12.2020 this path looks to cover all cases making the rest
112 # of the code unnecessary
113 jwsetup = self._parse_json(
114 self._search_regex(
115 r'(?s)jwsetup\s*=\s*({.+?})\s*;', webpage, 'jwsetup',
116 default='{}'),
117 video_id, transform_source=js_to_json, fatal=False)
118 if jwsetup:
119 info = self._parse_jwplayer_data(
120 jwsetup, video_id, require_title=False, m3u8_id='hls',
121 base_url=url)
122 add_referer(info['formats'])
30a074c2 123 for subtitles in info['subtitles'].values():
124 for subtitle in subtitles:
125 ext = determine_ext(subtitle['url'])
126 if ext == 'php':
127 ext = 'vtt'
128 subtitle['ext'] = ext
29f7c58a 129 ld_info = self._search_json_ld(webpage, video_id, default={})
edecb5f8
G
130 try:
131 title = get_element_by_class('video-page-title', webpage)
132 except compat_HTMLParseError:
133 title = None
134 if title is None:
135 title = self._og_search_title(webpage)
30a074c2 136 description = get_element_by_attribute('itemprop', 'description', webpage) or \
137 self._html_search_meta(['og:description', 'description'], webpage)
138 return merge_dicts(info, ld_info, {
139 'title': title,
140 'thumbnail': get_element_by_attribute('itemprop', 'thumbnailUrl', webpage),
141 'description': description,
142 'timestamp': parse_iso8601(get_element_by_attribute('itemprop', 'uploadDate', webpage)),
143 'location': get_element_by_attribute('itemprop', 'contentLocation', webpage),
144 'duration': int_or_none(self._search_regex(
145 r'jwsetup\.seclength\s*=\s*(\d+);',
146 webpage, 'duration', fatal=False)),
147 'view_count': str_to_int(self._search_regex(
148 r"<span[^>]+class='views'[^>]*>([\d,]+)\s+Views</span>",
149 webpage, 'views', fatal=False)),
150 })
29f7c58a 151
152 # Obsolete
6c6b8bd5 153 # We first look for clipid, because clipprog always appears before
add96eb9 154 patterns = [rf'id=\'clip({t})\'\s*value=\'([0-9]+)\'' for t in ('id', 'prog')]
6c6b8bd5
JMF
155 results = list(filter(None, (re.search(p, webpage) for p in patterns)))
156 if results:
157 matches = results[0]
30787f72 158 video_type, video_id = matches.groups()
6c6b8bd5 159 video_type = 'clip' if video_type == 'id' else 'program'
30787f72 160 else:
f6932135
S
161 m = re.search(r'data-(?P<type>clip|prog)id=["\'](?P<id>\d+)', webpage)
162 if m:
163 video_id = m.group('id')
164 video_type = 'program' if m.group('type') == 'prog' else 'clip'
165 else:
bfd973ec 166 senate_isvp_url = SenateISVPIE._extract_url(webpage)
f6932135
S
167 if senate_isvp_url:
168 title = self._og_search_title(webpage)
169 surl = smuggle_url(senate_isvp_url, {'force_title': title})
170 return self.url_result(surl, 'SenateISVP', video_id, title)
7e810109
RA
171 video_id = self._search_regex(
172 r'jwsetup\.clipprog\s*=\s*(\d+);',
173 webpage, 'jwsetup program id', default=None)
174 if video_id:
175 video_type = 'program'
04e24906 176 if video_type is None or video_id is None:
7e810109
RA
177 error_message = get_element_by_class('VLplayer-error-message', webpage)
178 if error_message:
179 raise ExtractorError(error_message)
04e24906 180 raise ExtractorError('unable to find video id and type')
ca9e7922 181
2a776f97 182 def get_text_attr(d, attr):
183 return d.get(attr, {}).get('#text')
184
30787f72 185 data = self._download_json(
add96eb9 186 f'http://www.c-span.org/assets/player/ajax-player.php?os=android&html5={video_type}&id={video_id}',
355c7ad3 187 video_id)['video']
188 if data['@status'] != 'Success':
add96eb9 189 raise ExtractorError('{} said: {}'.format(self.IE_NAME, get_text_attr(data, 'error')), expected=True)
ca9e7922 190
aea6e7fc 191 doc = self._download_xml(
add96eb9 192 f'http://www.c-span.org/common/services/flashXml.php?{video_type}id={video_id}',
009a3408
JMF
193 video_id)
194
30787f72 195 description = self._html_search_meta('description', webpage)
196
aea6e7fc
PH
197 title = find_xpath_attr(doc, './/string', 'name', 'title').text
198 thumbnail = find_xpath_attr(doc, './/string', 'name', 'poster').text
199
355c7ad3 200 files = data['files']
2a776f97 201 capfile = get_text_attr(data, 'capfile')
aea6e7fc 202
355c7ad3 203 entries = []
204 for partnum, f in enumerate(files):
205 formats = []
7e810109 206 for quality in f.get('qualities', []):
355c7ad3 207 formats.append({
add96eb9 208 'format_id': '{}-{}p'.format(get_text_attr(quality, 'bitrate'), get_text_attr(quality, 'height')),
2a776f97 209 'url': unescapeHTML(get_text_attr(quality, 'file')),
210 'height': int_or_none(get_text_attr(quality, 'height')),
211 'tbr': int_or_none(get_text_attr(quality, 'bitrate')),
355c7ad3 212 })
af9c2a07 213 if not formats:
68a0ea15 214 path = unescapeHTML(get_text_attr(f, 'path'))
af9c2a07
S
215 if not path:
216 continue
217 formats = self._extract_m3u8_formats(
218 path, video_id, 'mp4', entry_protocol='m3u8_native',
add96eb9 219 m3u8_id='hls') if determine_ext(path) == 'm3u8' else [{'url': path}]
29f7c58a 220 add_referer(formats)
355c7ad3 221 entries.append({
add96eb9 222 'id': f'{video_id}_{partnum + 1}',
355c7ad3 223 'title': (
224 title if len(files) == 1 else
add96eb9 225 f'{title} part {partnum + 1}'),
355c7ad3 226 'formats': formats,
227 'description': description,
228 'thumbnail': thumbnail,
2a776f97 229 'duration': int_or_none(get_text_attr(f, 'length')),
355c7ad3 230 'subtitles': {
231 'en': [{
232 'url': capfile,
add96eb9 233 'ext': determine_ext(capfile, 'dfxp'),
355c7ad3 234 }],
235 } if capfile else None,
236 })
009a3408 237
92dcba1e
YCH
238 if len(entries) == 1:
239 entry = dict(entries[0])
30787f72 240 entry['id'] = 'c' + video_id if video_type == 'clip' else video_id
92dcba1e
YCH
241 return entry
242 else:
243 return {
244 '_type': 'playlist',
245 'entries': entries,
246 'title': title,
30787f72 247 'id': 'c' + video_id if video_type == 'clip' else video_id,
92dcba1e 248 }
edecb5f8
G
249
250
251class CSpanCongressIE(InfoExtractor):
252 _VALID_URL = r'https?://(?:www\.)?c-span\.org/congress/'
253 _TESTS = [{
254 'url': 'https://www.c-span.org/congress/?chamber=house&date=2017-12-13&t=1513208380',
255 'info_dict': {
256 'id': 'house_2017-12-13',
257 'title': 'Congressional Chronicle - Members of Congress, Hearings and More',
258 'description': 'md5:54c264b7a8f219937987610243305a84',
259 'thumbnail': r're:https://ximage.c-spanvideo.org/.+',
add96eb9 260 'ext': 'mp4',
261 },
edecb5f8
G
262 }]
263
264 def _real_extract(self, url):
265 query = parse_qs(url)
266 video_date = query.get('date', [None])[0]
267 video_id = join_nonempty(query.get('chamber', ['senate'])[0], video_date, delim='_')
268 webpage = self._download_webpage(url, video_id)
269 if not video_date:
270 jwp_date = re.search(r'jwsetup.clipprogdate = \'(?P<date>\d{4}-\d{2}-\d{2})\';', webpage)
271 if jwp_date:
272 video_id = f'{video_id}_{jwp_date.group("date")}'
273 jwplayer_data = self._parse_json(
274 self._search_regex(r'jwsetup\s*=\s*({(?:.|\n)[^;]+});', webpage, 'player config'),
275 video_id, transform_source=js_to_json)
276
62b8dac4 277 title = self._generic_title('', webpage)
edecb5f8
G
278 description = (self._og_search_description(webpage, default=None)
279 or self._html_search_meta('description', webpage, 'description', default=None))
280
281 return {
282 **self._parse_jwplayer_data(jwplayer_data, video_id, False),
283 'title': re.sub(r'\s+', ' ', title.split('|')[0]).strip(),
284 'description': description,
285 'http_headers': {'Referer': 'https://www.c-span.org/'},
286 }