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