[cspan] handle error massages and extract qualities
[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
16
17 def get_text_attr(d, attr):
18     return d.get(attr, {}).get('#text')
19
20
21 class CSpanIE(InfoExtractor):
22     _VALID_URL = r'http://(?:www\.)?c-span\.org/video/\?(?P<id>[0-9a-f]+)'
23     IE_DESC = 'C-SPAN'
24     _TESTS = [{
25         'url': 'http://www.c-span.org/video/?313572-1/HolderonV',
26         'md5': '94b29a4f131ff03d23471dd6f60b6a1d',
27         'info_dict': {
28             'id': '315139',
29             'ext': 'mp4',
30             'title': 'Attorney General Eric Holder on Voting Rights Act Decision',
31             '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.',
32         },
33         'skip': 'Regularly fails on travis, for unknown reasons',
34     }, {
35         'url': 'http://www.c-span.org/video/?c4486943/cspan-international-health-care-models',
36         'md5': '8e5fbfabe6ad0f89f3012a7943c1287b',
37         'info_dict': {
38             'id': 'c4486943',
39             'ext': 'mp4',
40             'title': 'CSPAN - International Health Care Models',
41             'description': 'md5:7a985a2d595dba00af3d9c9f0783c967',
42         }
43     }, {
44         'url': 'http://www.c-span.org/video/?318608-1/gm-ignition-switch-recall',
45         'md5': '2ae5051559169baadba13fc35345ae74',
46         'info_dict': {
47             'id': '342759',
48             'ext': 'mp4',
49             'title': 'General Motors Ignition Switch Recall',
50             'duration': 14848,
51             'description': 'md5:118081aedd24bf1d3b68b3803344e7f3'
52         },
53     }, {
54         # Video from senate.gov
55         'url': 'http://www.c-span.org/video/?104517-1/immigration-reforms-needed-protect-skilled-american-workers',
56         'info_dict': {
57             'id': 'judiciary031715',
58             'ext': 'flv',
59             'title': 'Immigration Reforms Needed to Protect Skilled American Workers',
60         }
61     }]
62
63     def _real_extract(self, url):
64         video_id = self._match_id(url)
65         webpage = self._download_webpage(url, video_id)
66         matches = re.search(r'data-(prog|clip)id=\'([0-9]+)\'', webpage)
67         if matches:
68             video_type, video_id = matches.groups()
69             if video_type == 'prog':
70                 video_type = 'program'
71         else:
72             senate_isvp_url = SenateISVPIE._search_iframe_url(webpage)
73             if senate_isvp_url:
74                 title = self._og_search_title(webpage)
75                 surl = smuggle_url(senate_isvp_url, {'force_title': title})
76                 return self.url_result(surl, 'SenateISVP', video_id, title)
77
78         data = self._download_json(
79             'http://www.c-span.org/assets/player/ajax-player.php?os=android&html5=%s&id=%s' % (video_type, video_id),
80             video_id)['video']
81         if data['@status'] != 'Success':
82             raise ExtractorError('%s said: %s' % (self.IE_NAME, get_text_attr(data, 'error')), expected=True)
83
84         doc = self._download_xml(
85             'http://www.c-span.org/common/services/flashXml.php?%sid=%s' % (video_type, video_id),
86             video_id)
87
88         description = self._html_search_meta('description', webpage)
89
90         title = find_xpath_attr(doc, './/string', 'name', 'title').text
91         thumbnail = find_xpath_attr(doc, './/string', 'name', 'poster').text
92
93         files = data['files']
94         capfile = get_text_attr(data, 'capfile')
95
96         entries = []
97         for partnum, f in enumerate(files):
98             formats = []
99             for quality in f['qualities']:
100                 formats.append({
101                     'format_id': '%s-%sp' % (get_text_attr(quality, 'bitrate'), get_text_attr(quality, 'height')),
102                     'url': unescapeHTML(get_text_attr(quality, 'file')),
103                     'height': int_or_none(get_text_attr(quality, 'height')),
104                     'tbr': int_or_none(get_text_attr(quality, 'bitrate')),
105                 })
106             self._sort_formats(formats)
107             entries.append({
108                 'id': '%s_%d' % (video_id, partnum + 1),
109                 'title': (
110                     title if len(files) == 1 else
111                     '%s part %d' % (title, partnum + 1)),
112                 'formats': formats,
113                 'description': description,
114                 'thumbnail': thumbnail,
115                 'duration': int_or_none(get_text_attr(f, 'length')),
116                 'subtitles': {
117                     'en': [{
118                         'url': capfile,
119                         'ext': determine_ext(capfile, 'dfxp')
120                     }],
121                 } if capfile else None,
122             })
123
124         if len(entries) == 1:
125             entry = dict(entries[0])
126             entry['id'] = 'c' + video_id if video_type == 'clip' else video_id
127             return entry
128         else:
129             return {
130                 '_type': 'playlist',
131                 'entries': entries,
132                 'title': title,
133                 'id': 'c' + video_id if video_type == 'clip' else video_id,
134             }