]> git.bitcoin.ninja Git - youtube-dl/blob - youtube_dl/extractor/ooyala.py
[dailymotion] Error spelling
[youtube-dl] / youtube_dl / extractor / ooyala.py
1 from __future__ import unicode_literals
2 import re
3 import json
4 import base64
5
6 from .common import InfoExtractor
7 from ..utils import (
8     unescapeHTML,
9     ExtractorError,
10     determine_ext,
11     int_or_none,
12 )
13
14
15 class OoyalaBaseIE(InfoExtractor):
16
17     def _extract_result(self, info, more_info):
18         embedCode = info['embedCode']
19         video_url = info.get('ipad_url') or info['url']
20
21         if determine_ext(video_url) == 'm3u8':
22             formats = self._extract_m3u8_formats(video_url, embedCode, ext='mp4')
23         else:
24             formats = [{
25                 'url': video_url,
26                 'ext': 'mp4',
27             }]
28
29         return {
30             'id': embedCode,
31             'title': unescapeHTML(info['title']),
32             'formats': formats,
33             'description': unescapeHTML(more_info['description']),
34             'thumbnail': more_info['promo'],
35         }
36
37     def _extract(self, player_url, video_id):
38         player = self._download_webpage(player_url, video_id)
39         mobile_url = self._search_regex(r'mobile_player_url="(.+?)&device="',
40                                         player, 'mobile player url')
41         # Looks like some videos are only available for particular devices
42         # (e.g. http://player.ooyala.com/player.js?embedCode=x1b3lqZDq9y_7kMyC2Op5qo-p077tXD0
43         # is only available for ipad)
44         # Working around with fetching URLs for all the devices found starting with 'unknown'
45         # until we succeed or eventually fail for each device.
46         devices = re.findall(r'device\s*=\s*"([^"]+)";', player)
47         devices.remove('unknown')
48         devices.insert(0, 'unknown')
49         for device in devices:
50             mobile_player = self._download_webpage(
51                 '%s&device=%s' % (mobile_url, device), video_id,
52                 'Downloading mobile player JS for %s device' % device)
53             videos_info = self._search_regex(
54                 r'var streams=window.oo_testEnv\?\[\]:eval\("\((\[{.*?}\])\)"\);',
55                 mobile_player, 'info', fatal=False, default=None)
56             if videos_info:
57                 break
58
59         if not videos_info:
60             formats = []
61             auth_data = self._download_json(
62                 'http://player.ooyala.com/sas/player_api/v1/authorization/embed_code/%s/%s?domain=www.example.org&supportedFormats=mp4,webm' % (video_id, video_id),
63                 video_id)
64
65             cur_auth_data = auth_data['authorization_data'][video_id]
66
67             for stream in cur_auth_data['streams']:
68                 formats.append({
69                     'url': base64.b64decode(stream['url']['data'].encode('ascii')).decode('utf-8'),
70                     'ext': stream.get('delivery_type'),
71                     'format': stream.get('video_codec'),
72                     'format_id': stream.get('profile'),
73                     'width': int_or_none(stream.get('width')),
74                     'height': int_or_none(stream.get('height')),
75                     'abr': int_or_none(stream.get('audio_bitrate')),
76                     'vbr': int_or_none(stream.get('video_bitrate')),
77                 })
78             if formats:
79                 return {
80                     'id': video_id,
81                     'formats': formats,
82                     'title': 'Ooyala video',
83                 }
84
85             if not cur_auth_data['authorized']:
86                 raise ExtractorError(cur_auth_data['message'], expected=True)
87
88         if not videos_info:
89             raise ExtractorError('Unable to extract info')
90         videos_info = videos_info.replace('\\"', '"')
91         videos_more_info = self._search_regex(
92             r'eval\("\(({.*?\\"promo\\".*?})\)"', mobile_player, 'more info').replace('\\"', '"')
93         videos_info = json.loads(videos_info)
94         videos_more_info = json.loads(videos_more_info)
95
96         if videos_more_info.get('lineup'):
97             videos = [self._extract_result(info, more_info) for (info, more_info) in zip(videos_info, videos_more_info['lineup'])]
98             return {
99                 '_type': 'playlist',
100                 'id': video_id,
101                 'title': unescapeHTML(videos_more_info['title']),
102                 'entries': videos,
103             }
104         else:
105             return self._extract_result(videos_info[0], videos_more_info)
106
107
108 class OoyalaIE(OoyalaBaseIE):
109     _VALID_URL = r'(?:ooyala:|https?://.+?\.ooyala\.com/.*?(?:embedCode|ec)=)(?P<id>.+?)(&|$)'
110
111     _TESTS = [
112         {
113             # From http://it.slashdot.org/story/13/04/25/178216/recovering-data-from-broken-hard-drives-and-ssds-video
114             'url': 'http://player.ooyala.com/player.js?embedCode=pxczE2YjpfHfn1f3M-ykG_AmJRRn0PD8',
115             'info_dict': {
116                 'id': 'pxczE2YjpfHfn1f3M-ykG_AmJRRn0PD8',
117                 'ext': 'mp4',
118                 'title': 'Explaining Data Recovery from Hard Drives and SSDs',
119                 'description': 'How badly damaged does a drive have to be to defeat Russell and his crew? Apparently, smashed to bits.',
120             },
121         }, {
122             # Only available for ipad
123             'url': 'http://player.ooyala.com/player.js?embedCode=x1b3lqZDq9y_7kMyC2Op5qo-p077tXD0',
124             'info_dict': {
125                 'id': 'x1b3lqZDq9y_7kMyC2Op5qo-p077tXD0',
126                 'ext': 'mp4',
127                 'title': 'Simulation Overview - Levels of Simulation',
128                 'description': '',
129             },
130         },
131         {
132             # Information available only through SAS api
133             # From http://community.plm.automation.siemens.com/t5/News-NX-Manufacturing/Tool-Path-Divide/ba-p/4187
134             'url': 'http://player.ooyala.com/player.js?embedCode=FiOG81ZTrvckcchQxmalf4aQj590qTEx',
135             'md5': 'a84001441b35ea492bc03736e59e7935',
136             'info_dict': {
137                 'id': 'FiOG81ZTrvckcchQxmalf4aQj590qTEx',
138                 'ext': 'mp4',
139                 'title': 'Ooyala video',
140             }
141         }
142     ]
143
144     @staticmethod
145     def _url_for_embed_code(embed_code):
146         return 'http://player.ooyala.com/player.js?embedCode=%s' % embed_code
147
148     @classmethod
149     def _build_url_result(cls, embed_code):
150         return cls.url_result(cls._url_for_embed_code(embed_code),
151                               ie=cls.ie_key())
152
153     def _real_extract(self, url):
154         embed_code = self._match_id(url)
155         player_url = 'http://player.ooyala.com/player.js?embedCode=%s' % embed_code
156         return self._extract(player_url, embed_code)
157
158
159 class OoyalaExternalIE(OoyalaBaseIE):
160     _VALID_URL = r'''(?x)
161                     (?:
162                         ooyalaexternal:|
163                         https?://.+?\.ooyala\.com/.*?\bexternalId=
164                     )
165                     (?P<partner_id>[^:]+)
166                     :
167                     (?P<id>.+)
168                     (?:
169                         :|
170                         .*?&pcode=
171                     )
172                     (?P<pcode>.+?)
173                     (&|$)
174                     '''
175
176     _TEST = {
177         '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',
178         'info_dict': {
179             'id': 'FkYWtmazr6Ed8xmvILvKLWjd4QvYZpzG',
180             'ext': 'mp4',
181             'title': 'dm_140128_30for30Shorts___JudgingJewellv2',
182             'description': '',
183         },
184         'params': {
185             # m3u8 download
186             'skip_download': True,
187         },
188     }
189
190     def _real_extract(self, url):
191         mobj = re.match(self._VALID_URL, url)
192         partner_id = mobj.group('partner_id')
193         video_id = mobj.group('id')
194         pcode = mobj.group('pcode')
195         player_url = 'http://player.ooyala.com/player.js?externalId=%s:%s&pcode=%s' % (partner_id, video_id, pcode)
196         return self._extract(player_url, video_id)