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