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