]>
Commit | Line | Data |
---|---|---|
ed86f38a JMF |
1 | from __future__ import unicode_literals |
2 | ||
e9bf7479 JMF |
3 | import re |
4 | import json | |
9fb2f1cd S |
5 | import time |
6 | import hmac | |
7 | import binascii | |
8 | import hashlib | |
9 | ||
e9bf7479 | 10 | |
8807f127 | 11 | from .common import InfoExtractor |
1cc79574 | 12 | from ..compat import ( |
e35cb78c | 13 | compat_str, |
1cc79574 PH |
14 | ) |
15 | from ..utils import ( | |
10e3d734 | 16 | determine_ext, |
f8b56e95 | 17 | ExtractorError, |
e9bf7479 | 18 | xpath_with_ns, |
9fb2f1cd | 19 | unsmuggle_url, |
e9bf7479 JMF |
20 | ) |
21 | ||
22 | _x = lambda p: xpath_with_ns(p, {'smil': 'http://www.w3.org/2005/SMIL21/Language'}) | |
23 | ||
24 | ||
8807f127 | 25 | class ThePlatformIE(InfoExtractor): |
a97bcd80 | 26 | _VALID_URL = r'''(?x) |
9fb2f1cd | 27 | (?:https?://(?:link|player)\.theplatform\.com/[sp]/(?P<provider_id>[^/]+)/ |
020cf5eb | 28 | (?P<config>(?:[^/\?]+/(?:swf|config)|onsite)/select/)? |
a97bcd80 | 29 | |theplatform:)(?P<id>[^/\?&]+)''' |
e9bf7479 JMF |
30 | |
31 | _TEST = { | |
32 | # from http://www.metacafe.com/watch/cb-e9I_cZgTgIPd/blackberrys_big_bold_z30/ | |
ed86f38a JMF |
33 | 'url': 'http://link.theplatform.com/s/dJ5BDC/e9I_cZgTgIPd/meta.smil?format=smil&Tracking=true&mbr=true', |
34 | 'info_dict': { | |
35 | 'id': 'e9I_cZgTgIPd', | |
36 | 'ext': 'flv', | |
37 | 'title': 'Blackberry\'s big, bold Z30', | |
38 | 'description': 'The Z30 is Blackberry\'s biggest, baddest mobile messaging device yet.', | |
39 | 'duration': 247, | |
e9bf7479 | 40 | }, |
ed86f38a | 41 | 'params': { |
e9bf7479 | 42 | # rtmp download |
ed86f38a | 43 | 'skip_download': True, |
e9bf7479 JMF |
44 | }, |
45 | } | |
5f6a1245 | 46 | |
9fb2f1cd S |
47 | @staticmethod |
48 | def _sign_url(url, sig_key, sig_secret, life=600, include_qs=False): | |
49 | flags = '10' if include_qs else '00' | |
50 | expiration_date = '%x' % (int(time.time()) + life) | |
51 | ||
52 | def str_to_hex(str): | |
53 | return binascii.b2a_hex(str.encode('ascii')).decode('ascii') | |
54 | ||
55 | def hex_to_str(hex): | |
56 | return binascii.a2b_hex(hex) | |
57 | ||
58 | relative_path = url.split('http://link.theplatform.com/s/')[1].split('?')[0] | |
59 | clear_text = hex_to_str(flags + expiration_date + str_to_hex(relative_path)) | |
60 | checksum = hmac.new(sig_key.encode('ascii'), clear_text, hashlib.sha1).hexdigest() | |
61 | sig = flags + expiration_date + checksum + str_to_hex(sig_secret) | |
62 | return '%s&sig=%s' % (url, sig) | |
63 | ||
10e3d734 | 64 | def _real_extract(self, url): |
9fb2f1cd S |
65 | url, smuggled_data = unsmuggle_url(url, {}) |
66 | ||
10e3d734 | 67 | mobj = re.match(self._VALID_URL, url) |
9fb2f1cd | 68 | provider_id = mobj.group('provider_id') |
10e3d734 | 69 | video_id = mobj.group('id') |
9fb2f1cd S |
70 | |
71 | if not provider_id: | |
72 | provider_id = 'dJ5BDC' | |
73 | ||
6140baf4 JMF |
74 | if smuggled_data.get('force_smil_url', False): |
75 | smil_url = url | |
76 | elif mobj.group('config'): | |
5f6a1245 | 77 | config_url = url + '&form=json' |
10e3d734 PH |
78 | config_url = config_url.replace('swf/', 'config/') |
79 | config_url = config_url.replace('onsite/', 'onsite/config/') | |
80 | config = self._download_json(config_url, video_id, 'Downloading config') | |
81 | smil_url = config['releaseUrl'] + '&format=SMIL&formats=MPEG4&manifest=f4m' | |
82 | else: | |
9fb2f1cd S |
83 | smil_url = ('http://link.theplatform.com/s/{0}/{1}/meta.smil?' |
84 | 'format=smil&mbr=true'.format(provider_id, video_id)) | |
85 | ||
86 | sig = smuggled_data.get('sig') | |
87 | if sig: | |
88 | smil_url = self._sign_url(smil_url, sig['key'], sig['secret']) | |
e9bf7479 | 89 | |
10e3d734 | 90 | meta = self._download_xml(smil_url, video_id) |
f8b56e95 PH |
91 | try: |
92 | error_msg = next( | |
93 | n.attrib['abstract'] | |
94 | for n in meta.findall(_x('.//smil:ref')) | |
ed86f38a | 95 | if n.attrib.get('title') == 'Geographic Restriction') |
f8b56e95 PH |
96 | except StopIteration: |
97 | pass | |
98 | else: | |
99 | raise ExtractorError(error_msg, expected=True) | |
100 | ||
9fb2f1cd | 101 | info_url = 'http://link.theplatform.com/s/{0}/{1}?format=preview'.format(provider_id, video_id) |
e9bf7479 JMF |
102 | info_json = self._download_webpage(info_url, video_id) |
103 | info = json.loads(info_json) | |
104 | ||
748ec667 S |
105 | subtitles = {} |
106 | captions = info.get('captions') | |
107 | if isinstance(captions, list): | |
108 | for caption in captions: | |
8807f127 JMF |
109 | lang, src, mime = caption.get('lang', 'en'), caption.get('src'), caption.get('type') |
110 | subtitles[lang] = [{ | |
111 | 'ext': 'srt' if mime == 'text/srt' else 'ttml', | |
112 | 'url': src, | |
113 | }] | |
748ec667 | 114 | |
e9bf7479 JMF |
115 | head = meta.find(_x('smil:head')) |
116 | body = meta.find(_x('smil:body')) | |
360babf7 | 117 | |
22d99a80 | 118 | f4m_node = body.find(_x('smil:seq//smil:video')) |
e35cb78c | 119 | if f4m_node is not None and '.f4m' in f4m_node.attrib['src']: |
020cf5eb JMF |
120 | f4m_url = f4m_node.attrib['src'] |
121 | if 'manifest.f4m?' not in f4m_url: | |
122 | f4m_url += '?' | |
123 | # the parameters are from syfy.com, other sites may use others, | |
124 | # they also work for nbc.com | |
125 | f4m_url += '&g=UXWGVKRWHFSP&hdcore=3.0.3' | |
dd41e8c8 | 126 | formats = self._extract_f4m_formats(f4m_url, video_id) |
a97bcd80 | 127 | else: |
a97bcd80 | 128 | formats = [] |
e35cb78c JMF |
129 | switch = body.find(_x('smil:switch')) |
130 | if switch is not None: | |
131 | base_url = head.find(_x('smil:meta')).attrib['base'] | |
132 | for f in switch.findall(_x('smil:video')): | |
133 | attr = f.attrib | |
134 | width = int(attr['width']) | |
135 | height = int(attr['height']) | |
136 | vbr = int(attr['system-bitrate']) // 1000 | |
137 | format_id = '%dx%d_%dk' % (width, height, vbr) | |
138 | formats.append({ | |
139 | 'format_id': format_id, | |
140 | 'url': base_url, | |
141 | 'play_path': 'mp4:' + attr['src'], | |
142 | 'ext': 'flv', | |
143 | 'width': width, | |
144 | 'height': height, | |
145 | 'vbr': vbr, | |
146 | }) | |
147 | else: | |
148 | switch = body.find(_x('smil:seq//smil:switch')) | |
149 | for f in switch.findall(_x('smil:video')): | |
150 | attr = f.attrib | |
151 | vbr = int(attr['system-bitrate']) // 1000 | |
10e3d734 PH |
152 | ext = determine_ext(attr['src']) |
153 | if ext == 'once': | |
154 | ext = 'mp4' | |
e35cb78c JMF |
155 | formats.append({ |
156 | 'format_id': compat_str(vbr), | |
157 | 'url': attr['src'], | |
158 | 'vbr': vbr, | |
10e3d734 | 159 | 'ext': ext, |
e35cb78c | 160 | }) |
a97bcd80 | 161 | self._sort_formats(formats) |
e9bf7479 JMF |
162 | |
163 | return { | |
164 | 'id': video_id, | |
165 | 'title': info['title'], | |
748ec667 | 166 | 'subtitles': subtitles, |
e9bf7479 JMF |
167 | 'formats': formats, |
168 | 'description': info['description'], | |
169 | 'thumbnail': info['defaultThumbnailUrl'], | |
2514d263 | 170 | 'duration': info['duration'] // 1000, |
e9bf7479 | 171 | } |