]>
Commit | Line | Data |
---|---|---|
aa0c8739 JMF |
1 | import re |
2 | ||
3 | from .common import InfoExtractor | |
edecb5f8 | 4 | from ..compat import compat_HTMLParseError |
aa0c8739 | 5 | from ..utils import ( |
672f1bd8 | 6 | determine_ext, |
355c7ad3 | 7 | ExtractorError, |
5996d21a | 8 | extract_attributes, |
7e810109 | 9 | find_xpath_attr, |
30a074c2 | 10 | get_element_by_attribute, |
7e810109 RA |
11 | get_element_by_class, |
12 | int_or_none, | |
edecb5f8 | 13 | join_nonempty, |
29f7c58a | 14 | js_to_json, |
15 | merge_dicts, | |
30a074c2 | 16 | parse_iso8601, |
edecb5f8 | 17 | parse_qs, |
7e810109 | 18 | smuggle_url, |
30a074c2 | 19 | str_to_int, |
7e810109 | 20 | unescapeHTML, |
aa0c8739 | 21 | ) |
909b0d66 | 22 | from .senategov import SenateISVPIE |
4447fb23 | 23 | from .ustream import UstreamIE |
aa0c8739 | 24 | |
ca9e7922 | 25 | |
aa0c8739 | 26 | class 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 PH |
45 | 'description': 'md5:7a985a2d595dba00af3d9c9f0783c967', |
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 | |
2fe1b5bd | 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 JMF |
153 | # We first look for clipid, because clipprog always appears before |
154 | patterns = [r'id=\'clip(%s)\'\s*value=\'([0-9]+)\'' % t for t in ('id', 'prog')] | |
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: | |
166 | senate_isvp_url = SenateISVPIE._search_iframe_url(webpage) | |
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( |
355c7ad3 | 186 | 'http://www.c-span.org/assets/player/ajax-player.php?os=android&html5=%s&id=%s' % (video_type, video_id), |
187 | video_id)['video'] | |
188 | if data['@status'] != 'Success': | |
2a776f97 | 189 | raise ExtractorError('%s said: %s' % (self.IE_NAME, get_text_attr(data, 'error')), expected=True) |
ca9e7922 | 190 | |
aea6e7fc | 191 | doc = self._download_xml( |
30787f72 | 192 | 'http://www.c-span.org/common/services/flashXml.php?%sid=%s' % (video_type, 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({ |
2a776f97 | 208 | 'format_id': '%s-%sp' % (get_text_attr(quality, 'bitrate'), get_text_attr(quality, 'height')), |
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', | |
219 | m3u8_id='hls') if determine_ext(path) == 'm3u8' else [{'url': path, }] | |
29f7c58a | 220 | add_referer(formats) |
355c7ad3 | 221 | self._sort_formats(formats) |
222 | entries.append({ | |
223 | 'id': '%s_%d' % (video_id, partnum + 1), | |
224 | 'title': ( | |
225 | title if len(files) == 1 else | |
226 | '%s part %d' % (title, partnum + 1)), | |
227 | 'formats': formats, | |
228 | 'description': description, | |
229 | 'thumbnail': thumbnail, | |
2a776f97 | 230 | 'duration': int_or_none(get_text_attr(f, 'length')), |
355c7ad3 | 231 | 'subtitles': { |
232 | 'en': [{ | |
233 | 'url': capfile, | |
234 | 'ext': determine_ext(capfile, 'dfxp') | |
235 | }], | |
236 | } if capfile else None, | |
237 | }) | |
009a3408 | 238 | |
92dcba1e YCH |
239 | if len(entries) == 1: |
240 | entry = dict(entries[0]) | |
30787f72 | 241 | entry['id'] = 'c' + video_id if video_type == 'clip' else video_id |
92dcba1e YCH |
242 | return entry |
243 | else: | |
244 | return { | |
245 | '_type': 'playlist', | |
246 | 'entries': entries, | |
247 | 'title': title, | |
30787f72 | 248 | 'id': 'c' + video_id if video_type == 'clip' else video_id, |
92dcba1e | 249 | } |
edecb5f8 G |
250 | |
251 | ||
252 | class CSpanCongressIE(InfoExtractor): | |
253 | _VALID_URL = r'https?://(?:www\.)?c-span\.org/congress/' | |
254 | _TESTS = [{ | |
255 | 'url': 'https://www.c-span.org/congress/?chamber=house&date=2017-12-13&t=1513208380', | |
256 | 'info_dict': { | |
257 | 'id': 'house_2017-12-13', | |
258 | 'title': 'Congressional Chronicle - Members of Congress, Hearings and More', | |
259 | 'description': 'md5:54c264b7a8f219937987610243305a84', | |
260 | 'thumbnail': r're:https://ximage.c-spanvideo.org/.+', | |
261 | 'ext': 'mp4' | |
262 | } | |
263 | }] | |
264 | ||
265 | def _real_extract(self, url): | |
266 | query = parse_qs(url) | |
267 | video_date = query.get('date', [None])[0] | |
268 | video_id = join_nonempty(query.get('chamber', ['senate'])[0], video_date, delim='_') | |
269 | webpage = self._download_webpage(url, video_id) | |
270 | if not video_date: | |
271 | jwp_date = re.search(r'jwsetup.clipprogdate = \'(?P<date>\d{4}-\d{2}-\d{2})\';', webpage) | |
272 | if jwp_date: | |
273 | video_id = f'{video_id}_{jwp_date.group("date")}' | |
274 | jwplayer_data = self._parse_json( | |
275 | self._search_regex(r'jwsetup\s*=\s*({(?:.|\n)[^;]+});', webpage, 'player config'), | |
276 | video_id, transform_source=js_to_json) | |
277 | ||
278 | title = (self._og_search_title(webpage, default=None) | |
04f3fd2c | 279 | or self._html_extract_title(webpage, 'video title')) |
edecb5f8 G |
280 | description = (self._og_search_description(webpage, default=None) |
281 | or self._html_search_meta('description', webpage, 'description', default=None)) | |
282 | ||
283 | return { | |
284 | **self._parse_jwplayer_data(jwplayer_data, video_id, False), | |
285 | 'title': re.sub(r'\s+', ' ', title.split('|')[0]).strip(), | |
286 | 'description': description, | |
287 | 'http_headers': {'Referer': 'https://www.c-span.org/'}, | |
288 | } |