2 from __future__ import unicode_literals
6 from .common import InfoExtractor
7 from .brightcove import BrightcoveLegacyIE
19 class SafariBaseIE(InfoExtractor):
20 _LOGIN_URL = 'https://www.safaribooksonline.com/accounts/login/'
21 _SUCCESSFUL_LOGIN_REGEX = r'<a href="/accounts/logout/"[^>]*>Sign Out</a>'
22 _NETRC_MACHINE = 'safari'
24 _API_BASE = 'https://www.safaribooksonline.com/api/v1'
29 def _real_initialize(self):
30 # We only need to log in once for courses or individual videos
31 if not self.LOGGED_IN:
33 SafariBaseIE.LOGGED_IN = True
36 (username, password) = self._get_login_info()
40 headers = std_headers.copy()
41 if 'Referer' not in headers:
42 headers['Referer'] = self._LOGIN_URL
43 login_page_request = sanitized_Request(self._LOGIN_URL, headers=headers)
45 login_page = self._download_webpage(
46 login_page_request, None,
47 'Downloading login form')
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 as %s' % username)
66 if re.search(self._SUCCESSFUL_LOGIN_REGEX, login_page) is None:
68 'Login failed; make sure your credentials are correct and try again.',
71 self.to_screen('Login successful')
74 class SafariIE(SafariBaseIE):
76 IE_DESC = 'safaribooksonline.com online video'
77 _VALID_URL = r'''(?x)https?://
78 (?:www\.)?safaribooksonline\.com/
84 (?:chapter(?:-content)?/)?
85 (?P<part>part\d+)\.html
89 'url': 'https://www.safaribooksonline.com/library/view/hadoop-fundamentals-livelessons/9780133392838/part00.html',
90 'md5': 'dcc5a425e79f2564148652616af1f2a3',
94 'title': 'Introduction to Hadoop Fundamentals LiveLessons',
95 'timestamp': 1437758058,
96 'upload_date': '20150724',
97 'uploader_id': 'stork',
100 'url': 'https://www.safaribooksonline.com/api/v1/book/9780133392838/chapter/part00.html',
101 'only_matching': True,
103 # non-digits in course id
104 'url': 'https://www.safaribooksonline.com/library/view/create-a-nodejs/100000006A0210/part00.html',
105 'only_matching': True,
108 def _real_extract(self, url):
109 mobj = re.match(self._VALID_URL, url)
110 course_id = mobj.group('course_id')
111 part = mobj.group('part')
113 webpage = self._download_webpage(url, '%s/%s' % (course_id, part))
114 reference_id = self._search_regex(r'data-reference-id="([^"]+)"', webpage, 'kaltura reference id')
115 partner_id = self._search_regex(r'data-partner-id="([^"]+)"', webpage, 'kaltura widget id')
116 ui_id = self._search_regex(r'data-ui-id="([^"]+)"', webpage, 'kaltura uiconf id')
119 'wid': '_%s' % partner_id,
121 'flashvars[referenceId]': reference_id,
125 kaltura_session = self._download_json(
126 '%s/player/kaltura_session/?reference_id=%s' % (self._API_BASE, reference_id),
127 course_id, 'Downloading kaltura session JSON',
128 'Unable to download kaltura session JSON', fatal=False)
130 session = kaltura_session.get('session')
132 query['flashvars[ks]'] = session
134 return self.url_result(update_url_query(
135 'https://cdnapisec.kaltura.com/html5/html5lib/v2.37.1/mwEmbedFrame.php', query),
139 class SafariCourseIE(SafariBaseIE):
140 IE_NAME = 'safari:course'
141 IE_DESC = 'safaribooksonline.com online courses'
143 _VALID_URL = r'https?://(?:www\.)?safaribooksonline\.com/(?:library/view/[^/]+|api/v1/book)/(?P<id>[^/]+)/?(?:[#?]|$)'
146 'url': 'https://www.safaribooksonline.com/library/view/hadoop-fundamentals-livelessons/9780133392838/',
148 'id': '9780133392838',
149 'title': 'Hadoop Fundamentals LiveLessons',
151 'playlist_count': 22,
152 'skip': 'Requires safaribooksonline account credentials',
154 'url': 'https://www.safaribooksonline.com/api/v1/book/9781449396459/?override_format=json',
155 'only_matching': True,
158 def _real_extract(self, url):
159 course_id = self._match_id(url)
161 course_json = self._download_json(
162 '%s/book/%s/?override_format=%s' % (self._API_BASE, course_id, self._API_FORMAT),
163 course_id, 'Downloading course JSON')
165 if 'chapters' not in course_json:
166 raise ExtractorError(
167 'No chapters found for course %s' % course_id, expected=True)
170 self.url_result(chapter, 'Safari')
171 for chapter in course_json['chapters']]
173 course_title = course_json['title']
175 return self.playlist_result(entries, course_id, course_title)