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