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