[CSpan] Add detection for Senate ISVP. Closes #5302
[youtube-dl] / youtube_dl / extractor / senateisvp.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5 from .common import InfoExtractor
6 from ..utils import (
7     ExtractorError,
8     unsmuggle_url,
9 )
10 from ..compat import (
11     compat_parse_qs,
12     compat_urlparse,
13 )
14
15
16 class SenateISVPIE(InfoExtractor):
17     _COMM_MAP = [
18         ["ag", "76440", "http://ag-f.akamaihd.net"],
19         ["aging", "76442", "http://aging-f.akamaihd.net"],
20         ["approps", "76441", "http://approps-f.akamaihd.net"],
21         ["armed", "76445", "http://armed-f.akamaihd.net"],
22         ["banking", "76446", "http://banking-f.akamaihd.net"],
23         ["budget", "76447", "http://budget-f.akamaihd.net"],
24         ["cecc", "76486", "http://srs-f.akamaihd.net"],
25         ["commerce", "80177", "http://commerce1-f.akamaihd.net"],
26         ["csce", "75229", "http://srs-f.akamaihd.net"],
27         ["dpc", "76590", "http://dpc-f.akamaihd.net"],
28         ["energy", "76448", "http://energy-f.akamaihd.net"],
29         ["epw", "76478", "http://epw-f.akamaihd.net"],
30         ["ethics", "76449", "http://ethics-f.akamaihd.net"],
31         ["finance", "76450", "http://finance-f.akamaihd.net"],
32         ["foreign", "76451", "http://foreign-f.akamaihd.net"],
33         ["govtaff", "76453", "http://govtaff-f.akamaihd.net"],
34         ["help", "76452", "http://help-f.akamaihd.net"],
35         ["indian", "76455", "http://indian-f.akamaihd.net"],
36         ["intel", "76456", "http://intel-f.akamaihd.net"],
37         ["intlnarc", "76457", "http://intlnarc-f.akamaihd.net"],
38         ["jccic", "85180", "http://jccic-f.akamaihd.net"],
39         ["jec", "76458", "http://jec-f.akamaihd.net"],
40         ["judiciary", "76459", "http://judiciary-f.akamaihd.net"],
41         ["rpc", "76591", "http://rpc-f.akamaihd.net"],
42         ["rules", "76460", "http://rules-f.akamaihd.net"],
43         ["saa", "76489", "http://srs-f.akamaihd.net"],
44         ["smbiz", "76461", "http://smbiz-f.akamaihd.net"],
45         ["srs", "75229", "http://srs-f.akamaihd.net"],
46         ["uscc", "76487", "http://srs-f.akamaihd.net"],
47         ["vetaff", "76462", "http://vetaff-f.akamaihd.net"],
48         ["arch", "", "http://ussenate-f.akamaihd.net/"]
49     ]
50     _IE_NAME = 'senate.gov'
51     _VALID_URL = r'http://www\.senate\.gov/isvp/\?(?P<qs>.+)'
52     _TESTS = [{
53         'url': 'http://www.senate.gov/isvp/?comm=judiciary&type=live&stt=&filename=judiciary031715&auto_play=false&wmode=transparent&poster=http%3A%2F%2Fwww.judiciary.senate.gov%2Fthemes%2Fjudiciary%2Fimages%2Fvideo-poster-flash-fit.png',
54         'md5': '7314c4b96dad66dd8e63dc3518ceaa6f',
55         'info_dict': {
56             'id': 'judiciary031715',
57             'ext': 'flv',
58             'title': 'Integrated Senate Video Player',
59             'thumbnail': 're:^https?://.*\.(?:jpg|png)$',
60         }
61     }, {
62         'url': 'http://www.senate.gov/isvp/?type=live&comm=commerce&filename=commerce011514.mp4&auto_play=false',
63         'md5': '2917c827513700aa9b70eaebf25116da',
64         'info_dict': {
65             'id': 'commerce011514',
66             'ext': 'flv',
67             'title': 'Integrated Senate Video Player'
68         }
69     }, {
70         'url': 'http://www.senate.gov/isvp/?type=arch&comm=intel&filename=intel090613&hc_location=ufi',
71         # checksum differs each time
72         'info_dict': {
73             'id': 'intel090613',
74             'ext': 'mp4',
75             'title': 'Integrated Senate Video Player'
76         }
77     }]
78
79     @staticmethod
80     def _search_iframe_url(webpage):
81         mobj = re.search(
82             r"<iframe[^>]+src=['\"](?P<url>http://www\.senate\.gov/isvp/\?[^'\"]+)['\"]",
83             webpage)
84         if mobj:
85             return mobj.group('url')
86
87     def _get_info_for_comm(self, committee):
88         for entry in self._COMM_MAP:
89             if entry[0] == committee:
90                 return entry[1:]
91
92     def _real_extract(self, url):
93         url, smuggled_data = unsmuggle_url(url, {})
94
95         qs = compat_parse_qs(re.match(self._VALID_URL, url).group('qs'))
96         if not qs.get('filename') or not qs.get('type') or not qs.get('comm'):
97             raise ExtractorError('Invalid URL', expected=True)
98
99         video_id = re.sub(r'.mp4$', '', qs['filename'][0])
100
101         webpage = self._download_webpage(url, video_id)
102
103         if smuggled_data.get('force_title'):
104             title = smuggled_data['force_title']
105         else:
106             title = self._html_search_regex(r'<title>([^<]+)</title>', webpage, video_id)
107         poster = qs.get('poster')
108         if poster:
109             thumbnail = poster[0]
110
111         video_type = qs['type'][0]
112         committee = video_type if video_type == 'arch' else qs['comm'][0]
113         stream_num, domain = self._get_info_for_comm(committee)
114
115         formats = []
116         if video_type == 'arch':
117             filename = video_id if '.' in video_id else video_id + '.mp4'
118             formats = [{
119                 # All parameters in the query string are necessary to prevent a 403 error
120                 'url': compat_urlparse.urljoin(domain, filename) + '?v=3.1.0&fp=&r=&g=',
121             }]
122         else:
123             hdcore_sign = '?hdcore=3.1.0'
124             url_params = (domain, video_id, stream_num)
125             f4m_url = '%s/z/%s_1@%s/manifest.f4m' % url_params + hdcore_sign
126             m3u8_url = '%s/i/%s_1@%s/master.m3u8' % url_params
127             for entry in self._extract_f4m_formats(f4m_url, video_id, f4m_id='f4m'):
128                 # URLs without the extra param induce an 404 error
129                 entry.update({'extra_param_to_segment_url': hdcore_sign})
130                 formats.append(entry)
131             for entry in self._extract_m3u8_formats(m3u8_url, video_id, ext='mp4', m3u8_id='m3u8'):
132                 mobj = re.search(r'(?P<tag>(?:-p|-b)).m3u8', entry['url'])
133                 if mobj:
134                     entry['format_id'] += mobj.group('tag')
135                 formats.append(entry)
136
137             self._sort_formats(formats)
138
139         info_dict = {
140             'id': video_id,
141             'title': title,
142             'thumbnail': thumbnail,
143         }
144
145         if len(formats) >= 1:
146             info_dict.update({'formats': formats})
147         else:
148             info_dict.update(formats[0])
149
150         return info_dict