4 from .common
import InfoExtractor
16 class SafariBaseIE(InfoExtractor
):
17 _LOGIN_URL
= 'https://learning.oreilly.com/accounts/login/'
18 _NETRC_MACHINE
= 'safari'
20 _API_BASE
= 'https://learning.oreilly.com/api/v1'
25 def _perform_login(self
, username
, password
):
26 _
, urlh
= self
._download
_webpage
_handle
(
27 'https://learning.oreilly.com/accounts/login-check/', None,
28 'Downloading login page')
31 return 'learning.oreilly.com/home/' in urlh
.geturl()
37 redirect_url
= urlh
.geturl()
38 parsed_url
= compat_urlparse
.urlparse(redirect_url
)
39 qs
= compat_parse_qs(parsed_url
.query
)
40 next_uri
= compat_urlparse
.urljoin(
41 'https://api.oreilly.com', qs
['next'][0])
43 auth
, urlh
= self
._download
_json
_handle
(
44 'https://www.oreilly.com/member/auth/login/', None, 'Logging in',
48 'redirect_uri': next_uri
,
49 }).encode(), headers
={
50 'Content-Type': 'application/json',
51 'Referer': redirect_url
,
52 }, expected_status
=400)
54 credentials
= auth
.get('credentials')
55 if (not auth
.get('logged_in') and not auth
.get('redirect_uri')
58 'Unable to login: %s' % credentials
, expected
=True)
60 # oreilly serves two same instances of the following cookies
61 # in Set-Cookie header and expects first one to be actually set
62 for cookie
in ('groot_sessionid', 'orm-jwt', 'orm-rt'):
63 self
._apply
_first
_set
_cookie
_header
(urlh
, cookie
)
65 _
, urlh
= self
._download
_webpage
_handle
(
66 auth
.get('redirect_uri') or next_uri
, None, 'Completing login',)
72 raise ExtractorError('Unable to log in')
75 class SafariIE(SafariBaseIE
):
77 IE_DESC
= 'safaribooksonline.com online video'
80 (?:www\.)?(?:safaribooksonline|(?:learning\.)?oreilly)\.com/
82 library/view/[^/]+/(?P<course_id>[^/]+)/(?P<part>[^/?\#&]+)\.html|
83 videos/[^/]+/[^/]+/(?P<reference_id>[^-]+-[^/?\#&]+)
88 'url': 'https://www.safaribooksonline.com/library/view/hadoop-fundamentals-livelessons/9780133392838/part00.html',
89 'md5': 'dcc5a425e79f2564148652616af1f2a3',
93 'title': 'Introduction to Hadoop Fundamentals LiveLessons',
94 'timestamp': 1437758058,
95 'upload_date': '20150724',
96 'uploader_id': 'stork',
99 # non-digits in course id
100 'url': 'https://www.safaribooksonline.com/library/view/create-a-nodejs/100000006A0210/part00.html',
101 'only_matching': True,
103 'url': 'https://www.safaribooksonline.com/library/view/learning-path-red/9780134664057/RHCE_Introduction.html',
104 'only_matching': True,
106 'url': 'https://www.safaribooksonline.com/videos/python-programming-language/9780134217314/9780134217314-PYMC_13_00',
107 'only_matching': True,
109 'url': 'https://learning.oreilly.com/videos/hadoop-fundamentals-livelessons/9780133392838/9780133392838-00_SeriesIntro',
110 'only_matching': True,
112 'url': 'https://www.oreilly.com/library/view/hadoop-fundamentals-livelessons/9780133392838/00_SeriesIntro.html',
113 'only_matching': True,
116 _PARTNER_ID
= '1926081'
117 _UICONF_ID
= '29375172'
119 def _real_extract(self
, url
):
120 mobj
= self
._match
_valid
_url
(url
)
122 reference_id
= mobj
.group('reference_id')
124 video_id
= reference_id
125 partner_id
= self
._PARTNER
_ID
126 ui_id
= self
._UICONF
_ID
128 video_id
= '%s-%s' % (mobj
.group('course_id'), mobj
.group('part'))
130 webpage
, urlh
= self
._download
_webpage
_handle
(url
, video_id
)
132 mobj
= re
.match(self
._VALID
_URL
, urlh
.geturl())
133 reference_id
= mobj
.group('reference_id')
135 reference_id
= self
._search
_regex
(
136 r
'data-reference-id=(["\'])(?P
<id>(?
:(?
!\
1).)+)\
1',
137 webpage, 'kaltura reference
id', group='id')
138 partner_id = self._search_regex(
139 r'data
-partner
-id=(["\'])(?P<id>(?:(?!\1).)+)\1',
140 webpage, 'kaltura widget id', default=self._PARTNER_ID,
142 ui_id = self._search_regex(
143 r'data-ui-id=(["\'])(?P
<id>(?
:(?
!\
1).)+)\
1',
144 webpage, 'kaltura uiconf
id', default=self._UICONF_ID,
148 'wid
': '_
%s' % partner_id,
150 'flashvars
[referenceId
]': reference_id,
154 kaltura_session = self._download_json(
155 '%s/player
/kaltura_session
/?reference_id
=%s' % (self._API_BASE, reference_id),
156 video_id, 'Downloading kaltura session JSON
',
157 'Unable to download kaltura session JSON
', fatal=False,
158 headers={'Accept': 'application/json'})
160 session = kaltura_session.get('session
')
162 query['flashvars
[ks
]'] = session
164 return self.url_result(update_url_query(
165 'https
://cdnapisec
.kaltura
.com
/html5
/html5lib
/v2
.37
.1/mwEmbedFrame
.php
', query),
169 class SafariApiIE(SafariBaseIE):
170 IE_NAME = 'safari
:api
'
171 _VALID_URL = r'https?
://(?
:www\
.)?
(?
:safaribooksonline|
(?
:learning\
.)?oreilly
)\
.com
/api
/v1
/book
/(?P
<course_id
>[^
/]+)/chapter(?
:-content
)?
/(?P
<part
>[^
/?
#&]+)\.html'
174 'url': 'https://www.safaribooksonline.com/api/v1/book/9780133392838/chapter/part00.html',
175 'only_matching': True,
177 'url': 'https://www.safaribooksonline.com/api/v1/book/9780134664057/chapter/RHCE_Introduction.html',
178 'only_matching': True,
181 def _real_extract(self
, url
):
182 mobj
= self
._match
_valid
_url
(url
)
183 part
= self
._download
_json
(
184 url
, '%s/%s' % (mobj
.group('course_id'), mobj
.group('part')),
185 'Downloading part JSON')
186 web_url
= part
['web_url']
187 if 'library/view' in web_url
:
188 web_url
= web_url
.replace('library/view', 'videos')
189 natural_keys
= part
['natural_key']
190 web_url
= f
'{web_url.rsplit("/", 1)[0]}/{natural_keys[0]}-{natural_keys[1][:-5]}'
191 return self
.url_result(web_url
, SafariIE
.ie_key())
194 class SafariCourseIE(SafariBaseIE
):
195 IE_NAME
= 'safari:course'
196 IE_DESC
= 'safaribooksonline.com online courses'
198 _VALID_URL
= r
'''(?x)
201 (?:www\.)?(?:safaribooksonline|(?:learning\.)?oreilly)\.com/
207 techbus\.safaribooksonline\.com
213 'url': 'https://www.safaribooksonline.com/library/view/hadoop-fundamentals-livelessons/9780133392838/',
215 'id': '9780133392838',
216 'title': 'Hadoop Fundamentals LiveLessons',
218 'playlist_count': 22,
219 'skip': 'Requires safaribooksonline account credentials',
221 'url': 'https://www.safaribooksonline.com/api/v1/book/9781449396459/?override_format=json',
222 'only_matching': True,
224 'url': 'http://techbus.safaribooksonline.com/9780134426365',
225 'only_matching': True,
227 'url': 'https://www.safaribooksonline.com/videos/python-programming-language/9780134217314',
228 'only_matching': True,
230 'url': 'https://learning.oreilly.com/videos/hadoop-fundamentals-livelessons/9780133392838',
231 'only_matching': True,
233 'url': 'https://www.oreilly.com/library/view/hadoop-fundamentals-livelessons/9780133392838/',
234 'only_matching': True,
238 def suitable(cls
, url
):
239 return (False if SafariIE
.suitable(url
) or SafariApiIE
.suitable(url
)
240 else super(SafariCourseIE
, cls
).suitable(url
))
242 def _real_extract(self
, url
):
243 course_id
= self
._match
_id
(url
)
245 course_json
= self
._download
_json
(
246 '%s/book/%s/?override_format=%s' % (self
._API
_BASE
, course_id
, self
._API
_FORMAT
),
247 course_id
, 'Downloading course JSON')
249 if 'chapters' not in course_json
:
250 raise ExtractorError(
251 'No chapters found for course %s' % course_id
, expected
=True)
254 self
.url_result(chapter
, SafariApiIE
.ie_key())
255 for chapter
in course_json
['chapters']]
257 course_title
= course_json
['title']
259 return self
.playlist_result(entries
, course_id
, course_title
)