2 from __future__
import unicode_literals
7 from .common
import InfoExtractor
19 class SafariBaseIE(InfoExtractor
):
20 _LOGIN_URL
= 'https://learning.oreilly.com/accounts/login/'
21 _NETRC_MACHINE
= 'safari'
23 _API_BASE
= 'https://learning.oreilly.com/api/v1'
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')
34 return 'learning.oreilly.com/home/' in urlh
.geturl()
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])
46 auth
, urlh
= self
._download
_json
_handle
(
47 'https://www.oreilly.com/member/auth/login/', None, 'Logging in',
51 'redirect_uri': next_uri
,
52 }).encode(), headers
={
53 'Content-Type': 'application/json',
54 'Referer': redirect_url
,
55 }, expected_status
=400)
57 credentials
= auth
.get('credentials')
58 if (not auth
.get('logged_in') and not auth
.get('redirect_uri')
61 'Unable to login: %s' % credentials
, expected
=True)
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
)
68 _
, urlh
= self
._download
_webpage
_handle
(
69 auth
.get('redirect_uri') or next_uri
, None, 'Completing login',)
75 raise ExtractorError('Unable to log in')
78 class SafariIE(SafariBaseIE
):
80 IE_DESC
= 'safaribooksonline.com online video'
83 (?:www\.)?(?:safaribooksonline|(?:learning\.)?oreilly)\.com/
85 library/view/[^/]+/(?P<course_id>[^/]+)/(?P<part>[^/?\#&]+)\.html|
86 videos/[^/]+/[^/]+/(?P<reference_id>[^-]+-[^/?\#&]+)
91 'url': 'https://www.safaribooksonline.com/library/view/hadoop-fundamentals-livelessons/9780133392838/part00.html',
92 'md5': 'dcc5a425e79f2564148652616af1f2a3',
96 'title': 'Introduction to Hadoop Fundamentals LiveLessons',
97 'timestamp': 1437758058,
98 'upload_date': '20150724',
99 'uploader_id': 'stork',
102 # non-digits in course id
103 'url': 'https://www.safaribooksonline.com/library/view/create-a-nodejs/100000006A0210/part00.html',
104 'only_matching': True,
106 'url': 'https://www.safaribooksonline.com/library/view/learning-path-red/9780134664057/RHCE_Introduction.html',
107 'only_matching': True,
109 'url': 'https://www.safaribooksonline.com/videos/python-programming-language/9780134217314/9780134217314-PYMC_13_00',
110 'only_matching': True,
112 'url': 'https://learning.oreilly.com/videos/hadoop-fundamentals-livelessons/9780133392838/9780133392838-00_SeriesIntro',
113 'only_matching': True,
115 'url': 'https://www.oreilly.com/library/view/hadoop-fundamentals-livelessons/9780133392838/00_SeriesIntro.html',
116 'only_matching': True,
119 _PARTNER_ID
= '1926081'
120 _UICONF_ID
= '29375172'
122 def _real_extract(self
, url
):
123 mobj
= self
._match
_valid
_url
(url
)
125 reference_id
= mobj
.group('reference_id')
127 video_id
= reference_id
128 partner_id
= self
._PARTNER
_ID
129 ui_id
= self
._UICONF
_ID
131 video_id
= '%s-%s' % (mobj
.group('course_id'), mobj
.group('part'))
133 webpage
, urlh
= self
._download
_webpage
_handle
(url
, video_id
)
135 mobj
= re
.match(self
._VALID
_URL
, urlh
.geturl())
136 reference_id
= mobj
.group('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,
145 ui_id = self._search_regex(
146 r'data-ui-id=(["\'])(?P
<id>(?
:(?
!\
1).)+)\
1',
147 webpage, 'kaltura uiconf
id', default=self._UICONF_ID,
151 'wid
': '_
%s' % partner_id,
153 'flashvars
[referenceId
]': reference_id,
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'})
163 session = kaltura_session.get('session
')
165 query['flashvars
[ks
]'] = session
167 return self.url_result(update_url_query(
168 'https
://cdnapisec
.kaltura
.com
/html5
/html5lib
/v2
.37
.1/mwEmbedFrame
.php
', query),
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'
177 'url': 'https://www.safaribooksonline.com/api/v1/book/9780133392838/chapter/part00.html',
178 'only_matching': True,
180 'url': 'https://www.safaribooksonline.com/api/v1/book/9780134664057/chapter/RHCE_Introduction.html',
181 'only_matching': True,
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())
197 class SafariCourseIE(SafariBaseIE
):
198 IE_NAME
= 'safari:course'
199 IE_DESC
= 'safaribooksonline.com online courses'
201 _VALID_URL
= r
'''(?x)
204 (?:www\.)?(?:safaribooksonline|(?:learning\.)?oreilly)\.com/
210 techbus\.safaribooksonline\.com
216 'url': 'https://www.safaribooksonline.com/library/view/hadoop-fundamentals-livelessons/9780133392838/',
218 'id': '9780133392838',
219 'title': 'Hadoop Fundamentals LiveLessons',
221 'playlist_count': 22,
222 'skip': 'Requires safaribooksonline account credentials',
224 'url': 'https://www.safaribooksonline.com/api/v1/book/9781449396459/?override_format=json',
225 'only_matching': True,
227 'url': 'http://techbus.safaribooksonline.com/9780134426365',
228 'only_matching': True,
230 'url': 'https://www.safaribooksonline.com/videos/python-programming-language/9780134217314',
231 'only_matching': True,
233 'url': 'https://learning.oreilly.com/videos/hadoop-fundamentals-livelessons/9780133392838',
234 'only_matching': True,
236 'url': 'https://www.oreilly.com/library/view/hadoop-fundamentals-livelessons/9780133392838/',
237 'only_matching': True,
241 def suitable(cls
, url
):
242 return (False if SafariIE
.suitable(url
) or SafariApiIE
.suitable(url
)
243 else super(SafariCourseIE
, cls
).suitable(url
))
245 def _real_extract(self
, url
):
246 course_id
= self
._match
_id
(url
)
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')
252 if 'chapters' not in course_json
:
253 raise ExtractorError(
254 'No chapters found for course %s' % course_id
, expected
=True)
257 self
.url_result(chapter
, SafariApiIE
.ie_key())
258 for chapter
in course_json
['chapters']]
260 course_title
= course_json
['title']
262 return self
.playlist_result(entries
, course_id
, course_title
)