[CSpan] Add detection for Senate ISVP. Closes #5302
[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 )
12 from .senateisvp import SenateISVPIE
13
14
15 class CSpanIE(InfoExtractor):
16     _VALID_URL = r'http://(?:www\.)?c-span\.org/video/\?(?P<id>[0-9a-f]+)'
17     IE_DESC = 'C-SPAN'
18     _TESTS = [{
19         'url': 'http://www.c-span.org/video/?313572-1/HolderonV',
20         'md5': '8e44ce11f0f725527daccc453f553eb0',
21         'info_dict': {
22             'id': '315139',
23             'ext': 'mp4',
24             'title': 'Attorney General Eric Holder on Voting Rights Act Decision',
25             'description': 'Attorney General Eric Holder spoke 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 until Congress established new guidelines for review.',
26         },
27         'skip': 'Regularly fails on travis, for unknown reasons',
28     }, {
29         'url': 'http://www.c-span.org/video/?c4486943/cspan-international-health-care-models',
30         # For whatever reason, the served video alternates between
31         # two different ones
32         'info_dict': {
33             'id': '340723',
34             'ext': 'mp4',
35             'title': '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         'info_dict': {
41             'id': '342759',
42             'title': 'General Motors Ignition Switch Recall',
43         },
44         'playlist_duration_sum': 14855,
45     }, {
46         # Video from senate.gov
47         'url': 'http://www.c-span.org/video/?104517-1/immigration-reforms-needed-protect-skilled-american-workers',
48         'md5': '7314c4b96dad66dd8e63dc3518ceaa6f',
49         'info_dict': {
50             'id': 'judiciary031715',
51             'ext': 'flv',
52             'title': 'Immigration Reforms Needed to Protect Skilled American Workers',
53         }
54     }]
55
56     def _real_extract(self, url):
57         mobj = re.match(self._VALID_URL, url)
58         page_id = mobj.group('id')
59         webpage = self._download_webpage(url, page_id)
60         video_id = self._search_regex(r'progid=\'?([0-9]+)\'?>', webpage, 'video id')
61
62         description = self._html_search_regex(
63             [
64                 # The full description
65                 r'<div class=\'expandable\'>(.*?)<a href=\'#\'',
66                 # If the description is small enough the other div is not
67                 # present, otherwise this is a stripped version
68                 r'<p class=\'initial\'>(.*?)</p>'
69             ],
70             webpage, 'description', flags=re.DOTALL, default=None)
71
72         info_url = 'http://c-spanvideo.org/videoLibrary/assets/player/ajax-player.php?os=android&html5=program&id=' + video_id
73         data = self._download_json(info_url, video_id)
74
75         doc = self._download_xml(
76             'http://www.c-span.org/common/services/flashXml.php?programid=' + video_id,
77             video_id)
78
79         title = find_xpath_attr(doc, './/string', 'name', 'title').text
80         thumbnail = find_xpath_attr(doc, './/string', 'name', 'poster').text
81
82         senate_isvp_url = SenateISVPIE._search_iframe_url(webpage)
83         if senate_isvp_url:
84             surl = smuggle_url(senate_isvp_url, {'force_title': title})
85             return self.url_result(surl, 'SenateISVP', video_id, title)
86
87         files = data['video']['files']
88
89         entries = [{
90             'id': '%s_%d' % (video_id, partnum + 1),
91             'title': (
92                 title if len(files) == 1 else
93                 '%s part %d' % (title, partnum + 1)),
94             'url': unescapeHTML(f['path']['#text']),
95             'description': description,
96             'thumbnail': thumbnail,
97             'duration': int_or_none(f.get('length', {}).get('#text')),
98         } for partnum, f in enumerate(files)]
99
100         return {
101             '_type': 'playlist',
102             'entries': entries,
103             'title': title,
104             'id': video_id,
105         }