]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/cspan.py
Initiate new release.
[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 int_or_none,
8 unescapeHTML,
9 find_xpath_attr,
10 smuggle_url,
11 determine_ext,
12 ExtractorError,
13 extract_attributes,
14 )
15 from .senateisvp import SenateISVPIE
16 from .ustream import UstreamIE
17
18
19 class CSpanIE(InfoExtractor):
20 _VALID_URL = r'https?://(?:www\.)?c-span\.org/video/\?(?P<id>[0-9a-f]+)'
21 IE_DESC = 'C-SPAN'
22 _TESTS = [{
23 'url': 'http://www.c-span.org/video/?313572-1/HolderonV',
24 'md5': '94b29a4f131ff03d23471dd6f60b6a1d',
25 'info_dict': {
26 'id': '315139',
27 'title': 'Attorney General Eric Holder on Voting Rights Act Decision',
28 },
29 'playlist_mincount': 2,
30 'skip': 'Regularly fails on travis, for unknown reasons',
31 }, {
32 'url': 'http://www.c-span.org/video/?c4486943/cspan-international-health-care-models',
33 # md5 is unstable
34 'info_dict': {
35 'id': 'c4486943',
36 'ext': 'mp4',
37 'title': 'CSPAN - International Health Care Models',
38 'description': 'md5:7a985a2d595dba00af3d9c9f0783c967',
39 }
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 },
46 'playlist_mincount': 6,
47 }, {
48 # Video from senate.gov
49 'url': 'http://www.c-span.org/video/?104517-1/immigration-reforms-needed-protect-skilled-american-workers',
50 'info_dict': {
51 'id': 'judiciary031715',
52 'ext': 'mp4',
53 'title': 'Immigration Reforms Needed to Protect Skilled American Workers',
54 },
55 'params': {
56 'skip_download': True, # m3u8 downloads
57 }
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 },
71 }]
72 BRIGHTCOVE_URL_TEMPLATE = 'http://players.brightcove.net/%s/%s_%s/index.html?videoId=%s'
73
74 def _real_extract(self, url):
75 video_id = self._match_id(url)
76 video_type = None
77 webpage = self._download_webpage(url, video_id)
78
79 ustream_url = UstreamIE._extract_url(webpage)
80 if ustream_url:
81 return self.url_result(ustream_url, UstreamIE.ie_key())
82
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
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]
101 video_type, video_id = matches.groups()
102 video_type = 'clip' if video_type == 'id' else 'program'
103 else:
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)
114 if video_type is None or video_id is None:
115 raise ExtractorError('unable to find video id and type')
116
117 def get_text_attr(d, attr):
118 return d.get(attr, {}).get('#text')
119
120 data = self._download_json(
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':
124 raise ExtractorError('%s said: %s' % (self.IE_NAME, get_text_attr(data, 'error')), expected=True)
125
126 doc = self._download_xml(
127 'http://www.c-span.org/common/services/flashXml.php?%sid=%s' % (video_type, video_id),
128 video_id)
129
130 description = self._html_search_meta('description', webpage)
131
132 title = find_xpath_attr(doc, './/string', 'name', 'title').text
133 thumbnail = find_xpath_attr(doc, './/string', 'name', 'poster').text
134
135 files = data['files']
136 capfile = get_text_attr(data, 'capfile')
137
138 entries = []
139 for partnum, f in enumerate(files):
140 formats = []
141 for quality in f['qualities']:
142 formats.append({
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')),
147 })
148 if not formats:
149 path = unescapeHTML(get_text_attr(f, 'path'))
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, }]
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,
164 'duration': int_or_none(get_text_attr(f, 'length')),
165 'subtitles': {
166 'en': [{
167 'url': capfile,
168 'ext': determine_ext(capfile, 'dfxp')
169 }],
170 } if capfile else None,
171 })
172
173 if len(entries) == 1:
174 entry = dict(entries[0])
175 entry['id'] = 'c' + video_id if video_type == 'clip' else video_id
176 return entry
177 else:
178 return {
179 '_type': 'playlist',
180 'entries': entries,
181 'title': title,
182 'id': 'c' + video_id if video_type == 'clip' else video_id,
183 }