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