]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/cspan.py
Update upstream source from tag 'upstream/2019.09.28'
[youtubedl] / youtube_dl / extractor / cspan.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..utils import (
7 determine_ext,
8 ExtractorError,
9 extract_attributes,
10 find_xpath_attr,
11 get_element_by_class,
12 int_or_none,
13 smuggle_url,
14 unescapeHTML,
15 )
16 from .senateisvp import SenateISVPIE
17 from .ustream import UstreamIE
18
19
20 class CSpanIE(InfoExtractor):
21 _VALID_URL = r'https?://(?:www\.)?c-span\.org/video/\?(?P<id>[0-9a-f]+)'
22 IE_DESC = 'C-SPAN'
23 _TESTS = [{
24 'url': 'http://www.c-span.org/video/?313572-1/HolderonV',
25 'md5': '94b29a4f131ff03d23471dd6f60b6a1d',
26 'info_dict': {
27 'id': '315139',
28 'title': 'Attorney General Eric Holder on Voting Rights Act Decision',
29 },
30 'playlist_mincount': 2,
31 'skip': 'Regularly fails on travis, for unknown reasons',
32 }, {
33 'url': 'http://www.c-span.org/video/?c4486943/cspan-international-health-care-models',
34 # md5 is unstable
35 'info_dict': {
36 'id': 'c4486943',
37 'ext': 'mp4',
38 'title': 'CSPAN - International Health Care Models',
39 'description': 'md5:7a985a2d595dba00af3d9c9f0783c967',
40 }
41 }, {
42 'url': 'http://www.c-span.org/video/?318608-1/gm-ignition-switch-recall',
43 'info_dict': {
44 'id': '342759',
45 'title': 'General Motors Ignition Switch Recall',
46 },
47 'playlist_mincount': 6,
48 }, {
49 # Video from senate.gov
50 'url': 'http://www.c-span.org/video/?104517-1/immigration-reforms-needed-protect-skilled-american-workers',
51 'info_dict': {
52 'id': 'judiciary031715',
53 'ext': 'mp4',
54 'title': 'Immigration Reforms Needed to Protect Skilled American Workers',
55 },
56 'params': {
57 'skip_download': True, # m3u8 downloads
58 }
59 }, {
60 # Ustream embedded video
61 'url': 'https://www.c-span.org/video/?114917-1/armed-services',
62 'info_dict': {
63 'id': '58428542',
64 'ext': 'flv',
65 'title': 'USHR07 Armed Services Committee',
66 'description': 'hsas00-2118-20150204-1000et-07\n\n\nUSHR07 Armed Services Committee',
67 'timestamp': 1423060374,
68 'upload_date': '20150204',
69 'uploader': 'HouseCommittee',
70 'uploader_id': '12987475',
71 },
72 }, {
73 # Audio Only
74 'url': 'https://www.c-span.org/video/?437336-1/judiciary-antitrust-competition-policy-consumer-rights',
75 'only_matching': True,
76 }]
77 BRIGHTCOVE_URL_TEMPLATE = 'http://players.brightcove.net/%s/%s_%s/index.html?videoId=%s'
78
79 def _real_extract(self, url):
80 video_id = self._match_id(url)
81 video_type = None
82 webpage = self._download_webpage(url, video_id)
83
84 ustream_url = UstreamIE._extract_url(webpage)
85 if ustream_url:
86 return self.url_result(ustream_url, UstreamIE.ie_key())
87
88 if '&vod' not in url:
89 bc = self._search_regex(
90 r"(<[^>]+id='brightcove-player-embed'[^>]+>)",
91 webpage, 'brightcove embed', default=None)
92 if bc:
93 bc_attr = extract_attributes(bc)
94 bc_url = self.BRIGHTCOVE_URL_TEMPLATE % (
95 bc_attr.get('data-bcaccountid', '3162030207001'),
96 bc_attr.get('data-noprebcplayerid', 'SyGGpuJy3g'),
97 bc_attr.get('data-newbcplayerid', 'default'),
98 bc_attr['data-bcid'])
99 return self.url_result(smuggle_url(bc_url, {'source_url': url}))
100
101 # We first look for clipid, because clipprog always appears before
102 patterns = [r'id=\'clip(%s)\'\s*value=\'([0-9]+)\'' % t for t in ('id', 'prog')]
103 results = list(filter(None, (re.search(p, webpage) for p in patterns)))
104 if results:
105 matches = results[0]
106 video_type, video_id = matches.groups()
107 video_type = 'clip' if video_type == 'id' else 'program'
108 else:
109 m = re.search(r'data-(?P<type>clip|prog)id=["\'](?P<id>\d+)', webpage)
110 if m:
111 video_id = m.group('id')
112 video_type = 'program' if m.group('type') == 'prog' else 'clip'
113 else:
114 senate_isvp_url = SenateISVPIE._search_iframe_url(webpage)
115 if senate_isvp_url:
116 title = self._og_search_title(webpage)
117 surl = smuggle_url(senate_isvp_url, {'force_title': title})
118 return self.url_result(surl, 'SenateISVP', video_id, title)
119 video_id = self._search_regex(
120 r'jwsetup\.clipprog\s*=\s*(\d+);',
121 webpage, 'jwsetup program id', default=None)
122 if video_id:
123 video_type = 'program'
124 if video_type is None or video_id is None:
125 error_message = get_element_by_class('VLplayer-error-message', webpage)
126 if error_message:
127 raise ExtractorError(error_message)
128 raise ExtractorError('unable to find video id and type')
129
130 def get_text_attr(d, attr):
131 return d.get(attr, {}).get('#text')
132
133 data = self._download_json(
134 'http://www.c-span.org/assets/player/ajax-player.php?os=android&html5=%s&id=%s' % (video_type, video_id),
135 video_id)['video']
136 if data['@status'] != 'Success':
137 raise ExtractorError('%s said: %s' % (self.IE_NAME, get_text_attr(data, 'error')), expected=True)
138
139 doc = self._download_xml(
140 'http://www.c-span.org/common/services/flashXml.php?%sid=%s' % (video_type, video_id),
141 video_id)
142
143 description = self._html_search_meta('description', webpage)
144
145 title = find_xpath_attr(doc, './/string', 'name', 'title').text
146 thumbnail = find_xpath_attr(doc, './/string', 'name', 'poster').text
147
148 files = data['files']
149 capfile = get_text_attr(data, 'capfile')
150
151 entries = []
152 for partnum, f in enumerate(files):
153 formats = []
154 for quality in f.get('qualities', []):
155 formats.append({
156 'format_id': '%s-%sp' % (get_text_attr(quality, 'bitrate'), get_text_attr(quality, 'height')),
157 'url': unescapeHTML(get_text_attr(quality, 'file')),
158 'height': int_or_none(get_text_attr(quality, 'height')),
159 'tbr': int_or_none(get_text_attr(quality, 'bitrate')),
160 })
161 if not formats:
162 path = unescapeHTML(get_text_attr(f, 'path'))
163 if not path:
164 continue
165 formats = self._extract_m3u8_formats(
166 path, video_id, 'mp4', entry_protocol='m3u8_native',
167 m3u8_id='hls') if determine_ext(path) == 'm3u8' else [{'url': path, }]
168 self._sort_formats(formats)
169 entries.append({
170 'id': '%s_%d' % (video_id, partnum + 1),
171 'title': (
172 title if len(files) == 1 else
173 '%s part %d' % (title, partnum + 1)),
174 'formats': formats,
175 'description': description,
176 'thumbnail': thumbnail,
177 'duration': int_or_none(get_text_attr(f, 'length')),
178 'subtitles': {
179 'en': [{
180 'url': capfile,
181 'ext': determine_ext(capfile, 'dfxp')
182 }],
183 } if capfile else None,
184 })
185
186 if len(entries) == 1:
187 entry = dict(entries[0])
188 entry['id'] = 'c' + video_id if video_type == 'clip' else video_id
189 return entry
190 else:
191 return {
192 '_type': 'playlist',
193 'entries': entries,
194 'title': title,
195 'id': 'c' + video_id if video_type == 'clip' else video_id,
196 }