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