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'
79 (?:www\.)?safaribooksonline\.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,
109 _PARTNER_ID = '1926081'
110 _UICONF_ID = '29375172'
112 def _real_extract(self, url):
113 mobj = re.match(self._VALID_URL, url)
115 reference_id = mobj.group('reference_id')
117 video_id = reference_id
118 partner_id = self._PARTNER_ID
119 ui_id = self._UICONF_ID
121 video_id = '%s-%s' % (mobj.group('course_id'), mobj.group('part'))
123 webpage, urlh = self._download_webpage_handle(url, video_id)
125 mobj = re.match(self._VALID_URL, urlh.geturl())
126 reference_id = mobj.group('reference_id')
128 reference_id = self._search_regex(
129 r'data-reference-id=(["\'])(?P<id>(?:(?!\1).)+)\1',
130 webpage, 'kaltura reference id', group='id')
131 partner_id = self._search_regex(
132 r'data-partner-id=(["\'])(?P<id>(?:(?!\1).)+)\1',
133 webpage, 'kaltura widget id', default=self._PARTNER_ID,
135 ui_id = self._search_regex(
136 r'data-ui-id=(["\'])(?P<id>(?:(?!\1).)+)\1',
137 webpage, 'kaltura uiconf id', default=self._UICONF_ID,
141 'wid': '_%s' % partner_id,
143 'flashvars[referenceId]': reference_id,
147 kaltura_session = self._download_json(
148 '%s/player/kaltura_session/?reference_id=%s' % (self._API_BASE, reference_id),
149 video_id, 'Downloading kaltura session JSON',
150 'Unable to download kaltura session JSON', fatal=False)
152 session = kaltura_session.get('session')
154 query['flashvars[ks]'] = session
156 return self.url_result(update_url_query(
157 'https://cdnapisec.kaltura.com/html5/html5lib/v2.37.1/mwEmbedFrame.php', query),
161 class SafariApiIE(SafariBaseIE):
162 IE_NAME = 'safari:api'
163 _VALID_URL = r'https?://(?:www\.)?safaribooksonline\.com/api/v1/book/(?P<course_id>[^/]+)/chapter(?:-content)?/(?P<part>[^/?#&]+)\.html'
166 'url': 'https://www.safaribooksonline.com/api/v1/book/9780133392838/chapter/part00.html',
167 'only_matching': True,
169 'url': 'https://www.safaribooksonline.com/api/v1/book/9780134664057/chapter/RHCE_Introduction.html',
170 'only_matching': True,
173 def _real_extract(self, url):
174 mobj = re.match(self._VALID_URL, url)
175 part = self._download_json(
176 url, '%s/%s' % (mobj.group('course_id'), mobj.group('part')),
177 'Downloading part JSON')
178 return self.url_result(part['web_url'], SafariIE.ie_key())
181 class SafariCourseIE(SafariBaseIE):
182 IE_NAME = 'safari:course'
183 IE_DESC = 'safaribooksonline.com online courses'
185 _VALID_URL = r'''(?x)
188 (?:www\.)?safaribooksonline\.com/
194 techbus\.safaribooksonline\.com
200 'url': 'https://www.safaribooksonline.com/library/view/hadoop-fundamentals-livelessons/9780133392838/',
202 'id': '9780133392838',
203 'title': 'Hadoop Fundamentals LiveLessons',
205 'playlist_count': 22,
206 'skip': 'Requires safaribooksonline account credentials',
208 'url': 'https://www.safaribooksonline.com/api/v1/book/9781449396459/?override_format=json',
209 'only_matching': True,
211 'url': 'http://techbus.safaribooksonline.com/9780134426365',
212 'only_matching': True,
214 'url': 'https://www.safaribooksonline.com/videos/python-programming-language/9780134217314',
215 'only_matching': True,
219 def suitable(cls, url):
220 return (False if SafariIE.suitable(url) or SafariApiIE.suitable(url)
221 else super(SafariCourseIE, cls).suitable(url))
223 def _real_extract(self, url):
224 course_id = self._match_id(url)
226 course_json = self._download_json(
227 '%s/book/%s/?override_format=%s' % (self._API_BASE, course_id, self._API_FORMAT),
228 course_id, 'Downloading course JSON')
230 if 'chapters' not in course_json:
231 raise ExtractorError(
232 'No chapters found for course %s' % course_id, expected=True)
235 self.url_result(chapter, SafariApiIE.ie_key())
236 for chapter in course_json['chapters']]
238 course_title = course_json['title']
240 return self.playlist_result(entries, course_id, course_title)