]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/cspan.py
84b36f44cfac7bd45a8a7d28adb6767093a7d19b
[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
16
17 class CSpanIE(InfoExtractor):
18 _VALID_URL = r'https?://(?:www\.)?c-span\.org/video/\?(?P<id>[0-9a-f]+)'
19 IE_DESC = 'C-SPAN'
20 _TESTS = [{
21 'url': 'http://www.c-span.org/video/?313572-1/HolderonV',
22 'md5': '94b29a4f131ff03d23471dd6f60b6a1d',
23 'info_dict': {
24 'id': '315139',
25 'ext': 'mp4',
26 'title': 'Attorney General Eric Holder on Voting Rights Act Decision',
27 'description': 'Attorney General Eric Holder speaks to reporters following the Supreme Court decision in [Shelby County v. Holder], in which the court ruled that the preclearance provisions of the Voting Rights Act could not be enforced.',
28 },
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': '8e5fbfabe6ad0f89f3012a7943c1287b',
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 'md5': '2ae5051559169baadba13fc35345ae74',
42 'info_dict': {
43 'id': '342759',
44 'ext': 'mp4',
45 'title': 'General Motors Ignition Switch Recall',
46 'duration': 14848,
47 'description': 'md5:118081aedd24bf1d3b68b3803344e7f3'
48 },
49 }, {
50 # Video from senate.gov
51 'url': 'http://www.c-span.org/video/?104517-1/immigration-reforms-needed-protect-skilled-american-workers',
52 'info_dict': {
53 'id': 'judiciary031715',
54 'ext': 'flv',
55 'title': 'Immigration Reforms Needed to Protect Skilled American Workers',
56 }
57 }]
58
59 def _real_extract(self, url):
60 video_id = self._match_id(url)
61 video_type = None
62 webpage = self._download_webpage(url, video_id)
63 # We first look for clipid, because clipprog always appears before
64 patterns = [r'id=\'clip(%s)\'\s*value=\'([0-9]+)\'' % t for t in ('id', 'prog')]
65 results = list(filter(None, (re.search(p, webpage) for p in patterns)))
66 if results:
67 matches = results[0]
68 video_type, video_id = matches.groups()
69 video_type = 'clip' if video_type == 'id' else 'program'
70 else:
71 m = re.search(r'data-(?P<type>clip|prog)id=["\'](?P<id>\d+)', webpage)
72 if m:
73 video_id = m.group('id')
74 video_type = 'program' if m.group('type') == 'prog' else 'clip'
75 else:
76 senate_isvp_url = SenateISVPIE._search_iframe_url(webpage)
77 if senate_isvp_url:
78 title = self._og_search_title(webpage)
79 surl = smuggle_url(senate_isvp_url, {'force_title': title})
80 return self.url_result(surl, 'SenateISVP', video_id, title)
81 if video_type is None or video_id is None:
82 raise ExtractorError('unable to find video id and type')
83
84 def get_text_attr(d, attr):
85 return d.get(attr, {}).get('#text')
86
87 data = self._download_json(
88 'http://www.c-span.org/assets/player/ajax-player.php?os=android&html5=%s&id=%s' % (video_type, video_id),
89 video_id)['video']
90 if data['@status'] != 'Success':
91 raise ExtractorError('%s said: %s' % (self.IE_NAME, get_text_attr(data, 'error')), expected=True)
92
93 doc = self._download_xml(
94 'http://www.c-span.org/common/services/flashXml.php?%sid=%s' % (video_type, video_id),
95 video_id)
96
97 description = self._html_search_meta('description', webpage)
98
99 title = find_xpath_attr(doc, './/string', 'name', 'title').text
100 thumbnail = find_xpath_attr(doc, './/string', 'name', 'poster').text
101
102 files = data['files']
103 capfile = get_text_attr(data, 'capfile')
104
105 entries = []
106 for partnum, f in enumerate(files):
107 formats = []
108 for quality in f['qualities']:
109 formats.append({
110 'format_id': '%s-%sp' % (get_text_attr(quality, 'bitrate'), get_text_attr(quality, 'height')),
111 'url': unescapeHTML(get_text_attr(quality, 'file')),
112 'height': int_or_none(get_text_attr(quality, 'height')),
113 'tbr': int_or_none(get_text_attr(quality, 'bitrate')),
114 })
115 if not formats:
116 path = unescapeHTML(get_text_attr(f, 'path'))
117 if not path:
118 continue
119 formats = self._extract_m3u8_formats(
120 path, video_id, 'mp4', entry_protocol='m3u8_native',
121 m3u8_id='hls') if determine_ext(path) == 'm3u8' else [{'url': path, }]
122 self._sort_formats(formats)
123 entries.append({
124 'id': '%s_%d' % (video_id, partnum + 1),
125 'title': (
126 title if len(files) == 1 else
127 '%s part %d' % (title, partnum + 1)),
128 'formats': formats,
129 'description': description,
130 'thumbnail': thumbnail,
131 'duration': int_or_none(get_text_attr(f, 'length')),
132 'subtitles': {
133 'en': [{
134 'url': capfile,
135 'ext': determine_ext(capfile, 'dfxp')
136 }],
137 } if capfile else None,
138 })
139
140 if len(entries) == 1:
141 entry = dict(entries[0])
142 entry['id'] = 'c' + video_id if video_type == 'clip' else video_id
143 return entry
144 else:
145 return {
146 '_type': 'playlist',
147 'entries': entries,
148 'title': title,
149 'id': 'c' + video_id if video_type == 'clip' else video_id,
150 }