]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/safari.py
8d4806794ae5f5e83e667a66447179269b0fb1a7
[youtubedl] / youtube_dl / extractor / safari.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import json
5 import re
6
7 from .common import InfoExtractor
8
9 from ..compat import (
10 compat_parse_qs,
11 compat_str,
12 compat_urlparse,
13 )
14 from ..utils import (
15 ExtractorError,
16 update_url_query,
17 )
18
19
20 class SafariBaseIE(InfoExtractor):
21 _LOGIN_URL = 'https://learning.oreilly.com/accounts/login/'
22 _NETRC_MACHINE = 'safari'
23
24 _API_BASE = 'https://learning.oreilly.com/api/v1'
25 _API_FORMAT = 'json'
26
27 LOGGED_IN = False
28
29 def _real_initialize(self):
30 self._login()
31
32 def _login(self):
33 username, password = self._get_login_info()
34 if username is None:
35 return
36
37 _, urlh = self._download_webpage_handle(
38 'https://learning.oreilly.com/accounts/login-check/', None,
39 'Downloading login page')
40
41 def is_logged(urlh):
42 return 'learning.oreilly.com/home/' in compat_str(urlh.geturl())
43
44 if is_logged(urlh):
45 self.LOGGED_IN = True
46 return
47
48 redirect_url = compat_str(urlh.geturl())
49 parsed_url = compat_urlparse.urlparse(redirect_url)
50 qs = compat_parse_qs(parsed_url.query)
51 next_uri = compat_urlparse.urljoin(
52 'https://api.oreilly.com', qs['next'][0])
53
54 auth, urlh = self._download_json_handle(
55 'https://www.oreilly.com/member/auth/login/', None, 'Logging in',
56 data=json.dumps({
57 'email': username,
58 'password': password,
59 'redirect_uri': next_uri,
60 }).encode(), headers={
61 'Content-Type': 'application/json',
62 'Referer': redirect_url,
63 }, expected_status=400)
64
65 credentials = auth.get('credentials')
66 if (not auth.get('logged_in') and not auth.get('redirect_uri')
67 and credentials):
68 raise ExtractorError(
69 'Unable to login: %s' % credentials, expected=True)
70
71 # oreilly serves two same groot_sessionid cookies in Set-Cookie header
72 # and expects first one to be actually set
73 self._apply_first_set_cookie_header(urlh, 'groot_sessionid')
74
75 _, urlh = self._download_webpage_handle(
76 auth.get('redirect_uri') or next_uri, None, 'Completing login',)
77
78 if is_logged(urlh):
79 self.LOGGED_IN = True
80 return
81
82 raise ExtractorError('Unable to log in')
83
84
85 class SafariIE(SafariBaseIE):
86 IE_NAME = 'safari'
87 IE_DESC = 'safaribooksonline.com online video'
88 _VALID_URL = r'''(?x)
89 https?://
90 (?:www\.)?(?:safaribooksonline|(?:learning\.)?oreilly)\.com/
91 (?:
92 library/view/[^/]+/(?P<course_id>[^/]+)/(?P<part>[^/?\#&]+)\.html|
93 videos/[^/]+/[^/]+/(?P<reference_id>[^-]+-[^/?\#&]+)
94 )
95 '''
96
97 _TESTS = [{
98 'url': 'https://www.safaribooksonline.com/library/view/hadoop-fundamentals-livelessons/9780133392838/part00.html',
99 'md5': 'dcc5a425e79f2564148652616af1f2a3',
100 'info_dict': {
101 'id': '0_qbqx90ic',
102 'ext': 'mp4',
103 'title': 'Introduction to Hadoop Fundamentals LiveLessons',
104 'timestamp': 1437758058,
105 'upload_date': '20150724',
106 'uploader_id': 'stork',
107 },
108 }, {
109 # non-digits in course id
110 'url': 'https://www.safaribooksonline.com/library/view/create-a-nodejs/100000006A0210/part00.html',
111 'only_matching': True,
112 }, {
113 'url': 'https://www.safaribooksonline.com/library/view/learning-path-red/9780134664057/RHCE_Introduction.html',
114 'only_matching': True,
115 }, {
116 'url': 'https://www.safaribooksonline.com/videos/python-programming-language/9780134217314/9780134217314-PYMC_13_00',
117 'only_matching': True,
118 }, {
119 'url': 'https://learning.oreilly.com/videos/hadoop-fundamentals-livelessons/9780133392838/9780133392838-00_SeriesIntro',
120 'only_matching': True,
121 }, {
122 'url': 'https://www.oreilly.com/library/view/hadoop-fundamentals-livelessons/9780133392838/00_SeriesIntro.html',
123 'only_matching': True,
124 }]
125
126 _PARTNER_ID = '1926081'
127 _UICONF_ID = '29375172'
128
129 def _real_extract(self, url):
130 mobj = re.match(self._VALID_URL, url)
131
132 reference_id = mobj.group('reference_id')
133 if reference_id:
134 video_id = reference_id
135 partner_id = self._PARTNER_ID
136 ui_id = self._UICONF_ID
137 else:
138 video_id = '%s-%s' % (mobj.group('course_id'), mobj.group('part'))
139
140 webpage, urlh = self._download_webpage_handle(url, video_id)
141
142 mobj = re.match(self._VALID_URL, urlh.geturl())
143 reference_id = mobj.group('reference_id')
144 if not reference_id:
145 reference_id = self._search_regex(
146 r'data-reference-id=(["\'])(?P<id>(?:(?!\1).)+)\1',
147 webpage, 'kaltura reference id', group='id')
148 partner_id = self._search_regex(
149 r'data-partner-id=(["\'])(?P<id>(?:(?!\1).)+)\1',
150 webpage, 'kaltura widget id', default=self._PARTNER_ID,
151 group='id')
152 ui_id = self._search_regex(
153 r'data-ui-id=(["\'])(?P<id>(?:(?!\1).)+)\1',
154 webpage, 'kaltura uiconf id', default=self._UICONF_ID,
155 group='id')
156
157 query = {
158 'wid': '_%s' % partner_id,
159 'uiconf_id': ui_id,
160 'flashvars[referenceId]': reference_id,
161 }
162
163 if self.LOGGED_IN:
164 kaltura_session = self._download_json(
165 '%s/player/kaltura_session/?reference_id=%s' % (self._API_BASE, reference_id),
166 video_id, 'Downloading kaltura session JSON',
167 'Unable to download kaltura session JSON', fatal=False)
168 if kaltura_session:
169 session = kaltura_session.get('session')
170 if session:
171 query['flashvars[ks]'] = session
172
173 return self.url_result(update_url_query(
174 'https://cdnapisec.kaltura.com/html5/html5lib/v2.37.1/mwEmbedFrame.php', query),
175 'Kaltura')
176
177
178 class SafariApiIE(SafariBaseIE):
179 IE_NAME = 'safari:api'
180 _VALID_URL = r'https?://(?:www\.)?(?:safaribooksonline|(?:learning\.)?oreilly)\.com/api/v1/book/(?P<course_id>[^/]+)/chapter(?:-content)?/(?P<part>[^/?#&]+)\.html'
181
182 _TESTS = [{
183 'url': 'https://www.safaribooksonline.com/api/v1/book/9780133392838/chapter/part00.html',
184 'only_matching': True,
185 }, {
186 'url': 'https://www.safaribooksonline.com/api/v1/book/9780134664057/chapter/RHCE_Introduction.html',
187 'only_matching': True,
188 }]
189
190 def _real_extract(self, url):
191 mobj = re.match(self._VALID_URL, url)
192 part = self._download_json(
193 url, '%s/%s' % (mobj.group('course_id'), mobj.group('part')),
194 'Downloading part JSON')
195 return self.url_result(part['web_url'], SafariIE.ie_key())
196
197
198 class SafariCourseIE(SafariBaseIE):
199 IE_NAME = 'safari:course'
200 IE_DESC = 'safaribooksonline.com online courses'
201
202 _VALID_URL = r'''(?x)
203 https?://
204 (?:
205 (?:www\.)?(?:safaribooksonline|(?:learning\.)?oreilly)\.com/
206 (?:
207 library/view/[^/]+|
208 api/v1/book|
209 videos/[^/]+
210 )|
211 techbus\.safaribooksonline\.com
212 )
213 /(?P<id>[^/]+)
214 '''
215
216 _TESTS = [{
217 'url': 'https://www.safaribooksonline.com/library/view/hadoop-fundamentals-livelessons/9780133392838/',
218 'info_dict': {
219 'id': '9780133392838',
220 'title': 'Hadoop Fundamentals LiveLessons',
221 },
222 'playlist_count': 22,
223 'skip': 'Requires safaribooksonline account credentials',
224 }, {
225 'url': 'https://www.safaribooksonline.com/api/v1/book/9781449396459/?override_format=json',
226 'only_matching': True,
227 }, {
228 'url': 'http://techbus.safaribooksonline.com/9780134426365',
229 'only_matching': True,
230 }, {
231 'url': 'https://www.safaribooksonline.com/videos/python-programming-language/9780134217314',
232 'only_matching': True,
233 }, {
234 'url': 'https://learning.oreilly.com/videos/hadoop-fundamentals-livelessons/9780133392838',
235 'only_matching': True,
236 }, {
237 'url': 'https://www.oreilly.com/library/view/hadoop-fundamentals-livelessons/9780133392838/',
238 'only_matching': True,
239 }]
240
241 @classmethod
242 def suitable(cls, url):
243 return (False if SafariIE.suitable(url) or SafariApiIE.suitable(url)
244 else super(SafariCourseIE, cls).suitable(url))
245
246 def _real_extract(self, url):
247 course_id = self._match_id(url)
248
249 course_json = self._download_json(
250 '%s/book/%s/?override_format=%s' % (self._API_BASE, course_id, self._API_FORMAT),
251 course_id, 'Downloading course JSON')
252
253 if 'chapters' not in course_json:
254 raise ExtractorError(
255 'No chapters found for course %s' % course_id, expected=True)
256
257 entries = [
258 self.url_result(chapter, SafariApiIE.ie_key())
259 for chapter in course_json['chapters']]
260
261 course_title = course_json['title']
262
263 return self.playlist_result(entries, course_id, course_title)