4 from .common
import InfoExtractor
15 class SafariBaseIE(InfoExtractor
):
16 _LOGIN_URL
= 'https://learning.oreilly.com/accounts/login/'
17 _NETRC_MACHINE
= 'safari'
19 _API_BASE
= 'https://learning.oreilly.com/api/v1'
24 def _perform_login(self
, username
, password
):
25 _
, urlh
= self
._download
_webpage
_handle
(
26 'https://learning.oreilly.com/accounts/login-check/', None,
27 'Downloading login page')
30 return 'learning.oreilly.com/home/' in urlh
.url
36 redirect_url
= urlh
.url
37 parsed_url
= compat_urlparse
.urlparse(redirect_url
)
38 qs
= compat_parse_qs(parsed_url
.query
)
39 next_uri
= compat_urlparse
.urljoin(
40 'https://api.oreilly.com', qs
['next'][0])
42 auth
, urlh
= self
._download
_json
_handle
(
43 'https://www.oreilly.com/member/auth/login/', None, 'Logging in',
47 'redirect_uri': next_uri
,
48 }).encode(), headers
={
49 'Content-Type': 'application/json',
50 'Referer': redirect_url
,
51 }, expected_status
=400)
53 credentials
= auth
.get('credentials')
54 if (not auth
.get('logged_in') and not auth
.get('redirect_uri')
57 'Unable to login: %s' % credentials
, expected
=True)
59 # oreilly serves two same instances of the following cookies
60 # in Set-Cookie header and expects first one to be actually set
61 for cookie
in ('groot_sessionid', 'orm-jwt', 'orm-rt'):
62 self
._apply
_first
_set
_cookie
_header
(urlh
, cookie
)
64 _
, urlh
= self
._download
_webpage
_handle
(
65 auth
.get('redirect_uri') or next_uri
, None, 'Completing login',)
71 raise ExtractorError('Unable to log in')
74 class SafariIE(SafariBaseIE
):
76 IE_DESC
= 'safaribooksonline.com online video'
79 (?:www\.)?(?:safaribooksonline|(?:learning\.)?oreilly)\.com/
81 library/view/[^/]+/(?P<course_id>[^/]+)/(?P<part>[^/?\#&]+)\.html|
82 videos/[^/]+/[^/]+/(?P<reference_id>[^-]+-[^/?\#&]+)
87 'url': 'https://www.safaribooksonline.com/library/view/hadoop-fundamentals-livelessons/9780133392838/part00.html',
88 'md5': 'dcc5a425e79f2564148652616af1f2a3',
92 'title': 'Introduction to Hadoop Fundamentals LiveLessons',
93 'timestamp': 1437758058,
94 'upload_date': '20150724',
95 'uploader_id': 'stork',
98 # non-digits in course id
99 'url': 'https://www.safaribooksonline.com/library/view/create-a-nodejs/100000006A0210/part00.html',
100 'only_matching': True,
102 'url': 'https://www.safaribooksonline.com/library/view/learning-path-red/9780134664057/RHCE_Introduction.html',
103 'only_matching': True,
105 'url': 'https://www.safaribooksonline.com/videos/python-programming-language/9780134217314/9780134217314-PYMC_13_00',
106 'only_matching': True,
108 'url': 'https://learning.oreilly.com/videos/hadoop-fundamentals-livelessons/9780133392838/9780133392838-00_SeriesIntro',
109 'only_matching': True,
111 'url': 'https://www.oreilly.com/library/view/hadoop-fundamentals-livelessons/9780133392838/00_SeriesIntro.html',
112 'only_matching': True,
115 _PARTNER_ID
= '1926081'
116 _UICONF_ID
= '29375172'
118 def _real_extract(self
, url
):
119 mobj
= self
._match
_valid
_url
(url
)
121 reference_id
= mobj
.group('reference_id')
123 video_id
= reference_id
124 partner_id
= self
._PARTNER
_ID
125 ui_id
= self
._UICONF
_ID
127 video_id
= '%s-%s' % (mobj
.group('course_id'), mobj
.group('part'))
129 webpage
, urlh
= self
._download
_webpage
_handle
(url
, video_id
)
131 mobj
= re
.match(self
._VALID
_URL
, urlh
.url
)
132 reference_id
= mobj
.group('reference_id')
134 reference_id
= self
._search
_regex
(
135 r
'data-reference-id=(["\'])(?P
<id>(?
:(?
!\
1).)+)\
1',
136 webpage, 'kaltura reference
id', group='id')
137 partner_id = self._search_regex(
138 r'data
-partner
-id=(["\'])(?P<id>(?:(?!\1).)+)\1',
139 webpage, 'kaltura widget id', default=self._PARTNER_ID,
141 ui_id = self._search_regex(
142 r'data-ui-id=(["\'])(?P
<id>(?
:(?
!\
1).)+)\
1',
143 webpage, 'kaltura uiconf
id', default=self._UICONF_ID,
147 'wid
': '_
%s' % partner_id,
149 'flashvars
[referenceId
]': reference_id,
153 kaltura_session = self._download_json(
154 '%s/player
/kaltura_session
/?reference_id
=%s' % (self._API_BASE, reference_id),
155 video_id, 'Downloading kaltura session JSON
',
156 'Unable to download kaltura session JSON
', fatal=False,
157 headers={'Accept': 'application/json'})
159 session = kaltura_session.get('session
')
161 query['flashvars
[ks
]'] = session
163 return self.url_result(update_url_query(
164 'https
://cdnapisec
.kaltura
.com
/html5
/html5lib
/v2
.37
.1/mwEmbedFrame
.php
', query),
168 class SafariApiIE(SafariBaseIE):
169 IE_NAME = 'safari
:api
'
170 _VALID_URL = r'https?
://(?
:www\
.)?
(?
:safaribooksonline|
(?
:learning\
.)?oreilly
)\
.com
/api
/v1
/book
/(?P
<course_id
>[^
/]+)/chapter(?
:-content
)?
/(?P
<part
>[^
/?
#&]+)\.html'
173 'url': 'https://www.safaribooksonline.com/api/v1/book/9780133392838/chapter/part00.html',
174 'only_matching': True,
176 'url': 'https://www.safaribooksonline.com/api/v1/book/9780134664057/chapter/RHCE_Introduction.html',
177 'only_matching': True,
180 def _real_extract(self
, url
):
181 mobj
= self
._match
_valid
_url
(url
)
182 part
= self
._download
_json
(
183 url
, '%s/%s' % (mobj
.group('course_id'), mobj
.group('part')),
184 'Downloading part JSON')
185 web_url
= part
['web_url']
186 if 'library/view' in web_url
:
187 web_url
= web_url
.replace('library/view', 'videos')
188 natural_keys
= part
['natural_key']
189 web_url
= f
'{web_url.rsplit("/", 1)[0]}/{natural_keys[0]}-{natural_keys[1][:-5]}'
190 return self
.url_result(web_url
, SafariIE
.ie_key())
193 class SafariCourseIE(SafariBaseIE
):
194 IE_NAME
= 'safari:course'
195 IE_DESC
= 'safaribooksonline.com online courses'
197 _VALID_URL
= r
'''(?x)
200 (?:www\.)?(?:safaribooksonline|(?:learning\.)?oreilly)\.com/
206 techbus\.safaribooksonline\.com
212 'url': 'https://www.safaribooksonline.com/library/view/hadoop-fundamentals-livelessons/9780133392838/',
214 'id': '9780133392838',
215 'title': 'Hadoop Fundamentals LiveLessons',
217 'playlist_count': 22,
218 'skip': 'Requires safaribooksonline account credentials',
220 'url': 'https://www.safaribooksonline.com/api/v1/book/9781449396459/?override_format=json',
221 'only_matching': True,
223 'url': 'http://techbus.safaribooksonline.com/9780134426365',
224 'only_matching': True,
226 'url': 'https://www.safaribooksonline.com/videos/python-programming-language/9780134217314',
227 'only_matching': True,
229 'url': 'https://learning.oreilly.com/videos/hadoop-fundamentals-livelessons/9780133392838',
230 'only_matching': True,
232 'url': 'https://www.oreilly.com/library/view/hadoop-fundamentals-livelessons/9780133392838/',
233 'only_matching': True,
237 def suitable(cls
, url
):
238 return (False if SafariIE
.suitable(url
) or SafariApiIE
.suitable(url
)
239 else super(SafariCourseIE
, cls
).suitable(url
))
241 def _real_extract(self
, url
):
242 course_id
= self
._match
_id
(url
)
244 course_json
= self
._download
_json
(
245 '%s/book/%s/?override_format=%s' % (self
._API
_BASE
, course_id
, self
._API
_FORMAT
),
246 course_id
, 'Downloading course JSON')
248 if 'chapters' not in course_json
:
249 raise ExtractorError(
250 'No chapters found for course %s' % course_id
, expected
=True)
253 self
.url_result(chapter
, SafariApiIE
.ie_key())
254 for chapter
in course_json
['chapters']]
256 course_title
= course_json
['title']
258 return self
.playlist_result(entries
, course_id
, course_title
)