2016-10-02 07:39:18 -04:00
|
|
|
# coding: utf-8
|
2015-03-22 14:03:40 -04:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2019-05-17 16:23:40 -04:00
|
|
|
import json
|
2015-03-22 14:03:40 -04:00
|
|
|
import re
|
|
|
|
|
|
|
|
from .common import InfoExtractor
|
|
|
|
|
2019-05-17 16:23:40 -04:00
|
|
|
from ..compat import (
|
|
|
|
compat_parse_qs,
|
2020-08-26 10:52:32 -04:00
|
|
|
compat_str,
|
2019-05-17 16:23:40 -04:00
|
|
|
compat_urlparse,
|
|
|
|
)
|
2015-03-22 14:03:40 -04:00
|
|
|
from ..utils import (
|
|
|
|
ExtractorError,
|
2016-03-11 10:57:06 -05:00
|
|
|
update_url_query,
|
2015-03-22 14:03:40 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class SafariBaseIE(InfoExtractor):
|
2018-12-15 11:08:14 -05:00
|
|
|
_LOGIN_URL = 'https://learning.oreilly.com/accounts/login/'
|
2015-03-26 13:57:46 -04:00
|
|
|
_NETRC_MACHINE = 'safari'
|
|
|
|
|
2018-12-15 11:08:14 -05:00
|
|
|
_API_BASE = 'https://learning.oreilly.com/api/v1'
|
2015-03-26 13:57:46 -04:00
|
|
|
_API_FORMAT = 'json'
|
2015-03-22 14:03:40 -04:00
|
|
|
|
|
|
|
LOGGED_IN = False
|
|
|
|
|
|
|
|
def _real_initialize(self):
|
2016-03-12 15:08:36 -05:00
|
|
|
self._login()
|
2015-03-22 14:03:40 -04:00
|
|
|
|
|
|
|
def _login(self):
|
2018-05-26 11:12:44 -04:00
|
|
|
username, password = self._get_login_info()
|
2015-03-22 14:03:40 -04:00
|
|
|
if username is None:
|
2016-03-12 15:03:07 -05:00
|
|
|
return
|
2015-03-22 14:03:40 -04:00
|
|
|
|
2019-05-17 16:23:40 -04:00
|
|
|
_, urlh = self._download_webpage_handle(
|
|
|
|
'https://learning.oreilly.com/accounts/login-check/', None,
|
|
|
|
'Downloading login page')
|
2017-06-08 12:20:48 -04:00
|
|
|
|
2019-05-17 16:23:40 -04:00
|
|
|
def is_logged(urlh):
|
2020-08-26 10:52:32 -04:00
|
|
|
return 'learning.oreilly.com/home/' in compat_str(urlh.geturl())
|
2017-06-08 12:20:48 -04:00
|
|
|
|
2019-05-17 16:23:40 -04:00
|
|
|
if is_logged(urlh):
|
2017-06-08 12:20:48 -04:00
|
|
|
self.LOGGED_IN = True
|
|
|
|
return
|
2015-03-22 14:03:40 -04:00
|
|
|
|
2020-08-26 10:52:32 -04:00
|
|
|
redirect_url = compat_str(urlh.geturl())
|
2019-05-17 16:23:40 -04:00
|
|
|
parsed_url = compat_urlparse.urlparse(redirect_url)
|
|
|
|
qs = compat_parse_qs(parsed_url.query)
|
|
|
|
next_uri = compat_urlparse.urljoin(
|
|
|
|
'https://api.oreilly.com', qs['next'][0])
|
|
|
|
|
|
|
|
auth, urlh = self._download_json_handle(
|
|
|
|
'https://www.oreilly.com/member/auth/login/', None, 'Logging in',
|
|
|
|
data=json.dumps({
|
|
|
|
'email': username,
|
|
|
|
'password': password,
|
|
|
|
'redirect_uri': next_uri,
|
|
|
|
}).encode(), headers={
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Referer': redirect_url,
|
|
|
|
}, expected_status=400)
|
|
|
|
|
|
|
|
credentials = auth.get('credentials')
|
|
|
|
if (not auth.get('logged_in') and not auth.get('redirect_uri')
|
|
|
|
and credentials):
|
|
|
|
raise ExtractorError(
|
|
|
|
'Unable to login: %s' % credentials, expected=True)
|
2015-03-22 14:03:40 -04:00
|
|
|
|
2019-08-26 23:16:04 -04:00
|
|
|
# oreilly serves two same instances of the following cookies
|
|
|
|
# in Set-Cookie header and expects first one to be actually set
|
|
|
|
for cookie in ('groot_sessionid', 'orm-jwt', 'orm-rt'):
|
|
|
|
self._apply_first_set_cookie_header(urlh, cookie)
|
2015-03-22 14:03:40 -04:00
|
|
|
|
2019-05-17 16:23:40 -04:00
|
|
|
_, urlh = self._download_webpage_handle(
|
|
|
|
auth.get('redirect_uri') or next_uri, None, 'Completing login',)
|
2015-03-22 14:03:40 -04:00
|
|
|
|
2019-05-17 16:23:40 -04:00
|
|
|
if is_logged(urlh):
|
|
|
|
self.LOGGED_IN = True
|
|
|
|
return
|
2015-03-22 14:03:40 -04:00
|
|
|
|
2019-05-17 16:23:40 -04:00
|
|
|
raise ExtractorError('Unable to log in')
|
2016-03-12 15:08:36 -05:00
|
|
|
|
2015-03-22 14:03:40 -04:00
|
|
|
|
|
|
|
class SafariIE(SafariBaseIE):
|
|
|
|
IE_NAME = 'safari'
|
|
|
|
IE_DESC = 'safaribooksonline.com online video'
|
2018-06-02 13:52:22 -04:00
|
|
|
_VALID_URL = r'''(?x)
|
|
|
|
https?://
|
2019-05-17 16:23:40 -04:00
|
|
|
(?:www\.)?(?:safaribooksonline|(?:learning\.)?oreilly)\.com/
|
2018-06-02 13:52:22 -04:00
|
|
|
(?:
|
|
|
|
library/view/[^/]+/(?P<course_id>[^/]+)/(?P<part>[^/?\#&]+)\.html|
|
|
|
|
videos/[^/]+/[^/]+/(?P<reference_id>[^-]+-[^/?\#&]+)
|
|
|
|
)
|
|
|
|
'''
|
2015-03-26 13:57:46 -04:00
|
|
|
|
|
|
|
_TESTS = [{
|
|
|
|
'url': 'https://www.safaribooksonline.com/library/view/hadoop-fundamentals-livelessons/9780133392838/part00.html',
|
2016-03-11 10:57:06 -05:00
|
|
|
'md5': 'dcc5a425e79f2564148652616af1f2a3',
|
2015-03-22 14:03:40 -04:00
|
|
|
'info_dict': {
|
2016-03-11 10:57:06 -05:00
|
|
|
'id': '0_qbqx90ic',
|
2015-03-22 14:03:40 -04:00
|
|
|
'ext': 'mp4',
|
2016-03-11 10:57:06 -05:00
|
|
|
'title': 'Introduction to Hadoop Fundamentals LiveLessons',
|
|
|
|
'timestamp': 1437758058,
|
|
|
|
'upload_date': '20150724',
|
|
|
|
'uploader_id': 'stork',
|
2015-03-26 13:57:46 -04:00
|
|
|
},
|
2015-06-15 11:36:30 -04:00
|
|
|
}, {
|
|
|
|
# non-digits in course id
|
|
|
|
'url': 'https://www.safaribooksonline.com/library/view/create-a-nodejs/100000006A0210/part00.html',
|
|
|
|
'only_matching': True,
|
2016-08-01 10:48:48 -04:00
|
|
|
}, {
|
|
|
|
'url': 'https://www.safaribooksonline.com/library/view/learning-path-red/9780134664057/RHCE_Introduction.html',
|
|
|
|
'only_matching': True,
|
2018-06-02 13:52:22 -04:00
|
|
|
}, {
|
|
|
|
'url': 'https://www.safaribooksonline.com/videos/python-programming-language/9780134217314/9780134217314-PYMC_13_00',
|
|
|
|
'only_matching': True,
|
2018-12-15 11:08:14 -05:00
|
|
|
}, {
|
|
|
|
'url': 'https://learning.oreilly.com/videos/hadoop-fundamentals-livelessons/9780133392838/9780133392838-00_SeriesIntro',
|
|
|
|
'only_matching': True,
|
2019-05-17 16:23:40 -04:00
|
|
|
}, {
|
|
|
|
'url': 'https://www.oreilly.com/library/view/hadoop-fundamentals-livelessons/9780133392838/00_SeriesIntro.html',
|
|
|
|
'only_matching': True,
|
2015-03-26 13:57:46 -04:00
|
|
|
}]
|
2015-03-22 14:03:40 -04:00
|
|
|
|
2018-06-02 13:52:22 -04:00
|
|
|
_PARTNER_ID = '1926081'
|
|
|
|
_UICONF_ID = '29375172'
|
|
|
|
|
2015-03-22 14:03:40 -04:00
|
|
|
def _real_extract(self, url):
|
|
|
|
mobj = re.match(self._VALID_URL, url)
|
2018-06-02 13:52:22 -04:00
|
|
|
|
|
|
|
reference_id = mobj.group('reference_id')
|
|
|
|
if reference_id:
|
|
|
|
video_id = reference_id
|
|
|
|
partner_id = self._PARTNER_ID
|
|
|
|
ui_id = self._UICONF_ID
|
|
|
|
else:
|
|
|
|
video_id = '%s-%s' % (mobj.group('course_id'), mobj.group('part'))
|
|
|
|
|
|
|
|
webpage, urlh = self._download_webpage_handle(url, video_id)
|
|
|
|
|
|
|
|
mobj = re.match(self._VALID_URL, urlh.geturl())
|
|
|
|
reference_id = mobj.group('reference_id')
|
|
|
|
if not reference_id:
|
|
|
|
reference_id = self._search_regex(
|
|
|
|
r'data-reference-id=(["\'])(?P<id>(?:(?!\1).)+)\1',
|
|
|
|
webpage, 'kaltura reference id', group='id')
|
|
|
|
partner_id = self._search_regex(
|
|
|
|
r'data-partner-id=(["\'])(?P<id>(?:(?!\1).)+)\1',
|
|
|
|
webpage, 'kaltura widget id', default=self._PARTNER_ID,
|
|
|
|
group='id')
|
|
|
|
ui_id = self._search_regex(
|
|
|
|
r'data-ui-id=(["\'])(?P<id>(?:(?!\1).)+)\1',
|
|
|
|
webpage, 'kaltura uiconf id', default=self._UICONF_ID,
|
|
|
|
group='id')
|
2015-03-26 13:57:46 -04:00
|
|
|
|
2016-03-12 15:03:07 -05:00
|
|
|
query = {
|
2016-03-11 10:57:06 -05:00
|
|
|
'wid': '_%s' % partner_id,
|
|
|
|
'uiconf_id': ui_id,
|
|
|
|
'flashvars[referenceId]': reference_id,
|
2016-03-12 15:03:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if self.LOGGED_IN:
|
|
|
|
kaltura_session = self._download_json(
|
|
|
|
'%s/player/kaltura_session/?reference_id=%s' % (self._API_BASE, reference_id),
|
2016-03-19 12:30:48 -04:00
|
|
|
video_id, 'Downloading kaltura session JSON',
|
2020-08-26 10:52:32 -04:00
|
|
|
'Unable to download kaltura session JSON', fatal=False)
|
2016-03-12 15:03:07 -05:00
|
|
|
if kaltura_session:
|
|
|
|
session = kaltura_session.get('session')
|
|
|
|
if session:
|
|
|
|
query['flashvars[ks]'] = session
|
|
|
|
|
|
|
|
return self.url_result(update_url_query(
|
|
|
|
'https://cdnapisec.kaltura.com/html5/html5lib/v2.37.1/mwEmbedFrame.php', query),
|
|
|
|
'Kaltura')
|
2015-03-22 14:03:40 -04:00
|
|
|
|
|
|
|
|
2016-03-19 12:30:48 -04:00
|
|
|
class SafariApiIE(SafariBaseIE):
|
|
|
|
IE_NAME = 'safari:api'
|
2019-05-17 16:23:40 -04:00
|
|
|
_VALID_URL = r'https?://(?:www\.)?(?:safaribooksonline|(?:learning\.)?oreilly)\.com/api/v1/book/(?P<course_id>[^/]+)/chapter(?:-content)?/(?P<part>[^/?#&]+)\.html'
|
2016-03-19 12:30:48 -04:00
|
|
|
|
2016-08-01 10:48:48 -04:00
|
|
|
_TESTS = [{
|
2016-03-19 12:30:48 -04:00
|
|
|
'url': 'https://www.safaribooksonline.com/api/v1/book/9780133392838/chapter/part00.html',
|
|
|
|
'only_matching': True,
|
2016-08-01 10:48:48 -04:00
|
|
|
}, {
|
|
|
|
'url': 'https://www.safaribooksonline.com/api/v1/book/9780134664057/chapter/RHCE_Introduction.html',
|
|
|
|
'only_matching': True,
|
|
|
|
}]
|
2016-03-19 12:30:48 -04:00
|
|
|
|
|
|
|
def _real_extract(self, url):
|
|
|
|
mobj = re.match(self._VALID_URL, url)
|
|
|
|
part = self._download_json(
|
|
|
|
url, '%s/%s' % (mobj.group('course_id'), mobj.group('part')),
|
|
|
|
'Downloading part JSON')
|
|
|
|
return self.url_result(part['web_url'], SafariIE.ie_key())
|
|
|
|
|
|
|
|
|
2015-03-22 14:03:40 -04:00
|
|
|
class SafariCourseIE(SafariBaseIE):
|
|
|
|
IE_NAME = 'safari:course'
|
|
|
|
IE_DESC = 'safaribooksonline.com online courses'
|
|
|
|
|
2016-10-14 13:29:33 -04:00
|
|
|
_VALID_URL = r'''(?x)
|
|
|
|
https?://
|
|
|
|
(?:
|
2019-05-17 16:23:40 -04:00
|
|
|
(?:www\.)?(?:safaribooksonline|(?:learning\.)?oreilly)\.com/
|
2018-06-02 13:52:22 -04:00
|
|
|
(?:
|
|
|
|
library/view/[^/]+|
|
|
|
|
api/v1/book|
|
|
|
|
videos/[^/]+
|
|
|
|
)|
|
2016-10-14 13:29:33 -04:00
|
|
|
techbus\.safaribooksonline\.com
|
|
|
|
)
|
2018-06-02 13:52:22 -04:00
|
|
|
/(?P<id>[^/]+)
|
2016-10-14 13:29:33 -04:00
|
|
|
'''
|
2015-03-22 14:03:40 -04:00
|
|
|
|
2015-03-26 13:57:46 -04:00
|
|
|
_TESTS = [{
|
|
|
|
'url': 'https://www.safaribooksonline.com/library/view/hadoop-fundamentals-livelessons/9780133392838/',
|
|
|
|
'info_dict': {
|
|
|
|
'id': '9780133392838',
|
|
|
|
'title': 'Hadoop Fundamentals LiveLessons',
|
|
|
|
},
|
|
|
|
'playlist_count': 22,
|
|
|
|
'skip': 'Requires safaribooksonline account credentials',
|
|
|
|
}, {
|
|
|
|
'url': 'https://www.safaribooksonline.com/api/v1/book/9781449396459/?override_format=json',
|
|
|
|
'only_matching': True,
|
2016-10-14 13:29:33 -04:00
|
|
|
}, {
|
|
|
|
'url': 'http://techbus.safaribooksonline.com/9780134426365',
|
|
|
|
'only_matching': True,
|
2018-06-02 13:52:22 -04:00
|
|
|
}, {
|
|
|
|
'url': 'https://www.safaribooksonline.com/videos/python-programming-language/9780134217314',
|
|
|
|
'only_matching': True,
|
2018-12-15 11:08:14 -05:00
|
|
|
}, {
|
|
|
|
'url': 'https://learning.oreilly.com/videos/hadoop-fundamentals-livelessons/9780133392838',
|
|
|
|
'only_matching': True,
|
2019-05-17 16:23:40 -04:00
|
|
|
}, {
|
|
|
|
'url': 'https://www.oreilly.com/library/view/hadoop-fundamentals-livelessons/9780133392838/',
|
|
|
|
'only_matching': True,
|
2015-03-26 13:57:46 -04:00
|
|
|
}]
|
2015-03-22 14:03:40 -04:00
|
|
|
|
2018-06-02 13:52:22 -04:00
|
|
|
@classmethod
|
|
|
|
def suitable(cls, url):
|
|
|
|
return (False if SafariIE.suitable(url) or SafariApiIE.suitable(url)
|
|
|
|
else super(SafariCourseIE, cls).suitable(url))
|
|
|
|
|
2015-03-22 14:03:40 -04:00
|
|
|
def _real_extract(self, url):
|
2015-03-26 13:57:46 -04:00
|
|
|
course_id = self._match_id(url)
|
2015-03-22 14:03:40 -04:00
|
|
|
|
2015-03-26 13:57:46 -04:00
|
|
|
course_json = self._download_json(
|
2016-03-12 15:03:07 -05:00
|
|
|
'%s/book/%s/?override_format=%s' % (self._API_BASE, course_id, self._API_FORMAT),
|
2015-03-26 13:57:46 -04:00
|
|
|
course_id, 'Downloading course JSON')
|
2015-03-22 14:03:40 -04:00
|
|
|
|
|
|
|
if 'chapters' not in course_json:
|
2015-03-26 13:57:46 -04:00
|
|
|
raise ExtractorError(
|
|
|
|
'No chapters found for course %s' % course_id, expected=True)
|
2015-03-22 14:03:40 -04:00
|
|
|
|
|
|
|
entries = [
|
2016-03-19 12:30:48 -04:00
|
|
|
self.url_result(chapter, SafariApiIE.ie_key())
|
2015-03-26 13:57:46 -04:00
|
|
|
for chapter in course_json['chapters']]
|
2015-03-22 14:03:40 -04:00
|
|
|
|
|
|
|
course_title = course_json['title']
|
|
|
|
|
|
|
|
return self.playlist_result(entries, course_id, course_title)
|