[crackle] Fix extraction and update _TESTS (closes #10333)
[youtube-dl] / youtube_dl / extractor / crackle.py
1 # coding: utf-8
2 from __future__ import unicode_literals, division
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import int_or_none
8
9
10 class CrackleIE(InfoExtractor):
11     _VALID_URL = r'(?:crackle:|https?://(?:www\.)?crackle\.com/(?:playlist/\d+/|(?:[^/]+/)+))(?P<id>\d+)'
12     _TEST = {
13         'url': 'http://www.crackle.com/comedians-in-cars-getting-coffee/2498934',
14         'info_dict': {
15             'id': '2498934',
16             'ext': 'mp4',
17             'title': 'Everybody Respects A Bloody Nose',
18             '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.',
19             'thumbnail': 're:^https?://.*\.jpg',
20             'duration': 906,
21             'series': 'Comedians In Cars Getting Coffee',
22             'season_number': 8,
23             'episode_number': 4,
24             'subtitles': {
25                 'en-US': [{
26                     'ext': 'ttml',
27                 }]
28             },
29         },
30         'params': {
31             # m3u8 download
32             'skip_download': True,
33         }
34     }
35
36     # extracted from http://legacyweb-us.crackle.com/flash/ReferrerRedirect.ashx
37     _MEDIA_FILE_SLOTS = {
38         'c544.flv': {
39             'width': 544,
40             'height': 306,
41         },
42         '360p.mp4': {
43             'width': 640,
44             'height': 360,
45         },
46         '480p.mp4': {
47             'width': 852,
48             'height': 478,
49         },
50         '480p_1mbps.mp4': {
51             'width': 852,
52             'height': 478,
53         },
54     }
55
56     def _real_extract(self, url):
57         video_id = self._match_id(url)
58
59         config_doc = self._download_xml(
60             'http://legacyweb-us.crackle.com/flash/QueryReferrer.ashx?site=16',
61             video_id, 'Downloading config')
62
63         item = self._download_xml(
64             'http://legacyweb-us.crackle.com/app/revamp/vidwallcache.aspx?flags=-1&fm=%s' % video_id,
65             video_id).find('i')
66         title = item.attrib['t']
67
68         subtitles = {}
69         formats = self._extract_m3u8_formats(
70             'http://content.uplynk.com/ext/%s/%s.m3u8' % (config_doc.attrib['strUplynkOwnerId'], video_id),
71             video_id, 'mp4', m3u8_id='hls', fatal=None)
72         path = item.attrib.get('p')
73         if path:
74             http_base_url = 'http://ahttp.crackle.com/' + path
75             for mfs_path, mfs_info in self._MEDIA_FILE_SLOTS.items():
76                 formats.append({
77                     'url': http_base_url + mfs_path,
78                     'format_id': 'http-' + mfs_path.split('.')[0],
79                     'width': mfs_info['width'],
80                     'height': mfs_info['height'],
81                 })
82             for cc in item.findall('cc'):
83                 locale = cc.attrib.get('l')
84                 v = cc.attrib.get('v')
85                 if locale and v:
86                     if locale not in subtitles:
87                         subtitles[locale] = []
88                     subtitles[locale] = [{
89                         'url': '%s/%s%s_%s.xml' % (config_doc.attrib['strSubtitleServer'], path, locale, v),
90                         'ext': 'ttml',
91                     }]
92         self._sort_formats(formats, ('width', 'height', 'tbr', 'format_id'))
93
94         media_details = self._download_json(
95             'https://web-api-us.crackle.com/Service.svc/details/media/%s/TW?format=json' % video_id,
96             video_id, fatal=False)
97         thumbnails = []
98         if media_details:
99             for key, value in media_details.items():
100                 mobj = re.match('^Thumbnail_(\d+)x(\d+)$', key)
101                 if mobj:
102                     width, height = list(map(int, mobj.groups()))
103                     thumbnails.append({
104                         'id': '%dp' % height,
105                         'url': value,
106                         'width': width,
107                         'height': height,
108                     })
109
110         return {
111             'id': video_id,
112             'title': title,
113             'description': item.attrib.get('d'),
114             'duration': int(item.attrib.get('r'), 16) / 1000 if item.attrib.get('r') else None,
115             'series': item.attrib.get('sn'),
116             'season_number': int_or_none(item.attrib.get('se')),
117             'episode_number': int_or_none(item.attrib.get('ep')),
118             'thumbnails': thumbnails,
119             'subtitles': subtitles,
120             'formats': formats,
121         }