2 from __future__
import unicode_literals
6 from .common
import InfoExtractor
17 class SafariBaseIE(InfoExtractor
):
18 _LOGIN_URL
= 'https://www.safaribooksonline.com/accounts/login/'
19 _NETRC_MACHINE
= 'safari'
21 _API_BASE
= 'https://www.safaribooksonline.com/api/v1'
26 def _real_initialize(self
):
30 (username
, password
) = self
._get
_login
_info
()
34 headers
= std_headers
.copy()
35 if 'Referer' not in headers
:
36 headers
['Referer'] = self
._LOGIN
_URL
38 login_page
= self
._download
_webpage
(
39 self
._LOGIN
_URL
, None, 'Downloading login form', headers
=headers
)
41 def is_logged(webpage
):
42 return any(re
.search(p
, webpage
) for p
in (
43 r
'href=["\']/accounts
/logout
/', r'>Sign Out
<'))
45 if is_logged(login_page):
49 csrf = self._html_search_regex(
50 r"name='csrfmiddlewaretoken
'\s+value='([^
']+)'",
51 login_page, 'csrf token')
54 'csrfmiddlewaretoken': csrf,
56 'password1': password,
61 request = sanitized_Request(
62 self._LOGIN_URL, urlencode_postdata(login_form), headers=headers)
63 login_page = self._download_webpage(
64 request, None, 'Logging in')
66 if not is_logged(login_page):
68 'Login failed; make sure your credentials are correct and try again.',
74 class SafariIE(SafariBaseIE):
76 IE_DESC = 'safaribooksonline.com online video'
77 _VALID_URL = r'https?://(?:www\.)?safaribooksonline\.com/library/view/[^/]+/(?P<course_id>[^/]+)/(?P<part>[^/?#&]+)\.html'
80 'url': 'https://www.safaribooksonline.com/library/view/hadoop-fundamentals-livelessons/9780133392838/part00.html',
81 'md5': 'dcc5a425e79f2564148652616af1f2a3',
85 'title': 'Introduction to Hadoop Fundamentals LiveLessons',
86 'timestamp': 1437758058,
87 'upload_date': '20150724',
88 'uploader_id': 'stork',
91 # non-digits in course id
92 'url': 'https://www.safaribooksonline.com/library/view/create-a-nodejs/100000006A0210/part00.html',
93 'only_matching': True,
95 'url': 'https://www.safaribooksonline.com/library/view/learning-path-red/9780134664057/RHCE_Introduction.html',
96 'only_matching': True,
99 def _real_extract(self, url):
100 mobj = re.match(self._VALID_URL, url)
101 video_id = '%s/%s' % (mobj.group('course_id'), mobj.group('part'))
103 webpage = self._download_webpage(url, video_id)
104 reference_id = self._search_regex(
105 r'data-reference-id=(["\'])(?P
<id>(?
:(?
!\
1).)+)\
1',
106 webpage, 'kaltura reference
id', group='id')
107 partner_id = self._search_regex(
108 r'data
-partner
-id=(["\'])(?P<id>(?:(?!\1).)+)\1',
109 webpage, 'kaltura widget id', group='id')
110 ui_id = self._search_regex(
111 r'data-ui-id=(["\'])(?P
<id>(?
:(?
!\
1).)+)\
1',
112 webpage, 'kaltura uiconf
id', group='id')
115 'wid
': '_
%s' % partner_id,
117 'flashvars
[referenceId
]': reference_id,
121 kaltura_session = self._download_json(
122 '%s/player
/kaltura_session
/?reference_id
=%s' % (self._API_BASE, reference_id),
123 video_id, 'Downloading kaltura session JSON
',
124 'Unable to download kaltura session JSON
', fatal=False)
126 session = kaltura_session.get('session
')
128 query['flashvars
[ks
]'] = session
130 return self.url_result(update_url_query(
131 'https
://cdnapisec
.kaltura
.com
/html5
/html5lib
/v2
.37
.1/mwEmbedFrame
.php
', query),
135 class SafariApiIE(SafariBaseIE):
136 IE_NAME = 'safari
:api
'
137 _VALID_URL = r'https?
://(?
:www\
.)?safaribooksonline\
.com
/api
/v1
/book
/(?P
<course_id
>[^
/]+)/chapter(?
:-content
)?
/(?P
<part
>[^
/?
#&]+)\.html'
140 'url': 'https://www.safaribooksonline.com/api/v1/book/9780133392838/chapter/part00.html',
141 'only_matching': True,
143 'url': 'https://www.safaribooksonline.com/api/v1/book/9780134664057/chapter/RHCE_Introduction.html',
144 'only_matching': True,
147 def _real_extract(self
, url
):
148 mobj
= re
.match(self
._VALID
_URL
, url
)
149 part
= self
._download
_json
(
150 url
, '%s/%s' % (mobj
.group('course_id'), mobj
.group('part')),
151 'Downloading part JSON')
152 return self
.url_result(part
['web_url'], SafariIE
.ie_key())
155 class SafariCourseIE(SafariBaseIE
):
156 IE_NAME
= 'safari:course'
157 IE_DESC
= 'safaribooksonline.com online courses'
159 _VALID_URL
= r
'''(?x)
162 (?:www\.)?safaribooksonline\.com/(?:library/view/[^/]+|api/v1/book)|
163 techbus\.safaribooksonline\.com
165 /(?P<id>[^/]+)/?(?:[#?]|$)
169 'url': 'https://www.safaribooksonline.com/library/view/hadoop-fundamentals-livelessons/9780133392838/',
171 'id': '9780133392838',
172 'title': 'Hadoop Fundamentals LiveLessons',
174 'playlist_count': 22,
175 'skip': 'Requires safaribooksonline account credentials',
177 'url': 'https://www.safaribooksonline.com/api/v1/book/9781449396459/?override_format=json',
178 'only_matching': True,
180 'url': 'http://techbus.safaribooksonline.com/9780134426365',
181 'only_matching': True,
184 def _real_extract(self
, url
):
185 course_id
= self
._match
_id
(url
)
187 course_json
= self
._download
_json
(
188 '%s/book/%s/?override_format=%s' % (self
._API
_BASE
, course_id
, self
._API
_FORMAT
),
189 course_id
, 'Downloading course JSON')
191 if 'chapters' not in course_json
:
192 raise ExtractorError(
193 'No chapters found for course %s' % course_id
, expected
=True)
196 self
.url_result(chapter
, SafariApiIE
.ie_key())
197 for chapter
in course_json
['chapters']]
199 course_title
= course_json
['title']
201 return self
.playlist_result(entries
, course_id
, course_title
)