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