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