[crackle] Add new extractor
[youtube-dl] / youtube_dl / extractor / crackle.py
1 # coding: utf-8
2 from __future__ import unicode_literals
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\.)?crackle\.com/(?:playlist/\d+/|(?:[^/]+/)+))(?P<id>\d+)'
10     _TEST = {
11         'url': 'http://www.crackle.com/the-art-of-more/2496419',
12         'info_dict': {
13             'id': '2496419',
14             'ext': 'mp4',
15             'title': 'Heavy Lies the Head',
16             'description': 'md5:bb56aa0708fe7b9a4861535f15c3abca',
17         },
18         'params': {
19             # m3u8 download
20             'skip_download': True,
21         }
22     }
23
24     # extracted from http://legacyweb-us.crackle.com/flash/QueryReferrer.ashx
25     _SUBTITLE_SERVER = 'http://web-us-az.crackle.com'
26     _UPLYNK_OWNER_ID = 'e8773f7770a44dbd886eee4fca16a66b'
27     _THUMBNAIL_TEMPLATE = 'http://images-us-am.crackle.com/%stnl_1920x1080.jpg?ts=20140107233116?c=635333335057637614'
28
29     # extracted from http://legacyweb-us.crackle.com/flash/ReferrerRedirect.ashx
30     _MEDIA_FILE_SLOTS = {
31         'c544.flv': {
32             'width': 544,
33             'height': 306,
34         },
35         '360p.mp4': {
36             'width': 640,
37             'height': 360,
38         },
39         '480p.mp4': {
40             'width': 852,
41             'height': 478,
42         },
43         '480p_1mbps.mp4': {
44             'width': 852,
45             'height': 478,
46         },
47     }
48
49     def _real_extract(self, url):
50         video_id = self._match_id(url)
51         item = self._download_xml(
52             'http://legacyweb-us.crackle.com/app/revamp/vidwallcache.aspx?flags=-1&fm=%s' % video_id, video_id).find('i')
53         title = item.attrib['t']
54
55         thumbnail = None
56         subtitles = {}
57         formats = self._extract_m3u8_formats('http://content.uplynk.com/ext/%s/%s.m3u8' % (self._UPLYNK_OWNER_ID, video_id), video_id, 'mp4', fatal=None)
58         path = item.attrib.get('p')
59         if path:
60             thumbnail = self._THUMBNAIL_TEMPLATE % path
61             http_base_url = 'http://ahttp.crackle.com/' + path
62             for mfs_path, mfs_info in self._MEDIA_FILE_SLOTS.items():
63                 formats.append({
64                     'url': http_base_url + mfs_path,
65                     'format_id': mfs_path.split('.')[0],
66                     'width': mfs_info['width'],
67                     'height': mfs_info['height'],
68                 })
69             for cc in item.findall('cc'):
70                 locale = cc.attrib.get('l')
71                 v = cc.attrib.get('v')
72                 if locale and v:
73                     if locale not in subtitles:
74                         subtitles[locale] = []
75                     subtitles[locale] = [{
76                         'url': '%s/%s%s_%s.xml' % (self._SUBTITLE_SERVER, path, locale, v),
77                         'ext': 'ttml',
78                     }]
79         self._sort_formats(formats, ('width', 'height', 'tbr'))
80
81         return {
82             'id': video_id,
83             'title': title,
84             'description': item.attrib.get('d'),
85             'duration': int(item.attrib.get('r'), 16) if item.attrib.get('r') else None,
86             'series': item.attrib.get('sn'),
87             'season_number': int_or_none(item.attrib.get('se')),
88             'episode_number': int_or_none(item.attrib.get('ep')),
89             'thumbnail': thumbnail,
90             'subtitles': subtitles,
91             'formats': formats,
92         }