]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/ooyala.py
Add option `--ignore-no-formats-error`
[yt-dlp.git] / yt_dlp / extractor / ooyala.py
1 from __future__ import unicode_literals
2
3 import base64
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import (
8 compat_b64decode,
9 compat_str,
10 )
11 from ..utils import (
12 determine_ext,
13 float_or_none,
14 int_or_none,
15 try_get,
16 unsmuggle_url,
17 )
18
19
20 class OoyalaBaseIE(InfoExtractor):
21 _PLAYER_BASE = 'http://player.ooyala.com/'
22 _CONTENT_TREE_BASE = _PLAYER_BASE + 'player_api/v1/content_tree/'
23 _AUTHORIZATION_URL_TEMPLATE = _PLAYER_BASE + 'sas/player_api/v2/authorization/embed_code/%s/%s'
24
25 def _extract(self, content_tree_url, video_id, domain=None, supportedformats=None, embed_token=None):
26 content_tree = self._download_json(content_tree_url, video_id)['content_tree']
27 metadata = content_tree[list(content_tree)[0]]
28 embed_code = metadata['embed_code']
29 pcode = metadata.get('asset_pcode') or embed_code
30 title = metadata['title']
31
32 auth_data = self._download_json(
33 self._AUTHORIZATION_URL_TEMPLATE % (pcode, embed_code),
34 video_id, headers=self.geo_verification_headers(), query={
35 'domain': domain or 'player.ooyala.com',
36 'supportedFormats': supportedformats or 'mp4,rtmp,m3u8,hds,dash,smooth',
37 'embedToken': embed_token,
38 })['authorization_data'][embed_code]
39
40 urls = []
41 formats = []
42 streams = auth_data.get('streams') or [{
43 'delivery_type': 'hls',
44 'url': {
45 'data': base64.b64encode(('http://player.ooyala.com/hls/player/all/%s.m3u8' % embed_code).encode()).decode(),
46 }
47 }]
48 for stream in streams:
49 url_data = try_get(stream, lambda x: x['url']['data'], compat_str)
50 if not url_data:
51 continue
52 s_url = compat_b64decode(url_data).decode('utf-8')
53 if not s_url or s_url in urls:
54 continue
55 urls.append(s_url)
56 ext = determine_ext(s_url, None)
57 delivery_type = stream.get('delivery_type')
58 if delivery_type == 'hls' or ext == 'm3u8':
59 formats.extend(self._extract_m3u8_formats(
60 re.sub(r'/ip(?:ad|hone)/', '/all/', s_url), embed_code, 'mp4', 'm3u8_native',
61 m3u8_id='hls', fatal=False))
62 elif delivery_type == 'hds' or ext == 'f4m':
63 formats.extend(self._extract_f4m_formats(
64 s_url + '?hdcore=3.7.0', embed_code, f4m_id='hds', fatal=False))
65 elif delivery_type == 'dash' or ext == 'mpd':
66 formats.extend(self._extract_mpd_formats(
67 s_url, embed_code, mpd_id='dash', fatal=False))
68 elif delivery_type == 'smooth':
69 self._extract_ism_formats(
70 s_url, embed_code, ism_id='mss', fatal=False)
71 elif ext == 'smil':
72 formats.extend(self._extract_smil_formats(
73 s_url, embed_code, fatal=False))
74 else:
75 formats.append({
76 'url': s_url,
77 'ext': ext or delivery_type,
78 'vcodec': stream.get('video_codec'),
79 'format_id': delivery_type,
80 'width': int_or_none(stream.get('width')),
81 'height': int_or_none(stream.get('height')),
82 'abr': int_or_none(stream.get('audio_bitrate')),
83 'vbr': int_or_none(stream.get('video_bitrate')),
84 'fps': float_or_none(stream.get('framerate')),
85 })
86 if not formats and not auth_data.get('authorized'):
87 self.raise_no_formats('%s said: %s' % (
88 self.IE_NAME, auth_data['message']), expected=True)
89 self._sort_formats(formats)
90
91 subtitles = {}
92 for lang, sub in metadata.get('closed_captions_vtt', {}).get('captions', {}).items():
93 sub_url = sub.get('url')
94 if not sub_url:
95 continue
96 subtitles[lang] = [{
97 'url': sub_url,
98 }]
99
100 return {
101 'id': embed_code,
102 'title': title,
103 'description': metadata.get('description'),
104 'thumbnail': metadata.get('thumbnail_image') or metadata.get('promo_image'),
105 'duration': float_or_none(metadata.get('duration'), 1000),
106 'subtitles': subtitles,
107 'formats': formats,
108 }
109
110
111 class OoyalaIE(OoyalaBaseIE):
112 _VALID_URL = r'(?:ooyala:|https?://.+?\.ooyala\.com/.*?(?:embedCode|ec)=)(?P<id>.+?)(&|$)'
113
114 _TESTS = [
115 {
116 # From http://it.slashdot.org/story/13/04/25/178216/recovering-data-from-broken-hard-drives-and-ssds-video
117 'url': 'http://player.ooyala.com/player.js?embedCode=pxczE2YjpfHfn1f3M-ykG_AmJRRn0PD8',
118 'info_dict': {
119 'id': 'pxczE2YjpfHfn1f3M-ykG_AmJRRn0PD8',
120 'ext': 'mp4',
121 'title': 'Explaining Data Recovery from Hard Drives and SSDs',
122 'description': 'How badly damaged does a drive have to be to defeat Russell and his crew? Apparently, smashed to bits.',
123 'duration': 853.386,
124 },
125 # The video in the original webpage now uses PlayWire
126 'skip': 'Ooyala said: movie expired',
127 }, {
128 # Only available for ipad
129 'url': 'http://player.ooyala.com/player.js?embedCode=x1b3lqZDq9y_7kMyC2Op5qo-p077tXD0',
130 'info_dict': {
131 'id': 'x1b3lqZDq9y_7kMyC2Op5qo-p077tXD0',
132 'ext': 'mp4',
133 'title': 'Simulation Overview - Levels of Simulation',
134 'duration': 194.948,
135 },
136 },
137 {
138 # Information available only through SAS api
139 # From http://community.plm.automation.siemens.com/t5/News-NX-Manufacturing/Tool-Path-Divide/ba-p/4187
140 'url': 'http://player.ooyala.com/player.js?embedCode=FiOG81ZTrvckcchQxmalf4aQj590qTEx',
141 'md5': 'a84001441b35ea492bc03736e59e7935',
142 'info_dict': {
143 'id': 'FiOG81ZTrvckcchQxmalf4aQj590qTEx',
144 'ext': 'mp4',
145 'title': 'Divide Tool Path.mp4',
146 'duration': 204.405,
147 }
148 },
149 {
150 # empty stream['url']['data']
151 'url': 'http://player.ooyala.com/player.js?embedCode=w2bnZtYjE6axZ_dw1Cd0hQtXd_ige2Is',
152 'only_matching': True,
153 }
154 ]
155
156 @staticmethod
157 def _url_for_embed_code(embed_code):
158 return 'http://player.ooyala.com/player.js?embedCode=%s' % embed_code
159
160 @classmethod
161 def _build_url_result(cls, embed_code):
162 return cls.url_result(cls._url_for_embed_code(embed_code),
163 ie=cls.ie_key())
164
165 def _real_extract(self, url):
166 url, smuggled_data = unsmuggle_url(url, {})
167 embed_code = self._match_id(url)
168 domain = smuggled_data.get('domain')
169 supportedformats = smuggled_data.get('supportedformats')
170 embed_token = smuggled_data.get('embed_token')
171 content_tree_url = self._CONTENT_TREE_BASE + 'embed_code/%s/%s' % (embed_code, embed_code)
172 return self._extract(content_tree_url, embed_code, domain, supportedformats, embed_token)
173
174
175 class OoyalaExternalIE(OoyalaBaseIE):
176 _VALID_URL = r'''(?x)
177 (?:
178 ooyalaexternal:|
179 https?://.+?\.ooyala\.com/.*?\bexternalId=
180 )
181 (?P<partner_id>[^:]+)
182 :
183 (?P<id>.+)
184 (?:
185 :|
186 .*?&pcode=
187 )
188 (?P<pcode>.+?)
189 (?:&|$)
190 '''
191
192 _TEST = {
193 'url': 'https://player.ooyala.com/player.js?externalId=espn:10365079&pcode=1kNG061cgaoolOncv54OAO1ceO-I&adSetCode=91cDU6NuXTGKz3OdjOxFdAgJVtQcKJnI&callback=handleEvents&hasModuleParams=1&height=968&playerBrandingId=7af3bd04449c444c964f347f11873075&targetReplaceId=videoPlayer&width=1656&wmode=opaque&allowScriptAccess=always',
194 'info_dict': {
195 'id': 'FkYWtmazr6Ed8xmvILvKLWjd4QvYZpzG',
196 'ext': 'mp4',
197 'title': 'dm_140128_30for30Shorts___JudgingJewellv2',
198 'duration': 1302.0,
199 },
200 'params': {
201 # m3u8 download
202 'skip_download': True,
203 },
204 }
205
206 def _real_extract(self, url):
207 partner_id, video_id, pcode = re.match(self._VALID_URL, url).groups()
208 content_tree_url = self._CONTENT_TREE_BASE + 'external_id/%s/%s:%s' % (pcode, partner_id, video_id)
209 return self._extract(content_tree_url, video_id)