Merge pull request #14225 from Tithen-Firion/openload-phantomjs-method
[youtube-dl] / youtube_dl / extractor / crackle.py
1 # coding: utf-8
2 from __future__ import unicode_literals, division
3
4 from .common import InfoExtractor
5 from ..utils import int_or_none
6
7
8 class CrackleIE(InfoExtractor):
9     _GEO_COUNTRIES = ['US']
10     _VALID_URL = r'(?:crackle:|https?://(?:(?:www|m)\.)?crackle\.com/(?:playlist/\d+/|(?:[^/]+/)+))(?P<id>\d+)'
11     _TEST = {
12         'url': 'http://www.crackle.com/comedians-in-cars-getting-coffee/2498934',
13         'info_dict': {
14             'id': '2498934',
15             'ext': 'mp4',
16             'title': 'Everybody Respects A Bloody Nose',
17             'description': 'Jerry is kaffeeklatsching in L.A. with funnyman J.B. Smoove (Saturday Night Live, Real Husbands of Hollywood). They’re headed for brew at 10 Speed Coffee in a 1964 Studebaker Avanti.',
18             'thumbnail': r're:^https?://.*\.jpg',
19             'duration': 906,
20             'series': 'Comedians In Cars Getting Coffee',
21             'season_number': 8,
22             'episode_number': 4,
23             'subtitles': {
24                 'en-US': [
25                     {'ext': 'vtt'},
26                     {'ext': 'tt'},
27                 ]
28             },
29         },
30         'params': {
31             # m3u8 download
32             'skip_download': True,
33         }
34     }
35
36     _THUMBNAIL_RES = [
37         (120, 90),
38         (208, 156),
39         (220, 124),
40         (220, 220),
41         (240, 180),
42         (250, 141),
43         (315, 236),
44         (320, 180),
45         (360, 203),
46         (400, 300),
47         (421, 316),
48         (460, 330),
49         (460, 460),
50         (462, 260),
51         (480, 270),
52         (587, 330),
53         (640, 480),
54         (700, 330),
55         (700, 394),
56         (854, 480),
57         (1024, 1024),
58         (1920, 1080),
59     ]
60
61     # extracted from http://legacyweb-us.crackle.com/flash/ReferrerRedirect.ashx
62     _MEDIA_FILE_SLOTS = {
63         'c544.flv': {
64             'width': 544,
65             'height': 306,
66         },
67         '360p.mp4': {
68             'width': 640,
69             'height': 360,
70         },
71         '480p.mp4': {
72             'width': 852,
73             'height': 478,
74         },
75         '480p_1mbps.mp4': {
76             'width': 852,
77             'height': 478,
78         },
79     }
80
81     def _real_extract(self, url):
82         video_id = self._match_id(url)
83
84         config_doc = self._download_xml(
85             'http://legacyweb-us.crackle.com/flash/QueryReferrer.ashx?site=16',
86             video_id, 'Downloading config')
87
88         item = self._download_xml(
89             'http://legacyweb-us.crackle.com/app/revamp/vidwallcache.aspx?flags=-1&fm=%s' % video_id,
90             video_id, headers=self.geo_verification_headers()).find('i')
91         title = item.attrib['t']
92
93         subtitles = {}
94         formats = self._extract_m3u8_formats(
95             'http://content.uplynk.com/ext/%s/%s.m3u8' % (config_doc.attrib['strUplynkOwnerId'], video_id),
96             video_id, 'mp4', m3u8_id='hls', fatal=None)
97         thumbnails = []
98         path = item.attrib.get('p')
99         if path:
100             for width, height in self._THUMBNAIL_RES:
101                 res = '%dx%d' % (width, height)
102                 thumbnails.append({
103                     'id': res,
104                     'url': 'http://images-us-am.crackle.com/%stnl_%s.jpg' % (path, res),
105                     'width': width,
106                     'height': height,
107                     'resolution': res,
108                 })
109             http_base_url = 'http://ahttp.crackle.com/' + path
110             for mfs_path, mfs_info in self._MEDIA_FILE_SLOTS.items():
111                 formats.append({
112                     'url': http_base_url + mfs_path,
113                     'format_id': 'http-' + mfs_path.split('.')[0],
114                     'width': mfs_info['width'],
115                     'height': mfs_info['height'],
116                 })
117             for cc in item.findall('cc'):
118                 locale = cc.attrib.get('l')
119                 v = cc.attrib.get('v')
120                 if locale and v:
121                     if locale not in subtitles:
122                         subtitles[locale] = []
123                     for url_ext, ext in (('vtt', 'vtt'), ('xml', 'tt')):
124                         subtitles.setdefault(locale, []).append({
125                             'url': '%s/%s%s_%s.%s' % (config_doc.attrib['strSubtitleServer'], path, locale, v, url_ext),
126                             'ext': ext,
127                         })
128         self._sort_formats(formats, ('width', 'height', 'tbr', 'format_id'))
129
130         return {
131             'id': video_id,
132             'title': title,
133             'description': item.attrib.get('d'),
134             'duration': int(item.attrib.get('r'), 16) / 1000 if item.attrib.get('r') else None,
135             'series': item.attrib.get('sn'),
136             'season_number': int_or_none(item.attrib.get('se')),
137             'episode_number': int_or_none(item.attrib.get('ep')),
138             'thumbnails': thumbnails,
139             'subtitles': subtitles,
140             'formats': formats,
141         }